blob: c3ac4673db3e7e5f86a8a13589385fc9102489b6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200128/* When using exists() don't auto-load a script. */
129static int no_autoload = FALSE;
130
Bram Moolenaar532c7802005-01-27 14:44:31 +0000131/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 * When recursively copying lists and dicts we need to remember which ones we
133 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 */
136static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137#define COPYID_INC 2
138#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139
140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Structure to hold info for a user function.
162 */
163typedef struct ufunc ufunc_T;
164
165struct ufunc
166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000167 int uf_varargs; /* variable nr of arguments */
168 int uf_flags;
169 int uf_calls; /* nr of active calls */
170 garray_T uf_args; /* arguments */
171 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000172#ifdef FEAT_PROFILE
173 int uf_profiling; /* TRUE when func is being profiled */
174 /* profiling the function as a whole */
175 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T uf_tm_total; /* time spent in function + children */
177 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tm_children; /* time spent in children this call */
179 /* profiling the function per line */
180 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T *uf_tml_total; /* time spent in a line + children */
182 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tml_start; /* start time for current line */
184 proftime_T uf_tml_children; /* time spent in children for this line */
185 proftime_T uf_tml_wait; /* start wait time for current line */
186 int uf_tml_idx; /* index of line being timed; -1 if none */
187 int uf_tml_execed; /* line being timed was executed */
188#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 int uf_refcount; /* for numbered function: reference count */
192 char_u uf_name[1]; /* name of function (actually longer); can
193 start with <SNR>123_ (<SNR> is K_SPECIAL
194 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195};
196
197/* function flags */
198#define FC_ABORT 1 /* abort function on error */
199#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000200#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000203 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000205static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000207/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000208static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000210/* list heads for garbage collection */
211static dict_T *first_dict = NULL; /* list of all dicts */
212static list_T *first_list = NULL; /* list of all lists */
213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000214/* From user function to hashitem and back. */
215static ufunc_T dumuf;
216#define UF2HIKEY(fp) ((fp)->uf_name)
217#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
218#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
219
220#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
221#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar33570922005-01-25 22:26:29 +0000223#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
224#define VAR_SHORT_LEN 20 /* short variable name length */
225#define FIXVAR_CNT 12 /* number of fixed variables */
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000228typedef struct funccall_S funccall_T;
229
230struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 ufunc_T *func; /* function being called */
233 int linenr; /* next line to be executed */
234 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235 struct /* fixed variables for arguments */
236 {
237 dictitem_T var; /* variable (without room for name) */
238 char_u room[VAR_SHORT_LEN]; /* room for the name */
239 } fixvar[FIXVAR_CNT];
240 dict_T l_vars; /* l: local function variables */
241 dictitem_T l_vars_var; /* variable for l: scope */
242 dict_T l_avars; /* a: argument variables */
243 dictitem_T l_avars_var; /* variable for a: scope */
244 list_T l_varlist; /* list for a:000 */
245 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
246 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 linenr_T breakpoint; /* next line with breakpoint or zero */
248 int dbg_tick; /* debug_tick when breakpoint was set */
249 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000250#ifdef FEAT_PROFILE
251 proftime_T prof_child; /* time spent in a child */
252#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000253 funccall_T *caller; /* calling function or NULL */
254};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257 * Info used by a ":for" loop.
258 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000259typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260{
261 int fi_semicolon; /* TRUE if ending in '; var]' */
262 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 listwatch_T fi_lw; /* keep an eye on the item used. */
264 list_T *fi_list; /* list being used */
265} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268 * Struct used by trans_function_name()
269 */
270typedef struct
271{
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000273 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dictitem_T *fd_di; /* Dictionary item used */
275} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000276
Bram Moolenaara7043832005-01-21 11:56:39 +0000277
278/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 * Array to hold the value of v: variables.
280 * The value is in a dictitem, so that it can also be used in the v: scope.
281 * The reason to use this table anyway is for very quick access to the
282 * variables with the VV_ defines.
283 */
284#include "version.h"
285
286/* values for vv_flags: */
287#define VV_COMPAT 1 /* compatible, also used without "v:" */
288#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000291#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
293static struct vimvar
294{
295 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296 dictitem_T vv_di; /* value and name for key */
297 char vv_filler[16]; /* space for LONGEST name below!!! */
298 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
299} vimvars[VV_LEN] =
300{
301 /*
302 * The order here must match the VV_ defines in vim.h!
303 * Initializing a union does not work, leave tv.vval empty to get zero's.
304 */
305 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("count1", VAR_NUMBER), VV_RO},
307 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
308 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
309 {VV_NAME("warningmsg", VAR_STRING), 0},
310 {VV_NAME("statusmsg", VAR_STRING), 0},
311 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
313 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("termresponse", VAR_STRING), VV_RO},
316 {VV_NAME("fname", VAR_STRING), VV_RO},
317 {VV_NAME("lang", VAR_STRING), VV_RO},
318 {VV_NAME("lc_time", VAR_STRING), VV_RO},
319 {VV_NAME("ctype", VAR_STRING), VV_RO},
320 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
322 {VV_NAME("fname_in", VAR_STRING), VV_RO},
323 {VV_NAME("fname_out", VAR_STRING), VV_RO},
324 {VV_NAME("fname_new", VAR_STRING), VV_RO},
325 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
326 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
327 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
330 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("progname", VAR_STRING), VV_RO},
332 {VV_NAME("servername", VAR_STRING), VV_RO},
333 {VV_NAME("dying", VAR_NUMBER), VV_RO},
334 {VV_NAME("exception", VAR_STRING), VV_RO},
335 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
336 {VV_NAME("register", VAR_STRING), VV_RO},
337 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
338 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000339 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
340 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000341 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000342 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
343 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000344 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000349 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000350 {VV_NAME("swapname", VAR_STRING), VV_RO},
351 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000352 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200353 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000354 {VV_NAME("mouse_win", VAR_NUMBER), 0},
355 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
356 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000357 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000359 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200360 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200371static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000372#define vimvarht vimvardict.dv_hashtab
373
Bram Moolenaara40058a2005-07-11 22:42:07 +0000374static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
375static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
377static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
378static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
380static void list_glob_vars __ARGS((int *first));
381static void list_buf_vars __ARGS((int *first));
382static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_vim_vars __ARGS((int *first));
387static void list_script_vars __ARGS((int *first));
388static void list_func_vars __ARGS((int *first));
389static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
391static int check_changedtick __ARGS((char_u *arg));
392static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
393static void clear_lval __ARGS((lval_T *lp));
394static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
395static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
397static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
398static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
399static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
400static void item_lock __ARGS((typval_T *tv, int deep, int lock));
401static int tv_islocked __ARGS((typval_T *tv));
402
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
404static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000409static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
410static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000412static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000417static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100419static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
420static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
421static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100429static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000431static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200432static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000442#ifdef FEAT_FLOAT
443static int string2float __ARGS((char_u *text, float_T *value));
444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200449static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000450static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000451static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200455static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100458static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200464static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000482static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000485static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000487#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000488static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#ifdef FEAT_FLOAT
495static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200496static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
501static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512#ifdef FEAT_FLOAT
513static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000517static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
524static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200526static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000528static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000537static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000539static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000545static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000553static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000554static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000555static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000556static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200559static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000560static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000568static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000582static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100587static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000589static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000601#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200602static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605#ifdef FEAT_LUA
606static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000616static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620#ifdef vim_mkdir
621static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100624#ifdef FEAT_MZSCHEME
625static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100629static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631#ifdef FEAT_FLOAT
632static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000635static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000636static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200637#ifdef FEAT_PYTHON3
638static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
640#ifdef FEAT_PYTHON
641static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000645static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200660static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100662static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000665static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000667static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000674static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000675static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000676static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000677static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200679static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000680static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100682#ifdef FEAT_CRYPT
683static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
684#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200686static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
689static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200690static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000693static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000701static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200702static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200712static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200713static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000719static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200720static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000723static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000725static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000726static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000728static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200729#ifdef FEAT_FLOAT
730static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
737static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200740static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200741static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100745static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100756static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000758static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000759static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static int get_env_len __ARGS((char_u **arg));
761static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000763static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
764#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
765#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
766 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000767static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000769static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
771static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static typval_T *alloc_tv __ARGS((void));
773static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void init_tv __ARGS((typval_T *varp));
775static long get_tv_number __ARGS((typval_T *varp));
776static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000777static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static char_u *get_tv_string __ARGS((typval_T *varp));
779static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000780static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200782static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
784static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
785static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000786static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
787static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
789static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000790static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200791static int var_check_func_name __ARGS((char_u *name, int new_var));
792static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000793static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000794static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
796static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
797static int eval_fname_script __ARGS((char_u *p));
798static int eval_fname_sid __ARGS((char_u *p));
799static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static ufunc_T *find_func __ARGS((char_u *name));
801static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000802static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
804static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000805static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
806static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_total_cmp __ARGS((const void *s1, const void *s2));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200818static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000820static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000823static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
824static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000826static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
827static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000828static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200832
833#ifdef EBCDIC
834static int compare_func_name __ARGS((const void *s1, const void *s2));
835static void sortFunctions __ARGS(());
836#endif
837
Bram Moolenaar33570922005-01-25 22:26:29 +0000838/*
839 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000840 */
841 void
842eval_init()
843{
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 int i;
845 struct vimvar *p;
846
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
848 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200849 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000850 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000851 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 STRCPY(p->vv_di.di_key, p->vv_name);
857 if (p->vv_flags & VV_RO)
858 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
859 else if (p->vv_flags & VV_RO_SBX)
860 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
861 else
862 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000863
864 /* add to v: scope dict, unless the value is not always available */
865 if (p->vv_type != VAR_UNKNOWN)
866 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 /* add to compat scope dict */
869 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000871 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200872 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200873
874#ifdef EBCDIC
875 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100876 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200877 */
878 sortFunctions();
879#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000880}
881
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000882#if defined(EXITFREE) || defined(PROTO)
883 void
884eval_clear()
885{
886 int i;
887 struct vimvar *p;
888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000894 vim_free(p->vv_str);
895 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000896 }
897 else if (p->vv_di.di_tv.v_type == VAR_LIST)
898 {
899 list_unref(p->vv_list);
900 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 }
903 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000904 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 hash_clear(&compat_hashtab);
906
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200909 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100910# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaarcca74132013-09-25 21:00:28 +0200918 /* Script-local variables. First clear all the variables and in a second
919 * loop free the scriptvar_T, because a variable in one script might hold
920 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200923 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200924 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200925 ga_clear(&ga_scripts);
926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000927 /* unreferenced lists and dicts */
928 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000929
930 /* functions */
931 free_all_functions();
932 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933}
934#endif
935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 * Return the name of the executed function.
938 */
939 char_u *
940func_name(cookie)
941 void *cookie;
942{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000943 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944}
945
946/*
947 * Return the address holding the next breakpoint line for a funccall cookie.
948 */
949 linenr_T *
950func_breakpoint(cookie)
951 void *cookie;
952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
956/*
957 * Return the address holding the debug tick for a funccall cookie.
958 */
959 int *
960func_dbg_tick(cookie)
961 void *cookie;
962{
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/*
967 * Return the nesting level for a funccall cookie.
968 */
969 int
970func_level(cookie)
971 void *cookie;
972{
Bram Moolenaar33570922005-01-25 22:26:29 +0000973 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000977funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000979/* pointer to list of previously used funccal, still around because some
980 * item in it is still being used. */
981funccall_T *previous_funccal = NULL;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Return TRUE when a function was ended by a ":return" command.
985 */
986 int
987current_func_returned()
988{
989 return current_funccal->returned;
990}
991
992
993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Set an internal variable to a string value. Creates the variable if it does
995 * not already exist.
996 */
997 void
998set_internal_string_var(name, value)
999 char_u *name;
1000 char_u *value;
1001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001002 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 val = vim_strsave(value);
1006 if (val != NULL)
1007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001008 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001009 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014 }
1015}
1016
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001018static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static char_u *redir_endp = NULL;
1020static char_u *redir_varname = NULL;
1021
1022/*
1023 * Start recording command output to a variable
1024 * Returns OK if successfully completed the setup. FAIL otherwise.
1025 */
1026 int
1027var_redir_start(name, append)
1028 char_u *name;
1029 int append; /* append to an existing variable */
1030{
1031 int save_emsg;
1032 int err;
1033 typval_T tv;
1034
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001035 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001036 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 {
1038 EMSG(_(e_invarg));
1039 return FAIL;
1040 }
1041
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001042 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 redir_varname = vim_strsave(name);
1044 if (redir_varname == NULL)
1045 return FAIL;
1046
1047 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1048 if (redir_lval == NULL)
1049 {
1050 var_redir_stop();
1051 return FAIL;
1052 }
1053
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001054 /* The output is stored in growarray "redir_ga" until redirection ends. */
1055 ga_init2(&redir_ga, (int)sizeof(char), 500);
1056
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001058 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1059 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1061 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001062 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp != NULL && *redir_endp != NUL)
1064 /* Trailing characters are present after the variable name */
1065 EMSG(_(e_trailing));
1066 else
1067 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001068 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 var_redir_stop();
1070 return FAIL;
1071 }
1072
1073 /* check if we can write to the variable: set it to or append an empty
1074 * string */
1075 save_emsg = did_emsg;
1076 did_emsg = FALSE;
1077 tv.v_type = VAR_STRING;
1078 tv.vval.v_string = (char_u *)"";
1079 if (append)
1080 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1081 else
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001083 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001085 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 if (err)
1087 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001088 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 var_redir_stop();
1090 return FAIL;
1091 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097 * Append "value[value_len]" to the variable set by var_redir_start().
1098 * The actual appending is postponed until redirection ends, because the value
1099 * appended may in fact be the string we write to, changing it may cause freed
1100 * memory to be used:
1101 * :redir => foo
1102 * :let foo
1103 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 */
1105 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 if (redir_lval == NULL)
1113 return;
1114
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 {
1122 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 }
1125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127}
1128
1129/*
1130 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001131 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 */
1133 void
1134var_redir_stop()
1135{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 typval_T tv;
1137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_lval != NULL)
1139 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 /* If there was no error: assign the text to the variable. */
1141 if (redir_endp != NULL)
1142 {
1143 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1144 tv.v_type = VAR_STRING;
1145 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001146 /* Call get_lval() again, if it's inside a Dict or List it may
1147 * have changed. */
1148 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1149 FALSE, FALSE, FALSE, FNE_CHECK_START);
1150 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1151 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1152 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 /* free the collected output */
1156 vim_free(redir_ga.ga_data);
1157 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 vim_free(redir_lval);
1160 redir_lval = NULL;
1161 }
1162 vim_free(redir_varname);
1163 redir_varname = NULL;
1164}
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# if defined(FEAT_MBYTE) || defined(PROTO)
1167 int
1168eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1169 char_u *enc_from;
1170 char_u *enc_to;
1171 char_u *fname_from;
1172 char_u *fname_to;
1173{
1174 int err = FALSE;
1175
1176 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1177 set_vim_var_string(VV_CC_TO, enc_to, -1);
1178 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1179 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1180 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1181 err = TRUE;
1182 set_vim_var_string(VV_CC_FROM, NULL, -1);
1183 set_vim_var_string(VV_CC_TO, NULL, -1);
1184 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1185 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1186
1187 if (err)
1188 return FAIL;
1189 return OK;
1190}
1191# endif
1192
1193# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1194 int
1195eval_printexpr(fname, args)
1196 char_u *fname;
1197 char_u *args;
1198{
1199 int err = FALSE;
1200
1201 set_vim_var_string(VV_FNAME_IN, fname, -1);
1202 set_vim_var_string(VV_CMDARG, args, -1);
1203 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1204 err = TRUE;
1205 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1206 set_vim_var_string(VV_CMDARG, NULL, -1);
1207
1208 if (err)
1209 {
1210 mch_remove(fname);
1211 return FAIL;
1212 }
1213 return OK;
1214}
1215# endif
1216
1217# if defined(FEAT_DIFF) || defined(PROTO)
1218 void
1219eval_diff(origfile, newfile, outfile)
1220 char_u *origfile;
1221 char_u *newfile;
1222 char_u *outfile;
1223{
1224 int err = FALSE;
1225
1226 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1227 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1228 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1229 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1230 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1231 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1232 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1233}
1234
1235 void
1236eval_patch(origfile, difffile, outfile)
1237 char_u *origfile;
1238 char_u *difffile;
1239 char_u *outfile;
1240{
1241 int err;
1242
1243 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1244 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1245 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1246 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1247 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1248 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1249 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1250}
1251# endif
1252
1253/*
1254 * Top level evaluation function, returning a boolean.
1255 * Sets "error" to TRUE if there was an error.
1256 * Return TRUE or FALSE.
1257 */
1258 int
1259eval_to_bool(arg, error, nextcmd, skip)
1260 char_u *arg;
1261 int *error;
1262 char_u **nextcmd;
1263 int skip; /* only parse, don't execute */
1264{
Bram Moolenaar33570922005-01-25 22:26:29 +00001265 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 int retval = FALSE;
1267
1268 if (skip)
1269 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 else
1273 {
1274 *error = FALSE;
1275 if (!skip)
1276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001277 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280 }
1281 if (skip)
1282 --emsg_skip;
1283
1284 return retval;
1285}
1286
1287/*
1288 * Top level evaluation function, returning a string. If "skip" is TRUE,
1289 * only parsing to "nextcmd" is done, without reporting errors. Return
1290 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1291 */
1292 char_u *
1293eval_to_string_skip(arg, nextcmd, skip)
1294 char_u *arg;
1295 char_u **nextcmd;
1296 int skip; /* only parse, don't execute */
1297{
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 char_u *retval;
1300
1301 if (skip)
1302 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 retval = NULL;
1305 else
1306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 retval = vim_strsave(get_tv_string(&tv));
1308 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 if (skip)
1311 --emsg_skip;
1312
1313 return retval;
1314}
1315
1316/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001317 * Skip over an expression at "*pp".
1318 * Return FAIL for an error, OK otherwise.
1319 */
1320 int
1321skip_expr(pp)
1322 char_u **pp;
1323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325
1326 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328}
1329
1330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332 * When "convert" is TRUE convert a List into a sequence of lines and convert
1333 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Return pointer to allocated memory, or NULL for failure.
1335 */
1336 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 char_u *arg;
1339 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001344 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001345#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001354 {
1355 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001356 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001357 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001359 if (tv.vval.v_list->lv_len > 0)
1360 ga_append(&ga, NL);
1361 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 ga_append(&ga, NUL);
1363 retval = (char_u *)ga.ga_data;
1364 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365#ifdef FEAT_FLOAT
1366 else if (convert && tv.v_type == VAR_FLOAT)
1367 {
1368 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1369 retval = vim_strsave(numbuf);
1370 }
1371#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 else
1373 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376
1377 return retval;
1378}
1379
1380/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 * Call eval_to_string() without using current local variables and using
1382 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 */
1384 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 char_u *arg;
1387 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 char_u *retval;
1391 void *save_funccalp;
1392
1393 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 if (use_sandbox)
1395 ++sandbox;
1396 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (use_sandbox)
1399 --sandbox;
1400 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 restore_funccal(save_funccalp);
1402 return retval;
1403}
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405/*
1406 * Top level evaluation function, returning a number.
1407 * Evaluates "expr" silently.
1408 * Returns -1 for an error.
1409 */
1410 int
1411eval_to_number(expr)
1412 char_u *expr;
1413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001416 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
1418 ++emsg_off;
1419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 retval = -1;
1422 else
1423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001424 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 --emsg_off;
1428
1429 return retval;
1430}
1431
Bram Moolenaara40058a2005-07-11 22:42:07 +00001432/*
1433 * Prepare v: variable "idx" to be used.
1434 * Save the current typeval in "save_tv".
1435 * When not used yet add the variable to the v: hashtable.
1436 */
1437 static void
1438prepare_vimvar(idx, save_tv)
1439 int idx;
1440 typval_T *save_tv;
1441{
1442 *save_tv = vimvars[idx].vv_tv;
1443 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1444 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1445}
1446
1447/*
1448 * Restore v: variable "idx" to typeval "save_tv".
1449 * When no longer defined, remove the variable from the v: hashtable.
1450 */
1451 static void
1452restore_vimvar(idx, save_tv)
1453 int idx;
1454 typval_T *save_tv;
1455{
1456 hashitem_T *hi;
1457
Bram Moolenaara40058a2005-07-11 22:42:07 +00001458 vimvars[idx].vv_tv = *save_tv;
1459 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1460 {
1461 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1462 if (HASHITEM_EMPTY(hi))
1463 EMSG2(_(e_intern2), "restore_vimvar()");
1464 else
1465 hash_remove(&vimvarht, hi);
1466 }
1467}
1468
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001469#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001470/*
1471 * Evaluate an expression to a list with suggestions.
1472 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001473 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001474 */
1475 list_T *
1476eval_spell_expr(badword, expr)
1477 char_u *badword;
1478 char_u *expr;
1479{
1480 typval_T save_val;
1481 typval_T rettv;
1482 list_T *list = NULL;
1483 char_u *p = skipwhite(expr);
1484
1485 /* Set "v:val" to the bad word. */
1486 prepare_vimvar(VV_VAL, &save_val);
1487 vimvars[VV_VAL].vv_type = VAR_STRING;
1488 vimvars[VV_VAL].vv_str = badword;
1489 if (p_verbose == 0)
1490 ++emsg_off;
1491
1492 if (eval1(&p, &rettv, TRUE) == OK)
1493 {
1494 if (rettv.v_type != VAR_LIST)
1495 clear_tv(&rettv);
1496 else
1497 list = rettv.vval.v_list;
1498 }
1499
1500 if (p_verbose == 0)
1501 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001502 restore_vimvar(VV_VAL, &save_val);
1503
1504 return list;
1505}
1506
1507/*
1508 * "list" is supposed to contain two items: a word and a number. Return the
1509 * word in "pp" and the number as the return value.
1510 * Return -1 if anything isn't right.
1511 * Used to get the good word and score from the eval_spell_expr() result.
1512 */
1513 int
1514get_spellword(list, pp)
1515 list_T *list;
1516 char_u **pp;
1517{
1518 listitem_T *li;
1519
1520 li = list->lv_first;
1521 if (li == NULL)
1522 return -1;
1523 *pp = get_tv_string(&li->li_tv);
1524
1525 li = li->li_next;
1526 if (li == NULL)
1527 return -1;
1528 return get_tv_number(&li->li_tv);
1529}
1530#endif
1531
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001532/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001533 * Top level evaluation function.
1534 * Returns an allocated typval_T with the result.
1535 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536 */
1537 typval_T *
1538eval_expr(arg, nextcmd)
1539 char_u *arg;
1540 char_u **nextcmd;
1541{
1542 typval_T *tv;
1543
1544 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 {
1547 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 }
1550
1551 return tv;
1552}
1553
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001561 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001562call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001567 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 long n;
1572 int len;
1573 int i;
1574 int doesrange;
1575 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001578 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 for (i = 0; i < argc; i++)
1583 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001584 /* Pass a NULL or empty argument as an empty string */
1585 if (argv[i] == NULL || *argv[i] == NUL)
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 continue;
1590 }
1591
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001592 if (str_arg_only)
1593 len = 0;
1594 else
1595 /* Recognize a number argument, the others must be strings. */
1596 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (len != 0 && len == (int)STRLEN(argv[i]))
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_NUMBER;
1600 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 else
1603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001604 argvars[i].v_type = VAR_STRING;
1605 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608
1609 if (safe)
1610 {
1611 save_funccalp = save_funccal();
1612 ++sandbox;
1613 }
1614
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1616 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (safe)
1620 {
1621 --sandbox;
1622 restore_funccal(save_funccalp);
1623 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 vim_free(argvars);
1625
1626 if (ret == FAIL)
1627 clear_tv(rettv);
1628
1629 return ret;
1630}
1631
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001632/*
1633 * Call vimL function "func" and return the result as a number.
1634 * Returns -1 when calling the function fails.
1635 * Uses argv[argc] for the function arguments.
1636 */
1637 long
1638call_func_retnr(func, argc, argv, safe)
1639 char_u *func;
1640 int argc;
1641 char_u **argv;
1642 int safe; /* use the sandbox */
1643{
1644 typval_T rettv;
1645 long retval;
1646
1647 /* All arguments are passed as strings, no conversion to number. */
1648 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1649 return -1;
1650
1651 retval = get_tv_number_chk(&rettv, NULL);
1652 clear_tv(&rettv);
1653 return retval;
1654}
1655
1656#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1657 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1658
Bram Moolenaar4f688582007-07-24 12:34:30 +00001659# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001661 * Call vimL function "func" and return the result as a string.
1662 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 * Uses argv[argc] for the function arguments.
1664 */
1665 void *
1666call_func_retstr(func, argc, argv, safe)
1667 char_u *func;
1668 int argc;
1669 char_u **argv;
1670 int safe; /* use the sandbox */
1671{
1672 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001675 /* All arguments are passed as strings, no conversion to number. */
1676 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 return NULL;
1678
1679 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 return retval;
1682}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001683# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001688 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 */
1690 void *
1691call_func_retlist(func, argc, argv, safe)
1692 char_u *func;
1693 int argc;
1694 char_u **argv;
1695 int safe; /* use the sandbox */
1696{
1697 typval_T rettv;
1698
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001699 /* All arguments are passed as strings, no conversion to number. */
1700 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 return NULL;
1702
1703 if (rettv.v_type != VAR_LIST)
1704 {
1705 clear_tv(&rettv);
1706 return NULL;
1707 }
1708
1709 return rettv.vval.v_list;
1710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#endif
1712
1713/*
1714 * Save the current function call pointer, and set it to NULL.
1715 * Used when executing autocommands and for ":source".
1716 */
1717 void *
1718save_funccal()
1719{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 current_funccal = NULL;
1723 return (void *)fc;
1724}
1725
1726 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727restore_funccal(vfc)
1728 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001730 funccall_T *fc = (funccall_T *)vfc;
1731
1732 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733}
1734
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735#if defined(FEAT_PROFILE) || defined(PROTO)
1736/*
1737 * Prepare profiling for entering a child or something else that is not
1738 * counted for the script/function itself.
1739 * Should always be called in pair with prof_child_exit().
1740 */
1741 void
1742prof_child_enter(tm)
1743 proftime_T *tm; /* place to store waittime */
1744{
1745 funccall_T *fc = current_funccal;
1746
1747 if (fc != NULL && fc->func->uf_profiling)
1748 profile_start(&fc->prof_child);
1749 script_prof_save(tm);
1750}
1751
1752/*
1753 * Take care of time spent in a child.
1754 * Should always be called after prof_child_enter().
1755 */
1756 void
1757prof_child_exit(tm)
1758 proftime_T *tm; /* where waittime was stored */
1759{
1760 funccall_T *fc = current_funccal;
1761
1762 if (fc != NULL && fc->func->uf_profiling)
1763 {
1764 profile_end(&fc->prof_child);
1765 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1766 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1767 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1768 }
1769 script_prof_restore(tm);
1770}
1771#endif
1772
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_FOLDING
1775/*
1776 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1777 * it in "*cp". Doesn't give error messages.
1778 */
1779 int
1780eval_foldexpr(arg, cp)
1781 char_u *arg;
1782 int *cp;
1783{
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 int retval;
1786 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001787 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1788 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001791 if (use_sandbox)
1792 ++sandbox;
1793 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 if (tv.v_type == VAR_NUMBER)
1801 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001802 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 retval = 0;
1804 else
1805 {
1806 /* If the result is a string, check if there is a non-digit before
1807 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (!VIM_ISDIGIT(*s) && *s != '-')
1810 *cp = *s++;
1811 retval = atol((char *)s);
1812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001816 if (use_sandbox)
1817 --sandbox;
1818 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820 return retval;
1821}
1822#endif
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let" list all variable values
1826 * ":let var1 var2" list variable values
1827 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 * ":let var += expr" assignment command.
1829 * ":let var -= expr" assignment command.
1830 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 */
1833 void
1834ex_let(eap)
1835 exarg_T *eap;
1836{
1837 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 int var_count = 0;
1842 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001843 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaardb552d602006-03-23 22:59:57 +00001847 argend = skip_var_list(arg, &var_count, &semicolon);
1848 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001850 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1851 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001855 /*
1856 * ":let" without "=": list variables
1857 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 if (*arg == '[')
1859 EMSG(_(e_invarg));
1860 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_glob_vars(&first);
1867 list_buf_vars(&first);
1868 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001871#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_script_vars(&first);
1873 list_func_vars(&first);
1874 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 eap->nextcmd = check_nextcmd(arg);
1877 }
1878 else
1879 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 op[0] = '=';
1881 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 {
1884 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1885 op[0] = expr[-1]; /* +=, -= or .= */
1886 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (eap->skip)
1890 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (eap->skip)
1893 {
1894 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 --emsg_skip;
1897 }
1898 else if (i != FAIL)
1899 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 }
1905}
1906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907/*
1908 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1909 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 * When "nextchars" is not NULL it points to a string with characters that
1911 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1912 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 * Returns OK or FAIL;
1914 */
1915 static int
1916ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1917 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int copy; /* copy values from "tv", don't move */
1920 int semicolon; /* from skip_var_list() */
1921 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923{
1924 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 listitem_T *item;
1928 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929
1930 if (*arg != '[')
1931 {
1932 /*
1933 * ":let var = expr" or ":for var in list"
1934 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 return FAIL;
1937 return OK;
1938 }
1939
1940 /*
1941 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1942 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001943 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 {
1945 EMSG(_(e_listreq));
1946 return FAIL;
1947 }
1948
1949 i = list_len(l);
1950 if (semicolon == 0 && var_count < i)
1951 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001952 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 return FAIL;
1954 }
1955 if (var_count - semicolon > i)
1956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001957 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 }
1960
1961 item = l->lv_first;
1962 while (*arg != ']')
1963 {
1964 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 item = item->li_next;
1967 if (arg == NULL)
1968 return FAIL;
1969
1970 arg = skipwhite(arg);
1971 if (*arg == ';')
1972 {
1973 /* Put the rest of the list (may be empty) in the var after ';'.
1974 * Create a new list for this. */
1975 l = list_alloc();
1976 if (l == NULL)
1977 return FAIL;
1978 while (item != NULL)
1979 {
1980 list_append_tv(l, &item->li_tv);
1981 item = item->li_next;
1982 }
1983
1984 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001985 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 ltv.vval.v_list = l;
1987 l->lv_refcount = 1;
1988
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1990 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 clear_tv(&ltv);
1992 if (arg == NULL)
1993 return FAIL;
1994 break;
1995 }
1996 else if (*arg != ',' && *arg != ']')
1997 {
1998 EMSG2(_(e_intern2), "ex_let_vars()");
1999 return FAIL;
2000 }
2001 }
2002
2003 return OK;
2004}
2005
2006/*
2007 * Skip over assignable variable "var" or list of variables "[var, var]".
2008 * Used for ":let varvar = expr" and ":for varvar in expr".
2009 * For "[var, var]" increment "*var_count" for each variable.
2010 * for "[var, var; var]" set "semicolon".
2011 * Return NULL for an error.
2012 */
2013 static char_u *
2014skip_var_list(arg, var_count, semicolon)
2015 char_u *arg;
2016 int *var_count;
2017 int *semicolon;
2018{
2019 char_u *p, *s;
2020
2021 if (*arg == '[')
2022 {
2023 /* "[var, var]": find the matching ']'. */
2024 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002025 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 {
2027 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2028 s = skip_var_one(p);
2029 if (s == p)
2030 {
2031 EMSG2(_(e_invarg2), p);
2032 return NULL;
2033 }
2034 ++*var_count;
2035
2036 p = skipwhite(s);
2037 if (*p == ']')
2038 break;
2039 else if (*p == ';')
2040 {
2041 if (*semicolon == 1)
2042 {
2043 EMSG(_("Double ; in list of variables"));
2044 return NULL;
2045 }
2046 *semicolon = 1;
2047 }
2048 else if (*p != ',')
2049 {
2050 EMSG2(_(e_invarg2), p);
2051 return NULL;
2052 }
2053 }
2054 return p + 1;
2055 }
2056 else
2057 return skip_var_one(arg);
2058}
2059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002060/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002061 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 static char_u *
2065skip_var_one(arg)
2066 char_u *arg;
2067{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 if (*arg == '@' && arg[1] != NUL)
2069 return arg + 2;
2070 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2071 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072}
2073
Bram Moolenaara7043832005-01-21 11:56:39 +00002074/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 * List variables for hashtab "ht" with prefix "prefix".
2076 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002079list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084{
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 hashitem_T *hi;
2086 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 int todo;
2088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2091 {
2092 if (!HASHITEM_EMPTY(hi))
2093 {
2094 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 di = HI2DI(hi);
2096 if (empty || di->di_tv.v_type != VAR_STRING
2097 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 }
2100 }
2101}
2102
2103/*
2104 * List global variables.
2105 */
2106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_glob_vars(first)
2108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111}
2112
2113/*
2114 * List buffer variables.
2115 */
2116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_buf_vars(first)
2118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 char_u numbuf[NUMBUFLEN];
2121
Bram Moolenaar429fa852013-04-15 12:27:36 +02002122 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124
2125 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2127 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002128}
2129
2130/*
2131 * List window variables.
2132 */
2133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134list_win_vars(first)
2135 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141#ifdef FEAT_WINDOWS
2142/*
2143 * List tab page variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_tab_vars(first)
2147 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002151}
2152#endif
2153
Bram Moolenaara7043832005-01-21 11:56:39 +00002154/*
2155 * List Vim variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_vim_vars(first)
2159 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162}
2163
2164/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165 * List script-local variables, if there is a script.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_script_vars(first)
2169 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170{
2171 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2173 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174}
2175
2176/*
2177 * List function variables, if there is a function.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_func_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_funccal != NULL)
2184 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * List variables in "arg".
2190 */
2191 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 exarg_T *eap;
2194 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196{
2197 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 char_u *name_start;
2201 char_u *arg_subsc;
2202 char_u *tofree;
2203 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204
2205 while (!ends_excmd(*arg) && !got_int)
2206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002209 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2211 {
2212 emsg_severe = TRUE;
2213 EMSG(_(e_trailing));
2214 break;
2215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 /* get_name_len() takes care of expanding curly braces */
2220 name_start = name = arg;
2221 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2222 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224 /* This is mainly to keep test 49 working: when expanding
2225 * curly braces fails overrule the exception error message. */
2226 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 emsg_severe = TRUE;
2229 EMSG2(_(e_invarg2), arg);
2230 break;
2231 }
2232 error = TRUE;
2233 }
2234 else
2235 {
2236 if (tofree != NULL)
2237 name = tofree;
2238 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 /* handle d.key, l[idx], f(expr) */
2243 arg_subsc = arg;
2244 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'g': list_glob_vars(first); break;
2253 case 'b': list_buf_vars(first); break;
2254 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'v': list_vim_vars(first); break;
2259 case 's': list_script_vars(first); break;
2260 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 default:
2262 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 }
2265 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 {
2267 char_u numbuf[NUMBUFLEN];
2268 char_u *tf;
2269 int c;
2270 char_u *s;
2271
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002272 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 c = *arg;
2274 *arg = NUL;
2275 list_one_var_a((char_u *)"",
2276 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 tv.v_type,
2278 s == NULL ? (char_u *)"" : s,
2279 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 *arg = c;
2281 vim_free(tf);
2282 }
2283 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287
2288 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290
2291 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 }
2293
2294 return arg;
2295}
2296
2297/*
2298 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2299 * Returns a pointer to the char just after the var name.
2300 * Returns NULL if there is an error.
2301 */
2302 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002307 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309{
2310 int c1;
2311 char_u *name;
2312 char_u *p;
2313 char_u *arg_end = NULL;
2314 int len;
2315 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317
2318 /*
2319 * ":let $VAR = expr": Set environment variable.
2320 */
2321 if (*arg == '$')
2322 {
2323 /* Find the end of the name. */
2324 ++arg;
2325 name = arg;
2326 len = get_env_len(&arg);
2327 if (len == 0)
2328 EMSG2(_(e_invarg2), name - 1);
2329 else
2330 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 if (op != NULL && (*op == '+' || *op == '-'))
2332 EMSG2(_(e_letwrong), op);
2333 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002334 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002336 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
2338 c1 = name[len];
2339 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340 p = get_tv_string_chk(tv);
2341 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 {
2343 int mustfree = FALSE;
2344 char_u *s = vim_getenv(name, &mustfree);
2345
2346 if (s != NULL)
2347 {
2348 p = tofree = concat_str(s, p);
2349 if (mustfree)
2350 vim_free(s);
2351 }
2352 }
2353 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (STRICMP(name, "HOME") == 0)
2357 init_homedir();
2358 else if (didset_vim && STRICMP(name, "VIM") == 0)
2359 didset_vim = FALSE;
2360 else if (didset_vimruntime
2361 && STRICMP(name, "VIMRUNTIME") == 0)
2362 didset_vimruntime = FALSE;
2363 arg_end = arg;
2364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
2368 }
2369 }
2370
2371 /*
2372 * ":let &option = expr": Set option value.
2373 * ":let &l:option = expr": Set local option value.
2374 * ":let &g:option = expr": Set global option value.
2375 */
2376 else if (*arg == '&')
2377 {
2378 /* Find the end of the name. */
2379 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 if (p == NULL || (endchars != NULL
2381 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 EMSG(_(e_letunexp));
2383 else
2384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 long n;
2386 int opt_type;
2387 long numval;
2388 char_u *stringval = NULL;
2389 char_u *s;
2390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 c1 = *p;
2392 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393
2394 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 s = get_tv_string_chk(tv); /* != NULL if number or string */
2396 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 opt_type = get_option_value(arg, &numval,
2399 &stringval, opt_flags);
2400 if ((opt_type == 1 && *op == '.')
2401 || (opt_type == 0 && *op != '.'))
2402 EMSG2(_(e_letwrong), op);
2403 else
2404 {
2405 if (opt_type == 1) /* number */
2406 {
2407 if (*op == '+')
2408 n = numval + n;
2409 else
2410 n = numval - n;
2411 }
2412 else if (opt_type == 0 && stringval != NULL) /* string */
2413 {
2414 s = concat_str(stringval, s);
2415 vim_free(stringval);
2416 stringval = s;
2417 }
2418 }
2419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 if (s != NULL)
2421 {
2422 set_option_value(arg, n, s, opt_flags);
2423 arg_end = p;
2424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 }
2428 }
2429
2430 /*
2431 * ":let @r = expr": Set register contents.
2432 */
2433 else if (*arg == '@')
2434 {
2435 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 if (op != NULL && (*op == '+' || *op == '-'))
2437 EMSG2(_(e_letwrong), op);
2438 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 EMSG(_(e_letunexp));
2441 else
2442 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002443 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 char_u *s;
2445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 p = get_tv_string_chk(tv);
2447 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002449 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 if (s != NULL)
2451 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002452 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 vim_free(s);
2454 }
2455 }
2456 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 arg_end = arg + 1;
2460 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002461 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002469 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002473 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2477 EMSG(_(e_letunexp));
2478 else
2479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 arg_end = p;
2482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
2486
2487 else
2488 EMSG2(_(e_invarg2), arg);
2489
2490 return arg_end;
2491}
2492
2493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2495 */
2496 static int
2497check_changedtick(arg)
2498 char_u *arg;
2499{
2500 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2501 {
2502 EMSG2(_(e_readonlyvar), arg);
2503 return TRUE;
2504 }
2505 return FALSE;
2506}
2507
2508/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * Get an lval: variable, Dict item or List item that can be assigned a value
2510 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2511 * "name.key", "name.key[expr]" etc.
2512 * Indexing only works if "name" is an existing List or Dictionary.
2513 * "name" points to the start of the name.
2514 * If "rettv" is not NULL it points to the value to be assigned.
2515 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2516 * wrong; must end in space or cmd separator.
2517 *
2518 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002519 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 */
2522 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 typval_T *rettv;
2526 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 int unlet;
2528 int skip;
2529 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002530 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 char_u *expr_start, *expr_end;
2534 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 dictitem_T *v;
2536 typval_T var1;
2537 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546
2547 if (skip)
2548 {
2549 /* When skipping just find the end of the name. */
2550 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002551 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 }
2553
2554 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (expr_start != NULL)
2557 {
2558 /* Don't expand the name when we already know there is an error. */
2559 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2560 && *p != '[' && *p != '.')
2561 {
2562 EMSG(_(e_trailing));
2563 return NULL;
2564 }
2565
2566 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2567 if (lp->ll_exp_name == NULL)
2568 {
2569 /* Report an invalid expression in braces, unless the
2570 * expression evaluation has been cancelled due to an
2571 * aborting error, an interrupt, or an exception. */
2572 if (!aborting() && !quiet)
2573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002574 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 EMSG2(_(e_invarg2), name);
2576 return NULL;
2577 }
2578 }
2579 lp->ll_name = lp->ll_exp_name;
2580 }
2581 else
2582 lp->ll_name = name;
2583
2584 /* Without [idx] or .key we are done. */
2585 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2586 return p;
2587
2588 cc = *p;
2589 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002590 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (v == NULL && !quiet)
2592 EMSG2(_(e_undefvar), lp->ll_name);
2593 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 if (v == NULL)
2595 return NULL;
2596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /*
2598 * Loop until no more [idx] or .key is following.
2599 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2604 && !(lp->ll_tv->v_type == VAR_DICT
2605 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E689: Can only index a List or Dictionary"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSG(_("E708: [:] must come last"));
2615 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 len = -1;
2619 if (*p == '.')
2620 {
2621 key = p + 1;
2622 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2623 ;
2624 if (len == 0)
2625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (!quiet)
2627 EMSG(_(e_emptykey));
2628 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 }
2630 p = key + len;
2631 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 if (*p == ':')
2637 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 else
2639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 empty1 = FALSE;
2641 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002643 if (get_tv_string_chk(&var1) == NULL)
2644 {
2645 /* not a number or string */
2646 clear_tv(&var1);
2647 return NULL;
2648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650
2651 /* Optionally get the second index [ :expr]. */
2652 if (*p == ':')
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002657 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 if (!empty1)
2659 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (rettv != NULL && (rettv->v_type != VAR_LIST
2663 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (!empty1)
2668 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671 p = skipwhite(p + 1);
2672 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 else
2675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 if (get_tv_string_chk(&var2) == NULL)
2684 {
2685 /* not a number or string */
2686 if (!empty1)
2687 clear_tv(&var1);
2688 clear_tv(&var2);
2689 return NULL;
2690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707
2708 /* Skip to past ']'. */
2709 ++p;
2710 }
2711
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
2714 if (len == -1)
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (*key == NUL)
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (!quiet)
2721 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 lp->ll_list = NULL;
2727 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002728 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002729
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002730 /* When assigning to a scope dictionary check that a function and
2731 * variable name is valid (only variable name unless it is l: or
2732 * g: dictionary). Disallow overwriting a builtin function. */
2733 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002734 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002735 int prevval;
2736 int wrong;
2737
2738 if (len != -1)
2739 {
2740 prevval = key[len];
2741 key[len] = NUL;
2742 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002743 else
2744 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002745 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2746 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002747 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002748 || !valid_varname(key);
2749 if (len != -1)
2750 key[len] = prevval;
2751 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002752 return NULL;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 /* Can't add "v:" variable. */
2758 if (lp->ll_dict == &vimvardict)
2759 {
2760 EMSG2(_(e_illvar), name);
2761 return NULL;
2762 }
2763
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002764 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (len == -1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (len == -1)
2778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 p = NULL;
2781 break;
2782 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* existing variable, need to check if it can be changed */
2784 else if (var_check_ro(lp->ll_di->di_flags, name))
2785 return NULL;
2786
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 else
2792 {
2793 /*
2794 * Get the number and item for the only or first index of the List.
2795 */
2796 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 else
2799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002800 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 clear_tv(&var1);
2802 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 lp->ll_list = lp->ll_tv->vval.v_list;
2805 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2806 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002808 if (lp->ll_n1 < 0)
2809 {
2810 lp->ll_n1 = 0;
2811 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2812 }
2813 }
2814 if (lp->ll_li == NULL)
2815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002818 if (!quiet)
2819 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 }
2822
2823 /*
2824 * May need to find the item or absolute index for the second
2825 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 * When no index given: "lp->ll_empty2" is TRUE.
2827 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002831 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 {
2838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2846 if (lp->ll_n1 < 0)
2847 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2848 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 {
2850 if (!quiet)
2851 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 }
2859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return p;
2861}
2862
2863/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 */
2866 static void
2867clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869{
2870 vim_free(lp->ll_exp_name);
2871 vim_free(lp->ll_newkey);
2872}
2873
2874/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002875 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 */
2879 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002885 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886{
2887 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 listitem_T *ri;
2889 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890
2891 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 cc = *endp;
2896 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 if (op != NULL && *op != '=')
2898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002902 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002903 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 {
2905 if (tv_op(&tv, rettv, op) == OK)
2906 set_var(lp->ll_name, &tv, FALSE);
2907 clear_tv(&tv);
2908 }
2909 }
2910 else
2911 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 else if (tv_check_lock(lp->ll_newkey == NULL
2916 ? lp->ll_tv->v_lock
2917 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2918 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 else if (lp->ll_range)
2920 {
2921 /*
2922 * Assign the List values to the list items.
2923 */
2924 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 if (op != NULL && *op != '=')
2927 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2928 else
2929 {
2930 clear_tv(&lp->ll_li->li_tv);
2931 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 ri = ri->li_next;
2934 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2935 break;
2936 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002939 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 ri = NULL;
2942 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 lp->ll_li = lp->ll_li->li_next;
2946 ++lp->ll_n1;
2947 }
2948 if (ri != NULL)
2949 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 else if (lp->ll_empty2
2951 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 : lp->ll_n1 != lp->ll_n2)
2953 EMSG(_("E711: List value has not enough items"));
2954 }
2955 else
2956 {
2957 /*
2958 * Assign to a List or Dictionary item.
2959 */
2960 if (lp->ll_newkey != NULL)
2961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 if (op != NULL && *op != '=')
2963 {
2964 EMSG2(_(e_letwrong), op);
2965 return;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002969 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 if (di == NULL)
2971 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002972 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2973 {
2974 vim_free(di);
2975 return;
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 else if (op != NULL && *op != '=')
2980 {
2981 tv_op(lp->ll_tv, rettv, op);
2982 return;
2983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002986
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /*
2988 * Assign the value to the variable or list item.
2989 */
2990 if (copy)
2991 copy_tv(rettv, lp->ll_tv);
2992 else
2993 {
2994 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002995 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 }
2998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999}
3000
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3003 * Returns OK or FAIL.
3004 */
3005 static int
3006tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003007 typval_T *tv1;
3008 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 char_u *op;
3010{
3011 long n;
3012 char_u numbuf[NUMBUFLEN];
3013 char_u *s;
3014
3015 /* Can't do anything with a Funcref or a Dict on the right. */
3016 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3017 {
3018 switch (tv1->v_type)
3019 {
3020 case VAR_DICT:
3021 case VAR_FUNC:
3022 break;
3023
3024 case VAR_LIST:
3025 if (*op != '+' || tv2->v_type != VAR_LIST)
3026 break;
3027 /* List += List */
3028 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3029 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3030 return OK;
3031
3032 case VAR_NUMBER:
3033 case VAR_STRING:
3034 if (tv2->v_type == VAR_LIST)
3035 break;
3036 if (*op == '+' || *op == '-')
3037 {
3038 /* nr += nr or nr -= nr*/
3039 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040#ifdef FEAT_FLOAT
3041 if (tv2->v_type == VAR_FLOAT)
3042 {
3043 float_T f = n;
3044
3045 if (*op == '+')
3046 f += tv2->vval.v_float;
3047 else
3048 f -= tv2->vval.v_float;
3049 clear_tv(tv1);
3050 tv1->v_type = VAR_FLOAT;
3051 tv1->vval.v_float = f;
3052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054#endif
3055 {
3056 if (*op == '+')
3057 n += get_tv_number(tv2);
3058 else
3059 n -= get_tv_number(tv2);
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_NUMBER;
3062 tv1->vval.v_number = n;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 }
3065 else
3066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067 if (tv2->v_type == VAR_FLOAT)
3068 break;
3069
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 /* str .= str */
3071 s = get_tv_string(tv1);
3072 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3073 clear_tv(tv1);
3074 tv1->v_type = VAR_STRING;
3075 tv1->vval.v_string = s;
3076 }
3077 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078
3079#ifdef FEAT_FLOAT
3080 case VAR_FLOAT:
3081 {
3082 float_T f;
3083
3084 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3085 && tv2->v_type != VAR_NUMBER
3086 && tv2->v_type != VAR_STRING))
3087 break;
3088 if (tv2->v_type == VAR_FLOAT)
3089 f = tv2->vval.v_float;
3090 else
3091 f = get_tv_number(tv2);
3092 if (*op == '+')
3093 tv1->vval.v_float += f;
3094 else
3095 tv1->vval.v_float -= f;
3096 }
3097 return OK;
3098#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 }
3100 }
3101
3102 EMSG2(_(e_letwrong), op);
3103 return FAIL;
3104}
3105
3106/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 * Add a watcher to a list.
3108 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003109 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 list_T *l;
3112 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113{
3114 lw->lw_next = l->lv_watch;
3115 l->lv_watch = lw;
3116}
3117
3118/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003119 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 * No warning when it isn't found...
3121 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003122 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003124 list_T *l;
3125 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126{
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128
3129 lwp = &l->lv_watch;
3130 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3131 {
3132 if (lw == lwrem)
3133 {
3134 *lwp = lw->lw_next;
3135 break;
3136 }
3137 lwp = &lw->lw_next;
3138 }
3139}
3140
3141/*
3142 * Just before removing an item from a list: advance watchers to the next
3143 * item.
3144 */
3145 static void
3146list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 list_T *l;
3148 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151
3152 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3153 if (lw->lw_item == item)
3154 lw->lw_item = item->li_next;
3155}
3156
3157/*
3158 * Evaluate the expression used in a ":for var in expr" command.
3159 * "arg" points to "var".
3160 * Set "*errp" to TRUE for an error, FALSE otherwise;
3161 * Return a pointer that holds the info. Null when there is an error.
3162 */
3163 void *
3164eval_for_line(arg, errp, nextcmdp, skip)
3165 char_u *arg;
3166 int *errp;
3167 char_u **nextcmdp;
3168 int skip;
3169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 typval_T tv;
3173 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174
3175 *errp = TRUE; /* default: there is an error */
3176
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 if (fi == NULL)
3179 return NULL;
3180
3181 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3182 if (expr == NULL)
3183 return fi;
3184
3185 expr = skipwhite(expr);
3186 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3187 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003188 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 return fi;
3190 }
3191
3192 if (skip)
3193 ++emsg_skip;
3194 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3195 {
3196 *errp = FALSE;
3197 if (!skip)
3198 {
3199 l = tv.vval.v_list;
3200 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 clear_tv(&tv);
3204 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 else
3206 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003207 /* No need to increment the refcount, it's already set for the
3208 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 fi->fi_list = l;
3210 list_add_watch(l, &fi->fi_lw);
3211 fi->fi_lw.lw_item = l->lv_first;
3212 }
3213 }
3214 }
3215 if (skip)
3216 --emsg_skip;
3217
3218 return fi;
3219}
3220
3221/*
3222 * Use the first item in a ":for" list. Advance to the next.
3223 * Assign the values to the variable (list). "arg" points to the first one.
3224 * Return TRUE when a valid item was found, FALSE when at end of list or
3225 * something wrong.
3226 */
3227 int
3228next_for_item(fi_void, arg)
3229 void *fi_void;
3230 char_u *arg;
3231{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003232 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 item = fi->fi_lw.lw_item;
3237 if (item == NULL)
3238 result = FALSE;
3239 else
3240 {
3241 fi->fi_lw.lw_item = item->li_next;
3242 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3243 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3244 }
3245 return result;
3246}
3247
3248/*
3249 * Free the structure used to store info used by ":for".
3250 */
3251 void
3252free_for_info(fi_void)
3253 void *fi_void;
3254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003257 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 list_unref(fi->fi_list);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 vim_free(fi);
3263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3266
3267 void
3268set_context_for_expression(xp, arg, cmdidx)
3269 expand_T *xp;
3270 char_u *arg;
3271 cmdidx_T cmdidx;
3272{
3273 int got_eq = FALSE;
3274 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 if (cmdidx == CMD_let)
3278 {
3279 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003280 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 {
3282 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003283 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 {
3285 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (vim_iswhite(*p))
3288 break;
3289 }
3290 return;
3291 }
3292 }
3293 else
3294 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3295 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 while ((xp->xp_pattern = vim_strpbrk(arg,
3297 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3298 {
3299 c = *xp->xp_pattern;
3300 if (c == '&')
3301 {
3302 c = xp->xp_pattern[1];
3303 if (c == '&')
3304 {
3305 ++xp->xp_pattern;
3306 xp->xp_context = cmdidx != CMD_let || got_eq
3307 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3308 }
3309 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003312 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3313 xp->xp_pattern += 2;
3314
3315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 else if (c == '$')
3318 {
3319 /* environment variable */
3320 xp->xp_context = EXPAND_ENV_VARS;
3321 }
3322 else if (c == '=')
3323 {
3324 got_eq = TRUE;
3325 xp->xp_context = EXPAND_EXPRESSION;
3326 }
3327 else if (c == '<'
3328 && xp->xp_context == EXPAND_FUNCTIONS
3329 && vim_strchr(xp->xp_pattern, '(') == NULL)
3330 {
3331 /* Function name can start with "<SNR>" */
3332 break;
3333 }
3334 else if (cmdidx != CMD_let || got_eq)
3335 {
3336 if (c == '"') /* string */
3337 {
3338 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3339 if (c == '\\' && xp->xp_pattern[1] != NUL)
3340 ++xp->xp_pattern;
3341 xp->xp_context = EXPAND_NOTHING;
3342 }
3343 else if (c == '\'') /* literal string */
3344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3347 /* skip */ ;
3348 xp->xp_context = EXPAND_NOTHING;
3349 }
3350 else if (c == '|')
3351 {
3352 if (xp->xp_pattern[1] == '|')
3353 {
3354 ++xp->xp_pattern;
3355 xp->xp_context = EXPAND_EXPRESSION;
3356 }
3357 else
3358 xp->xp_context = EXPAND_COMMANDS;
3359 }
3360 else
3361 xp->xp_context = EXPAND_EXPRESSION;
3362 }
3363 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364 /* Doesn't look like something valid, expand as an expression
3365 * anyway. */
3366 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 arg = xp->xp_pattern;
3368 if (*arg != NUL)
3369 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3370 /* skip */ ;
3371 }
3372 xp->xp_pattern = arg;
3373}
3374
3375#endif /* FEAT_CMDL_COMPL */
3376
3377/*
3378 * ":1,25call func(arg1, arg2)" function call.
3379 */
3380 void
3381ex_call(eap)
3382 exarg_T *eap;
3383{
3384 char_u *arg = eap->arg;
3385 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003387 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 linenr_T lnum;
3391 int doesrange;
3392 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003395 if (eap->skip)
3396 {
3397 /* trans_function_name() doesn't work well when skipping, use eval0()
3398 * instead to skip to any following command, e.g. for:
3399 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003400 ++emsg_skip;
3401 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3402 clear_tv(&rettv);
3403 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003404 return;
3405 }
3406
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003407 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003408 if (fudi.fd_newkey != NULL)
3409 {
3410 /* Still need to give an error message for missing key. */
3411 EMSG2(_(e_dictkey), fudi.fd_newkey);
3412 vim_free(fudi.fd_newkey);
3413 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003414 if (tofree == NULL)
3415 return;
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 /* Increase refcount on dictionary, it could get deleted when evaluating
3418 * the arguments. */
3419 if (fudi.fd_dict != NULL)
3420 ++fudi.fd_dict->dv_refcount;
3421
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003422 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar532c7802005-01-27 14:44:31 +00003426 /* Skip white space to allow ":call func ()". Not good, but required for
3427 * backward compatibility. */
3428 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 if (*startarg != '(')
3432 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003433 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 goto end;
3435 }
3436
3437 /*
3438 * When skipping, evaluate the function once, to find the end of the
3439 * arguments.
3440 * When the function takes a range, this is discovered after the first
3441 * call, and the loop is broken.
3442 */
3443 if (eap->skip)
3444 {
3445 ++emsg_skip;
3446 lnum = eap->line2; /* do it once, also with an invalid range */
3447 }
3448 else
3449 lnum = eap->line1;
3450 for ( ; lnum <= eap->line2; ++lnum)
3451 {
3452 if (!eap->skip && eap->addr_count > 0)
3453 {
3454 curwin->w_cursor.lnum = lnum;
3455 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003456#ifdef FEAT_VIRTUALEDIT
3457 curwin->w_cursor.coladd = 0;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003461 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003462 eap->line1, eap->line2, &doesrange,
3463 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 failed = TRUE;
3466 break;
3467 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003468
3469 /* Handle a function returning a Funcref, Dictionary or List. */
3470 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3471 {
3472 failed = TRUE;
3473 break;
3474 }
3475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (doesrange || eap->skip)
3478 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (aborting())
3485 break;
3486 }
3487 if (eap->skip)
3488 --emsg_skip;
3489
3490 if (!failed)
3491 {
3492 /* Check for trailing illegal characters and a following command. */
3493 if (!ends_excmd(*arg))
3494 {
3495 emsg_severe = TRUE;
3496 EMSG(_(e_trailing));
3497 }
3498 else
3499 eap->nextcmd = check_nextcmd(arg);
3500 }
3501
3502end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003503 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003504 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505}
3506
3507/*
3508 * ":unlet[!] var1 ... " command.
3509 */
3510 void
3511ex_unlet(eap)
3512 exarg_T *eap;
3513{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003514 ex_unletlock(eap, eap->arg, 0);
3515}
3516
3517/*
3518 * ":lockvar" and ":unlockvar" commands
3519 */
3520 void
3521ex_lockvar(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 int deep = 2;
3526
3527 if (eap->forceit)
3528 deep = -1;
3529 else if (vim_isdigit(*arg))
3530 {
3531 deep = getdigits(&arg);
3532 arg = skipwhite(arg);
3533 }
3534
3535 ex_unletlock(eap, arg, deep);
3536}
3537
3538/*
3539 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3540 */
3541 static void
3542ex_unletlock(eap, argstart, deep)
3543 exarg_T *eap;
3544 char_u *argstart;
3545 int deep;
3546{
3547 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 do
3553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003555 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3556 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 if (lv.ll_name == NULL)
3558 error = TRUE; /* error but continue parsing */
3559 if (name_end == NULL || (!vim_iswhite(*name_end)
3560 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 if (name_end != NULL)
3563 {
3564 emsg_severe = TRUE;
3565 EMSG(_(e_trailing));
3566 }
3567 if (!(eap->skip || error))
3568 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
3570 }
3571
3572 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 {
3574 if (eap->cmdidx == CMD_unlet)
3575 {
3576 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3577 error = TRUE;
3578 }
3579 else
3580 {
3581 if (do_lock_var(&lv, name_end, deep,
3582 eap->cmdidx == CMD_lockvar) == FAIL)
3583 error = TRUE;
3584 }
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 if (!eap->skip)
3588 clear_lval(&lv);
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 arg = skipwhite(name_end);
3591 } while (!ends_excmd(*arg));
3592
3593 eap->nextcmd = check_nextcmd(arg);
3594}
3595
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 int forceit;
3601{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 int ret = OK;
3603 int cc;
3604
3605 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 cc = *name_end;
3608 *name_end = NUL;
3609
3610 /* Normal name or expanded name. */
3611 if (check_changedtick(lp->ll_name))
3612 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3618 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 else if (lp->ll_range)
3620 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622
3623 /* Delete a range of List items. */
3624 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3625 {
3626 li = lp->ll_li->li_next;
3627 listitem_remove(lp->ll_list, lp->ll_li);
3628 lp->ll_li = li;
3629 ++lp->ll_n1;
3630 }
3631 }
3632 else
3633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 /* unlet a List item. */
3636 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003639 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 }
3641
3642 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645/*
3646 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
3649 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 hashtab_T *ht;
3655 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003657 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 ht = find_var_ht(name, &varname);
3660 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 hi = hash_find(ht, varname);
3663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003664 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003665 di = HI2DI(hi);
3666 if (var_check_fixed(di->di_flags, name)
3667 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 return FAIL;
3669 delete_var(ht, hi);
3670 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 if (forceit)
3674 return OK;
3675 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677}
3678
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679/*
3680 * Lock or unlock variable indicated by "lp".
3681 * "deep" is the levels to go (-1 for unlimited);
3682 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3683 */
3684 static int
3685do_lock_var(lp, name_end, deep, lock)
3686 lval_T *lp;
3687 char_u *name_end;
3688 int deep;
3689 int lock;
3690{
3691 int ret = OK;
3692 int cc;
3693 dictitem_T *di;
3694
3695 if (deep == 0) /* nothing to do */
3696 return OK;
3697
3698 if (lp->ll_tv == NULL)
3699 {
3700 cc = *name_end;
3701 *name_end = NUL;
3702
3703 /* Normal name or expanded name. */
3704 if (check_changedtick(lp->ll_name))
3705 ret = FAIL;
3706 else
3707 {
3708 di = find_var(lp->ll_name, NULL);
3709 if (di == NULL)
3710 ret = FAIL;
3711 else
3712 {
3713 if (lock)
3714 di->di_flags |= DI_FLAGS_LOCK;
3715 else
3716 di->di_flags &= ~DI_FLAGS_LOCK;
3717 item_lock(&di->di_tv, deep, lock);
3718 }
3719 }
3720 *name_end = cc;
3721 }
3722 else if (lp->ll_range)
3723 {
3724 listitem_T *li = lp->ll_li;
3725
3726 /* (un)lock a range of List items. */
3727 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3728 {
3729 item_lock(&li->li_tv, deep, lock);
3730 li = li->li_next;
3731 ++lp->ll_n1;
3732 }
3733 }
3734 else if (lp->ll_list != NULL)
3735 /* (un)lock a List item. */
3736 item_lock(&lp->ll_li->li_tv, deep, lock);
3737 else
3738 /* un(lock) a Dictionary item. */
3739 item_lock(&lp->ll_di->di_tv, deep, lock);
3740
3741 return ret;
3742}
3743
3744/*
3745 * Lock or unlock an item. "deep" is nr of levels to go.
3746 */
3747 static void
3748item_lock(tv, deep, lock)
3749 typval_T *tv;
3750 int deep;
3751 int lock;
3752{
3753 static int recurse = 0;
3754 list_T *l;
3755 listitem_T *li;
3756 dict_T *d;
3757 hashitem_T *hi;
3758 int todo;
3759
3760 if (recurse >= DICT_MAXNEST)
3761 {
3762 EMSG(_("E743: variable nested too deep for (un)lock"));
3763 return;
3764 }
3765 if (deep == 0)
3766 return;
3767 ++recurse;
3768
3769 /* lock/unlock the item itself */
3770 if (lock)
3771 tv->v_lock |= VAR_LOCKED;
3772 else
3773 tv->v_lock &= ~VAR_LOCKED;
3774
3775 switch (tv->v_type)
3776 {
3777 case VAR_LIST:
3778 if ((l = tv->vval.v_list) != NULL)
3779 {
3780 if (lock)
3781 l->lv_lock |= VAR_LOCKED;
3782 else
3783 l->lv_lock &= ~VAR_LOCKED;
3784 if (deep < 0 || deep > 1)
3785 /* recursive: lock/unlock the items the List contains */
3786 for (li = l->lv_first; li != NULL; li = li->li_next)
3787 item_lock(&li->li_tv, deep - 1, lock);
3788 }
3789 break;
3790 case VAR_DICT:
3791 if ((d = tv->vval.v_dict) != NULL)
3792 {
3793 if (lock)
3794 d->dv_lock |= VAR_LOCKED;
3795 else
3796 d->dv_lock &= ~VAR_LOCKED;
3797 if (deep < 0 || deep > 1)
3798 {
3799 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003800 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3802 {
3803 if (!HASHITEM_EMPTY(hi))
3804 {
3805 --todo;
3806 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3807 }
3808 }
3809 }
3810 }
3811 }
3812 --recurse;
3813}
3814
Bram Moolenaara40058a2005-07-11 22:42:07 +00003815/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003816 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3817 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003818 */
3819 static int
3820tv_islocked(tv)
3821 typval_T *tv;
3822{
3823 return (tv->v_lock & VAR_LOCKED)
3824 || (tv->v_type == VAR_LIST
3825 && tv->vval.v_list != NULL
3826 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3827 || (tv->v_type == VAR_DICT
3828 && tv->vval.v_dict != NULL
3829 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3830}
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3833/*
3834 * Delete all "menutrans_" variables.
3835 */
3836 void
3837del_menutrans_vars()
3838{
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003840 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003843 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003844 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 {
3846 if (!HASHITEM_EMPTY(hi))
3847 {
3848 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3850 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 }
3852 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854}
3855#endif
3856
3857#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3858
3859/*
3860 * Local string buffer for the next two functions to store a variable name
3861 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3862 * get_user_var_name().
3863 */
3864
3865static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3866
3867static char_u *varnamebuf = NULL;
3868static int varnamebuflen = 0;
3869
3870/*
3871 * Function to concatenate a prefix and a variable name.
3872 */
3873 static char_u *
3874cat_prefix_varname(prefix, name)
3875 int prefix;
3876 char_u *name;
3877{
3878 int len;
3879
3880 len = (int)STRLEN(name) + 3;
3881 if (len > varnamebuflen)
3882 {
3883 vim_free(varnamebuf);
3884 len += 10; /* some additional space */
3885 varnamebuf = alloc(len);
3886 if (varnamebuf == NULL)
3887 {
3888 varnamebuflen = 0;
3889 return NULL;
3890 }
3891 varnamebuflen = len;
3892 }
3893 *varnamebuf = prefix;
3894 varnamebuf[1] = ':';
3895 STRCPY(varnamebuf + 2, name);
3896 return varnamebuf;
3897}
3898
3899/*
3900 * Function given to ExpandGeneric() to obtain the list of user defined
3901 * (global/buffer/window/built-in) variable names.
3902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 char_u *
3904get_user_var_name(xp, idx)
3905 expand_T *xp;
3906 int idx;
3907{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 static long_u gdone;
3909 static long_u bdone;
3910 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003911#ifdef FEAT_WINDOWS
3912 static long_u tdone;
3913#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003914 static int vidx;
3915 static hashitem_T *hi;
3916 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003919 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003920 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 tdone = 0;
3923#endif
3924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003925
3926 /* Global variables */
3927 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 else
3932 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 while (HASHITEM_EMPTY(hi))
3934 ++hi;
3935 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3936 return cat_prefix_varname('g', hi->hi_key);
3937 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003939
3940 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003941 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003946 else
3947 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 while (HASHITEM_EMPTY(hi))
3949 ++hi;
3950 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return (char_u *)"b:changedtick";
3956 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003957
3958 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003959 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003962 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003964 else
3965 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003966 while (HASHITEM_EMPTY(hi))
3967 ++hi;
3968 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003970
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003971#ifdef FEAT_WINDOWS
3972 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003973 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 if (tdone < ht->ht_used)
3975 {
3976 if (tdone++ == 0)
3977 hi = ht->ht_array;
3978 else
3979 ++hi;
3980 while (HASHITEM_EMPTY(hi))
3981 ++hi;
3982 return cat_prefix_varname('t', hi->hi_key);
3983 }
3984#endif
3985
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 /* v: variables */
3987 if (vidx < VV_LEN)
3988 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 vim_free(varnamebuf);
3991 varnamebuf = NULL;
3992 varnamebuflen = 0;
3993 return NULL;
3994}
3995
3996#endif /* FEAT_CMDL_COMPL */
3997
3998/*
3999 * types for expressions.
4000 */
4001typedef enum
4002{
4003 TYPE_UNKNOWN = 0
4004 , TYPE_EQUAL /* == */
4005 , TYPE_NEQUAL /* != */
4006 , TYPE_GREATER /* > */
4007 , TYPE_GEQUAL /* >= */
4008 , TYPE_SMALLER /* < */
4009 , TYPE_SEQUAL /* <= */
4010 , TYPE_MATCH /* =~ */
4011 , TYPE_NOMATCH /* !~ */
4012} exptype_T;
4013
4014/*
4015 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4018 */
4019
4020/*
4021 * Handle zero level expression.
4022 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004024 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 * Return OK or FAIL.
4026 */
4027 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 char_u **nextcmd;
4032 int evaluate;
4033{
4034 int ret;
4035 char_u *p;
4036
4037 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (ret == FAIL || !ends_excmd(*p))
4040 {
4041 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /*
4044 * Report the invalid expression unless the expression evaluation has
4045 * been cancelled due to an aborting error, an interrupt, or an
4046 * exception.
4047 */
4048 if (!aborting())
4049 EMSG2(_(e_invexpr2), arg);
4050 ret = FAIL;
4051 }
4052 if (nextcmd != NULL)
4053 *nextcmd = check_nextcmd(p);
4054
4055 return ret;
4056}
4057
4058/*
4059 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004060 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 *
4062 * "arg" must point to the first non-white of the expression.
4063 * "arg" is advanced to the next non-white after the recognized expression.
4064 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004065 * Note: "rettv.v_lock" is not set.
4066 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Return OK or FAIL.
4068 */
4069 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 int evaluate;
4074{
4075 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 /*
4079 * Get the first variable.
4080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return FAIL;
4083
4084 if ((*arg)[0] == '?')
4085 {
4086 result = FALSE;
4087 if (evaluate)
4088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 int error = FALSE;
4090
4091 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (error)
4095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 /*
4099 * Get the second variable.
4100 */
4101 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return FAIL;
4104
4105 /*
4106 * Check for the ":".
4107 */
4108 if ((*arg)[0] != ':')
4109 {
4110 EMSG(_("E109: Missing ':' after '?'"));
4111 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114 }
4115
4116 /*
4117 * Get the third variable.
4118 */
4119 *arg = skipwhite(*arg + 1);
4120 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4121 {
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129
4130 return OK;
4131}
4132
4133/*
4134 * Handle first level expression:
4135 * expr2 || expr2 || expr2 logical OR
4136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
4140 * Return OK or FAIL.
4141 */
4142 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int evaluate;
4147{
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 long result;
4150 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004151 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Repeat until there is no following "||".
4161 */
4162 first = TRUE;
4163 result = FALSE;
4164 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4165 {
4166 if (evaluate && first)
4167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (error)
4172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 first = FALSE;
4174 }
4175
4176 /*
4177 * Get the second variable.
4178 */
4179 *arg = skipwhite(*arg + 2);
4180 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4181 return FAIL;
4182
4183 /*
4184 * Compute the result.
4185 */
4186 if (evaluate && !result)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 if (evaluate)
4195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 rettv->v_type = VAR_NUMBER;
4197 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200
4201 return OK;
4202}
4203
4204/*
4205 * Handle second level expression:
4206 * expr3 && expr3 && expr3 logical AND
4207 *
4208 * "arg" must point to the first non-white of the expression.
4209 * "arg" is advanced to the next non-white after the recognized expression.
4210 *
4211 * Return OK or FAIL.
4212 */
4213 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 int evaluate;
4218{
Bram Moolenaar33570922005-01-25 22:26:29 +00004219 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 long result;
4221 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 /*
4225 * Get the first variable.
4226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229
4230 /*
4231 * Repeat until there is no following "&&".
4232 */
4233 first = TRUE;
4234 result = TRUE;
4235 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4236 {
4237 if (evaluate && first)
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (error)
4243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 first = FALSE;
4245 }
4246
4247 /*
4248 * Get the second variable.
4249 */
4250 *arg = skipwhite(*arg + 2);
4251 if (eval4(arg, &var2, evaluate && result) == FAIL)
4252 return FAIL;
4253
4254 /*
4255 * Compute the result.
4256 */
4257 if (evaluate && result)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 if (evaluate)
4266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 rettv->v_type = VAR_NUMBER;
4268 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 }
4271
4272 return OK;
4273}
4274
4275/*
4276 * Handle third level expression:
4277 * var1 == var2
4278 * var1 =~ var2
4279 * var1 != var2
4280 * var1 !~ var2
4281 * var1 > var2
4282 * var1 >= var2
4283 * var1 < var2
4284 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004285 * var1 is var2
4286 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 *
4288 * "arg" must point to the first non-white of the expression.
4289 * "arg" is advanced to the next non-white after the recognized expression.
4290 *
4291 * Return OK or FAIL.
4292 */
4293 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 int evaluate;
4298{
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 char_u *p;
4301 int i;
4302 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004303 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 int len = 2;
4305 long n1, n2;
4306 char_u *s1, *s2;
4307 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4308 regmatch_T regmatch;
4309 int ic;
4310 char_u *save_cpo;
4311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 p = *arg;
4319 switch (p[0])
4320 {
4321 case '=': if (p[1] == '=')
4322 type = TYPE_EQUAL;
4323 else if (p[1] == '~')
4324 type = TYPE_MATCH;
4325 break;
4326 case '!': if (p[1] == '=')
4327 type = TYPE_NEQUAL;
4328 else if (p[1] == '~')
4329 type = TYPE_NOMATCH;
4330 break;
4331 case '>': if (p[1] != '=')
4332 {
4333 type = TYPE_GREATER;
4334 len = 1;
4335 }
4336 else
4337 type = TYPE_GEQUAL;
4338 break;
4339 case '<': if (p[1] != '=')
4340 {
4341 type = TYPE_SMALLER;
4342 len = 1;
4343 }
4344 else
4345 type = TYPE_SEQUAL;
4346 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004347 case 'i': if (p[1] == 's')
4348 {
4349 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4350 len = 5;
4351 if (!vim_isIDc(p[len]))
4352 {
4353 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4354 type_is = TRUE;
4355 }
4356 }
4357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359
4360 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
4363 if (type != TYPE_UNKNOWN)
4364 {
4365 /* extra question mark appended: ignore case */
4366 if (p[len] == '?')
4367 {
4368 ic = TRUE;
4369 ++len;
4370 }
4371 /* extra '#' appended: match case */
4372 else if (p[len] == '#')
4373 {
4374 ic = FALSE;
4375 ++len;
4376 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004377 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 else
4379 ic = p_ic;
4380
4381 /*
4382 * Get the second variable.
4383 */
4384 *arg = skipwhite(p + len);
4385 if (eval5(arg, &var2, evaluate) == FAIL)
4386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 return FAIL;
4389 }
4390
4391 if (evaluate)
4392 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 if (type_is && rettv->v_type != var2.v_type)
4394 {
4395 /* For "is" a different type always means FALSE, for "notis"
4396 * it means TRUE. */
4397 n1 = (type == TYPE_NEQUAL);
4398 }
4399 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4400 {
4401 if (type_is)
4402 {
4403 n1 = (rettv->v_type == var2.v_type
4404 && rettv->vval.v_list == var2.vval.v_list);
4405 if (type == TYPE_NEQUAL)
4406 n1 = !n1;
4407 }
4408 else if (rettv->v_type != var2.v_type
4409 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4410 {
4411 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004412 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004414 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004415 clear_tv(rettv);
4416 clear_tv(&var2);
4417 return FAIL;
4418 }
4419 else
4420 {
4421 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004422 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4423 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 if (type == TYPE_NEQUAL)
4425 n1 = !n1;
4426 }
4427 }
4428
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004429 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4430 {
4431 if (type_is)
4432 {
4433 n1 = (rettv->v_type == var2.v_type
4434 && rettv->vval.v_dict == var2.vval.v_dict);
4435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 else if (rettv->v_type != var2.v_type
4439 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4440 {
4441 if (rettv->v_type != var2.v_type)
4442 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4443 else
4444 EMSG(_("E736: Invalid operation for Dictionary"));
4445 clear_tv(rettv);
4446 clear_tv(&var2);
4447 return FAIL;
4448 }
4449 else
4450 {
4451 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004452 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4453 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004454 if (type == TYPE_NEQUAL)
4455 n1 = !n1;
4456 }
4457 }
4458
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004459 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4460 {
4461 if (rettv->v_type != var2.v_type
4462 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4463 {
4464 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004465 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004467 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 clear_tv(rettv);
4469 clear_tv(&var2);
4470 return FAIL;
4471 }
4472 else
4473 {
4474 /* Compare two Funcrefs for being equal or unequal. */
4475 if (rettv->vval.v_string == NULL
4476 || var2.vval.v_string == NULL)
4477 n1 = FALSE;
4478 else
4479 n1 = STRCMP(rettv->vval.v_string,
4480 var2.vval.v_string) == 0;
4481 if (type == TYPE_NEQUAL)
4482 n1 = !n1;
4483 }
4484 }
4485
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004486#ifdef FEAT_FLOAT
4487 /*
4488 * If one of the two variables is a float, compare as a float.
4489 * When using "=~" or "!~", always compare as string.
4490 */
4491 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4492 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4493 {
4494 float_T f1, f2;
4495
4496 if (rettv->v_type == VAR_FLOAT)
4497 f1 = rettv->vval.v_float;
4498 else
4499 f1 = get_tv_number(rettv);
4500 if (var2.v_type == VAR_FLOAT)
4501 f2 = var2.vval.v_float;
4502 else
4503 f2 = get_tv_number(&var2);
4504 n1 = FALSE;
4505 switch (type)
4506 {
4507 case TYPE_EQUAL: n1 = (f1 == f2); break;
4508 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4509 case TYPE_GREATER: n1 = (f1 > f2); break;
4510 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4511 case TYPE_SMALLER: n1 = (f1 < f2); break;
4512 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4513 case TYPE_UNKNOWN:
4514 case TYPE_MATCH:
4515 case TYPE_NOMATCH: break; /* avoid gcc warning */
4516 }
4517 }
4518#endif
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /*
4521 * If one of the two variables is a number, compare as a number.
4522 * When using "=~" or "!~", always compare as string.
4523 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527 n1 = get_tv_number(rettv);
4528 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 switch (type)
4530 {
4531 case TYPE_EQUAL: n1 = (n1 == n2); break;
4532 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4533 case TYPE_GREATER: n1 = (n1 > n2); break;
4534 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4535 case TYPE_SMALLER: n1 = (n1 < n2); break;
4536 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4537 case TYPE_UNKNOWN:
4538 case TYPE_MATCH:
4539 case TYPE_NOMATCH: break; /* avoid gcc warning */
4540 }
4541 }
4542 else
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 s1 = get_tv_string_buf(rettv, buf1);
4545 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4547 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4548 else
4549 i = 0;
4550 n1 = FALSE;
4551 switch (type)
4552 {
4553 case TYPE_EQUAL: n1 = (i == 0); break;
4554 case TYPE_NEQUAL: n1 = (i != 0); break;
4555 case TYPE_GREATER: n1 = (i > 0); break;
4556 case TYPE_GEQUAL: n1 = (i >= 0); break;
4557 case TYPE_SMALLER: n1 = (i < 0); break;
4558 case TYPE_SEQUAL: n1 = (i <= 0); break;
4559
4560 case TYPE_MATCH:
4561 case TYPE_NOMATCH:
4562 /* avoid 'l' flag in 'cpoptions' */
4563 save_cpo = p_cpo;
4564 p_cpo = (char_u *)"";
4565 regmatch.regprog = vim_regcomp(s2,
4566 RE_MAGIC + RE_STRING);
4567 regmatch.rm_ic = ic;
4568 if (regmatch.regprog != NULL)
4569 {
4570 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004571 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (type == TYPE_NOMATCH)
4573 n1 = !n1;
4574 }
4575 p_cpo = save_cpo;
4576 break;
4577
4578 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4579 }
4580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 clear_tv(rettv);
4582 clear_tv(&var2);
4583 rettv->v_type = VAR_NUMBER;
4584 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 }
4587
4588 return OK;
4589}
4590
4591/*
4592 * Handle fourth level expression:
4593 * + number addition
4594 * - number subtraction
4595 * . string concatenation
4596 *
4597 * "arg" must point to the first non-white of the expression.
4598 * "arg" is advanced to the next non-white after the recognized expression.
4599 *
4600 * Return OK or FAIL.
4601 */
4602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 int evaluate;
4607{
Bram Moolenaar33570922005-01-25 22:26:29 +00004608 typval_T var2;
4609 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 int op;
4611 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004612#ifdef FEAT_FLOAT
4613 float_T f1 = 0, f2 = 0;
4614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u *s1, *s2;
4616 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4617 char_u *p;
4618
4619 /*
4620 * Get the first variable.
4621 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004622 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 return FAIL;
4624
4625 /*
4626 * Repeat computing, until no '+', '-' or '.' is following.
4627 */
4628 for (;;)
4629 {
4630 op = **arg;
4631 if (op != '+' && op != '-' && op != '.')
4632 break;
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 if ((op != '+' || rettv->v_type != VAR_LIST)
4635#ifdef FEAT_FLOAT
4636 && (op == '.' || rettv->v_type != VAR_FLOAT)
4637#endif
4638 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 {
4640 /* For "list + ...", an illegal use of the first operand as
4641 * a number cannot be determined before evaluating the 2nd
4642 * operand: if this is also a list, all is ok.
4643 * For "something . ...", "something - ..." or "non-list + ...",
4644 * we know that the first operand needs to be a string or number
4645 * without evaluating the 2nd operand. So check before to avoid
4646 * side effects after an error. */
4647 if (evaluate && get_tv_string_chk(rettv) == NULL)
4648 {
4649 clear_tv(rettv);
4650 return FAIL;
4651 }
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
4655 * Get the second variable.
4656 */
4657 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004658 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return FAIL;
4662 }
4663
4664 if (evaluate)
4665 {
4666 /*
4667 * Compute the result.
4668 */
4669 if (op == '.')
4670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4672 s2 = get_tv_string_buf_chk(&var2, buf2);
4673 if (s2 == NULL) /* type error ? */
4674 {
4675 clear_tv(rettv);
4676 clear_tv(&var2);
4677 return FAIL;
4678 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004679 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
4681 rettv->v_type = VAR_STRING;
4682 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 else if (op == '+' && rettv->v_type == VAR_LIST
4685 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004686 {
4687 /* concatenate Lists */
4688 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4689 &var3) == FAIL)
4690 {
4691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 return FAIL;
4694 }
4695 clear_tv(rettv);
4696 *rettv = var3;
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 else
4699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 int error = FALSE;
4701
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 f1 = rettv->vval.v_float;
4706 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708 else
4709#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 n1 = get_tv_number_chk(rettv, &error);
4712 if (error)
4713 {
4714 /* This can only happen for "list + non-list". For
4715 * "non-list + ..." or "something - ...", we returned
4716 * before evaluating the 2nd operand. */
4717 clear_tv(rettv);
4718 return FAIL;
4719 }
4720#ifdef FEAT_FLOAT
4721 if (var2.v_type == VAR_FLOAT)
4722 f1 = n1;
4723#endif
4724 }
4725#ifdef FEAT_FLOAT
4726 if (var2.v_type == VAR_FLOAT)
4727 {
4728 f2 = var2.vval.v_float;
4729 n2 = 0;
4730 }
4731 else
4732#endif
4733 {
4734 n2 = get_tv_number_chk(&var2, &error);
4735 if (error)
4736 {
4737 clear_tv(rettv);
4738 clear_tv(&var2);
4739 return FAIL;
4740 }
4741#ifdef FEAT_FLOAT
4742 if (rettv->v_type == VAR_FLOAT)
4743 f2 = n2;
4744#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747
4748#ifdef FEAT_FLOAT
4749 /* If there is a float on either side the result is a float. */
4750 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4751 {
4752 if (op == '+')
4753 f1 = f1 + f2;
4754 else
4755 f1 = f1 - f2;
4756 rettv->v_type = VAR_FLOAT;
4757 rettv->vval.v_float = f1;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760#endif
4761 {
4762 if (op == '+')
4763 n1 = n1 + n2;
4764 else
4765 n1 = n1 - n2;
4766 rettv->v_type = VAR_NUMBER;
4767 rettv->vval.v_number = n1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 }
4773 return OK;
4774}
4775
4776/*
4777 * Handle fifth level expression:
4778 * * number multiplication
4779 * / number division
4780 * % number modulo
4781 *
4782 * "arg" must point to the first non-white of the expression.
4783 * "arg" is advanced to the next non-white after the recognized expression.
4784 *
4785 * Return OK or FAIL.
4786 */
4787 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004788eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int op;
4796 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797#ifdef FEAT_FLOAT
4798 int use_float = FALSE;
4799 float_T f1 = 0, f2;
4800#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004801 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 /*
4804 * Get the first variable.
4805 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004806 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return FAIL;
4808
4809 /*
4810 * Repeat computing, until no '*', '/' or '%' is following.
4811 */
4812 for (;;)
4813 {
4814 op = **arg;
4815 if (op != '*' && op != '/' && op != '%')
4816 break;
4817
4818 if (evaluate)
4819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
4822 {
4823 f1 = rettv->vval.v_float;
4824 use_float = TRUE;
4825 n1 = 0;
4826 }
4827 else
4828#endif
4829 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 if (error)
4832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 else
4835 n1 = 0;
4836
4837 /*
4838 * Get the second variable.
4839 */
4840 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004841 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 return FAIL;
4843
4844 if (evaluate)
4845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#ifdef FEAT_FLOAT
4847 if (var2.v_type == VAR_FLOAT)
4848 {
4849 if (!use_float)
4850 {
4851 f1 = n1;
4852 use_float = TRUE;
4853 }
4854 f2 = var2.vval.v_float;
4855 n2 = 0;
4856 }
4857 else
4858#endif
4859 {
4860 n2 = get_tv_number_chk(&var2, &error);
4861 clear_tv(&var2);
4862 if (error)
4863 return FAIL;
4864#ifdef FEAT_FLOAT
4865 if (use_float)
4866 f2 = n2;
4867#endif
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 /*
4871 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 if (op == '*')
4878 f1 = f1 * f2;
4879 else if (op == '/')
4880 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004881# ifdef VMS
4882 /* VMS crashes on divide by zero, work around it */
4883 if (f2 == 0.0)
4884 {
4885 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004888 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004889 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004890 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891 }
4892 else
4893 f1 = f1 / f2;
4894# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 /* We rely on the floating point library to handle divide
4896 * by zero to result in "inf" and not a crash. */
4897 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004902 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 return FAIL;
4904 }
4905 rettv->v_type = VAR_FLOAT;
4906 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 if (op == '*')
4912 n1 = n1 * n2;
4913 else if (op == '/')
4914 {
4915 if (n2 == 0) /* give an error message? */
4916 {
4917 if (n1 == 0)
4918 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4919 else if (n1 < 0)
4920 n1 = -0x7fffffffL;
4921 else
4922 n1 = 0x7fffffffL;
4923 }
4924 else
4925 n1 = n1 / n2;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 {
4929 if (n2 == 0) /* give an error message? */
4930 n1 = 0;
4931 else
4932 n1 = n1 % n2;
4933 }
4934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 }
4939
4940 return OK;
4941}
4942
4943/*
4944 * Handle sixth level expression:
4945 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004946 * "string" string constant
4947 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 * &option-name option value
4949 * @r register contents
4950 * identifier variable value
4951 * function() function call
4952 * $VAR environment variable
4953 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004954 * [expr, expr] List
4955 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 *
4957 * Also handle:
4958 * ! in front logical NOT
4959 * - in front unary minus
4960 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 * trailing [] subscript in String or List
4962 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 *
4964 * "arg" must point to the first non-white of the expression.
4965 * "arg" is advanced to the next non-white after the recognized expression.
4966 *
4967 * Return OK or FAIL.
4968 */
4969 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004970eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004974 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 long n;
4977 int len;
4978 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 char_u *start_leader, *end_leader;
4980 int ret = OK;
4981 char_u *alias;
4982
4983 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 /*
4990 * Skip '!' and '-' characters. They are handled later.
4991 */
4992 start_leader = *arg;
4993 while (**arg == '!' || **arg == '-' || **arg == '+')
4994 *arg = skipwhite(*arg + 1);
4995 end_leader = *arg;
4996
4997 switch (**arg)
4998 {
4999 /*
5000 * Number constant.
5001 */
5002 case '0':
5003 case '1':
5004 case '2':
5005 case '3':
5006 case '4':
5007 case '5':
5008 case '6':
5009 case '7':
5010 case '8':
5011 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013#ifdef FEAT_FLOAT
5014 char_u *p = skipdigits(*arg + 1);
5015 int get_float = FALSE;
5016
5017 /* We accept a float when the format matches
5018 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005019 * strict to avoid backwards compatibility problems.
5020 * Don't look for a float after the "." operator, so that
5021 * ":let vers = 1.2.3" doesn't fail. */
5022 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 get_float = TRUE;
5025 p = skipdigits(p + 2);
5026 if (*p == 'e' || *p == 'E')
5027 {
5028 ++p;
5029 if (*p == '-' || *p == '+')
5030 ++p;
5031 if (!vim_isdigit(*p))
5032 get_float = FALSE;
5033 else
5034 p = skipdigits(p + 1);
5035 }
5036 if (ASCII_ISALPHA(*p) || *p == '.')
5037 get_float = FALSE;
5038 }
5039 if (get_float)
5040 {
5041 float_T f;
5042
5043 *arg += string2float(*arg, &f);
5044 if (evaluate)
5045 {
5046 rettv->v_type = VAR_FLOAT;
5047 rettv->vval.v_float = f;
5048 }
5049 }
5050 else
5051#endif
5052 {
5053 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5054 *arg += len;
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_NUMBER;
5058 rettv->vval.v_number = n;
5059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * String constant: "string".
5066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069
5070 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 break;
5075
5076 /*
5077 * List: [expr, expr]
5078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081
5082 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 * Dictionary: {key: val, key: val}
5084 */
5085 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5086 break;
5087
5088 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005089 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005091 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093
5094 /*
5095 * Environment variable: $VAR.
5096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /*
5101 * Register contents: @r.
5102 */
5103 case '@': ++*arg;
5104 if (evaluate)
5105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005107 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 if (**arg != NUL)
5110 ++*arg;
5111 break;
5112
5113 /*
5114 * nested expression: (expression).
5115 */
5116 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (**arg == ')')
5119 ++*arg;
5120 else if (ret == OK)
5121 {
5122 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ret = FAIL;
5125 }
5126 break;
5127
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131
5132 if (ret == NOTDONE)
5133 {
5134 /*
5135 * Must be a variable or function name.
5136 * Can also be a curly-braces kind of name: {expr}.
5137 */
5138 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 if (alias != NULL)
5141 s = alias;
5142
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 ret = FAIL;
5145 else
5146 {
5147 if (**arg == '(') /* recursive! */
5148 {
5149 /* If "s" is the name of a variable of type VAR_FUNC
5150 * use its contents. */
5151 s = deref_func_name(s, &len);
5152
5153 /* Invoke the function. */
5154 ret = get_func_tv(s, len, rettv, arg,
5155 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005156 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005157
5158 /* If evaluate is FALSE rettv->v_type was not set in
5159 * get_func_tv, but it's needed in handle_subscript() to parse
5160 * what follows. So set it here. */
5161 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5162 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005163 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005164 rettv->v_type = VAR_FUNC;
5165 }
5166
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 /* Stop the expression evaluation when immediately
5168 * aborting on error, or when an interrupt occurred or
5169 * an exception was thrown but not caught. */
5170 if (aborting())
5171 {
5172 if (ret == OK)
5173 clear_tv(rettv);
5174 ret = FAIL;
5175 }
5176 }
5177 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005178 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005179 else
5180 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005182 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 }
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 *arg = skipwhite(*arg);
5186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5188 * expr(expr). */
5189 if (ret == OK)
5190 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192 /*
5193 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5194 */
5195 if (ret == OK && evaluate && end_leader > start_leader)
5196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 int val = 0;
5199#ifdef FEAT_FLOAT
5200 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 if (rettv->v_type == VAR_FLOAT)
5203 f = rettv->vval.v_float;
5204 else
5205#endif
5206 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 clear_tv(rettv);
5210 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 else
5213 {
5214 while (end_leader > start_leader)
5215 {
5216 --end_leader;
5217 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 {
5219#ifdef FEAT_FLOAT
5220 if (rettv->v_type == VAR_FLOAT)
5221 f = !f;
5222 else
5223#endif
5224 val = !val;
5225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005227 {
5228#ifdef FEAT_FLOAT
5229 if (rettv->v_type == VAR_FLOAT)
5230 f = -f;
5231 else
5232#endif
5233 val = -val;
5234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005236#ifdef FEAT_FLOAT
5237 if (rettv->v_type == VAR_FLOAT)
5238 {
5239 clear_tv(rettv);
5240 rettv->vval.v_float = f;
5241 }
5242 else
5243#endif
5244 {
5245 clear_tv(rettv);
5246 rettv->v_type = VAR_NUMBER;
5247 rettv->vval.v_number = val;
5248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251
5252 return ret;
5253}
5254
5255/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005256 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5257 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5259 */
5260 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266{
5267 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 long len = -1;
5271 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005275 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 if (verbose)
5278 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 return FAIL;
5280 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005281#ifdef FEAT_FLOAT
5282 else if (rettv->v_type == VAR_FLOAT)
5283 {
5284 if (verbose)
5285 EMSG(_(e_float_as_string));
5286 return FAIL;
5287 }
5288#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 /*
5293 * dict.name
5294 */
5295 key = *arg + 1;
5296 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5297 ;
5298 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 }
5302 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /*
5305 * something[idx]
5306 *
5307 * Get the (first) variable from inside the [].
5308 */
5309 *arg = skipwhite(*arg + 1);
5310 if (**arg == ':')
5311 empty1 = TRUE;
5312 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5313 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5315 {
5316 /* not a number or string */
5317 clear_tv(&var1);
5318 return FAIL;
5319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320
5321 /*
5322 * Get the second variable from inside the [:].
5323 */
5324 if (**arg == ':')
5325 {
5326 range = TRUE;
5327 *arg = skipwhite(*arg + 1);
5328 if (**arg == ']')
5329 empty2 = TRUE;
5330 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 if (!empty1)
5333 clear_tv(&var1);
5334 return FAIL;
5335 }
5336 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5337 {
5338 /* not a number or string */
5339 if (!empty1)
5340 clear_tv(&var1);
5341 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 return FAIL;
5343 }
5344 }
5345
5346 /* Check for the ']'. */
5347 if (**arg != ']')
5348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005349 if (verbose)
5350 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 clear_tv(&var1);
5352 if (range)
5353 clear_tv(&var2);
5354 return FAIL;
5355 }
5356 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358
5359 if (evaluate)
5360 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 n1 = 0;
5362 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 n1 = get_tv_number(&var1);
5365 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 }
5367 if (range)
5368 {
5369 if (empty2)
5370 n2 = -1;
5371 else
5372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 n2 = get_tv_number(&var2);
5374 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 }
5376 }
5377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
5380 case VAR_NUMBER:
5381 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 len = (long)STRLEN(s);
5384 if (range)
5385 {
5386 /* The resulting variable is a substring. If the indexes
5387 * are out of range the result is empty. */
5388 if (n1 < 0)
5389 {
5390 n1 = len + n1;
5391 if (n1 < 0)
5392 n1 = 0;
5393 }
5394 if (n2 < 0)
5395 n2 = len + n2;
5396 else if (n2 >= len)
5397 n2 = len;
5398 if (n1 >= len || n2 < 0 || n1 > n2)
5399 s = NULL;
5400 else
5401 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5402 }
5403 else
5404 {
5405 /* The resulting variable is a string of a single
5406 * character. If the index is too big or negative the
5407 * result is empty. */
5408 if (n1 >= len || n1 < 0)
5409 s = NULL;
5410 else
5411 s = vim_strnsave(s + n1, 1);
5412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413 clear_tv(rettv);
5414 rettv->v_type = VAR_STRING;
5415 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 break;
5417
5418 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 if (n1 < 0)
5421 n1 = len + n1;
5422 if (!empty1 && (n1 < 0 || n1 >= len))
5423 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005424 /* For a range we allow invalid values and return an empty
5425 * list. A list index out of range is an error. */
5426 if (!range)
5427 {
5428 if (verbose)
5429 EMSGN(_(e_listidx), n1);
5430 return FAIL;
5431 }
5432 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 if (range)
5435 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 list_T *l;
5437 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438
5439 if (n2 < 0)
5440 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005441 else if (n2 >= len)
5442 n2 = len - 1;
5443 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005444 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 l = list_alloc();
5446 if (l == NULL)
5447 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 n1 <= n2; ++n1)
5450 {
5451 if (list_append_tv(l, &item->li_tv) == FAIL)
5452 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005453 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
5455 }
5456 item = item->li_next;
5457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 clear_tv(rettv);
5459 rettv->v_type = VAR_LIST;
5460 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005461 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 else
5464 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 clear_tv(rettv);
5467 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470
5471 case VAR_DICT:
5472 if (range)
5473 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005474 if (verbose)
5475 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476 if (len == -1)
5477 clear_tv(&var1);
5478 return FAIL;
5479 }
5480 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005481 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482
5483 if (len == -1)
5484 {
5485 key = get_tv_string(&var1);
5486 if (*key == NUL)
5487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005488 if (verbose)
5489 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 clear_tv(&var1);
5491 return FAIL;
5492 }
5493 }
5494
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005495 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005497 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005498 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499 if (len == -1)
5500 clear_tv(&var1);
5501 if (item == NULL)
5502 return FAIL;
5503
5504 copy_tv(&item->di_tv, &var1);
5505 clear_tv(rettv);
5506 *rettv = var1;
5507 }
5508 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 }
5510 }
5511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 return OK;
5513}
5514
5515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * Get an option value.
5517 * "arg" points to the '&' or '+' before the option name.
5518 * "arg" is advanced to character after the option name.
5519 * Return OK or FAIL.
5520 */
5521 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 int evaluate;
5526{
5527 char_u *option_end;
5528 long numval;
5529 char_u *stringval;
5530 int opt_type;
5531 int c;
5532 int working = (**arg == '+'); /* has("+option") */
5533 int ret = OK;
5534 int opt_flags;
5535
5536 /*
5537 * Isolate the option name and find its value.
5538 */
5539 option_end = find_option_end(arg, &opt_flags);
5540 if (option_end == NULL)
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 EMSG2(_("E112: Option name missing: %s"), *arg);
5544 return FAIL;
5545 }
5546
5547 if (!evaluate)
5548 {
5549 *arg = option_end;
5550 return OK;
5551 }
5552
5553 c = *option_end;
5554 *option_end = NUL;
5555 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 if (opt_type == -3) /* invalid name */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 EMSG2(_("E113: Unknown option: %s"), *arg);
5562 ret = FAIL;
5563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 if (opt_type == -2) /* hidden string option */
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 rettv->v_type = VAR_STRING;
5569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 else if (opt_type == -1) /* hidden number option */
5572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv->v_type = VAR_NUMBER;
5574 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 else if (opt_type == 1) /* number option */
5577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 rettv->v_type = VAR_NUMBER;
5579 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581 else /* string option */
5582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 rettv->v_type = VAR_STRING;
5584 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
5586 }
5587 else if (working && (opt_type == -2 || opt_type == -1))
5588 ret = FAIL;
5589
5590 *option_end = c; /* put back for error messages */
5591 *arg = option_end;
5592
5593 return ret;
5594}
5595
5596/*
5597 * Allocate a variable for a string constant.
5598 * Return OK or FAIL.
5599 */
5600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 int evaluate;
5605{
5606 char_u *p;
5607 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 int extra = 0;
5609
5610 /*
5611 * Find the end of the string, skipping backslashed characters.
5612 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005613 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 if (*p == '\\' && p[1] != NUL)
5616 {
5617 ++p;
5618 /* A "\<x>" form occupies at least 4 characters, and produces up
5619 * to 6 characters: reserve space for 2 extra */
5620 if (*p == '<')
5621 extra += 2;
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624
5625 if (*p != '"')
5626 {
5627 EMSG2(_("E114: Missing quote: %s"), *arg);
5628 return FAIL;
5629 }
5630
5631 /* If only parsing, set *arg and return here */
5632 if (!evaluate)
5633 {
5634 *arg = p + 1;
5635 return OK;
5636 }
5637
5638 /*
5639 * Copy the string into allocated memory, handling backslashed
5640 * characters.
5641 */
5642 name = alloc((unsigned)(p - *arg + extra));
5643 if (name == NULL)
5644 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 rettv->v_type = VAR_STRING;
5646 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
5650 if (*p == '\\')
5651 {
5652 switch (*++p)
5653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 case 'b': *name++ = BS; ++p; break;
5655 case 'e': *name++ = ESC; ++p; break;
5656 case 'f': *name++ = FF; ++p; break;
5657 case 'n': *name++ = NL; ++p; break;
5658 case 'r': *name++ = CAR; ++p; break;
5659 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 case 'X': /* hex: "\x1", "\x12" */
5662 case 'x':
5663 case 'u': /* Unicode: "\u0023" */
5664 case 'U':
5665 if (vim_isxdigit(p[1]))
5666 {
5667 int n, nr;
5668 int c = toupper(*p);
5669
5670 if (c == 'X')
5671 n = 2;
5672 else
5673 n = 4;
5674 nr = 0;
5675 while (--n >= 0 && vim_isxdigit(p[1]))
5676 {
5677 ++p;
5678 nr = (nr << 4) + hex2nr(*p);
5679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_MBYTE
5682 /* For "\u" store the number according to
5683 * 'encoding'. */
5684 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else
5687#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691
5692 /* octal: "\1", "\12", "\123" */
5693 case '0':
5694 case '1':
5695 case '2':
5696 case '3':
5697 case '4':
5698 case '5':
5699 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 case '7': *name = *p++ - '0';
5701 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 *name = (*name << 3) + *p++ - '0';
5704 if (*p >= '0' && *p <= '7')
5705 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 break;
5709
5710 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 if (extra != 0)
5713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 break;
5716 }
5717 /* FALLTHROUGH */
5718
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 break;
5721 }
5722 }
5723 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 *arg = p + 1;
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 return OK;
5731}
5732
5733/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * Return OK or FAIL.
5736 */
5737 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005738get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 int evaluate;
5742{
5743 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 int reduce = 0;
5746
5747 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 */
5750 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 break;
5756 ++reduce;
5757 ++p;
5758 }
5759 }
5760
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 return FAIL;
5765 }
5766
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 if (!evaluate)
5769 {
5770 *arg = p + 1;
5771 return OK;
5772 }
5773
5774 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 */
5777 str = alloc((unsigned)((p - *arg) - reduce));
5778 if (str == NULL)
5779 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 rettv->v_type = VAR_STRING;
5781 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 break;
5789 ++p;
5790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 *arg = p + 1;
5795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 return OK;
5797}
5798
5799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800 * Allocate a variable for a List and fill it from "*arg".
5801 * Return OK or FAIL.
5802 */
5803 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005804get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 int evaluate;
5808{
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l = NULL;
5810 typval_T tv;
5811 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812
5813 if (evaluate)
5814 {
5815 l = list_alloc();
5816 if (l == NULL)
5817 return FAIL;
5818 }
5819
5820 *arg = skipwhite(*arg + 1);
5821 while (**arg != ']' && **arg != NUL)
5822 {
5823 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5824 goto failret;
5825 if (evaluate)
5826 {
5827 item = listitem_alloc();
5828 if (item != NULL)
5829 {
5830 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005831 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 list_append(l, item);
5833 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005834 else
5835 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 }
5837
5838 if (**arg == ']')
5839 break;
5840 if (**arg != ',')
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 goto failret;
5844 }
5845 *arg = skipwhite(*arg + 1);
5846 }
5847
5848 if (**arg != ']')
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851failret:
5852 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005853 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 return FAIL;
5855 }
5856
5857 *arg = skipwhite(*arg + 1);
5858 if (evaluate)
5859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 rettv->v_type = VAR_LIST;
5861 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 ++l->lv_refcount;
5863 }
5864
5865 return OK;
5866}
5867
5868/*
5869 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005870 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005872 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873list_alloc()
5874{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005875 list_T *l;
5876
5877 l = (list_T *)alloc_clear(sizeof(list_T));
5878 if (l != NULL)
5879 {
5880 /* Prepend the list to the list of lists for garbage collection. */
5881 if (first_list != NULL)
5882 first_list->lv_used_prev = l;
5883 l->lv_used_prev = NULL;
5884 l->lv_used_next = first_list;
5885 first_list = l;
5886 }
5887 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888}
5889
5890/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005891 * Allocate an empty list for a return value.
5892 * Returns OK or FAIL.
5893 */
5894 static int
5895rettv_list_alloc(rettv)
5896 typval_T *rettv;
5897{
5898 list_T *l = list_alloc();
5899
5900 if (l == NULL)
5901 return FAIL;
5902
5903 rettv->vval.v_list = l;
5904 rettv->v_type = VAR_LIST;
5905 ++l->lv_refcount;
5906 return OK;
5907}
5908
5909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 * Unreference a list: decrement the reference count and free it when it
5911 * becomes zero.
5912 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005913 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005917 if (l != NULL && --l->lv_refcount <= 0)
5918 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919}
5920
5921/*
5922 * Free a list, including all items it points to.
5923 * Ignores the reference count.
5924 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005925 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005926list_free(l, recurse)
5927 list_T *l;
5928 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929{
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005932 /* Remove the list from the list of lists for garbage collection. */
5933 if (l->lv_used_prev == NULL)
5934 first_list = l->lv_used_next;
5935 else
5936 l->lv_used_prev->lv_used_next = l->lv_used_next;
5937 if (l->lv_used_next != NULL)
5938 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5939
Bram Moolenaard9fba312005-06-26 22:34:35 +00005940 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942 /* Remove the item before deleting it. */
5943 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005944 if (recurse || (item->li_tv.v_type != VAR_LIST
5945 && item->li_tv.v_type != VAR_DICT))
5946 clear_tv(&item->li_tv);
5947 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 }
5949 vim_free(l);
5950}
5951
5952/*
5953 * Allocate a list item.
5954 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005955 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956listitem_alloc()
5957{
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959}
5960
5961/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005962 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005968 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 vim_free(item);
5970}
5971
5972/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005973 * Remove a list item from a List and free it. Also clears the value.
5974 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005975 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005976listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 list_T *l;
5978 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005979{
5980 list_remove(l, item, item);
5981 listitem_free(item);
5982}
5983
5984/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 * Get the number of items in a list.
5986 */
5987 static long
5988list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 if (l == NULL)
5992 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005993 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994}
5995
5996/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 * Return TRUE when two lists have exactly the same values.
5998 */
5999 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l1;
6002 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006004 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006008 if (l1 == NULL || l2 == NULL)
6009 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006010 if (l1 == l2)
6011 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006012 if (list_len(l1) != list_len(l2))
6013 return FALSE;
6014
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015 for (item1 = l1->lv_first, item2 = l2->lv_first;
6016 item1 != NULL && item2 != NULL;
6017 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006018 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006019 return FALSE;
6020 return item1 == NULL && item2 == NULL;
6021}
6022
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006023#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6024 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006025/*
6026 * Return the dictitem that an entry in a hashtable points to.
6027 */
6028 dictitem_T *
6029dict_lookup(hi)
6030 hashitem_T *hi;
6031{
6032 return HI2DI(hi);
6033}
6034#endif
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037 * Return TRUE when two dictionaries have exactly the same key/values.
6038 */
6039 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 dict_T *d1;
6042 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045{
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 hashitem_T *hi;
6047 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006050 if (d1 == NULL || d2 == NULL)
6051 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006052 if (d1 == d2)
6053 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 if (dict_len(d1) != dict_len(d2))
6055 return FALSE;
6056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006057 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 if (!HASHITEM_EMPTY(hi))
6061 {
6062 item2 = dict_find(d2, hi->hi_key, -1);
6063 if (item2 == NULL)
6064 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 return FALSE;
6067 --todo;
6068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006069 }
6070 return TRUE;
6071}
6072
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073static int tv_equal_recurse_limit;
6074
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006077 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006078 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006079 */
6080 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 typval_T *tv1;
6083 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084 int ic; /* ignore case */
6085 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086{
6087 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006088 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006090 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006092 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006093 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006095 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096 * recursiveness to a limit. We guess they are equal then.
6097 * A fixed limit has the problem of still taking an awful long time.
6098 * Reduce the limit every time running into it. That should work fine for
6099 * deeply linked structures that are not recursively linked and catch
6100 * recursiveness quickly. */
6101 if (!recursive)
6102 tv_equal_recurse_limit = 1000;
6103 if (recursive_cnt >= tv_equal_recurse_limit)
6104 {
6105 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 ++recursive_cnt;
6113 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6114 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006115 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006116
6117 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 ++recursive_cnt;
6119 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6120 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006121 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122
6123 case VAR_FUNC:
6124 return (tv1->vval.v_string != NULL
6125 && tv2->vval.v_string != NULL
6126 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6127
6128 case VAR_NUMBER:
6129 return tv1->vval.v_number == tv2->vval.v_number;
6130
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006131#ifdef FEAT_FLOAT
6132 case VAR_FLOAT:
6133 return tv1->vval.v_float == tv2->vval.v_float;
6134#endif
6135
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006136 case VAR_STRING:
6137 s1 = get_tv_string_buf(tv1, buf1);
6138 s2 = get_tv_string_buf(tv2, buf2);
6139 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006140 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006141
6142 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 return TRUE;
6144}
6145
6146/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 * Locate item with index "n" in list "l" and return it.
6148 * A negative index is counted from the end; -1 is the last item.
6149 * Returns NULL when "n" is out of range.
6150 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006151 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 long n;
6155{
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 long idx;
6158
6159 if (l == NULL)
6160 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006161
6162 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006164 n = l->lv_len + n;
6165
6166 /* Check for index out of range. */
6167 if (n < 0 || n >= l->lv_len)
6168 return NULL;
6169
6170 /* When there is a cached index may start search from there. */
6171 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006173 if (n < l->lv_idx / 2)
6174 {
6175 /* closest to the start of the list */
6176 item = l->lv_first;
6177 idx = 0;
6178 }
6179 else if (n > (l->lv_idx + l->lv_len) / 2)
6180 {
6181 /* closest to the end of the list */
6182 item = l->lv_last;
6183 idx = l->lv_len - 1;
6184 }
6185 else
6186 {
6187 /* closest to the cached index */
6188 item = l->lv_idx_item;
6189 idx = l->lv_idx;
6190 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 }
6192 else
6193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_len / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006207
6208 while (n > idx)
6209 {
6210 /* search forward */
6211 item = item->li_next;
6212 ++idx;
6213 }
6214 while (n < idx)
6215 {
6216 /* search backward */
6217 item = item->li_prev;
6218 --idx;
6219 }
6220
6221 /* cache the used index */
6222 l->lv_idx = idx;
6223 l->lv_idx_item = item;
6224
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 return item;
6226}
6227
6228/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006229 * Get list item "l[idx]" as a number.
6230 */
6231 static long
6232list_find_nr(l, idx, errorp)
6233 list_T *l;
6234 long idx;
6235 int *errorp; /* set to TRUE when something wrong */
6236{
6237 listitem_T *li;
6238
6239 li = list_find(l, idx);
6240 if (li == NULL)
6241 {
6242 if (errorp != NULL)
6243 *errorp = TRUE;
6244 return -1L;
6245 }
6246 return get_tv_number_chk(&li->li_tv, errorp);
6247}
6248
6249/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006250 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6251 */
6252 char_u *
6253list_find_str(l, idx)
6254 list_T *l;
6255 long idx;
6256{
6257 listitem_T *li;
6258
6259 li = list_find(l, idx - 1);
6260 if (li == NULL)
6261 {
6262 EMSGN(_(e_listidx), idx);
6263 return NULL;
6264 }
6265 return get_tv_string(&li->li_tv);
6266}
6267
6268/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006269 * Locate "item" list "l" and return its index.
6270 * Returns -1 when "item" is not in the list.
6271 */
6272 static long
6273list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 list_T *l;
6275 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006276{
6277 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006279
6280 if (l == NULL)
6281 return -1;
6282 idx = 0;
6283 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6284 ++idx;
6285 if (li == NULL)
6286 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006287 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006288}
6289
6290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 * Append item "item" to the end of list "l".
6292 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006293 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297{
6298 if (l->lv_last == NULL)
6299 {
6300 /* empty list */
6301 l->lv_first = item;
6302 l->lv_last = item;
6303 item->li_prev = NULL;
6304 }
6305 else
6306 {
6307 l->lv_last->li_next = item;
6308 item->li_prev = l->lv_last;
6309 l->lv_last = item;
6310 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006311 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 item->li_next = NULL;
6313}
6314
6315/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006317 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006319 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 list_T *l;
6322 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006324 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325
Bram Moolenaar05159a02005-02-26 23:04:13 +00006326 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006328 copy_tv(tv, &li->li_tv);
6329 list_append(l, li);
6330 return OK;
6331}
6332
6333/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006334 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 * Return FAIL when out of memory.
6336 */
6337 int
6338list_append_dict(list, dict)
6339 list_T *list;
6340 dict_T *dict;
6341{
6342 listitem_T *li = listitem_alloc();
6343
6344 if (li == NULL)
6345 return FAIL;
6346 li->li_tv.v_type = VAR_DICT;
6347 li->li_tv.v_lock = 0;
6348 li->li_tv.vval.v_dict = dict;
6349 list_append(list, li);
6350 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 return OK;
6352}
6353
6354/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006355 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006356 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006357 * Returns FAIL when out of memory.
6358 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006359 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006360list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361 list_T *l;
6362 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006363 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006364{
6365 listitem_T *li = listitem_alloc();
6366
6367 if (li == NULL)
6368 return FAIL;
6369 list_append(l, li);
6370 li->li_tv.v_type = VAR_STRING;
6371 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006372 if (str == NULL)
6373 li->li_tv.vval.v_string = NULL;
6374 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006375 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 return FAIL;
6377 return OK;
6378}
6379
6380/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381 * Append "n" to list "l".
6382 * Returns FAIL when out of memory.
6383 */
6384 static int
6385list_append_number(l, n)
6386 list_T *l;
6387 varnumber_T n;
6388{
6389 listitem_T *li;
6390
6391 li = listitem_alloc();
6392 if (li == NULL)
6393 return FAIL;
6394 li->li_tv.v_type = VAR_NUMBER;
6395 li->li_tv.v_lock = 0;
6396 li->li_tv.vval.v_number = n;
6397 list_append(l, li);
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 * If "item" is NULL append at the end.
6404 * Return FAIL when out of memory.
6405 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006406 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 list_T *l;
6409 typval_T *tv;
6410 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411{
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413
6414 if (ni == NULL)
6415 return FAIL;
6416 copy_tv(tv, &ni->li_tv);
6417 if (item == NULL)
6418 /* Append new item at end of list. */
6419 list_append(l, ni);
6420 else
6421 {
6422 /* Insert new item before existing item. */
6423 ni->li_prev = item->li_prev;
6424 ni->li_next = item;
6425 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006426 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_idx;
6429 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006431 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006433 l->lv_idx_item = NULL;
6434 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006436 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 }
6438 return OK;
6439}
6440
6441/*
6442 * Extend "l1" with "l2".
6443 * If "bef" is NULL append at the end, otherwise insert before this item.
6444 * Returns FAIL when out of memory.
6445 */
6446 static int
6447list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 list_T *l1;
6449 list_T *l2;
6450 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451{
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006453 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006455 /* We also quit the loop when we have inserted the original item count of
6456 * the list, avoid a hang when we extend a list with itself. */
6457 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6459 return FAIL;
6460 return OK;
6461}
6462
6463/*
6464 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6465 * Return FAIL when out of memory.
6466 */
6467 static int
6468list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 list_T *l1;
6470 list_T *l2;
6471 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472{
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006475 if (l1 == NULL || l2 == NULL)
6476 return FAIL;
6477
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006479 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480 if (l == NULL)
6481 return FAIL;
6482 tv->v_type = VAR_LIST;
6483 tv->vval.v_list = l;
6484
6485 /* append all items from the second list */
6486 return list_extend(l, l2, NULL);
6487}
6488
6489/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 * Returns NULL when out of memory.
6494 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 list_T *copy;
6502 listitem_T *item;
6503 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504
6505 if (orig == NULL)
6506 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507
6508 copy = list_alloc();
6509 if (copy != NULL)
6510 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006511 if (copyID != 0)
6512 {
6513 /* Do this before adding the items, because one of the items may
6514 * refer back to this list. */
6515 orig->lv_copyID = copyID;
6516 orig->lv_copylist = copy;
6517 }
6518 for (item = orig->lv_first; item != NULL && !got_int;
6519 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 {
6521 ni = listitem_alloc();
6522 if (ni == NULL)
6523 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 {
6526 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6527 {
6528 vim_free(ni);
6529 break;
6530 }
6531 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 list_append(copy, ni);
6535 }
6536 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006537 if (item != NULL)
6538 {
6539 list_unref(copy);
6540 copy = NULL;
6541 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 }
6543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544 return copy;
6545}
6546
6547/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006549 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006551 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006552list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 list_T *l;
6554 listitem_T *item;
6555 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556{
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* notify watchers */
6560 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006562 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 list_fix_watch(l, ip);
6564 if (ip == item2)
6565 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567
6568 if (item2->li_next == NULL)
6569 l->lv_last = item->li_prev;
6570 else
6571 item2->li_next->li_prev = item->li_prev;
6572 if (item->li_prev == NULL)
6573 l->lv_first = item2->li_next;
6574 else
6575 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006576 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577}
6578
6579/*
6580 * Return an allocated string with the string representation of a list.
6581 * May return NULL.
6582 */
6583 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006584list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587{
6588 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 if (tv->vval.v_list == NULL)
6591 return NULL;
6592 ga_init2(&ga, (int)sizeof(char), 80);
6593 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006594 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 {
6596 vim_free(ga.ga_data);
6597 return NULL;
6598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 ga_append(&ga, ']');
6600 ga_append(&ga, NUL);
6601 return (char_u *)ga.ga_data;
6602}
6603
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006604typedef struct join_S {
6605 char_u *s;
6606 char_u *tofree;
6607} join_T;
6608
6609 static int
6610list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6611 garray_T *gap; /* to store the result in */
6612 list_T *l;
6613 char_u *sep;
6614 int echo_style;
6615 int copyID;
6616 garray_T *join_gap; /* to keep each list item string */
6617{
6618 int i;
6619 join_T *p;
6620 int len;
6621 int sumlen = 0;
6622 int first = TRUE;
6623 char_u *tofree;
6624 char_u numbuf[NUMBUFLEN];
6625 listitem_T *item;
6626 char_u *s;
6627
6628 /* Stringify each item in the list. */
6629 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6630 {
6631 if (echo_style)
6632 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6633 else
6634 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6635 if (s == NULL)
6636 return FAIL;
6637
6638 len = (int)STRLEN(s);
6639 sumlen += len;
6640
6641 ga_grow(join_gap, 1);
6642 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6643 if (tofree != NULL || s != numbuf)
6644 {
6645 p->s = s;
6646 p->tofree = tofree;
6647 }
6648 else
6649 {
6650 p->s = vim_strnsave(s, len);
6651 p->tofree = p->s;
6652 }
6653
6654 line_breakcheck();
6655 }
6656
6657 /* Allocate result buffer with its total size, avoid re-allocation and
6658 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6659 if (join_gap->ga_len >= 2)
6660 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6661 if (ga_grow(gap, sumlen + 2) == FAIL)
6662 return FAIL;
6663
6664 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6665 {
6666 if (first)
6667 first = FALSE;
6668 else
6669 ga_concat(gap, sep);
6670 p = ((join_T *)join_gap->ga_data) + i;
6671
6672 if (p->s != NULL)
6673 ga_concat(gap, p->s);
6674 line_breakcheck();
6675 }
6676
6677 return OK;
6678}
6679
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006686list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006690 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006691 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006692{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006693 garray_T join_ga;
6694 int retval;
6695 join_T *p;
6696 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006697
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006698 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6699 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6700
6701 /* Dispose each item in join_ga. */
6702 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704 p = (join_T *)join_ga.ga_data;
6705 for (i = 0; i < join_ga.ga_len; ++i)
6706 {
6707 vim_free(p->tofree);
6708 ++p;
6709 }
6710 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006712
6713 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714}
6715
6716/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006717 * Garbage collection for lists and dictionaries.
6718 *
6719 * We use reference counts to be able to free most items right away when they
6720 * are no longer used. But for composite items it's possible that it becomes
6721 * unused while the reference count is > 0: When there is a recursive
6722 * reference. Example:
6723 * :let l = [1, 2, 3]
6724 * :let d = {9: l}
6725 * :let l[1] = d
6726 *
6727 * Since this is quite unusual we handle this with garbage collection: every
6728 * once in a while find out which lists and dicts are not referenced from any
6729 * variable.
6730 *
6731 * Here is a good reference text about garbage collection (refers to Python
6732 * but it applies to all reference-counting mechanisms):
6733 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735
6736/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 * Do garbage collection for lists and dicts.
6738 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006739 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 int
6741garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006742{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006743 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 buf_T *buf;
6745 win_T *wp;
6746 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006747 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006748 int did_free;
6749 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006750#ifdef FEAT_WINDOWS
6751 tabpage_T *tp;
6752#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006754 /* Only do this once. */
6755 want_garbage_collect = FALSE;
6756 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006757 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006758
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006759 /* We advance by two because we add one for items referenced through
6760 * previous_funccal. */
6761 current_copyID += COPYID_INC;
6762 copyID = current_copyID;
6763
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 /*
6765 * 1. Go through all accessible variables and mark all lists and dicts
6766 * with copyID.
6767 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768
6769 /* Don't free variables in the previous_funccal list unless they are only
6770 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006771 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006772 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6773 {
6774 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6775 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6776 }
6777
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 /* script-local variables */
6779 for (i = 1; i <= ga_scripts.ga_len; ++i)
6780 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6781
6782 /* buffer-local variables */
6783 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006784 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
6786 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006787 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006788 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006789#ifdef FEAT_AUTOCMD
6790 if (aucmd_win != NULL)
6791 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6792#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006793
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006794#ifdef FEAT_WINDOWS
6795 /* tabpage-local variables */
6796 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006797 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006798#endif
6799
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006800 /* global variables */
6801 set_ref_in_ht(&globvarht, copyID);
6802
6803 /* function-local variables */
6804 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6805 {
6806 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6807 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6808 }
6809
Bram Moolenaard812df62008-11-09 12:46:09 +00006810 /* v: vars */
6811 set_ref_in_ht(&vimvarht, copyID);
6812
Bram Moolenaar1dced572012-04-05 16:54:08 +02006813#ifdef FEAT_LUA
6814 set_ref_in_lua(copyID);
6815#endif
6816
Bram Moolenaardb913952012-06-29 12:54:53 +02006817#ifdef FEAT_PYTHON
6818 set_ref_in_python(copyID);
6819#endif
6820
6821#ifdef FEAT_PYTHON3
6822 set_ref_in_python3(copyID);
6823#endif
6824
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006825 /*
6826 * 2. Free lists and dictionaries that are not referenced.
6827 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006828 did_free = free_unref_items(copyID);
6829
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006830 /*
6831 * 3. Check if any funccal can be freed now.
6832 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833 for (pfc = &previous_funccal; *pfc != NULL; )
6834 {
6835 if (can_free_funccal(*pfc, copyID))
6836 {
6837 fc = *pfc;
6838 *pfc = fc->caller;
6839 free_funccal(fc, TRUE);
6840 did_free = TRUE;
6841 did_free_funccal = TRUE;
6842 }
6843 else
6844 pfc = &(*pfc)->caller;
6845 }
6846 if (did_free_funccal)
6847 /* When a funccal was freed some more items might be garbage
6848 * collected, so run again. */
6849 (void)garbage_collect();
6850
6851 return did_free;
6852}
6853
6854/*
6855 * Free lists and dictionaries that are no longer referenced.
6856 */
6857 static int
6858free_unref_items(copyID)
6859 int copyID;
6860{
6861 dict_T *dd;
6862 list_T *ll;
6863 int did_free = FALSE;
6864
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 */
6868 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006871 /* Free the Dictionary and ordinary items it contains, but don't
6872 * recurse into Lists and Dictionaries, they will be in the list
6873 * of dicts or list of lists. */
6874 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 did_free = TRUE;
6876
6877 /* restart, next dict may also have been freed */
6878 dd = first_dict;
6879 }
6880 else
6881 dd = dd->dv_used_next;
6882
6883 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 * Go through the list of lists and free items without the copyID.
6885 * But don't free a list that has a watcher (used in a for loop), these
6886 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 */
6888 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6890 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006892 /* Free the List and ordinary items it contains, but don't recurse
6893 * into Lists and Dictionaries, they will be in the list of dicts
6894 * or list of lists. */
6895 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 did_free = TRUE;
6897
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006898 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 ll = first_list;
6900 }
6901 else
6902 ll = ll->lv_used_next;
6903
6904 return did_free;
6905}
6906
6907/*
6908 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6909 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006910 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911set_ref_in_ht(ht, copyID)
6912 hashtab_T *ht;
6913 int copyID;
6914{
6915 int todo;
6916 hashitem_T *hi;
6917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006918 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 for (hi = ht->ht_array; todo > 0; ++hi)
6920 if (!HASHITEM_EMPTY(hi))
6921 {
6922 --todo;
6923 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6924 }
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through list "l" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_list(l, copyID)
6932 list_T *l;
6933 int copyID;
6934{
6935 listitem_T *li;
6936
6937 for (li = l->lv_first; li != NULL; li = li->li_next)
6938 set_ref_in_item(&li->li_tv, copyID);
6939}
6940
6941/*
6942 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6943 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006944 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945set_ref_in_item(tv, copyID)
6946 typval_T *tv;
6947 int copyID;
6948{
6949 dict_T *dd;
6950 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951
6952 switch (tv->v_type)
6953 {
6954 case VAR_DICT:
6955 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006956 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 /* Didn't see this dict yet. */
6959 dd->dv_copyID = copyID;
6960 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963
6964 case VAR_LIST:
6965 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006966 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 /* Didn't see this list yet. */
6969 ll->lv_copyID = copyID;
6970 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006975}
6976
6977/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978 * Allocate an empty header for a dictionary.
6979 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006980 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006981dict_alloc()
6982{
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006984
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006987 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006988 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 if (first_dict != NULL)
6990 first_dict->dv_used_prev = d;
6991 d->dv_used_next = first_dict;
6992 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006993 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006996 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006997 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006998 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006999 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007001 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007002}
7003
7004/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007005 * Allocate an empty dict for a return value.
7006 * Returns OK or FAIL.
7007 */
7008 static int
7009rettv_dict_alloc(rettv)
7010 typval_T *rettv;
7011{
7012 dict_T *d = dict_alloc();
7013
7014 if (d == NULL)
7015 return FAIL;
7016
7017 rettv->vval.v_dict = d;
7018 rettv->v_type = VAR_DICT;
7019 ++d->dv_refcount;
7020 return OK;
7021}
7022
7023
7024/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025 * Unreference a Dictionary: decrement the reference count and free it when it
7026 * becomes zero.
7027 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007028 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007032 if (d != NULL && --d->dv_refcount <= 0)
7033 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034}
7035
7036/*
7037 * Free a Dictionary, including all items it contains.
7038 * Ignores the reference count.
7039 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007040 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007041dict_free(d, recurse)
7042 dict_T *d;
7043 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007045 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007047 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 /* Remove the dict from the list of dicts for garbage collection. */
7050 if (d->dv_used_prev == NULL)
7051 first_dict = d->dv_used_next;
7052 else
7053 d->dv_used_prev->dv_used_next = d->dv_used_next;
7054 if (d->dv_used_next != NULL)
7055 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7056
7057 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007058 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007059 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 if (!HASHITEM_EMPTY(hi))
7063 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007064 /* Remove the item before deleting it, just in case there is
7065 * something recursive causing trouble. */
7066 di = HI2DI(hi);
7067 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007068 if (recurse || (di->di_tv.v_type != VAR_LIST
7069 && di->di_tv.v_type != VAR_DICT))
7070 clear_tv(&di->di_tv);
7071 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072 --todo;
7073 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007075 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 vim_free(d);
7077}
7078
7079/*
7080 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007081 * The "key" is copied to the new item.
7082 * Note that the value of the item "di_tv" still needs to be initialized!
7083 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007085 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086dictitem_alloc(key)
7087 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088{
Bram Moolenaar33570922005-01-25 22:26:29 +00007089 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007090
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007091 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007095 di->di_flags = 0;
7096 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098}
7099
7100/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007101 * Make a copy of a Dictionary item.
7102 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106{
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007109 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7110 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 if (di != NULL)
7112 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007114 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115 copy_tv(&org->di_tv, &di->di_tv);
7116 }
7117 return di;
7118}
7119
7120/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 * Remove item "item" from Dictionary "dict" and free it.
7122 */
7123 static void
7124dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 dict_T *dict;
7126 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127{
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 if (HASHITEM_EMPTY(hi))
7132 EMSG2(_(e_intern2), "dictitem_remove()");
7133 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135 dictitem_free(item);
7136}
7137
7138/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 * Free a dict item. Also clears the value.
7140 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007141 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007142dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 clear_tv(&item->di_tv);
7146 vim_free(item);
7147}
7148
7149/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7151 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007152 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153 * Returns NULL when out of memory.
7154 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007156dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007159 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160{
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 dict_T *copy;
7162 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007165
7166 if (orig == NULL)
7167 return NULL;
7168
7169 copy = dict_alloc();
7170 if (copy != NULL)
7171 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 if (copyID != 0)
7173 {
7174 orig->dv_copyID = copyID;
7175 orig->dv_copydict = copy;
7176 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007177 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 --todo;
7183
7184 di = dictitem_alloc(hi->hi_key);
7185 if (di == NULL)
7186 break;
7187 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188 {
7189 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7190 copyID) == FAIL)
7191 {
7192 vim_free(di);
7193 break;
7194 }
7195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 else
7197 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7198 if (dict_add(copy, di) == FAIL)
7199 {
7200 dictitem_free(di);
7201 break;
7202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007207 if (todo > 0)
7208 {
7209 dict_unref(copy);
7210 copy = NULL;
7211 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 }
7213
7214 return copy;
7215}
7216
7217/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007219 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007221 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 dict_T *d;
7224 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227}
7228
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007230 * Add a number or string entry to dictionary "d".
7231 * When "str" is NULL use number "nr", otherwise use "str".
7232 * Returns FAIL when out of memory and when key already exists.
7233 */
7234 int
7235dict_add_nr_str(d, key, nr, str)
7236 dict_T *d;
7237 char *key;
7238 long nr;
7239 char_u *str;
7240{
7241 dictitem_T *item;
7242
7243 item = dictitem_alloc((char_u *)key);
7244 if (item == NULL)
7245 return FAIL;
7246 item->di_tv.v_lock = 0;
7247 if (str == NULL)
7248 {
7249 item->di_tv.v_type = VAR_NUMBER;
7250 item->di_tv.vval.v_number = nr;
7251 }
7252 else
7253 {
7254 item->di_tv.v_type = VAR_STRING;
7255 item->di_tv.vval.v_string = vim_strsave(str);
7256 }
7257 if (dict_add(d, item) == FAIL)
7258 {
7259 dictitem_free(item);
7260 return FAIL;
7261 }
7262 return OK;
7263}
7264
7265/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007266 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007267 * Returns FAIL when out of memory and when key already exists.
7268 */
7269 int
7270dict_add_list(d, key, list)
7271 dict_T *d;
7272 char *key;
7273 list_T *list;
7274{
7275 dictitem_T *item;
7276
7277 item = dictitem_alloc((char_u *)key);
7278 if (item == NULL)
7279 return FAIL;
7280 item->di_tv.v_lock = 0;
7281 item->di_tv.v_type = VAR_LIST;
7282 item->di_tv.vval.v_list = list;
7283 if (dict_add(d, item) == FAIL)
7284 {
7285 dictitem_free(item);
7286 return FAIL;
7287 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007288 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007289 return OK;
7290}
7291
7292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 * Get the number of items in a Dictionary.
7294 */
7295 static long
7296dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 if (d == NULL)
7300 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302}
7303
7304/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 * Find item "key[len]" in Dictionary "d".
7306 * If "len" is negative use strlen(key).
7307 * Returns NULL when not found.
7308 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007309 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 char_u *key;
7313 int len;
7314{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315#define AKEYLEN 200
7316 char_u buf[AKEYLEN];
7317 char_u *akey;
7318 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 if (len < 0)
7322 akey = key;
7323 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 tofree = akey = vim_strnsave(key, len);
7326 if (akey == NULL)
7327 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007328 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 else
7330 {
7331 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007332 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 akey = buf;
7334 }
7335
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 vim_free(tofree);
7338 if (HASHITEM_EMPTY(hi))
7339 return NULL;
7340 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341}
7342
7343/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007344 * Get a string item from a dictionary.
7345 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007346 * Returns NULL if the entry doesn't exist or out of memory.
7347 */
7348 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007349get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007350 dict_T *d;
7351 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007352 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007353{
7354 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007355 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007356
7357 di = dict_find(d, key, -1);
7358 if (di == NULL)
7359 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007360 s = get_tv_string(&di->di_tv);
7361 if (save && s != NULL)
7362 s = vim_strsave(s);
7363 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007364}
7365
7366/*
7367 * Get a number item from a dictionary.
7368 * Returns 0 if the entry doesn't exist or out of memory.
7369 */
7370 long
7371get_dict_number(d, key)
7372 dict_T *d;
7373 char_u *key;
7374{
7375 dictitem_T *di;
7376
7377 di = dict_find(d, key, -1);
7378 if (di == NULL)
7379 return 0;
7380 return get_tv_number(&di->di_tv);
7381}
7382
7383/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 * Return an allocated string with the string representation of a Dictionary.
7385 * May return NULL.
7386 */
7387 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007388dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391{
7392 garray_T ga;
7393 int first = TRUE;
7394 char_u *tofree;
7395 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 return NULL;
7403 ga_init2(&ga, (int)sizeof(char), 80);
7404 ga_append(&ga, '{');
7405
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 --todo;
7412
7413 if (first)
7414 first = FALSE;
7415 else
7416 ga_concat(&ga, (char_u *)", ");
7417
7418 tofree = string_quote(hi->hi_key, FALSE);
7419 if (tofree != NULL)
7420 {
7421 ga_concat(&ga, tofree);
7422 vim_free(tofree);
7423 }
7424 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007425 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426 if (s != NULL)
7427 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 if (s == NULL)
7430 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 if (todo > 0)
7434 {
7435 vim_free(ga.ga_data);
7436 return NULL;
7437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438
7439 ga_append(&ga, '}');
7440 ga_append(&ga, NUL);
7441 return (char_u *)ga.ga_data;
7442}
7443
7444/*
7445 * Allocate a variable for a Dictionary and fill it from "*arg".
7446 * Return OK or FAIL. Returns NOTDONE for {expr}.
7447 */
7448 static int
7449get_dict_tv(arg, rettv, evaluate)
7450 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 int evaluate;
7453{
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 dict_T *d = NULL;
7455 typval_T tvkey;
7456 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007457 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007460 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461
7462 /*
7463 * First check if it's not a curly-braces thing: {expr}.
7464 * Must do this without evaluating, otherwise a function may be called
7465 * twice. Unfortunately this means we need to call eval1() twice for the
7466 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 if (*start != '}')
7470 {
7471 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7472 return FAIL;
7473 if (*start == '}')
7474 return NOTDONE;
7475 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476
7477 if (evaluate)
7478 {
7479 d = dict_alloc();
7480 if (d == NULL)
7481 return FAIL;
7482 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 tvkey.v_type = VAR_UNKNOWN;
7484 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485
7486 *arg = skipwhite(*arg + 1);
7487 while (**arg != '}' && **arg != NUL)
7488 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 goto failret;
7491 if (**arg != ':')
7492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007493 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 goto failret;
7496 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007497 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007499 key = get_tv_string_buf_chk(&tvkey, buf);
7500 if (key == NULL || *key == NUL)
7501 {
7502 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7503 if (key != NULL)
7504 EMSG(_(e_emptykey));
7505 clear_tv(&tvkey);
7506 goto failret;
7507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509
7510 *arg = skipwhite(*arg + 1);
7511 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7512 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007513 if (evaluate)
7514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 }
7517 if (evaluate)
7518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 if (item != NULL)
7521 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007522 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 clear_tv(&tv);
7525 goto failret;
7526 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 item = dictitem_alloc(key);
7528 clear_tv(&tvkey);
7529 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007532 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 if (dict_add(d, item) == FAIL)
7534 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 }
7536 }
7537
7538 if (**arg == '}')
7539 break;
7540 if (**arg != ',')
7541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 goto failret;
7544 }
7545 *arg = skipwhite(*arg + 1);
7546 }
7547
7548 if (**arg != '}')
7549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007550 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551failret:
7552 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007553 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 return FAIL;
7555 }
7556
7557 *arg = skipwhite(*arg + 1);
7558 if (evaluate)
7559 {
7560 rettv->v_type = VAR_DICT;
7561 rettv->vval.v_dict = d;
7562 ++d->dv_refcount;
7563 }
7564
7565 return OK;
7566}
7567
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 * Return a string with the string representation of a variable.
7570 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007571 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007572 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007574 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 */
7576 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007577echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007580 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007581 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 static int recurse = 0;
7584 char_u *r = NULL;
7585
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007588 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 *tofree = NULL;
7590 return NULL;
7591 }
7592 ++recurse;
7593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007594 switch (tv->v_type)
7595 {
7596 case VAR_FUNC:
7597 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 r = tv->vval.v_string;
7599 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602 if (tv->vval.v_list == NULL)
7603 {
7604 *tofree = NULL;
7605 r = NULL;
7606 }
7607 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7608 {
7609 *tofree = NULL;
7610 r = (char_u *)"[...]";
7611 }
7612 else
7613 {
7614 tv->vval.v_list->lv_copyID = copyID;
7615 *tofree = list2string(tv, copyID);
7616 r = *tofree;
7617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007621 if (tv->vval.v_dict == NULL)
7622 {
7623 *tofree = NULL;
7624 r = NULL;
7625 }
7626 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7627 {
7628 *tofree = NULL;
7629 r = (char_u *)"{...}";
7630 }
7631 else
7632 {
7633 tv->vval.v_dict->dv_copyID = copyID;
7634 *tofree = dict2string(tv, copyID);
7635 r = *tofree;
7636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 case VAR_STRING:
7640 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 *tofree = NULL;
7642 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007643 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007645#ifdef FEAT_FLOAT
7646 case VAR_FLOAT:
7647 *tofree = NULL;
7648 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7649 r = numbuf;
7650 break;
7651#endif
7652
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007653 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007654 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657
7658 --recurse;
7659 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660}
7661
7662/*
7663 * Return a string with the string representation of a variable.
7664 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7665 * "numbuf" is used for a number.
7666 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007667 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007668 */
7669 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672 char_u **tofree;
7673 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007674 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675{
7676 switch (tv->v_type)
7677 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007678 case VAR_FUNC:
7679 *tofree = string_quote(tv->vval.v_string, TRUE);
7680 return *tofree;
7681 case VAR_STRING:
7682 *tofree = string_quote(tv->vval.v_string, FALSE);
7683 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007684#ifdef FEAT_FLOAT
7685 case VAR_FLOAT:
7686 *tofree = NULL;
7687 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7688 return numbuf;
7689#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007691 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007696 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007697 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698}
7699
7700/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 * Return string "str" in ' quotes, doubling ' characters.
7702 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 */
7705 static char_u *
7706string_quote(str, function)
7707 char_u *str;
7708 int function;
7709{
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 char_u *p, *r, *s;
7712
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 len = (function ? 13 : 3);
7714 if (str != NULL)
7715 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007716 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 for (p = str; *p != NUL; mb_ptr_adv(p))
7718 if (*p == '\'')
7719 ++len;
7720 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 s = r = alloc(len);
7722 if (r != NULL)
7723 {
7724 if (function)
7725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 r += 10;
7728 }
7729 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 if (str != NULL)
7732 for (p = str; *p != NUL; )
7733 {
7734 if (*p == '\'')
7735 *r++ = '\'';
7736 MB_COPY_CHAR(p, r);
7737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007739 if (function)
7740 *r++ = ')';
7741 *r++ = NUL;
7742 }
7743 return s;
7744}
7745
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007746#ifdef FEAT_FLOAT
7747/*
7748 * Convert the string "text" to a floating point number.
7749 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7750 * this always uses a decimal point.
7751 * Returns the length of the text that was consumed.
7752 */
7753 static int
7754string2float(text, value)
7755 char_u *text;
7756 float_T *value; /* result stored here */
7757{
7758 char *s = (char *)text;
7759 float_T f;
7760
7761 f = strtod(s, &s);
7762 *value = f;
7763 return (int)((char_u *)s - text);
7764}
7765#endif
7766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 * Get the value of an environment variable.
7769 * "arg" is pointing to the '$'. It is advanced to after the name.
7770 * If the environment variable was not set, silently assume it is empty.
7771 * Always return OK.
7772 */
7773 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 int evaluate;
7778{
7779 char_u *string = NULL;
7780 int len;
7781 int cc;
7782 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007783 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
7785 ++*arg;
7786 name = *arg;
7787 len = get_env_len(arg);
7788 if (evaluate)
7789 {
7790 if (len != 0)
7791 {
7792 cc = name[len];
7793 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007794 /* first try vim_getenv(), fast for normal environment vars */
7795 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007797 {
7798 if (!mustfree)
7799 string = vim_strsave(string);
7800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 else
7802 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 if (mustfree)
7804 vim_free(string);
7805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 /* next try expanding things like $VIM and ${HOME} */
7807 string = expand_env_save(name - 1);
7808 if (string != NULL && *string == '$')
7809 {
7810 vim_free(string);
7811 string = NULL;
7812 }
7813 }
7814 name[len] = cc;
7815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 rettv->v_type = VAR_STRING;
7817 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819
7820 return OK;
7821}
7822
7823/*
7824 * Array with names and number of arguments of all internal functions
7825 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7826 */
7827static struct fst
7828{
7829 char *f_name; /* function name */
7830 char f_min_argc; /* minimal number of arguments */
7831 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007833 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834} functions[] =
7835{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836#ifdef FEAT_FLOAT
7837 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007838 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007839#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007840 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007841 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"append", 2, 2, f_append},
7843 {"argc", 0, 0, f_argc},
7844 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007845 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007846#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007847 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007852 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"bufexists", 1, 1, f_bufexists},
7854 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7855 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7856 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7857 {"buflisted", 1, 1, f_buflisted},
7858 {"bufloaded", 1, 1, f_bufloaded},
7859 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007860 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"bufwinnr", 1, 1, f_bufwinnr},
7862 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007863 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#ifdef FEAT_FLOAT
7866 {"ceil", 1, 1, f_ceil},
7867#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007868 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007869 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007871 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007873#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007874 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007875 {"complete_add", 1, 1, f_complete_add},
7876 {"complete_check", 0, 0, f_complete_check},
7877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#ifdef FEAT_FLOAT
7881 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007882 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007886 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007887 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"delete", 1, 1, f_delete},
7889 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007890 {"diff_filler", 1, 1, f_diff_filler},
7891 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007892 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"eventhandler", 0, 0, f_eventhandler},
7896 {"executable", 1, 1, f_executable},
7897 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007898#ifdef FEAT_FLOAT
7899 {"exp", 1, 1, f_exp},
7900#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007901 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007902 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007903 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7905 {"filereadable", 1, 1, f_filereadable},
7906 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007908 {"finddir", 1, 3, f_finddir},
7909 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910#ifdef FEAT_FLOAT
7911 {"float2nr", 1, 1, f_float2nr},
7912 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007913 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007915 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"fnamemodify", 2, 2, f_fnamemodify},
7917 {"foldclosed", 1, 1, f_foldclosed},
7918 {"foldclosedend", 1, 1, f_foldclosedend},
7919 {"foldlevel", 1, 1, f_foldlevel},
7920 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007921 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007923 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007924 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007925 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007926 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007927 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"getchar", 0, 1, f_getchar},
7929 {"getcharmod", 0, 0, f_getcharmod},
7930 {"getcmdline", 0, 0, f_getcmdline},
7931 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007932 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007934 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007935 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"getfsize", 1, 1, f_getfsize},
7937 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007938 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007939 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007940 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007941 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007942 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007943 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007944 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007945 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007947 {"gettabvar", 2, 3, f_gettabvar},
7948 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getwinposx", 0, 0, f_getwinposx},
7950 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007951 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007952 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007953 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007955 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007956 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007957 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7959 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7960 {"histadd", 2, 2, f_histadd},
7961 {"histdel", 1, 2, f_histdel},
7962 {"histget", 1, 2, f_histget},
7963 {"histnr", 1, 1, f_histnr},
7964 {"hlID", 1, 1, f_hlID},
7965 {"hlexists", 1, 1, f_hlexists},
7966 {"hostname", 0, 0, f_hostname},
7967 {"iconv", 3, 3, f_iconv},
7968 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007970 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007972 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"inputrestore", 0, 0, f_inputrestore},
7974 {"inputsave", 0, 0, f_inputsave},
7975 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007977 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007979 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007984 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"libcall", 3, 3, f_libcall},
7986 {"libcallnr", 3, 3, f_libcallnr},
7987 {"line", 1, 1, f_line},
7988 {"line2byte", 1, 1, f_line2byte},
7989 {"lispindent", 1, 1, f_lispindent},
7990 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007991#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007992 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993 {"log10", 1, 1, f_log10},
7994#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007995#ifdef FEAT_LUA
7996 {"luaeval", 1, 2, f_luaeval},
7997#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007999 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008000 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008001 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008002 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008003 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008004 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008005 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008006 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008007 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008008 {"max", 1, 1, f_max},
8009 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008010#ifdef vim_mkdir
8011 {"mkdir", 1, 3, f_mkdir},
8012#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008014#ifdef FEAT_MZSCHEME
8015 {"mzeval", 1, 1, f_mzeval},
8016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008018 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008019 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008020 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008021#ifdef FEAT_FLOAT
8022 {"pow", 2, 2, f_pow},
8023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008025 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008026 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008027#ifdef FEAT_PYTHON3
8028 {"py3eval", 1, 1, f_py3eval},
8029#endif
8030#ifdef FEAT_PYTHON
8031 {"pyeval", 1, 1, f_pyeval},
8032#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008034 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008035 {"reltime", 0, 2, f_reltime},
8036 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"remote_expr", 2, 3, f_remote_expr},
8038 {"remote_foreground", 1, 1, f_remote_foreground},
8039 {"remote_peek", 1, 2, f_remote_peek},
8040 {"remote_read", 1, 1, f_remote_read},
8041 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008042 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008044 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008047#ifdef FEAT_FLOAT
8048 {"round", 1, 1, f_round},
8049#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008050 {"screenattr", 2, 2, f_screenattr},
8051 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008052 {"screencol", 0, 0, f_screencol},
8053 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008054 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008055 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008056 {"searchpair", 3, 7, f_searchpair},
8057 {"searchpairpos", 3, 7, f_searchpairpos},
8058 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"server2client", 2, 2, f_server2client},
8060 {"serverlist", 0, 0, f_serverlist},
8061 {"setbufvar", 3, 3, f_setbufvar},
8062 {"setcmdpos", 1, 1, f_setcmdpos},
8063 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008064 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008065 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008066 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008067 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008069 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008070 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008072#ifdef FEAT_CRYPT
8073 {"sha256", 1, 1, f_sha256},
8074#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008075 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008076 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008080 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008082 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008083 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008084 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008085 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008086 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087#ifdef FEAT_FLOAT
8088 {"sqrt", 1, 1, f_sqrt},
8089 {"str2float", 1, 1, f_str2float},
8090#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008091 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008092 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008093 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#ifdef HAVE_STRFTIME
8095 {"strftime", 1, 2, f_strftime},
8096#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"strlen", 1, 1, f_strlen},
8100 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008101 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008103 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"submatch", 1, 1, f_submatch},
8105 {"substitute", 4, 4, f_substitute},
8106 {"synID", 3, 3, f_synID},
8107 {"synIDattr", 2, 3, f_synIDattr},
8108 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008109 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008110 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008111 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008112 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008113 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008114 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008115 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008116 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008117#ifdef FEAT_FLOAT
8118 {"tan", 1, 1, f_tan},
8119 {"tanh", 1, 1, f_tanh},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008122 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"tolower", 1, 1, f_tolower},
8124 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008125 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#ifdef FEAT_FLOAT
8127 {"trunc", 1, 1, f_trunc},
8128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008130 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008131 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"virtcol", 1, 1, f_virtcol},
8134 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008135 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"winbufnr", 1, 1, f_winbufnr},
8137 {"wincol", 0, 0, f_wincol},
8138 {"winheight", 1, 1, f_winheight},
8139 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008140 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008142 {"winrestview", 1, 1, f_winrestview},
8143 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008145 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008146 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147};
8148
8149#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8150
8151/*
8152 * Function given to ExpandGeneric() to obtain the list of internal
8153 * or user defined function names.
8154 */
8155 char_u *
8156get_function_name(xp, idx)
8157 expand_T *xp;
8158 int idx;
8159{
8160 static int intidx = -1;
8161 char_u *name;
8162
8163 if (idx == 0)
8164 intidx = -1;
8165 if (intidx < 0)
8166 {
8167 name = get_user_func_name(xp, idx);
8168 if (name != NULL)
8169 return name;
8170 }
8171 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8172 {
8173 STRCPY(IObuff, functions[intidx].f_name);
8174 STRCAT(IObuff, "(");
8175 if (functions[intidx].f_max_argc == 0)
8176 STRCAT(IObuff, ")");
8177 return IObuff;
8178 }
8179
8180 return NULL;
8181}
8182
8183/*
8184 * Function given to ExpandGeneric() to obtain the list of internal or
8185 * user defined variable or function names.
8186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 char_u *
8188get_expr_name(xp, idx)
8189 expand_T *xp;
8190 int idx;
8191{
8192 static int intidx = -1;
8193 char_u *name;
8194
8195 if (idx == 0)
8196 intidx = -1;
8197 if (intidx < 0)
8198 {
8199 name = get_function_name(xp, idx);
8200 if (name != NULL)
8201 return name;
8202 }
8203 return get_user_var_name(xp, ++intidx);
8204}
8205
8206#endif /* FEAT_CMDL_COMPL */
8207
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008208#if defined(EBCDIC) || defined(PROTO)
8209/*
8210 * Compare struct fst by function name.
8211 */
8212 static int
8213compare_func_name(s1, s2)
8214 const void *s1;
8215 const void *s2;
8216{
8217 struct fst *p1 = (struct fst *)s1;
8218 struct fst *p2 = (struct fst *)s2;
8219
8220 return STRCMP(p1->f_name, p2->f_name);
8221}
8222
8223/*
8224 * Sort the function table by function name.
8225 * The sorting of the table above is ASCII dependant.
8226 * On machines using EBCDIC we have to sort it.
8227 */
8228 static void
8229sortFunctions()
8230{
8231 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8232
8233 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8234}
8235#endif
8236
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238/*
8239 * Find internal function in table above.
8240 * Return index, or -1 if not found
8241 */
8242 static int
8243find_internal_func(name)
8244 char_u *name; /* name of the function */
8245{
8246 int first = 0;
8247 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8248 int cmp;
8249 int x;
8250
8251 /*
8252 * Find the function name in the table. Binary search.
8253 */
8254 while (first <= last)
8255 {
8256 x = first + ((unsigned)(last - first) >> 1);
8257 cmp = STRCMP(name, functions[x].f_name);
8258 if (cmp < 0)
8259 last = x - 1;
8260 else if (cmp > 0)
8261 first = x + 1;
8262 else
8263 return x;
8264 }
8265 return -1;
8266}
8267
8268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8270 * name it contains, otherwise return "name".
8271 */
8272 static char_u *
8273deref_func_name(name, lenp)
8274 char_u *name;
8275 int *lenp;
8276{
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278 int cc;
8279
8280 cc = name[*lenp];
8281 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008282 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287 {
8288 *lenp = 0;
8289 return (char_u *)""; /* just in case */
8290 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008291 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 }
8294
8295 return name;
8296}
8297
8298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * Allocate a variable for the result of a function.
8300 * Return OK or FAIL.
8301 */
8302 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8304 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 char_u *name; /* name of the function */
8306 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 char_u **arg; /* argument, pointing to the '(' */
8309 linenr_T firstline; /* first line of range */
8310 linenr_T lastline; /* last line of range */
8311 int *doesrange; /* return: function handled range */
8312 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 char_u *argp;
8316 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 int argcount = 0; /* number of arguments found */
8319
8320 /*
8321 * Get the arguments.
8322 */
8323 argp = *arg;
8324 while (argcount < MAX_FUNC_ARGS)
8325 {
8326 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8327 if (*argp == ')' || *argp == ',' || *argp == NUL)
8328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8330 {
8331 ret = FAIL;
8332 break;
8333 }
8334 ++argcount;
8335 if (*argp != ',')
8336 break;
8337 }
8338 if (*argp == ')')
8339 ++argp;
8340 else
8341 ret = FAIL;
8342
8343 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 {
8348 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008349 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008351 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 *arg = skipwhite(argp);
8358 return ret;
8359}
8360
8361
8362/*
8363 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008364 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008365 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 */
8367 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008368call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008370 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008374 typval_T *argvars; /* vars for arguments, must have "argcount"
8375 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 linenr_T firstline; /* first line of range */
8377 linenr_T lastline; /* last line of range */
8378 int *doesrange; /* return: function handled range */
8379 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#define ERROR_UNKNOWN 0
8384#define ERROR_TOOMANY 1
8385#define ERROR_TOOFEW 2
8386#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387#define ERROR_DICT 4
8388#define ERROR_NONE 5
8389#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int error = ERROR_NONE;
8391 int i;
8392 int llen;
8393 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#define FLEN_FIXED 40
8395 char_u fname_buf[FLEN_FIXED + 1];
8396 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008397 char_u *name;
8398
8399 /* Make a copy of the name, if it comes from a funcref variable it could
8400 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008401 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008402 if (name == NULL)
8403 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 /*
8406 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8407 * Change <SNR>123_name() to K_SNR 123_name().
8408 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 llen = eval_fname_script(name);
8411 if (llen > 0)
8412 {
8413 fname_buf[0] = K_SPECIAL;
8414 fname_buf[1] = KS_EXTRA;
8415 fname_buf[2] = (int)KE_SNR;
8416 i = 3;
8417 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8418 {
8419 if (current_SID <= 0)
8420 error = ERROR_SCRIPT;
8421 else
8422 {
8423 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8424 i = (int)STRLEN(fname_buf);
8425 }
8426 }
8427 if (i + STRLEN(name + llen) < FLEN_FIXED)
8428 {
8429 STRCPY(fname_buf + i, name + llen);
8430 fname = fname_buf;
8431 }
8432 else
8433 {
8434 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8435 if (fname == NULL)
8436 error = ERROR_OTHER;
8437 else
8438 {
8439 mch_memmove(fname, fname_buf, (size_t)i);
8440 STRCPY(fname + i, name + llen);
8441 }
8442 }
8443 }
8444 else
8445 fname = name;
8446
8447 *doesrange = FALSE;
8448
8449
8450 /* execute the function if no errors detected and executing */
8451 if (evaluate && error == ERROR_NONE)
8452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008453 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 error = ERROR_UNKNOWN;
8456
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 /*
8460 * User defined function.
8461 */
8462 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* Trigger FuncUndefined event, may load the function. */
8466 if (fp == NULL
8467 && apply_autocmds(EVENT_FUNCUNDEFINED,
8468 fname, fname, TRUE, NULL)
8469 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 fp = find_func(fname);
8473 }
8474#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008476 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 {
8478 /* loaded a package, search for the function again */
8479 fp = find_func(fname);
8480 }
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (fp != NULL)
8483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008484 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008488 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else
8493 {
8494 /*
8495 * Call the user function.
8496 * Save and restore search patterns, script variables and
8497 * redo buffer.
8498 */
8499 save_search_patterns();
8500 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008501 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008504 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8505 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8506 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008507 /* Function was unreferenced while being used, free it
8508 * now. */
8509 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 restoreRedobuff();
8511 restore_search_patterns();
8512 error = ERROR_NONE;
8513 }
8514 }
8515 }
8516 else
8517 {
8518 /*
8519 * Find the function name in the table, call its implementation.
8520 */
8521 i = find_internal_func(fname);
8522 if (i >= 0)
8523 {
8524 if (argcount < functions[i].f_min_argc)
8525 error = ERROR_TOOFEW;
8526 else if (argcount > functions[i].f_max_argc)
8527 error = ERROR_TOOMANY;
8528 else
8529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 error = ERROR_NONE;
8533 }
8534 }
8535 }
8536 /*
8537 * The function call (or "FuncUndefined" autocommand sequence) might
8538 * have been aborted by an error, an interrupt, or an explicitly thrown
8539 * exception that has not been caught so far. This situation can be
8540 * tested for by calling aborting(). For an error in an internal
8541 * function or for the "E132" error in call_user_func(), however, the
8542 * throw point at which the "force_abort" flag (temporarily reset by
8543 * emsg()) is normally updated has not been reached yet. We need to
8544 * update that flag first to make aborting() reliable.
8545 */
8546 update_force_abort();
8547 }
8548 if (error == ERROR_NONE)
8549 ret = OK;
8550
8551 /*
8552 * Report an error unless the argument evaluation or function call has been
8553 * cancelled due to an aborting error, an interrupt, or an exception.
8554 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 if (!aborting())
8556 {
8557 switch (error)
8558 {
8559 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 break;
8562 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008564 break;
8565 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008566 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 name);
8568 break;
8569 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008570 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008571 name);
8572 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008573 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008574 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 name);
8576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 }
8578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (fname != name && fname != fname_buf)
8581 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008582 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
8584 return ret;
8585}
8586
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008587/*
8588 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008589 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008590 */
8591 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008592emsg_funcname(ermsg, name)
8593 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 char_u *name;
8595{
8596 char_u *p;
8597
8598 if (*name == K_SPECIAL)
8599 p = concat_str((char_u *)"<SNR>", name + 3);
8600 else
8601 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008602 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008603 if (p != name)
8604 vim_free(p);
8605}
8606
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008607/*
8608 * Return TRUE for a non-zero Number and a non-empty String.
8609 */
8610 static int
8611non_zero_arg(argvars)
8612 typval_T *argvars;
8613{
8614 return ((argvars[0].v_type == VAR_NUMBER
8615 && argvars[0].vval.v_number != 0)
8616 || (argvars[0].v_type == VAR_STRING
8617 && argvars[0].vval.v_string != NULL
8618 && *argvars[0].vval.v_string != NUL));
8619}
8620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*********************************************
8622 * Implementation of the built-in functions
8623 */
8624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008626static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8627
8628/*
8629 * Get the float value of "argvars[0]" into "f".
8630 * Returns FAIL when the argument is not a Number or Float.
8631 */
8632 static int
8633get_float_arg(argvars, f)
8634 typval_T *argvars;
8635 float_T *f;
8636{
8637 if (argvars[0].v_type == VAR_FLOAT)
8638 {
8639 *f = argvars[0].vval.v_float;
8640 return OK;
8641 }
8642 if (argvars[0].v_type == VAR_NUMBER)
8643 {
8644 *f = (float_T)argvars[0].vval.v_number;
8645 return OK;
8646 }
8647 EMSG(_("E808: Number or Float required"));
8648 return FAIL;
8649}
8650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651/*
8652 * "abs(expr)" function
8653 */
8654 static void
8655f_abs(argvars, rettv)
8656 typval_T *argvars;
8657 typval_T *rettv;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 rettv->v_type = VAR_FLOAT;
8662 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8663 }
8664 else
8665 {
8666 varnumber_T n;
8667 int error = FALSE;
8668
8669 n = get_tv_number_chk(&argvars[0], &error);
8670 if (error)
8671 rettv->vval.v_number = -1;
8672 else if (n > 0)
8673 rettv->vval.v_number = n;
8674 else
8675 rettv->vval.v_number = -n;
8676 }
8677}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008678
8679/*
8680 * "acos()" function
8681 */
8682 static void
8683f_acos(argvars, rettv)
8684 typval_T *argvars;
8685 typval_T *rettv;
8686{
8687 float_T f;
8688
8689 rettv->v_type = VAR_FLOAT;
8690 if (get_float_arg(argvars, &f) == OK)
8691 rettv->vval.v_float = acos(f);
8692 else
8693 rettv->vval.v_float = 0.0;
8694}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695#endif
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 */
8700 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008701f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *argvars;
8703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008710 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008711 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008712 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 }
8715 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008716 EMSG(_(e_listreq));
8717}
8718
8719/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008720 * "and(expr, expr)" function
8721 */
8722 static void
8723f_and(argvars, rettv)
8724 typval_T *argvars;
8725 typval_T *rettv;
8726{
8727 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8728 & get_tv_number_chk(&argvars[1], NULL);
8729}
8730
8731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 * "append(lnum, string/list)" function
8733 */
8734 static void
8735f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738{
8739 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 list_T *l = NULL;
8742 listitem_T *li = NULL;
8743 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 long added = 0;
8745
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008746 /* When coming here from Insert mode, sync undo, so that this can be
8747 * undone separately from what was previously inserted. */
8748 if (u_sync_once == 2)
8749 {
8750 u_sync_once = 1; /* notify that u_sync() was called */
8751 u_sync(TRUE);
8752 }
8753
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 lnum = get_tv_lnum(argvars);
8755 if (lnum >= 0
8756 && lnum <= curbuf->b_ml.ml_line_count
8757 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008758 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008759 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008760 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761 l = argvars[1].vval.v_list;
8762 if (l == NULL)
8763 return;
8764 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008765 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 for (;;)
8767 {
8768 if (l == NULL)
8769 tv = &argvars[1]; /* append a string */
8770 else if (li == NULL)
8771 break; /* end of list */
8772 else
8773 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 line = get_tv_string_chk(tv);
8775 if (line == NULL) /* type error */
8776 {
8777 rettv->vval.v_number = 1; /* Failed */
8778 break;
8779 }
8780 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781 ++added;
8782 if (l == NULL)
8783 break;
8784 li = li->li_next;
8785 }
8786
8787 appended_lines_mark(lnum, added);
8788 if (curwin->w_cursor.lnum > lnum)
8789 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008791 else
8792 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795/*
8796 * "argc()" function
8797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
8806/*
8807 * "argidx()" function
8808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008811 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "argv(nr)" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 int idx;
8826
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008827 if (argvars[0].v_type != VAR_UNKNOWN)
8828 {
8829 idx = get_tv_number_chk(&argvars[0], NULL);
8830 if (idx >= 0 && idx < ARGCOUNT)
8831 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8832 else
8833 rettv->vval.v_string = NULL;
8834 rettv->v_type = VAR_STRING;
8835 }
8836 else if (rettv_list_alloc(rettv) == OK)
8837 for (idx = 0; idx < ARGCOUNT; ++idx)
8838 list_append_string(rettv->vval.v_list,
8839 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840}
8841
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008843/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008844 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008845 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008846 static void
8847f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008848 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008849 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008851 float_T f;
8852
8853 rettv->v_type = VAR_FLOAT;
8854 if (get_float_arg(argvars, &f) == OK)
8855 rettv->vval.v_float = asin(f);
8856 else
8857 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008858}
8859
8860/*
8861 * "atan()" function
8862 */
8863 static void
8864f_atan(argvars, rettv)
8865 typval_T *argvars;
8866 typval_T *rettv;
8867{
8868 float_T f;
8869
8870 rettv->v_type = VAR_FLOAT;
8871 if (get_float_arg(argvars, &f) == OK)
8872 rettv->vval.v_float = atan(f);
8873 else
8874 rettv->vval.v_float = 0.0;
8875}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008876
8877/*
8878 * "atan2()" function
8879 */
8880 static void
8881f_atan2(argvars, rettv)
8882 typval_T *argvars;
8883 typval_T *rettv;
8884{
8885 float_T fx, fy;
8886
8887 rettv->v_type = VAR_FLOAT;
8888 if (get_float_arg(argvars, &fx) == OK
8889 && get_float_arg(&argvars[1], &fy) == OK)
8890 rettv->vval.v_float = atan2(fx, fy);
8891 else
8892 rettv->vval.v_float = 0.0;
8893}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008894#endif
8895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896/*
8897 * "browse(save, title, initdir, default)" function
8898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008901 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904#ifdef FEAT_BROWSE
8905 int save;
8906 char_u *title;
8907 char_u *initdir;
8908 char_u *defname;
8909 char_u buf[NUMBUFLEN];
8910 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008913 save = get_tv_number_chk(&argvars[0], &error);
8914 title = get_tv_string_chk(&argvars[1]);
8915 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8916 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 if (error || title == NULL || initdir == NULL || defname == NULL)
8919 rettv->vval.v_string = NULL;
8920 else
8921 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008922 do_browse(save ? BROWSE_SAVE : 0,
8923 title, defname, NULL, initdir, NULL, curbuf);
8924#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008928}
8929
8930/*
8931 * "browsedir(title, initdir)" function
8932 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008937{
8938#ifdef FEAT_BROWSE
8939 char_u *title;
8940 char_u *initdir;
8941 char_u buf[NUMBUFLEN];
8942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 title = get_tv_string_chk(&argvars[0]);
8944 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 if (title == NULL || initdir == NULL)
8947 rettv->vval.v_string = NULL;
8948 else
8949 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955}
8956
Bram Moolenaar33570922005-01-25 22:26:29 +00008957static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008958
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959/*
8960 * Find a buffer by number or exact name.
8961 */
8962 static buf_T *
8963find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008964 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008968 if (avar->v_type == VAR_NUMBER)
8969 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008970 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008972 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008973 if (buf == NULL)
8974 {
8975 /* No full path name match, try a match with a URL or a "nofile"
8976 * buffer, these don't use the full path. */
8977 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8978 if (buf->b_fname != NULL
8979 && (path_with_url(buf->b_fname)
8980#ifdef FEAT_QUICKFIX
8981 || bt_nofile(buf)
8982#endif
8983 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008985 break;
8986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
8988 return buf;
8989}
8990
8991/*
8992 * "bufexists(expr)" function
8993 */
8994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 typval_T *argvars;
8997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008999 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002/*
9003 * "buflisted(expr)" function
9004 */
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 buf_T *buf;
9011
9012 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014}
9015
9016/*
9017 * "bufloaded(expr)" function
9018 */
9019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *argvars;
9022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024 buf_T *buf;
9025
9026 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009030static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032/*
9033 * Get buffer by number or pattern.
9034 */
9035 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009036get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009038 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 int save_magic;
9042 char_u *save_cpo;
9043 buf_T *buf;
9044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 if (tv->v_type == VAR_NUMBER)
9046 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009047 if (tv->v_type != VAR_STRING)
9048 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 if (name == NULL || *name == NUL)
9050 return curbuf;
9051 if (name[0] == '$' && name[1] == NUL)
9052 return lastbuf;
9053
9054 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9055 save_magic = p_magic;
9056 p_magic = TRUE;
9057 save_cpo = p_cpo;
9058 p_cpo = (char_u *)"";
9059
9060 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009061 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
9063 p_magic = save_magic;
9064 p_cpo = save_cpo;
9065
9066 /* If not found, try expanding the name, like done for bufexists(). */
9067 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
9070 return buf;
9071}
9072
9073/*
9074 * "bufname(expr)" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 buf_T *buf;
9082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009085 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 --emsg_off;
9092}
9093
9094/*
9095 * "bufnr(expr)" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009103 int error = FALSE;
9104 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009108 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009109 --emsg_off;
9110
9111 /* If the buffer isn't found and the second argument is not zero create a
9112 * new buffer. */
9113 if (buf == NULL
9114 && argvars[1].v_type != VAR_UNKNOWN
9115 && get_tv_number_chk(&argvars[1], &error) != 0
9116 && !error
9117 && (name = get_tv_string_chk(&argvars[0])) != NULL
9118 && !error)
9119 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * "bufwinnr(nr)" function
9129 */
9130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135#ifdef FEAT_WINDOWS
9136 win_T *wp;
9137 int winnr = 0;
9138#endif
9139 buf_T *buf;
9140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009143 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144#ifdef FEAT_WINDOWS
9145 for (wp = firstwin; wp; wp = wp->w_next)
9146 {
9147 ++winnr;
9148 if (wp->w_buffer == buf)
9149 break;
9150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#endif
9155 --emsg_off;
9156}
9157
9158/*
9159 * "byte2line(byte)" function
9160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#else
9169 long boff = 0;
9170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 (linenr_T)0, &boff);
9177#endif
9178}
9179
9180/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009181 * "byteidx()" function
9182 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *argvars;
9186 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009187{
9188#ifdef FEAT_MBYTE
9189 char_u *t;
9190#endif
9191 char_u *str;
9192 long idx;
9193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009194 str = get_tv_string_chk(&argvars[0]);
9195 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009197 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198 return;
9199
9200#ifdef FEAT_MBYTE
9201 t = str;
9202 for ( ; idx > 0; idx--)
9203 {
9204 if (*t == NUL) /* EOL reached */
9205 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009206 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009207 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009208 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009209#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009210 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009212#endif
9213}
9214
Bram Moolenaardb913952012-06-29 12:54:53 +02009215 int
9216func_call(name, args, selfdict, rettv)
9217 char_u *name;
9218 typval_T *args;
9219 dict_T *selfdict;
9220 typval_T *rettv;
9221{
9222 listitem_T *item;
9223 typval_T argv[MAX_FUNC_ARGS + 1];
9224 int argc = 0;
9225 int dummy;
9226 int r = 0;
9227
9228 for (item = args->vval.v_list->lv_first; item != NULL;
9229 item = item->li_next)
9230 {
9231 if (argc == MAX_FUNC_ARGS)
9232 {
9233 EMSG(_("E699: Too many arguments"));
9234 break;
9235 }
9236 /* Make a copy of each argument. This is needed to be able to set
9237 * v_lock to VAR_FIXED in the copy without changing the original list.
9238 */
9239 copy_tv(&item->li_tv, &argv[argc++]);
9240 }
9241
9242 if (item == NULL)
9243 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9244 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9245 &dummy, TRUE, selfdict);
9246
9247 /* Free the arguments. */
9248 while (argc > 0)
9249 clear_tv(&argv[--argc]);
9250
9251 return r;
9252}
9253
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009254/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009255 * "call(func, arglist)" function
9256 */
9257 static void
9258f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009259 typval_T *argvars;
9260 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009261{
9262 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009263 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009264
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009265 if (argvars[1].v_type != VAR_LIST)
9266 {
9267 EMSG(_(e_listreq));
9268 return;
9269 }
9270 if (argvars[1].vval.v_list == NULL)
9271 return;
9272
9273 if (argvars[0].v_type == VAR_FUNC)
9274 func = argvars[0].vval.v_string;
9275 else
9276 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 if (*func == NUL)
9278 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009279
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 if (argvars[2].v_type != VAR_UNKNOWN)
9281 {
9282 if (argvars[2].v_type != VAR_DICT)
9283 {
9284 EMSG(_(e_dictreq));
9285 return;
9286 }
9287 selfdict = argvars[2].vval.v_dict;
9288 }
9289
Bram Moolenaardb913952012-06-29 12:54:53 +02009290 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009291}
9292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009293#ifdef FEAT_FLOAT
9294/*
9295 * "ceil({float})" function
9296 */
9297 static void
9298f_ceil(argvars, rettv)
9299 typval_T *argvars;
9300 typval_T *rettv;
9301{
9302 float_T f;
9303
9304 rettv->v_type = VAR_FLOAT;
9305 if (get_float_arg(argvars, &f) == OK)
9306 rettv->vval.v_float = ceil(f);
9307 else
9308 rettv->vval.v_float = 0.0;
9309}
9310#endif
9311
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009312/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009313 * "changenr()" function
9314 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009315 static void
9316f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009317 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009318 typval_T *rettv;
9319{
9320 rettv->vval.v_number = curbuf->b_u_seq_cur;
9321}
9322
9323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 * "char2nr(string)" function
9325 */
9326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
9331#ifdef FEAT_MBYTE
9332 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009333 {
9334 int utf8 = 0;
9335
9336 if (argvars[1].v_type != VAR_UNKNOWN)
9337 utf8 = get_tv_number_chk(&argvars[1], NULL);
9338
9339 if (utf8)
9340 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9341 else
9342 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 else
9345#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347}
9348
9349/*
9350 * "cindent(lnum)" function
9351 */
9352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009354 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
9357#ifdef FEAT_CINDENT
9358 pos_T pos;
9359 linenr_T lnum;
9360
9361 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9364 {
9365 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 curwin->w_cursor = pos;
9368 }
9369 else
9370#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372}
9373
9374/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009375 * "clearmatches()" function
9376 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009377 static void
9378f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009379 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009380 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009381{
9382#ifdef FEAT_SEARCH_EXTRA
9383 clear_matches(curwin);
9384#endif
9385}
9386
9387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 * "col(string)" function
9389 */
9390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 typval_T *argvars;
9393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
9395 colnr_T col = 0;
9396 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009397 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009399 fp = var2fpos(&argvars[0], FALSE, &fnum);
9400 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
9402 if (fp->col == MAXCOL)
9403 {
9404 /* '> can be MAXCOL, get the length of the line then */
9405 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009406 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 else
9408 col = MAXCOL;
9409 }
9410 else
9411 {
9412 col = fp->col + 1;
9413#ifdef FEAT_VIRTUALEDIT
9414 /* col(".") when the cursor is on the NUL at the end of the line
9415 * because of "coladd" can be seen as an extra column. */
9416 if (virtual_active() && fp == &curwin->w_cursor)
9417 {
9418 char_u *p = ml_get_cursor();
9419
9420 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9421 curwin->w_virtcol - curwin->w_cursor.coladd))
9422 {
9423# ifdef FEAT_MBYTE
9424 int l;
9425
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009426 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 col += l;
9428# else
9429 if (*p != NUL && p[1] == NUL)
9430 ++col;
9431# endif
9432 }
9433 }
9434#endif
9435 }
9436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438}
9439
Bram Moolenaar572cb562005-08-05 21:35:02 +00009440#if defined(FEAT_INS_EXPAND)
9441/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009442 * "complete()" function
9443 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009444 static void
9445f_complete(argvars, rettv)
9446 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009448{
9449 int startcol;
9450
9451 if ((State & INSERT) == 0)
9452 {
9453 EMSG(_("E785: complete() can only be used in Insert mode"));
9454 return;
9455 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009456
9457 /* Check for undo allowed here, because if something was already inserted
9458 * the line was already saved for undo and this check isn't done. */
9459 if (!undo_allowed())
9460 return;
9461
Bram Moolenaarade00832006-03-10 21:46:58 +00009462 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9463 {
9464 EMSG(_(e_invarg));
9465 return;
9466 }
9467
9468 startcol = get_tv_number_chk(&argvars[0], NULL);
9469 if (startcol <= 0)
9470 return;
9471
9472 set_completion(startcol - 1, argvars[1].vval.v_list);
9473}
9474
9475/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009476 * "complete_add()" function
9477 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009478 static void
9479f_complete_add(argvars, rettv)
9480 typval_T *argvars;
9481 typval_T *rettv;
9482{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009483 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009484}
9485
9486/*
9487 * "complete_check()" function
9488 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009489 static void
9490f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009491 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009492 typval_T *rettv;
9493{
9494 int saved = RedrawingDisabled;
9495
9496 RedrawingDisabled = 0;
9497 ins_compl_check_keys(0);
9498 rettv->vval.v_number = compl_interrupted;
9499 RedrawingDisabled = saved;
9500}
9501#endif
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * "confirm(message, buttons[, default [, type]])" function
9505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009508 typval_T *argvars UNUSED;
9509 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9512 char_u *message;
9513 char_u *buttons = NULL;
9514 char_u buf[NUMBUFLEN];
9515 char_u buf2[NUMBUFLEN];
9516 int def = 1;
9517 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 char_u *typestr;
9519 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 message = get_tv_string_chk(&argvars[0]);
9522 if (message == NULL)
9523 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009526 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9527 if (buttons == NULL)
9528 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009532 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9535 if (typestr == NULL)
9536 error = TRUE;
9537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 switch (TOUPPER_ASC(*typestr))
9540 {
9541 case 'E': type = VIM_ERROR; break;
9542 case 'Q': type = VIM_QUESTION; break;
9543 case 'I': type = VIM_INFO; break;
9544 case 'W': type = VIM_WARNING; break;
9545 case 'G': type = VIM_GENERIC; break;
9546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
9548 }
9549 }
9550 }
9551
9552 if (buttons == NULL || *buttons == NUL)
9553 buttons = (char_u *)_("&Ok");
9554
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009555 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009557 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#endif
9559}
9560
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561/*
9562 * "copy()" function
9563 */
9564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009566 typval_T *argvars;
9567 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009568{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009569 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009572#ifdef FEAT_FLOAT
9573/*
9574 * "cos()" function
9575 */
9576 static void
9577f_cos(argvars, rettv)
9578 typval_T *argvars;
9579 typval_T *rettv;
9580{
9581 float_T f;
9582
9583 rettv->v_type = VAR_FLOAT;
9584 if (get_float_arg(argvars, &f) == OK)
9585 rettv->vval.v_float = cos(f);
9586 else
9587 rettv->vval.v_float = 0.0;
9588}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009589
9590/*
9591 * "cosh()" function
9592 */
9593 static void
9594f_cosh(argvars, rettv)
9595 typval_T *argvars;
9596 typval_T *rettv;
9597{
9598 float_T f;
9599
9600 rettv->v_type = VAR_FLOAT;
9601 if (get_float_arg(argvars, &f) == OK)
9602 rettv->vval.v_float = cosh(f);
9603 else
9604 rettv->vval.v_float = 0.0;
9605}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009606#endif
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009609 * "count()" function
9610 */
9611 static void
9612f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009615{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009616 long n = 0;
9617 int ic = FALSE;
9618
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009620 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 listitem_T *li;
9622 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009624
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 if ((l = argvars[0].vval.v_list) != NULL)
9626 {
9627 li = l->lv_first;
9628 if (argvars[2].v_type != VAR_UNKNOWN)
9629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009630 int error = FALSE;
9631
9632 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 if (argvars[3].v_type != VAR_UNKNOWN)
9634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 idx = get_tv_number_chk(&argvars[3], &error);
9636 if (!error)
9637 {
9638 li = list_find(l, idx);
9639 if (li == NULL)
9640 EMSGN(_(e_listidx), idx);
9641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 if (error)
9644 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 }
9646
9647 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009648 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 ++n;
9650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 else if (argvars[0].v_type == VAR_DICT)
9653 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 int todo;
9655 dict_T *d;
9656 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009658 if ((d = argvars[0].vval.v_dict) != NULL)
9659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 int error = FALSE;
9661
Bram Moolenaare9a41262005-01-15 22:18:47 +00009662 if (argvars[2].v_type != VAR_UNKNOWN)
9663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009665 if (argvars[3].v_type != VAR_UNKNOWN)
9666 EMSG(_(e_invarg));
9667 }
9668
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009669 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009671 {
9672 if (!HASHITEM_EMPTY(hi))
9673 {
9674 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009675 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009676 ++n;
9677 }
9678 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 }
9680 }
9681 else
9682 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009683 rettv->vval.v_number = n;
9684}
9685
9686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9688 *
9689 * Checks the existence of a cscope connection.
9690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009693 typval_T *argvars UNUSED;
9694 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696#ifdef FEAT_CSCOPE
9697 int num = 0;
9698 char_u *dbpath = NULL;
9699 char_u *prepend = NULL;
9700 char_u buf[NUMBUFLEN];
9701
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 if (argvars[0].v_type != VAR_UNKNOWN
9703 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 num = (int)get_tv_number(&argvars[0]);
9706 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 }
9710
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712#endif
9713}
9714
9715/*
9716 * "cursor(lnum, col)" function
9717 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009718 * Moves the cursor to the specified line and column.
9719 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009723 typval_T *argvars;
9724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009727#ifdef FEAT_VIRTUALEDIT
9728 long coladd = 0;
9729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009731 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009732 if (argvars[1].v_type == VAR_UNKNOWN)
9733 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009734 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009735
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009736 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009737 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009738 line = pos.lnum;
9739 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009740#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009741 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009742#endif
9743 }
9744 else
9745 {
9746 line = get_tv_lnum(argvars);
9747 col = get_tv_number_chk(&argvars[1], NULL);
9748#ifdef FEAT_VIRTUALEDIT
9749 if (argvars[2].v_type != VAR_UNKNOWN)
9750 coladd = get_tv_number_chk(&argvars[2], NULL);
9751#endif
9752 }
9753 if (line < 0 || col < 0
9754#ifdef FEAT_VIRTUALEDIT
9755 || coladd < 0
9756#endif
9757 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (line > 0)
9760 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (col > 0)
9762 curwin->w_cursor.col = col - 1;
9763#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009764 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#endif
9766
9767 /* Make sure the cursor is in a valid position. */
9768 check_cursor();
9769#ifdef FEAT_MBYTE
9770 /* Correct cursor for multi-byte character. */
9771 if (has_mbyte)
9772 mb_adjust_cursor();
9773#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009774
9775 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009780 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 */
9782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009787 int noref = 0;
9788
9789 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009791 if (noref < 0 || noref > 1)
9792 EMSG(_(e_invarg));
9793 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009794 {
9795 current_copyID += COPYID_INC;
9796 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * "delete()" function
9802 */
9803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 typval_T *argvars;
9806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
9815 * "did_filetype()" function
9816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009819 typval_T *argvars UNUSED;
9820 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824#endif
9825}
9826
9827/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828 * "diff_filler()" function
9829 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009832 typval_T *argvars UNUSED;
9833 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834{
9835#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009837#endif
9838}
9839
9840/*
9841 * "diff_hlID()" function
9842 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009845 typval_T *argvars UNUSED;
9846 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847{
9848#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009850 static linenr_T prev_lnum = 0;
9851 static int changedtick = 0;
9852 static int fnum = 0;
9853 static int change_start = 0;
9854 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009855 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856 int filler_lines;
9857 int col;
9858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 if (lnum < 0) /* ignore type error in {lnum} arg */
9860 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009861 if (lnum != prev_lnum
9862 || changedtick != curbuf->b_changedtick
9863 || fnum != curbuf->b_fnum)
9864 {
9865 /* New line, buffer, change: need to get the values. */
9866 filler_lines = diff_check(curwin, lnum);
9867 if (filler_lines < 0)
9868 {
9869 if (filler_lines == -1)
9870 {
9871 change_start = MAXCOL;
9872 change_end = -1;
9873 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9874 hlID = HLF_ADD; /* added line */
9875 else
9876 hlID = HLF_CHD; /* changed line */
9877 }
9878 else
9879 hlID = HLF_ADD; /* added line */
9880 }
9881 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009882 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883 prev_lnum = lnum;
9884 changedtick = curbuf->b_changedtick;
9885 fnum = curbuf->b_fnum;
9886 }
9887
9888 if (hlID == HLF_CHD || hlID == HLF_TXD)
9889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009891 if (col >= change_start && col <= change_end)
9892 hlID = HLF_TXD; /* changed text */
9893 else
9894 hlID = HLF_CHD; /* changed line */
9895 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009896 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009897#endif
9898}
9899
9900/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009901 * "empty({expr})" function
9902 */
9903 static void
9904f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 typval_T *argvars;
9906 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009907{
9908 int n;
9909
9910 switch (argvars[0].v_type)
9911 {
9912 case VAR_STRING:
9913 case VAR_FUNC:
9914 n = argvars[0].vval.v_string == NULL
9915 || *argvars[0].vval.v_string == NUL;
9916 break;
9917 case VAR_NUMBER:
9918 n = argvars[0].vval.v_number == 0;
9919 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009920#ifdef FEAT_FLOAT
9921 case VAR_FLOAT:
9922 n = argvars[0].vval.v_float == 0.0;
9923 break;
9924#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009925 case VAR_LIST:
9926 n = argvars[0].vval.v_list == NULL
9927 || argvars[0].vval.v_list->lv_first == NULL;
9928 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 case VAR_DICT:
9930 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009933 default:
9934 EMSG2(_(e_intern2), "f_empty()");
9935 n = 0;
9936 }
9937
9938 rettv->vval.v_number = n;
9939}
9940
9941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 * "escape({string}, {chars})" function
9943 */
9944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009946 typval_T *argvars;
9947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949 char_u buf[NUMBUFLEN];
9950
Bram Moolenaar758711c2005-02-02 23:11:38 +00009951 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9952 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954}
9955
9956/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009957 * "eval()" function
9958 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959 static void
9960f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *argvars;
9962 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009963{
9964 char_u *s;
9965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 s = get_tv_string_chk(&argvars[0]);
9967 if (s != NULL)
9968 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9971 {
9972 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009973 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009975 else if (*s != NUL)
9976 EMSG(_(e_trailing));
9977}
9978
9979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 * "eventhandler()" function
9981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988}
9989
9990/*
9991 * "executable()" function
9992 */
9993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 typval_T *argvars;
9996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
10002 * "exists()" function
10003 */
10004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010005f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010006 typval_T *argvars;
10007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008{
10009 char_u *p;
10010 char_u *name;
10011 int n = FALSE;
10012 int len = 0;
10013
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010014 no_autoload = TRUE;
10015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if (*p == '$') /* environment variable */
10018 {
10019 /* first try "normal" environment variables (fast) */
10020 if (mch_getenv(p + 1) != NULL)
10021 n = TRUE;
10022 else
10023 {
10024 /* try expanding things like $VIM and ${HOME} */
10025 p = expand_env_save(p);
10026 if (p != NULL && *p != '$')
10027 n = TRUE;
10028 vim_free(p);
10029 }
10030 }
10031 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010034 if (*skipwhite(p) != NUL)
10035 n = FALSE; /* trailing garbage */
10036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 else if (*p == '*') /* internal or user defined function */
10038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010039 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 }
10041 else if (*p == ':')
10042 {
10043 n = cmd_exists(p + 1);
10044 }
10045 else if (*p == '#')
10046 {
10047#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010048 if (p[1] == '#')
10049 n = autocmd_supported(p + 2);
10050 else
10051 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052#endif
10053 }
10054 else /* internal variable */
10055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010056 char_u *tofree;
10057 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010059 /* get_name_len() takes care of expanding curly braces */
10060 name = p;
10061 len = get_name_len(&p, &tofree, TRUE, FALSE);
10062 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010064 if (tofree != NULL)
10065 name = tofree;
10066 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10067 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010069 /* handle d.key, l[idx], f(expr) */
10070 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10071 if (n)
10072 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 }
10074 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010075 if (*p != NUL)
10076 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010078 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010082
10083 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010086#ifdef FEAT_FLOAT
10087/*
10088 * "exp()" function
10089 */
10090 static void
10091f_exp(argvars, rettv)
10092 typval_T *argvars;
10093 typval_T *rettv;
10094{
10095 float_T f;
10096
10097 rettv->v_type = VAR_FLOAT;
10098 if (get_float_arg(argvars, &f) == OK)
10099 rettv->vval.v_float = exp(f);
10100 else
10101 rettv->vval.v_float = 0.0;
10102}
10103#endif
10104
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105/*
10106 * "expand()" function
10107 */
10108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010110 typval_T *argvars;
10111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
10113 char_u *s;
10114 int len;
10115 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010116 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010119 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010122 if (argvars[1].v_type != VAR_UNKNOWN
10123 && argvars[2].v_type != VAR_UNKNOWN
10124 && get_tv_number_chk(&argvars[2], &error)
10125 && !error)
10126 {
10127 rettv->v_type = VAR_LIST;
10128 rettv->vval.v_list = NULL;
10129 }
10130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 if (*s == '%' || *s == '#' || *s == '<')
10133 {
10134 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010135 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010137 if (rettv->v_type == VAR_LIST)
10138 {
10139 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10140 list_append_string(rettv->vval.v_list, result, -1);
10141 else
10142 vim_free(result);
10143 }
10144 else
10145 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147 else
10148 {
10149 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010150 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 if (argvars[1].v_type != VAR_UNKNOWN
10152 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010153 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 if (!error)
10155 {
10156 ExpandInit(&xpc);
10157 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010158 if (p_wic)
10159 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010160 if (rettv->v_type == VAR_STRING)
10161 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10162 options, WILD_ALL);
10163 else if (rettv_list_alloc(rettv) != FAIL)
10164 {
10165 int i;
10166
10167 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10168 for (i = 0; i < xpc.xp_numfiles; i++)
10169 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10170 ExpandCleanup(&xpc);
10171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 }
10173 else
10174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 }
10176}
10177
10178/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010179 * Go over all entries in "d2" and add them to "d1".
10180 * When "action" is "error" then a duplicate key is an error.
10181 * When "action" is "force" then a duplicate key is overwritten.
10182 * Otherwise duplicate keys are ignored ("action" is "keep").
10183 */
10184 void
10185dict_extend(d1, d2, action)
10186 dict_T *d1;
10187 dict_T *d2;
10188 char_u *action;
10189{
10190 dictitem_T *di1;
10191 hashitem_T *hi2;
10192 int todo;
10193
10194 todo = (int)d2->dv_hashtab.ht_used;
10195 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10196 {
10197 if (!HASHITEM_EMPTY(hi2))
10198 {
10199 --todo;
10200 di1 = dict_find(d1, hi2->hi_key, -1);
10201 if (d1->dv_scope != 0)
10202 {
10203 /* Disallow replacing a builtin function in l: and g:.
10204 * Check the key to be valid when adding to any
10205 * scope. */
10206 if (d1->dv_scope == VAR_DEF_SCOPE
10207 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10208 && var_check_func_name(hi2->hi_key,
10209 di1 == NULL))
10210 break;
10211 if (!valid_varname(hi2->hi_key))
10212 break;
10213 }
10214 if (di1 == NULL)
10215 {
10216 di1 = dictitem_copy(HI2DI(hi2));
10217 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10218 dictitem_free(di1);
10219 }
10220 else if (*action == 'e')
10221 {
10222 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10223 break;
10224 }
10225 else if (*action == 'f' && HI2DI(hi2) != di1)
10226 {
10227 clear_tv(&di1->di_tv);
10228 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10229 }
10230 }
10231 }
10232}
10233
10234/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010235 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010237 */
10238 static void
10239f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010240 typval_T *argvars;
10241 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010242{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010243 char *arg_errmsg = N_("extend() argument");
10244
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010246 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 list_T *l1, *l2;
10248 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010249 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010251
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 l1 = argvars[0].vval.v_list;
10253 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010254 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010255 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010256 {
10257 if (argvars[2].v_type != VAR_UNKNOWN)
10258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 before = get_tv_number_chk(&argvars[2], &error);
10260 if (error)
10261 return; /* type error; errmsg already given */
10262
Bram Moolenaar758711c2005-02-02 23:11:38 +000010263 if (before == l1->lv_len)
10264 item = NULL;
10265 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010267 item = list_find(l1, before);
10268 if (item == NULL)
10269 {
10270 EMSGN(_(e_listidx), before);
10271 return;
10272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 }
10274 }
10275 else
10276 item = NULL;
10277 list_extend(l1, l2, item);
10278
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 copy_tv(&argvars[0], rettv);
10280 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10283 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010284 dict_T *d1, *d2;
10285 char_u *action;
10286 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010287
10288 d1 = argvars[0].vval.v_dict;
10289 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010290 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010291 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 {
10293 /* Check the third argument. */
10294 if (argvars[2].v_type != VAR_UNKNOWN)
10295 {
10296 static char *(av[]) = {"keep", "force", "error"};
10297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 action = get_tv_string_chk(&argvars[2]);
10299 if (action == NULL)
10300 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301 for (i = 0; i < 3; ++i)
10302 if (STRCMP(action, av[i]) == 0)
10303 break;
10304 if (i == 3)
10305 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010306 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 return;
10308 }
10309 }
10310 else
10311 action = (char_u *)"force";
10312
Bram Moolenaara9922d62013-05-30 13:01:18 +020010313 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 copy_tv(&argvars[0], rettv);
10316 }
10317 }
10318 else
10319 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320}
10321
10322/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010323 * "feedkeys()" function
10324 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010325 static void
10326f_feedkeys(argvars, rettv)
10327 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010328 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010329{
10330 int remap = TRUE;
10331 char_u *keys, *flags;
10332 char_u nbuf[NUMBUFLEN];
10333 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010334 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010335
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010336 /* This is not allowed in the sandbox. If the commands would still be
10337 * executed in the sandbox it would be OK, but it probably happens later,
10338 * when "sandbox" is no longer set. */
10339 if (check_secure())
10340 return;
10341
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010342 keys = get_tv_string(&argvars[0]);
10343 if (*keys != NUL)
10344 {
10345 if (argvars[1].v_type != VAR_UNKNOWN)
10346 {
10347 flags = get_tv_string_buf(&argvars[1], nbuf);
10348 for ( ; *flags != NUL; ++flags)
10349 {
10350 switch (*flags)
10351 {
10352 case 'n': remap = FALSE; break;
10353 case 'm': remap = TRUE; break;
10354 case 't': typed = TRUE; break;
10355 }
10356 }
10357 }
10358
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010359 /* Need to escape K_SPECIAL and CSI before putting the string in the
10360 * typeahead buffer. */
10361 keys_esc = vim_strsave_escape_csi(keys);
10362 if (keys_esc != NULL)
10363 {
10364 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010365 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010366 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010367 if (vgetc_busy)
10368 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010369 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370 }
10371}
10372
10373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 * "filereadable()" function
10375 */
10376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010377f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010378 typval_T *argvars;
10379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010381 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 char_u *p;
10383 int n;
10384
Bram Moolenaarc236c162008-07-13 17:41:49 +000010385#ifndef O_NONBLOCK
10386# define O_NONBLOCK 0
10387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010389 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10390 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 {
10392 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010393 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395 else
10396 n = FALSE;
10397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400
10401/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010402 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 * rights to write into.
10404 */
10405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010410 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010413static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010414
10415 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010416findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010417 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010419 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010420{
10421#ifdef FEAT_SEARCHPATH
10422 char_u *fname;
10423 char_u *fresult = NULL;
10424 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10425 char_u *p;
10426 char_u pathbuf[NUMBUFLEN];
10427 int count = 1;
10428 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010429 int error = FALSE;
10430#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010431
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010432 rettv->vval.v_string = NULL;
10433 rettv->v_type = VAR_STRING;
10434
10435#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010438 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10441 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010442 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 else
10444 {
10445 if (*p != NUL)
10446 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010449 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010451 }
10452
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010453 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10454 error = TRUE;
10455
10456 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 do
10459 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010460 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010461 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 fresult = find_file_in_path_option(first ? fname : NULL,
10463 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010464 0, first, path,
10465 find_what,
10466 curbuf->b_ffname,
10467 find_what == FINDFILE_DIR
10468 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010470
10471 if (fresult != NULL && rettv->v_type == VAR_LIST)
10472 list_append_string(rettv->vval.v_list, fresult, -1);
10473
10474 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010476
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010477 if (rettv->v_type == VAR_STRING)
10478 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010480}
10481
Bram Moolenaar33570922005-01-25 22:26:29 +000010482static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10483static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010484
10485/*
10486 * Implementation of map() and filter().
10487 */
10488 static void
10489filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010490 typval_T *argvars;
10491 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010492 int map;
10493{
10494 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 listitem_T *li, *nli;
10497 list_T *l = NULL;
10498 dictitem_T *di;
10499 hashtab_T *ht;
10500 hashitem_T *hi;
10501 dict_T *d = NULL;
10502 typval_T save_val;
10503 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010505 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010506 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10507 char *arg_errmsg = (map ? N_("map() argument")
10508 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010509 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010510 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010511
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010514 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010515 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516 return;
10517 }
10518 else if (argvars[0].v_type == VAR_DICT)
10519 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010520 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010521 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010522 return;
10523 }
10524 else
10525 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010526 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 return;
10528 }
10529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 expr = get_tv_string_buf_chk(&argvars[1], buf);
10531 /* On type errors, the preceding call has already displayed an error
10532 * message. Avoid a misleading error message for an empty string that
10533 * was not passed as argument. */
10534 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 prepare_vimvar(VV_VAL, &save_val);
10537 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010538
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010539 /* We reset "did_emsg" to be able to detect whether an error
10540 * occurred during evaluation of the expression. */
10541 save_did_emsg = did_emsg;
10542 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010543
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010544 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 vimvars[VV_KEY].vv_type = VAR_STRING;
10548
10549 ht = &d->dv_hashtab;
10550 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010551 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 if (!HASHITEM_EMPTY(hi))
10555 {
10556 --todo;
10557 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010558 if (tv_check_lock(di->di_tv.v_lock,
10559 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 break;
10561 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010562 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010563 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 break;
10565 if (!map && rem)
10566 dictitem_remove(d, di);
10567 clear_tv(&vimvars[VV_KEY].vv_tv);
10568 }
10569 }
10570 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 }
10572 else
10573 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010574 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 for (li = l->lv_first; li != NULL; li = nli)
10577 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010578 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010579 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010581 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010582 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010583 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010584 break;
10585 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010587 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010590
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010591 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010593
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010594 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596
10597 copy_tv(&argvars[0], rettv);
10598}
10599
10600 static int
10601filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 char_u *expr;
10604 int map;
10605 int *remp;
10606{
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010612 s = expr;
10613 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010614 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 if (*s != NUL) /* check for trailing chars after expr */
10616 {
10617 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010618 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 }
10620 if (map)
10621 {
10622 /* map(): replace the list item value */
10623 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010624 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010625 *tv = rettv;
10626 }
10627 else
10628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 int error = FALSE;
10630
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 /* On type error, nothing has been removed; return FAIL to stop the
10635 * loop. The error message was given by get_tv_number_chk(). */
10636 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010637 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010639 retval = OK;
10640theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010642 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643}
10644
10645/*
10646 * "filter()" function
10647 */
10648 static void
10649f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010650 typval_T *argvars;
10651 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652{
10653 filter_map(argvars, rettv, FALSE);
10654}
10655
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010657 * "finddir({fname}[, {path}[, {count}]])" function
10658 */
10659 static void
10660f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *argvars;
10662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010663{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010664 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010665}
10666
10667/*
10668 * "findfile({fname}[, {path}[, {count}]])" function
10669 */
10670 static void
10671f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T *argvars;
10673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010674{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010675 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676}
10677
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010678#ifdef FEAT_FLOAT
10679/*
10680 * "float2nr({float})" function
10681 */
10682 static void
10683f_float2nr(argvars, rettv)
10684 typval_T *argvars;
10685 typval_T *rettv;
10686{
10687 float_T f;
10688
10689 if (get_float_arg(argvars, &f) == OK)
10690 {
10691 if (f < -0x7fffffff)
10692 rettv->vval.v_number = -0x7fffffff;
10693 else if (f > 0x7fffffff)
10694 rettv->vval.v_number = 0x7fffffff;
10695 else
10696 rettv->vval.v_number = (varnumber_T)f;
10697 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010698}
10699
10700/*
10701 * "floor({float})" function
10702 */
10703 static void
10704f_floor(argvars, rettv)
10705 typval_T *argvars;
10706 typval_T *rettv;
10707{
10708 float_T f;
10709
10710 rettv->v_type = VAR_FLOAT;
10711 if (get_float_arg(argvars, &f) == OK)
10712 rettv->vval.v_float = floor(f);
10713 else
10714 rettv->vval.v_float = 0.0;
10715}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010716
10717/*
10718 * "fmod()" function
10719 */
10720 static void
10721f_fmod(argvars, rettv)
10722 typval_T *argvars;
10723 typval_T *rettv;
10724{
10725 float_T fx, fy;
10726
10727 rettv->v_type = VAR_FLOAT;
10728 if (get_float_arg(argvars, &fx) == OK
10729 && get_float_arg(&argvars[1], &fy) == OK)
10730 rettv->vval.v_float = fmod(fx, fy);
10731 else
10732 rettv->vval.v_float = 0.0;
10733}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010734#endif
10735
Bram Moolenaar0d660222005-01-07 21:51:51 +000010736/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010737 * "fnameescape({string})" function
10738 */
10739 static void
10740f_fnameescape(argvars, rettv)
10741 typval_T *argvars;
10742 typval_T *rettv;
10743{
10744 rettv->vval.v_string = vim_strsave_fnameescape(
10745 get_tv_string(&argvars[0]), FALSE);
10746 rettv->v_type = VAR_STRING;
10747}
10748
10749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 * "fnamemodify({fname}, {mods})" function
10751 */
10752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010754 typval_T *argvars;
10755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756{
10757 char_u *fname;
10758 char_u *mods;
10759 int usedlen = 0;
10760 int len;
10761 char_u *fbuf = NULL;
10762 char_u buf[NUMBUFLEN];
10763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 fname = get_tv_string_chk(&argvars[0]);
10765 mods = get_tv_string_buf_chk(&argvars[1], buf);
10766 if (fname == NULL || mods == NULL)
10767 fname = NULL;
10768 else
10769 {
10770 len = (int)STRLEN(fname);
10771 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 vim_free(fbuf);
10780}
10781
Bram Moolenaar33570922005-01-25 22:26:29 +000010782static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
10784/*
10785 * "foldclosed()" function
10786 */
10787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010789 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010791 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
10793#ifdef FEAT_FOLDING
10794 linenr_T lnum;
10795 linenr_T first, last;
10796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10799 {
10800 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10801 {
10802 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 return;
10807 }
10808 }
10809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811}
10812
10813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814 * "foldclosed()" function
10815 */
10816 static void
10817f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *argvars;
10819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820{
10821 foldclosed_both(argvars, rettv, FALSE);
10822}
10823
10824/*
10825 * "foldclosedend()" function
10826 */
10827 static void
10828f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 typval_T *argvars;
10830 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831{
10832 foldclosed_both(argvars, rettv, TRUE);
10833}
10834
10835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 * "foldlevel()" function
10837 */
10838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010840 typval_T *argvars UNUSED;
10841 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843#ifdef FEAT_FOLDING
10844 linenr_T lnum;
10845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850}
10851
10852/*
10853 * "foldtext()" function
10854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860#ifdef FEAT_FOLDING
10861 linenr_T lnum;
10862 char_u *s;
10863 char_u *r;
10864 int len;
10865 char *txt;
10866#endif
10867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 rettv->v_type = VAR_STRING;
10869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10872 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10873 <= curbuf->b_ml.ml_line_count
10874 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 {
10876 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10878 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 {
10880 if (!linewhite(lnum))
10881 break;
10882 ++lnum;
10883 }
10884
10885 /* Find interesting text in this line. */
10886 s = skipwhite(ml_get(lnum));
10887 /* skip C comment-start */
10888 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010891 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010893 {
10894 s = skipwhite(ml_get(lnum + 1));
10895 if (*s == '*')
10896 s = skipwhite(s + 1);
10897 }
10898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 txt = _("+-%s%3ld lines: ");
10900 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 + 20 /* for %3ld */
10903 + STRLEN(s))); /* concatenated */
10904 if (r != NULL)
10905 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010906 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10907 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10908 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 len = (int)STRLEN(r);
10910 STRCAT(r, s);
10911 /* remove 'foldmarker' and 'commentstring' */
10912 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 }
10915 }
10916#endif
10917}
10918
10919/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010920 * "foldtextresult(lnum)" function
10921 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010924 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010926{
10927#ifdef FEAT_FOLDING
10928 linenr_T lnum;
10929 char_u *text;
10930 char_u buf[51];
10931 foldinfo_T foldinfo;
10932 int fold_count;
10933#endif
10934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 rettv->v_type = VAR_STRING;
10936 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010937#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 /* treat illegal types and illegal string values for {lnum} the same */
10940 if (lnum < 0)
10941 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010942 fold_count = foldedCount(curwin, lnum, &foldinfo);
10943 if (fold_count > 0)
10944 {
10945 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10946 &foldinfo, buf);
10947 if (text == buf)
10948 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010950 }
10951#endif
10952}
10953
10954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 * "foreground()" function
10956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010959 typval_T *argvars UNUSED;
10960 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962#ifdef FEAT_GUI
10963 if (gui.in_use)
10964 gui_mch_set_foreground();
10965#else
10966# ifdef WIN32
10967 win32_set_foreground();
10968# endif
10969#endif
10970}
10971
10972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010973 * "function()" function
10974 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010977 typval_T *argvars;
10978 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010979{
10980 char_u *s;
10981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010983 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010984 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010985 /* Don't check an autoload name for existence here. */
10986 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010987 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010988 else
10989 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010990 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010991 {
10992 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010993 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010994
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010995 /* Expand s: and <SID> into <SNR>nr_, so that the function can
10996 * also be called from another script. Using trans_function_name()
10997 * would also work, but some plugins depend on the name being
10998 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010999 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011000 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011001 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011002 if (rettv->vval.v_string != NULL)
11003 {
11004 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011005 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011006 }
11007 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011008 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011009 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011011 }
11012}
11013
11014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011015 * "garbagecollect()" function
11016 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011017 static void
11018f_garbagecollect(argvars, rettv)
11019 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011020 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011021{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011022 /* This is postponed until we are back at the toplevel, because we may be
11023 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11024 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011025
11026 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11027 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011028}
11029
11030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011031 * "get()" function
11032 */
11033 static void
11034f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011037{
Bram Moolenaar33570922005-01-25 22:26:29 +000011038 listitem_T *li;
11039 list_T *l;
11040 dictitem_T *di;
11041 dict_T *d;
11042 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011043
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011045 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 int error = FALSE;
11049
11050 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11051 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011052 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011054 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011055 else if (argvars[0].v_type == VAR_DICT)
11056 {
11057 if ((d = argvars[0].vval.v_dict) != NULL)
11058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011059 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011060 if (di != NULL)
11061 tv = &di->di_tv;
11062 }
11063 }
11064 else
11065 EMSG2(_(e_listdictarg), "get()");
11066
11067 if (tv == NULL)
11068 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 copy_tv(&argvars[2], rettv);
11071 }
11072 else
11073 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011074}
11075
Bram Moolenaar342337a2005-07-21 21:11:17 +000011076static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011077
11078/*
11079 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011080 * Return a range (from start to end) of lines in rettv from the specified
11081 * buffer.
11082 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011083 */
11084 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011085get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011086 buf_T *buf;
11087 linenr_T start;
11088 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011089 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011090 typval_T *rettv;
11091{
11092 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011093
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011094 if (retlist && rettv_list_alloc(rettv) == FAIL)
11095 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011096
11097 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11098 return;
11099
11100 if (!retlist)
11101 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011102 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11103 p = ml_get_buf(buf, start, FALSE);
11104 else
11105 p = (char_u *)"";
11106
11107 rettv->v_type = VAR_STRING;
11108 rettv->vval.v_string = vim_strsave(p);
11109 }
11110 else
11111 {
11112 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011113 return;
11114
11115 if (start < 1)
11116 start = 1;
11117 if (end > buf->b_ml.ml_line_count)
11118 end = buf->b_ml.ml_line_count;
11119 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011120 if (list_append_string(rettv->vval.v_list,
11121 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011122 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011123 }
11124}
11125
11126/*
11127 * "getbufline()" function
11128 */
11129 static void
11130f_getbufline(argvars, rettv)
11131 typval_T *argvars;
11132 typval_T *rettv;
11133{
11134 linenr_T lnum;
11135 linenr_T end;
11136 buf_T *buf;
11137
11138 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11139 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011140 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011141 --emsg_off;
11142
Bram Moolenaar661b1822005-07-28 22:36:45 +000011143 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 if (argvars[2].v_type == VAR_UNKNOWN)
11145 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011146 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011147 end = get_tv_lnum_buf(&argvars[2], buf);
11148
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011150}
11151
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152/*
11153 * "getbufvar()" function
11154 */
11155 static void
11156f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159{
11160 buf_T *buf;
11161 buf_T *save_curbuf;
11162 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011164 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11167 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011169 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011170
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011171 rettv->v_type = VAR_STRING;
11172 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173
11174 if (buf != NULL && varname != NULL)
11175 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011176 /* set curbuf to be our buf, temporarily */
11177 save_curbuf = curbuf;
11178 curbuf = buf;
11179
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011181 {
11182 if (get_option_tv(&varname, rettv, TRUE) == OK)
11183 done = TRUE;
11184 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011185 else if (STRCMP(varname, "changedtick") == 0)
11186 {
11187 rettv->v_type = VAR_NUMBER;
11188 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011189 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191 else
11192 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011193 /* Look up the variable. */
11194 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11195 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11196 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011197 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011198 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011199 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011200 done = TRUE;
11201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011202 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011203
11204 /* restore previous notion of curbuf */
11205 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206 }
11207
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011208 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11209 /* use the default value */
11210 copy_tv(&argvars[2], rettv);
11211
Bram Moolenaar0d660222005-01-07 21:51:51 +000011212 --emsg_off;
11213}
11214
11215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 * "getchar()" function
11217 */
11218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
11223 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011226 /* Position the cursor. Needed after a message that ends in a space. */
11227 windgoto(msg_row, msg_col);
11228
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 ++no_mapping;
11230 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011231 for (;;)
11232 {
11233 if (argvars[0].v_type == VAR_UNKNOWN)
11234 /* getchar(): blocking wait. */
11235 n = safe_vgetc();
11236 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11237 /* getchar(1): only check if char avail */
11238 n = vpeekc();
11239 else if (error || vpeekc() == NUL)
11240 /* illegal argument or getchar(0) and no char avail: return zero */
11241 n = 0;
11242 else
11243 /* getchar(0) and char avail: return char */
11244 n = safe_vgetc();
11245 if (n == K_IGNORE)
11246 continue;
11247 break;
11248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 --no_mapping;
11250 --allow_keys;
11251
Bram Moolenaar219b8702006-11-01 14:32:36 +000011252 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11253 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11254 vimvars[VV_MOUSE_COL].vv_nr = 0;
11255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 if (IS_SPECIAL(n) || mod_mask != 0)
11258 {
11259 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11260 int i = 0;
11261
11262 /* Turn a special key into three bytes, plus modifier. */
11263 if (mod_mask != 0)
11264 {
11265 temp[i++] = K_SPECIAL;
11266 temp[i++] = KS_MODIFIER;
11267 temp[i++] = mod_mask;
11268 }
11269 if (IS_SPECIAL(n))
11270 {
11271 temp[i++] = K_SPECIAL;
11272 temp[i++] = K_SECOND(n);
11273 temp[i++] = K_THIRD(n);
11274 }
11275#ifdef FEAT_MBYTE
11276 else if (has_mbyte)
11277 i += (*mb_char2bytes)(n, temp + i);
11278#endif
11279 else
11280 temp[i++] = n;
11281 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->v_type = VAR_STRING;
11283 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011284
11285#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011286 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011287 {
11288 int row = mouse_row;
11289 int col = mouse_col;
11290 win_T *win;
11291 linenr_T lnum;
11292# ifdef FEAT_WINDOWS
11293 win_T *wp;
11294# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011295 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011296
11297 if (row >= 0 && col >= 0)
11298 {
11299 /* Find the window at the mouse coordinates and compute the
11300 * text position. */
11301 win = mouse_find_win(&row, &col);
11302 (void)mouse_comp_pos(win, &row, &col, &lnum);
11303# ifdef FEAT_WINDOWS
11304 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011305 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011306# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011307 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011308 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11309 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11310 }
11311 }
11312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 }
11314}
11315
11316/*
11317 * "getcharmod()" function
11318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325}
11326
11327/*
11328 * "getcmdline()" function
11329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->v_type = VAR_STRING;
11336 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337}
11338
11339/*
11340 * "getcmdpos()" function
11341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348}
11349
11350/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011351 * "getcmdtype()" function
11352 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011353 static void
11354f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011355 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011356 typval_T *rettv;
11357{
11358 rettv->v_type = VAR_STRING;
11359 rettv->vval.v_string = alloc(2);
11360 if (rettv->vval.v_string != NULL)
11361 {
11362 rettv->vval.v_string[0] = get_cmdline_type();
11363 rettv->vval.v_string[1] = NUL;
11364 }
11365}
11366
11367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 * "getcwd()" function
11369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011375 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011378 rettv->vval.v_string = NULL;
11379 cwd = alloc(MAXPATHL);
11380 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011382 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11383 {
11384 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011386 if (rettv->vval.v_string != NULL)
11387 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011389 }
11390 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 }
11392}
11393
11394/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011395 * "getfontname()" function
11396 */
11397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 rettv->v_type = VAR_STRING;
11403 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011404#ifdef FEAT_GUI
11405 if (gui.in_use)
11406 {
11407 GuiFont font;
11408 char_u *name = NULL;
11409
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011410 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011411 {
11412 /* Get the "Normal" font. Either the name saved by
11413 * hl_set_font_name() or from the font ID. */
11414 font = gui.norm_font;
11415 name = hl_get_font_name();
11416 }
11417 else
11418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011420 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11421 return;
11422 font = gui_mch_get_font(name, FALSE);
11423 if (font == NOFONT)
11424 return; /* Invalid font name, return empty string. */
11425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011427 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011428 gui_mch_free_font(font);
11429 }
11430#endif
11431}
11432
11433/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011434 * "getfperm({fname})" function
11435 */
11436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *argvars;
11439 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011440{
11441 char_u *fname;
11442 struct stat st;
11443 char_u *perm = NULL;
11444 char_u flags[] = "rwx";
11445 int i;
11446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011450 if (mch_stat((char *)fname, &st) >= 0)
11451 {
11452 perm = vim_strsave((char_u *)"---------");
11453 if (perm != NULL)
11454 {
11455 for (i = 0; i < 9; i++)
11456 {
11457 if (st.st_mode & (1 << (8 - i)))
11458 perm[i] = flags[i % 3];
11459 }
11460 }
11461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011463}
11464
11465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * "getfsize({fname})" function
11467 */
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
11473 char_u *fname;
11474 struct stat st;
11475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480 if (mch_stat((char *)fname, &st) >= 0)
11481 {
11482 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011487
11488 /* non-perfect check for overflow */
11489 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11490 rettv->vval.v_number = -2;
11491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 }
11493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495}
11496
11497/*
11498 * "getftime({fname})" function
11499 */
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504{
11505 char_u *fname;
11506 struct stat st;
11507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509
11510 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514}
11515
11516/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011517 * "getftype({fname})" function
11518 */
11519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011521 typval_T *argvars;
11522 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011523{
11524 char_u *fname;
11525 struct stat st;
11526 char_u *type = NULL;
11527 char *t;
11528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011532 if (mch_lstat((char *)fname, &st) >= 0)
11533 {
11534#ifdef S_ISREG
11535 if (S_ISREG(st.st_mode))
11536 t = "file";
11537 else if (S_ISDIR(st.st_mode))
11538 t = "dir";
11539# ifdef S_ISLNK
11540 else if (S_ISLNK(st.st_mode))
11541 t = "link";
11542# endif
11543# ifdef S_ISBLK
11544 else if (S_ISBLK(st.st_mode))
11545 t = "bdev";
11546# endif
11547# ifdef S_ISCHR
11548 else if (S_ISCHR(st.st_mode))
11549 t = "cdev";
11550# endif
11551# ifdef S_ISFIFO
11552 else if (S_ISFIFO(st.st_mode))
11553 t = "fifo";
11554# endif
11555# ifdef S_ISSOCK
11556 else if (S_ISSOCK(st.st_mode))
11557 t = "fifo";
11558# endif
11559 else
11560 t = "other";
11561#else
11562# ifdef S_IFMT
11563 switch (st.st_mode & S_IFMT)
11564 {
11565 case S_IFREG: t = "file"; break;
11566 case S_IFDIR: t = "dir"; break;
11567# ifdef S_IFLNK
11568 case S_IFLNK: t = "link"; break;
11569# endif
11570# ifdef S_IFBLK
11571 case S_IFBLK: t = "bdev"; break;
11572# endif
11573# ifdef S_IFCHR
11574 case S_IFCHR: t = "cdev"; break;
11575# endif
11576# ifdef S_IFIFO
11577 case S_IFIFO: t = "fifo"; break;
11578# endif
11579# ifdef S_IFSOCK
11580 case S_IFSOCK: t = "socket"; break;
11581# endif
11582 default: t = "other";
11583 }
11584# else
11585 if (mch_isdir(fname))
11586 t = "dir";
11587 else
11588 t = "file";
11589# endif
11590#endif
11591 type = vim_strsave((char_u *)t);
11592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011594}
11595
11596/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598 */
11599 static void
11600f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 typval_T *argvars;
11602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011603{
11604 linenr_T lnum;
11605 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011606 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607
11608 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011609 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011610 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011611 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011612 retlist = FALSE;
11613 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011614 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011615 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011616 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011617 retlist = TRUE;
11618 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011619
Bram Moolenaar342337a2005-07-21 21:11:17 +000011620 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011621}
11622
11623/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011624 * "getmatches()" function
11625 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011626 static void
11627f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011628 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011629 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011630{
11631#ifdef FEAT_SEARCH_EXTRA
11632 dict_T *dict;
11633 matchitem_T *cur = curwin->w_match_head;
11634
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011635 if (rettv_list_alloc(rettv) == OK)
11636 {
11637 while (cur != NULL)
11638 {
11639 dict = dict_alloc();
11640 if (dict == NULL)
11641 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011642 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11643 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11644 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11645 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11646 list_append_dict(rettv->vval.v_list, dict);
11647 cur = cur->next;
11648 }
11649 }
11650#endif
11651}
11652
11653/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011654 * "getpid()" function
11655 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011656 static void
11657f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011658 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011659 typval_T *rettv;
11660{
11661 rettv->vval.v_number = mch_get_pid();
11662}
11663
11664/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011665 * "getpos(string)" function
11666 */
11667 static void
11668f_getpos(argvars, rettv)
11669 typval_T *argvars;
11670 typval_T *rettv;
11671{
11672 pos_T *fp;
11673 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011674 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011675
11676 if (rettv_list_alloc(rettv) == OK)
11677 {
11678 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011679 fp = var2fpos(&argvars[0], TRUE, &fnum);
11680 if (fnum != -1)
11681 list_append_number(l, (varnumber_T)fnum);
11682 else
11683 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011684 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11685 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011686 list_append_number(l, (fp != NULL)
11687 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011688 : (varnumber_T)0);
11689 list_append_number(l,
11690#ifdef FEAT_VIRTUALEDIT
11691 (fp != NULL) ? (varnumber_T)fp->coladd :
11692#endif
11693 (varnumber_T)0);
11694 }
11695 else
11696 rettv->vval.v_number = FALSE;
11697}
11698
11699/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011700 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011701 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011702 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011703f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011704 typval_T *argvars UNUSED;
11705 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011706{
11707#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011708 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011709#endif
11710
Bram Moolenaar2641f772005-03-25 21:58:17 +000011711#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011712 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011713 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011714 wp = NULL;
11715 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11716 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011717 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011718 if (wp == NULL)
11719 return;
11720 }
11721
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011722 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011723 }
11724#endif
11725}
11726
11727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 * "getreg()" function
11729 */
11730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *argvars;
11733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734{
11735 char_u *strregname;
11736 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011737 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011740 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 strregname = get_tv_string_chk(&argvars[0]);
11743 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011744 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 regname = (strregname == NULL ? '"' : *strregname);
11750 if (regname == 0)
11751 regname = '"';
11752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 rettv->vval.v_string = error ? NULL :
11755 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756}
11757
11758/*
11759 * "getregtype()" function
11760 */
11761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011763 typval_T *argvars;
11764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765{
11766 char_u *strregname;
11767 int regname;
11768 char_u buf[NUMBUFLEN + 2];
11769 long reglen = 0;
11770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011771 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011772 {
11773 strregname = get_tv_string_chk(&argvars[0]);
11774 if (strregname == NULL) /* type error; errmsg already given */
11775 {
11776 rettv->v_type = VAR_STRING;
11777 rettv->vval.v_string = NULL;
11778 return;
11779 }
11780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 else
11782 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011783 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784
11785 regname = (strregname == NULL ? '"' : *strregname);
11786 if (regname == 0)
11787 regname = '"';
11788
11789 buf[0] = NUL;
11790 buf[1] = NUL;
11791 switch (get_reg_type(regname, &reglen))
11792 {
11793 case MLINE: buf[0] = 'V'; break;
11794 case MCHAR: buf[0] = 'v'; break;
11795#ifdef FEAT_VISUAL
11796 case MBLOCK:
11797 buf[0] = Ctrl_V;
11798 sprintf((char *)buf + 1, "%ld", reglen + 1);
11799 break;
11800#endif
11801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->v_type = VAR_STRING;
11803 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011807 * "gettabvar()" function
11808 */
11809 static void
11810f_gettabvar(argvars, rettv)
11811 typval_T *argvars;
11812 typval_T *rettv;
11813{
11814 tabpage_T *tp;
11815 dictitem_T *v;
11816 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011817 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011818
11819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = NULL;
11821
11822 varname = get_tv_string_chk(&argvars[1]);
11823 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11824 if (tp != NULL && varname != NULL)
11825 {
11826 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011827 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011828 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011829 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011830 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011831 done = TRUE;
11832 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011833 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011834
11835 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011836 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011837}
11838
11839/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011840 * "gettabwinvar()" function
11841 */
11842 static void
11843f_gettabwinvar(argvars, rettv)
11844 typval_T *argvars;
11845 typval_T *rettv;
11846{
11847 getwinvar(argvars, rettv, 1);
11848}
11849
11850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 * "getwinposx()" function
11852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#ifdef FEAT_GUI
11860 if (gui.in_use)
11861 {
11862 int x, y;
11863
11864 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 }
11867#endif
11868}
11869
11870/*
11871 * "getwinposy()" function
11872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011875 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879#ifdef FEAT_GUI
11880 if (gui.in_use)
11881 {
11882 int x, y;
11883
11884 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 }
11887#endif
11888}
11889
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011890/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011891 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011892 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011893 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011894find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011895 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011896 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011897{
11898#ifdef FEAT_WINDOWS
11899 win_T *wp;
11900#endif
11901 int nr;
11902
11903 nr = get_tv_number_chk(vp, NULL);
11904
11905#ifdef FEAT_WINDOWS
11906 if (nr < 0)
11907 return NULL;
11908 if (nr == 0)
11909 return curwin;
11910
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011911 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11912 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011913 if (--nr <= 0)
11914 break;
11915 return wp;
11916#else
11917 if (nr == 0 || nr == 1)
11918 return curwin;
11919 return NULL;
11920#endif
11921}
11922
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923/*
11924 * "getwinvar()" function
11925 */
11926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 typval_T *argvars;
11929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011931 getwinvar(argvars, rettv, 0);
11932}
11933
11934/*
11935 * getwinvar() and gettabwinvar()
11936 */
11937 static void
11938getwinvar(argvars, rettv, off)
11939 typval_T *argvars;
11940 typval_T *rettv;
11941 int off; /* 1 for gettabwinvar() */
11942{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 win_T *win, *oldcurwin;
11944 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011946 tabpage_T *tp = NULL;
11947 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011948 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011950#ifdef FEAT_WINDOWS
11951 if (off == 1)
11952 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11953 else
11954 tp = curtab;
11955#endif
11956 win = find_win_by_nr(&argvars[off], tp);
11957 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011960 rettv->v_type = VAR_STRING;
11961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962
11963 if (win != NULL && varname != NULL)
11964 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011965 /* Set curwin to be our win, temporarily. Also set the tabpage,
11966 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011967 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011968
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011970 {
11971 if (get_option_tv(&varname, rettv, 1) == OK)
11972 done = TRUE;
11973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 else
11975 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011976 /* Look up the variable. */
11977 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11978 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011982 done = TRUE;
11983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011985
11986 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020011987 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 }
11989
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011990 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11991 /* use the default return value */
11992 copy_tv(&argvars[off + 2], rettv);
11993
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 --emsg_off;
11995}
11996
11997/*
11998 * "glob()" function
11999 */
12000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012005 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012007 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012010 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012012 if (argvars[1].v_type != VAR_UNKNOWN)
12013 {
12014 if (get_tv_number_chk(&argvars[1], &error))
12015 options |= WILD_KEEP_ALL;
12016 if (argvars[2].v_type != VAR_UNKNOWN
12017 && get_tv_number_chk(&argvars[2], &error))
12018 {
12019 rettv->v_type = VAR_LIST;
12020 rettv->vval.v_list = NULL;
12021 }
12022 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012023 if (!error)
12024 {
12025 ExpandInit(&xpc);
12026 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012027 if (p_wic)
12028 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012029 if (rettv->v_type == VAR_STRING)
12030 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012031 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012032 else if (rettv_list_alloc(rettv) != FAIL)
12033 {
12034 int i;
12035
12036 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12037 NULL, options, WILD_ALL_KEEP);
12038 for (i = 0; i < xpc.xp_numfiles; i++)
12039 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12040
12041 ExpandCleanup(&xpc);
12042 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012043 }
12044 else
12045 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046}
12047
12048/*
12049 * "globpath()" function
12050 */
12051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012056 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012059 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012061 /* When the optional second argument is non-zero, don't remove matches
12062 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12063 if (argvars[2].v_type != VAR_UNKNOWN
12064 && get_tv_number_chk(&argvars[2], &error))
12065 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012067 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 rettv->vval.v_string = NULL;
12069 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012070 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12071 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072}
12073
12074/*
12075 * "has()" function
12076 */
12077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012079 typval_T *argvars;
12080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
12082 int i;
12083 char_u *name;
12084 int n = FALSE;
12085 static char *(has_list[]) =
12086 {
12087#ifdef AMIGA
12088 "amiga",
12089# ifdef FEAT_ARP
12090 "arp",
12091# endif
12092#endif
12093#ifdef __BEOS__
12094 "beos",
12095#endif
12096#ifdef MSDOS
12097# ifdef DJGPP
12098 "dos32",
12099# else
12100 "dos16",
12101# endif
12102#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012103#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 "mac",
12105#endif
12106#if defined(MACOS_X_UNIX)
12107 "macunix",
12108#endif
12109#ifdef OS2
12110 "os2",
12111#endif
12112#ifdef __QNX__
12113 "qnx",
12114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef UNIX
12116 "unix",
12117#endif
12118#ifdef VMS
12119 "vms",
12120#endif
12121#ifdef WIN16
12122 "win16",
12123#endif
12124#ifdef WIN32
12125 "win32",
12126#endif
12127#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12128 "win32unix",
12129#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012130#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 "win64",
12132#endif
12133#ifdef EBCDIC
12134 "ebcdic",
12135#endif
12136#ifndef CASE_INSENSITIVE_FILENAME
12137 "fname_case",
12138#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012139#ifdef HAVE_ACL
12140 "acl",
12141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142#ifdef FEAT_ARABIC
12143 "arabic",
12144#endif
12145#ifdef FEAT_AUTOCMD
12146 "autocmd",
12147#endif
12148#ifdef FEAT_BEVAL
12149 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012150# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12151 "balloon_multiline",
12152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153#endif
12154#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12155 "builtin_terms",
12156# ifdef ALL_BUILTIN_TCAPS
12157 "all_builtin_terms",
12158# endif
12159#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012160#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12161 || defined(FEAT_GUI_W32) \
12162 || defined(FEAT_GUI_MOTIF))
12163 "browsefilter",
12164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165#ifdef FEAT_BYTEOFF
12166 "byte_offset",
12167#endif
12168#ifdef FEAT_CINDENT
12169 "cindent",
12170#endif
12171#ifdef FEAT_CLIENTSERVER
12172 "clientserver",
12173#endif
12174#ifdef FEAT_CLIPBOARD
12175 "clipboard",
12176#endif
12177#ifdef FEAT_CMDL_COMPL
12178 "cmdline_compl",
12179#endif
12180#ifdef FEAT_CMDHIST
12181 "cmdline_hist",
12182#endif
12183#ifdef FEAT_COMMENTS
12184 "comments",
12185#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012186#ifdef FEAT_CONCEAL
12187 "conceal",
12188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189#ifdef FEAT_CRYPT
12190 "cryptv",
12191#endif
12192#ifdef FEAT_CSCOPE
12193 "cscope",
12194#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012195#ifdef FEAT_CURSORBIND
12196 "cursorbind",
12197#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012198#ifdef CURSOR_SHAPE
12199 "cursorshape",
12200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201#ifdef DEBUG
12202 "debug",
12203#endif
12204#ifdef FEAT_CON_DIALOG
12205 "dialog_con",
12206#endif
12207#ifdef FEAT_GUI_DIALOG
12208 "dialog_gui",
12209#endif
12210#ifdef FEAT_DIFF
12211 "diff",
12212#endif
12213#ifdef FEAT_DIGRAPHS
12214 "digraphs",
12215#endif
12216#ifdef FEAT_DND
12217 "dnd",
12218#endif
12219#ifdef FEAT_EMACS_TAGS
12220 "emacs_tags",
12221#endif
12222 "eval", /* always present, of course! */
12223#ifdef FEAT_EX_EXTRA
12224 "ex_extra",
12225#endif
12226#ifdef FEAT_SEARCH_EXTRA
12227 "extra_search",
12228#endif
12229#ifdef FEAT_FKMAP
12230 "farsi",
12231#endif
12232#ifdef FEAT_SEARCHPATH
12233 "file_in_path",
12234#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012235#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012236 "filterpipe",
12237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238#ifdef FEAT_FIND_ID
12239 "find_in_path",
12240#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012241#ifdef FEAT_FLOAT
12242 "float",
12243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244#ifdef FEAT_FOLDING
12245 "folding",
12246#endif
12247#ifdef FEAT_FOOTER
12248 "footer",
12249#endif
12250#if !defined(USE_SYSTEM) && defined(UNIX)
12251 "fork",
12252#endif
12253#ifdef FEAT_GETTEXT
12254 "gettext",
12255#endif
12256#ifdef FEAT_GUI
12257 "gui",
12258#endif
12259#ifdef FEAT_GUI_ATHENA
12260# ifdef FEAT_GUI_NEXTAW
12261 "gui_neXtaw",
12262# else
12263 "gui_athena",
12264# endif
12265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266#ifdef FEAT_GUI_GTK
12267 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012270#ifdef FEAT_GUI_GNOME
12271 "gui_gnome",
12272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273#ifdef FEAT_GUI_MAC
12274 "gui_mac",
12275#endif
12276#ifdef FEAT_GUI_MOTIF
12277 "gui_motif",
12278#endif
12279#ifdef FEAT_GUI_PHOTON
12280 "gui_photon",
12281#endif
12282#ifdef FEAT_GUI_W16
12283 "gui_win16",
12284#endif
12285#ifdef FEAT_GUI_W32
12286 "gui_win32",
12287#endif
12288#ifdef FEAT_HANGULIN
12289 "hangul_input",
12290#endif
12291#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12292 "iconv",
12293#endif
12294#ifdef FEAT_INS_EXPAND
12295 "insert_expand",
12296#endif
12297#ifdef FEAT_JUMPLIST
12298 "jumplist",
12299#endif
12300#ifdef FEAT_KEYMAP
12301 "keymap",
12302#endif
12303#ifdef FEAT_LANGMAP
12304 "langmap",
12305#endif
12306#ifdef FEAT_LIBCALL
12307 "libcall",
12308#endif
12309#ifdef FEAT_LINEBREAK
12310 "linebreak",
12311#endif
12312#ifdef FEAT_LISP
12313 "lispindent",
12314#endif
12315#ifdef FEAT_LISTCMDS
12316 "listcmds",
12317#endif
12318#ifdef FEAT_LOCALMAP
12319 "localmap",
12320#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012321#ifdef FEAT_LUA
12322# ifndef DYNAMIC_LUA
12323 "lua",
12324# endif
12325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326#ifdef FEAT_MENU
12327 "menu",
12328#endif
12329#ifdef FEAT_SESSION
12330 "mksession",
12331#endif
12332#ifdef FEAT_MODIFY_FNAME
12333 "modify_fname",
12334#endif
12335#ifdef FEAT_MOUSE
12336 "mouse",
12337#endif
12338#ifdef FEAT_MOUSESHAPE
12339 "mouseshape",
12340#endif
12341#if defined(UNIX) || defined(VMS)
12342# ifdef FEAT_MOUSE_DEC
12343 "mouse_dec",
12344# endif
12345# ifdef FEAT_MOUSE_GPM
12346 "mouse_gpm",
12347# endif
12348# ifdef FEAT_MOUSE_JSB
12349 "mouse_jsbterm",
12350# endif
12351# ifdef FEAT_MOUSE_NET
12352 "mouse_netterm",
12353# endif
12354# ifdef FEAT_MOUSE_PTERM
12355 "mouse_pterm",
12356# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012357# ifdef FEAT_MOUSE_SGR
12358 "mouse_sgr",
12359# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012360# ifdef FEAT_SYSMOUSE
12361 "mouse_sysmouse",
12362# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012363# ifdef FEAT_MOUSE_URXVT
12364 "mouse_urxvt",
12365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366# ifdef FEAT_MOUSE_XTERM
12367 "mouse_xterm",
12368# endif
12369#endif
12370#ifdef FEAT_MBYTE
12371 "multi_byte",
12372#endif
12373#ifdef FEAT_MBYTE_IME
12374 "multi_byte_ime",
12375#endif
12376#ifdef FEAT_MULTI_LANG
12377 "multi_lang",
12378#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012379#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012380#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012381 "mzscheme",
12382#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384#ifdef FEAT_OLE
12385 "ole",
12386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387#ifdef FEAT_PATH_EXTRA
12388 "path_extra",
12389#endif
12390#ifdef FEAT_PERL
12391#ifndef DYNAMIC_PERL
12392 "perl",
12393#endif
12394#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012395#ifdef FEAT_PERSISTENT_UNDO
12396 "persistent_undo",
12397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#ifdef FEAT_PYTHON
12399#ifndef DYNAMIC_PYTHON
12400 "python",
12401#endif
12402#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012403#ifdef FEAT_PYTHON3
12404#ifndef DYNAMIC_PYTHON3
12405 "python3",
12406#endif
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_POSTSCRIPT
12409 "postscript",
12410#endif
12411#ifdef FEAT_PRINTER
12412 "printer",
12413#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012414#ifdef FEAT_PROFILE
12415 "profile",
12416#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012417#ifdef FEAT_RELTIME
12418 "reltime",
12419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420#ifdef FEAT_QUICKFIX
12421 "quickfix",
12422#endif
12423#ifdef FEAT_RIGHTLEFT
12424 "rightleft",
12425#endif
12426#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12427 "ruby",
12428#endif
12429#ifdef FEAT_SCROLLBIND
12430 "scrollbind",
12431#endif
12432#ifdef FEAT_CMDL_INFO
12433 "showcmd",
12434 "cmdline_info",
12435#endif
12436#ifdef FEAT_SIGNS
12437 "signs",
12438#endif
12439#ifdef FEAT_SMARTINDENT
12440 "smartindent",
12441#endif
12442#ifdef FEAT_SNIFF
12443 "sniff",
12444#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012445#ifdef STARTUPTIME
12446 "startuptime",
12447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448#ifdef FEAT_STL_OPT
12449 "statusline",
12450#endif
12451#ifdef FEAT_SUN_WORKSHOP
12452 "sun_workshop",
12453#endif
12454#ifdef FEAT_NETBEANS_INTG
12455 "netbeans_intg",
12456#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012457#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012458 "spell",
12459#endif
12460#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 "syntax",
12462#endif
12463#if defined(USE_SYSTEM) || !defined(UNIX)
12464 "system",
12465#endif
12466#ifdef FEAT_TAG_BINS
12467 "tag_binary",
12468#endif
12469#ifdef FEAT_TAG_OLDSTATIC
12470 "tag_old_static",
12471#endif
12472#ifdef FEAT_TAG_ANYWHITE
12473 "tag_any_white",
12474#endif
12475#ifdef FEAT_TCL
12476# ifndef DYNAMIC_TCL
12477 "tcl",
12478# endif
12479#endif
12480#ifdef TERMINFO
12481 "terminfo",
12482#endif
12483#ifdef FEAT_TERMRESPONSE
12484 "termresponse",
12485#endif
12486#ifdef FEAT_TEXTOBJ
12487 "textobjects",
12488#endif
12489#ifdef HAVE_TGETENT
12490 "tgetent",
12491#endif
12492#ifdef FEAT_TITLE
12493 "title",
12494#endif
12495#ifdef FEAT_TOOLBAR
12496 "toolbar",
12497#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012498#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12499 "unnamedplus",
12500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#ifdef FEAT_USR_CMDS
12502 "user-commands", /* was accidentally included in 5.4 */
12503 "user_commands",
12504#endif
12505#ifdef FEAT_VIMINFO
12506 "viminfo",
12507#endif
12508#ifdef FEAT_VERTSPLIT
12509 "vertsplit",
12510#endif
12511#ifdef FEAT_VIRTUALEDIT
12512 "virtualedit",
12513#endif
12514#ifdef FEAT_VISUAL
12515 "visual",
12516#endif
12517#ifdef FEAT_VISUALEXTRA
12518 "visualextra",
12519#endif
12520#ifdef FEAT_VREPLACE
12521 "vreplace",
12522#endif
12523#ifdef FEAT_WILDIGN
12524 "wildignore",
12525#endif
12526#ifdef FEAT_WILDMENU
12527 "wildmenu",
12528#endif
12529#ifdef FEAT_WINDOWS
12530 "windows",
12531#endif
12532#ifdef FEAT_WAK
12533 "winaltkeys",
12534#endif
12535#ifdef FEAT_WRITEBACKUP
12536 "writebackup",
12537#endif
12538#ifdef FEAT_XIM
12539 "xim",
12540#endif
12541#ifdef FEAT_XFONTSET
12542 "xfontset",
12543#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012544#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012545 "xpm",
12546 "xpm_w32", /* for backward compatibility */
12547#else
12548# if defined(HAVE_XPM)
12549 "xpm",
12550# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552#ifdef USE_XSMP
12553 "xsmp",
12554#endif
12555#ifdef USE_XSMP_INTERACT
12556 "xsmp_interact",
12557#endif
12558#ifdef FEAT_XCLIPBOARD
12559 "xterm_clipboard",
12560#endif
12561#ifdef FEAT_XTERM_SAVE
12562 "xterm_save",
12563#endif
12564#if defined(UNIX) && defined(FEAT_X11)
12565 "X11",
12566#endif
12567 NULL
12568 };
12569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 for (i = 0; has_list[i] != NULL; ++i)
12572 if (STRICMP(name, has_list[i]) == 0)
12573 {
12574 n = TRUE;
12575 break;
12576 }
12577
12578 if (n == FALSE)
12579 {
12580 if (STRNICMP(name, "patch", 5) == 0)
12581 n = has_patch(atoi((char *)name + 5));
12582 else if (STRICMP(name, "vim_starting") == 0)
12583 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012584#ifdef FEAT_MBYTE
12585 else if (STRICMP(name, "multi_byte_encoding") == 0)
12586 n = has_mbyte;
12587#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012588#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12589 else if (STRICMP(name, "balloon_multiline") == 0)
12590 n = multiline_balloon_available();
12591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592#ifdef DYNAMIC_TCL
12593 else if (STRICMP(name, "tcl") == 0)
12594 n = tcl_enabled(FALSE);
12595#endif
12596#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12597 else if (STRICMP(name, "iconv") == 0)
12598 n = iconv_enabled(FALSE);
12599#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012600#ifdef DYNAMIC_LUA
12601 else if (STRICMP(name, "lua") == 0)
12602 n = lua_enabled(FALSE);
12603#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012604#ifdef DYNAMIC_MZSCHEME
12605 else if (STRICMP(name, "mzscheme") == 0)
12606 n = mzscheme_enabled(FALSE);
12607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608#ifdef DYNAMIC_RUBY
12609 else if (STRICMP(name, "ruby") == 0)
12610 n = ruby_enabled(FALSE);
12611#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012612#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613#ifdef DYNAMIC_PYTHON
12614 else if (STRICMP(name, "python") == 0)
12615 n = python_enabled(FALSE);
12616#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012617#endif
12618#ifdef FEAT_PYTHON3
12619#ifdef DYNAMIC_PYTHON3
12620 else if (STRICMP(name, "python3") == 0)
12621 n = python3_enabled(FALSE);
12622#endif
12623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624#ifdef DYNAMIC_PERL
12625 else if (STRICMP(name, "perl") == 0)
12626 n = perl_enabled(FALSE);
12627#endif
12628#ifdef FEAT_GUI
12629 else if (STRICMP(name, "gui_running") == 0)
12630 n = (gui.in_use || gui.starting);
12631# ifdef FEAT_GUI_W32
12632 else if (STRICMP(name, "gui_win32s") == 0)
12633 n = gui_is_win32s();
12634# endif
12635# ifdef FEAT_BROWSE
12636 else if (STRICMP(name, "browse") == 0)
12637 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12638# endif
12639#endif
12640#ifdef FEAT_SYN_HL
12641 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012642 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643#endif
12644#if defined(WIN3264)
12645 else if (STRICMP(name, "win95") == 0)
12646 n = mch_windows95();
12647#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012648#ifdef FEAT_NETBEANS_INTG
12649 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012650 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 }
12653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655}
12656
12657/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012658 * "has_key()" function
12659 */
12660 static void
12661f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 typval_T *argvars;
12663 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012664{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012665 if (argvars[0].v_type != VAR_DICT)
12666 {
12667 EMSG(_(e_dictreq));
12668 return;
12669 }
12670 if (argvars[0].vval.v_dict == NULL)
12671 return;
12672
12673 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012674 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012675}
12676
12677/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012678 * "haslocaldir()" function
12679 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012680 static void
12681f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012682 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012683 typval_T *rettv;
12684{
12685 rettv->vval.v_number = (curwin->w_localdir != NULL);
12686}
12687
12688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 * "hasmapto()" function
12690 */
12691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
12696 char_u *name;
12697 char_u *mode;
12698 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012699 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 mode = (char_u *)"nvo";
12704 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012707 if (argvars[2].v_type != VAR_UNKNOWN)
12708 abbr = get_tv_number(&argvars[2]);
12709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
Bram Moolenaar2c932302006-03-18 21:42:09 +000012711 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
12718 * "histadd()" function
12719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
12725#ifdef FEAT_CMDHIST
12726 int histype;
12727 char_u *str;
12728 char_u buf[NUMBUFLEN];
12729#endif
12730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 if (check_restricted() || check_secure())
12733 return;
12734#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12736 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 if (histype >= 0)
12738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 if (*str != NUL)
12741 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012742 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 return;
12746 }
12747 }
12748#endif
12749}
12750
12751/*
12752 * "histdel()" function
12753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012756 typval_T *argvars UNUSED;
12757 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758{
12759#ifdef FEAT_CMDHIST
12760 int n;
12761 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012762 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012764 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12765 if (str == NULL)
12766 n = 0;
12767 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012769 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012770 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 else
12775 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 get_tv_string_buf(&argvars[1], buf));
12778 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779#endif
12780}
12781
12782/*
12783 * "histget()" function
12784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790#ifdef FEAT_CMDHIST
12791 int type;
12792 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12796 if (str == NULL)
12797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 {
12800 type = get_histtype(str);
12801 if (argvars[1].v_type == VAR_UNKNOWN)
12802 idx = get_history_idx(type);
12803 else
12804 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12805 /* -1 on type error */
12806 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812}
12813
12814/*
12815 * "histnr()" function
12816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821{
12822 int i;
12823
12824#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012825 char_u *history = get_tv_string_chk(&argvars[0]);
12826
12827 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 if (i >= HIST_CMD && i < HIST_COUNT)
12829 i = get_history_idx(i);
12830 else
12831#endif
12832 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834}
12835
12836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 * "highlightID(name)" function
12838 */
12839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 typval_T *argvars;
12842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845}
12846
12847/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012848 * "highlight_exists()" function
12849 */
12850 static void
12851f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 typval_T *argvars;
12853 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012854{
12855 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12856}
12857
12858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 * "hostname()" function
12860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865{
12866 char_u hostname[256];
12867
12868 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->v_type = VAR_STRING;
12870 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871}
12872
12873/*
12874 * iconv() function
12875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012878 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880{
12881#ifdef FEAT_MBYTE
12882 char_u buf1[NUMBUFLEN];
12883 char_u buf2[NUMBUFLEN];
12884 char_u *from, *to, *str;
12885 vimconv_T vimconv;
12886#endif
12887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890
12891#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 str = get_tv_string(&argvars[0]);
12893 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12894 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 vimconv.vc_type = CONV_NONE;
12896 convert_setup(&vimconv, from, to);
12897
12898 /* If the encodings are equal, no conversion needed. */
12899 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903
12904 convert_setup(&vimconv, NULL, NULL);
12905 vim_free(from);
12906 vim_free(to);
12907#endif
12908}
12909
12910/*
12911 * "indent()" function
12912 */
12913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012915 typval_T *argvars;
12916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
12918 linenr_T lnum;
12919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012924 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925}
12926
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012927/*
12928 * "index()" function
12929 */
12930 static void
12931f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012934{
Bram Moolenaar33570922005-01-25 22:26:29 +000012935 list_T *l;
12936 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012937 long idx = 0;
12938 int ic = FALSE;
12939
12940 rettv->vval.v_number = -1;
12941 if (argvars[0].v_type != VAR_LIST)
12942 {
12943 EMSG(_(e_listreq));
12944 return;
12945 }
12946 l = argvars[0].vval.v_list;
12947 if (l != NULL)
12948 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012949 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012950 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012952 int error = FALSE;
12953
Bram Moolenaar758711c2005-02-02 23:11:38 +000012954 /* Start at specified item. Use the cached index that list_find()
12955 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012957 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012958 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012959 ic = get_tv_number_chk(&argvars[3], &error);
12960 if (error)
12961 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012962 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012963
Bram Moolenaar758711c2005-02-02 23:11:38 +000012964 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012965 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012966 {
12967 rettv->vval.v_number = idx;
12968 break;
12969 }
12970 }
12971}
12972
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973static int inputsecret_flag = 0;
12974
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012975static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12976
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012978 * This function is used by f_input() and f_inputdialog() functions. The third
12979 * argument to f_input() specifies the type of completion to use at the
12980 * prompt. The third argument to f_inputdialog() specifies the value to return
12981 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 */
12983 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012984get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012985 typval_T *argvars;
12986 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012987 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 char_u *p = NULL;
12991 int c;
12992 char_u buf[NUMBUFLEN];
12993 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012994 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012995 int xp_type = EXPAND_NOTHING;
12996 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000
13001#ifdef NO_CONSOLE_INPUT
13002 /* While starting up, there is no place to enter text. */
13003 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005#endif
13006
13007 cmd_silent = FALSE; /* Want to see the prompt. */
13008 if (prompt != NULL)
13009 {
13010 /* Only the part of the message after the last NL is considered as
13011 * prompt for the command line */
13012 p = vim_strrchr(prompt, '\n');
13013 if (p == NULL)
13014 p = prompt;
13015 else
13016 {
13017 ++p;
13018 c = *p;
13019 *p = NUL;
13020 msg_start();
13021 msg_clr_eos();
13022 msg_puts_attr(prompt, echo_attr);
13023 msg_didout = FALSE;
13024 msg_starthere();
13025 *p = c;
13026 }
13027 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 if (argvars[1].v_type != VAR_UNKNOWN)
13030 {
13031 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13032 if (defstr != NULL)
13033 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013035 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013036 {
13037 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013038 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013039 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013040
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013041 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013042 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013043
Bram Moolenaar4463f292005-09-25 22:20:24 +000013044 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13045 if (xp_name == NULL)
13046 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013047
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013048 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013049
Bram Moolenaar4463f292005-09-25 22:20:24 +000013050 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13051 &xp_arg) == FAIL)
13052 return;
13053 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013054 }
13055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013057 {
13058# ifdef FEAT_EX_EXTRA
13059 int save_ex_normal_busy = ex_normal_busy;
13060 ex_normal_busy = 0;
13061# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013063 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13064 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013065# ifdef FEAT_EX_EXTRA
13066 ex_normal_busy = save_ex_normal_busy;
13067# endif
13068 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013069 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013070 && argvars[1].v_type != VAR_UNKNOWN
13071 && argvars[2].v_type != VAR_UNKNOWN)
13072 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13073 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013074
13075 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013077 /* since the user typed this, no need to wait for return */
13078 need_wait_return = FALSE;
13079 msg_didout = FALSE;
13080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 cmd_silent = cmd_silent_save;
13082}
13083
13084/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013085 * "input()" function
13086 * Also handles inputsecret() when inputsecret is set.
13087 */
13088 static void
13089f_input(argvars, rettv)
13090 typval_T *argvars;
13091 typval_T *rettv;
13092{
13093 get_user_input(argvars, rettv, FALSE);
13094}
13095
13096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 * "inputdialog()" function
13098 */
13099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013101 typval_T *argvars;
13102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103{
13104#if defined(FEAT_GUI_TEXTDIALOG)
13105 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13106 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13107 {
13108 char_u *message;
13109 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013110 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 message = get_tv_string_chk(&argvars[0]);
13113 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013114 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013115 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 else
13117 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 if (message != NULL && defstr != NULL
13119 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013120 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 else
13123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013124 if (message != NULL && defstr != NULL
13125 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127 rettv->vval.v_string = vim_strsave(
13128 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 }
13134 else
13135#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013136 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137}
13138
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013139/*
13140 * "inputlist()" function
13141 */
13142 static void
13143f_inputlist(argvars, rettv)
13144 typval_T *argvars;
13145 typval_T *rettv;
13146{
13147 listitem_T *li;
13148 int selected;
13149 int mouse_used;
13150
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013151#ifdef NO_CONSOLE_INPUT
13152 /* While starting up, there is no place to enter text. */
13153 if (no_console_input())
13154 return;
13155#endif
13156 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13157 {
13158 EMSG2(_(e_listarg), "inputlist()");
13159 return;
13160 }
13161
13162 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013163 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013164 lines_left = Rows; /* avoid more prompt */
13165 msg_scroll = TRUE;
13166 msg_clr_eos();
13167
13168 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13169 {
13170 msg_puts(get_tv_string(&li->li_tv));
13171 msg_putchar('\n');
13172 }
13173
13174 /* Ask for choice. */
13175 selected = prompt_for_number(&mouse_used);
13176 if (mouse_used)
13177 selected -= lines_left;
13178
13179 rettv->vval.v_number = selected;
13180}
13181
13182
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13184
13185/*
13186 * "inputrestore()" function
13187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013190 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192{
13193 if (ga_userinput.ga_len > 0)
13194 {
13195 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13197 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013198 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 }
13200 else if (p_verbose > 1)
13201 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013202 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 }
13205}
13206
13207/*
13208 * "inputsave()" function
13209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013212 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013215 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 if (ga_grow(&ga_userinput, 1) == OK)
13217 {
13218 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13219 + ga_userinput.ga_len);
13220 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013221 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 }
13223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225}
13226
13227/*
13228 * "inputsecret()" function
13229 */
13230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 typval_T *argvars;
13233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234{
13235 ++cmdline_star;
13236 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 --cmdline_star;
13239 --inputsecret_flag;
13240}
13241
13242/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013243 * "insert()" function
13244 */
13245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 typval_T *argvars;
13248 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013249{
13250 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 listitem_T *item;
13252 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013254
13255 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013257 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013258 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 {
13260 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 before = get_tv_number_chk(&argvars[2], &error);
13262 if (error)
13263 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013264
Bram Moolenaar758711c2005-02-02 23:11:38 +000013265 if (before == l->lv_len)
13266 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013267 else
13268 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013269 item = list_find(l, before);
13270 if (item == NULL)
13271 {
13272 EMSGN(_(e_listidx), before);
13273 l = NULL;
13274 }
13275 }
13276 if (l != NULL)
13277 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013278 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013279 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280 }
13281 }
13282}
13283
13284/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013285 * "invert(expr)" function
13286 */
13287 static void
13288f_invert(argvars, rettv)
13289 typval_T *argvars;
13290 typval_T *rettv;
13291{
13292 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13293}
13294
13295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 * "isdirectory()" function
13297 */
13298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013299f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *argvars;
13301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304}
13305
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013306/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013307 * "islocked()" function
13308 */
13309 static void
13310f_islocked(argvars, rettv)
13311 typval_T *argvars;
13312 typval_T *rettv;
13313{
13314 lval_T lv;
13315 char_u *end;
13316 dictitem_T *di;
13317
13318 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013319 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13320 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013321 if (end != NULL && lv.ll_name != NULL)
13322 {
13323 if (*end != NUL)
13324 EMSG(_(e_trailing));
13325 else
13326 {
13327 if (lv.ll_tv == NULL)
13328 {
13329 if (check_changedtick(lv.ll_name))
13330 rettv->vval.v_number = 1; /* always locked */
13331 else
13332 {
13333 di = find_var(lv.ll_name, NULL);
13334 if (di != NULL)
13335 {
13336 /* Consider a variable locked when:
13337 * 1. the variable itself is locked
13338 * 2. the value of the variable is locked.
13339 * 3. the List or Dict value is locked.
13340 */
13341 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13342 || tv_islocked(&di->di_tv));
13343 }
13344 }
13345 }
13346 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013347 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013348 else if (lv.ll_newkey != NULL)
13349 EMSG2(_(e_dictkey), lv.ll_newkey);
13350 else if (lv.ll_list != NULL)
13351 /* List item. */
13352 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13353 else
13354 /* Dictionary item. */
13355 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13356 }
13357 }
13358
13359 clear_lval(&lv);
13360}
13361
Bram Moolenaar33570922005-01-25 22:26:29 +000013362static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013363
13364/*
13365 * Turn a dict into a list:
13366 * "what" == 0: list of keys
13367 * "what" == 1: list of values
13368 * "what" == 2: list of items
13369 */
13370 static void
13371dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 typval_T *argvars;
13373 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013374 int what;
13375{
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 list_T *l2;
13377 dictitem_T *di;
13378 hashitem_T *hi;
13379 listitem_T *li;
13380 listitem_T *li2;
13381 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013383
Bram Moolenaar8c711452005-01-14 21:53:12 +000013384 if (argvars[0].v_type != VAR_DICT)
13385 {
13386 EMSG(_(e_dictreq));
13387 return;
13388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013390 return;
13391
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013392 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013393 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013394
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013395 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013397 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013398 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013400 --todo;
13401 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013403 li = listitem_alloc();
13404 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013405 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013406 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013407
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013408 if (what == 0)
13409 {
13410 /* keys() */
13411 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013412 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013413 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13414 }
13415 else if (what == 1)
13416 {
13417 /* values() */
13418 copy_tv(&di->di_tv, &li->li_tv);
13419 }
13420 else
13421 {
13422 /* items() */
13423 l2 = list_alloc();
13424 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013425 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013426 li->li_tv.vval.v_list = l2;
13427 if (l2 == NULL)
13428 break;
13429 ++l2->lv_refcount;
13430
13431 li2 = listitem_alloc();
13432 if (li2 == NULL)
13433 break;
13434 list_append(l2, li2);
13435 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013436 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013437 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13438
13439 li2 = listitem_alloc();
13440 if (li2 == NULL)
13441 break;
13442 list_append(l2, li2);
13443 copy_tv(&di->di_tv, &li2->li_tv);
13444 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013445 }
13446 }
13447}
13448
13449/*
13450 * "items(dict)" function
13451 */
13452 static void
13453f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013454 typval_T *argvars;
13455 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013456{
13457 dict_list(argvars, rettv, 2);
13458}
13459
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461 * "join()" function
13462 */
13463 static void
13464f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 typval_T *argvars;
13466 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013467{
13468 garray_T ga;
13469 char_u *sep;
13470
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013471 if (argvars[0].v_type != VAR_LIST)
13472 {
13473 EMSG(_(e_listreq));
13474 return;
13475 }
13476 if (argvars[0].vval.v_list == NULL)
13477 return;
13478 if (argvars[1].v_type == VAR_UNKNOWN)
13479 sep = (char_u *)" ";
13480 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013482
13483 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484
13485 if (sep != NULL)
13486 {
13487 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013488 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 ga_append(&ga, NUL);
13490 rettv->vval.v_string = (char_u *)ga.ga_data;
13491 }
13492 else
13493 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013494}
13495
13496/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013497 * "keys()" function
13498 */
13499 static void
13500f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013501 typval_T *argvars;
13502 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013503{
13504 dict_list(argvars, rettv, 0);
13505}
13506
13507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 * "last_buffer_nr()" function.
13509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013512 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514{
13515 int n = 0;
13516 buf_T *buf;
13517
13518 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13519 if (n < buf->b_fnum)
13520 n = buf->b_fnum;
13521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523}
13524
13525/*
13526 * "len()" function
13527 */
13528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532{
13533 switch (argvars[0].v_type)
13534 {
13535 case VAR_STRING:
13536 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 rettv->vval.v_number = (varnumber_T)STRLEN(
13538 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013539 break;
13540 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013541 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013542 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013543 case VAR_DICT:
13544 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13545 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013546 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013547 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013548 break;
13549 }
13550}
13551
Bram Moolenaar33570922005-01-25 22:26:29 +000013552static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013553
13554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558 int type;
13559{
13560#ifdef FEAT_LIBCALL
13561 char_u *string_in;
13562 char_u **string_result;
13563 int nr_result;
13564#endif
13565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013567 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013569
13570 if (check_restricted() || check_secure())
13571 return;
13572
13573#ifdef FEAT_LIBCALL
13574 /* The first two args must be strings, otherwise its meaningless */
13575 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13576 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013577 string_in = NULL;
13578 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 string_in = argvars[2].vval.v_string;
13580 if (type == VAR_NUMBER)
13581 string_result = NULL;
13582 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013584 if (mch_libcall(argvars[0].vval.v_string,
13585 argvars[1].vval.v_string,
13586 string_in,
13587 argvars[2].vval.v_number,
13588 string_result,
13589 &nr_result) == OK
13590 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013592 }
13593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594}
13595
13596/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013597 * "libcall()" function
13598 */
13599 static void
13600f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *argvars;
13602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013603{
13604 libcall_common(argvars, rettv, VAR_STRING);
13605}
13606
13607/*
13608 * "libcallnr()" function
13609 */
13610 static void
13611f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013612 typval_T *argvars;
13613 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013614{
13615 libcall_common(argvars, rettv, VAR_NUMBER);
13616}
13617
13618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 * "line(string)" function
13620 */
13621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *argvars;
13624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625{
13626 linenr_T lnum = 0;
13627 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013628 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013630 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 if (fp != NULL)
13632 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
13637 * "line2byte(lnum)" function
13638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643{
13644#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646#else
13647 linenr_T lnum;
13648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13654 if (rettv->vval.v_number >= 0)
13655 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656#endif
13657}
13658
13659/*
13660 * "lispindent(lnum)" function
13661 */
13662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
13667#ifdef FEAT_LISP
13668 pos_T pos;
13669 linenr_T lnum;
13670
13671 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13674 {
13675 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 curwin->w_cursor = pos;
13678 }
13679 else
13680#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682}
13683
13684/*
13685 * "localtime()" function
13686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693}
13694
Bram Moolenaar33570922005-01-25 22:26:29 +000013695static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
13697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013699 typval_T *argvars;
13700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 int exact;
13702{
13703 char_u *keys;
13704 char_u *which;
13705 char_u buf[NUMBUFLEN];
13706 char_u *keys_buf = NULL;
13707 char_u *rhs;
13708 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013709 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013710 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013711 mapblock_T *mp;
13712 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713
13714 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->v_type = VAR_STRING;
13716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 if (*keys == NUL)
13720 return;
13721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013722 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013724 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013725 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013726 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013727 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013728 if (argvars[3].v_type != VAR_UNKNOWN)
13729 get_dict = get_tv_number(&argvars[3]);
13730 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 else
13733 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734 if (which == NULL)
13735 return;
13736
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 mode = get_map_mode(&which, 0);
13738
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013739 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013740 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013742
13743 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013745 /* Return a string. */
13746 if (rhs != NULL)
13747 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748
Bram Moolenaarbd743252010-10-20 21:23:33 +020013749 }
13750 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13751 {
13752 /* Return a dictionary. */
13753 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13754 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13755 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756
Bram Moolenaarbd743252010-10-20 21:23:33 +020013757 dict_add_nr_str(dict, "lhs", 0L, lhs);
13758 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13759 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13760 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13761 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13762 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13763 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013764 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013765 dict_add_nr_str(dict, "mode", 0L, mapmode);
13766
13767 vim_free(lhs);
13768 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 }
13770}
13771
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013772#ifdef FEAT_FLOAT
13773/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013774 * "log()" function
13775 */
13776 static void
13777f_log(argvars, rettv)
13778 typval_T *argvars;
13779 typval_T *rettv;
13780{
13781 float_T f;
13782
13783 rettv->v_type = VAR_FLOAT;
13784 if (get_float_arg(argvars, &f) == OK)
13785 rettv->vval.v_float = log(f);
13786 else
13787 rettv->vval.v_float = 0.0;
13788}
13789
13790/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013791 * "log10()" function
13792 */
13793 static void
13794f_log10(argvars, rettv)
13795 typval_T *argvars;
13796 typval_T *rettv;
13797{
13798 float_T f;
13799
13800 rettv->v_type = VAR_FLOAT;
13801 if (get_float_arg(argvars, &f) == OK)
13802 rettv->vval.v_float = log10(f);
13803 else
13804 rettv->vval.v_float = 0.0;
13805}
13806#endif
13807
Bram Moolenaar1dced572012-04-05 16:54:08 +020013808#ifdef FEAT_LUA
13809/*
13810 * "luaeval()" function
13811 */
13812 static void
13813f_luaeval(argvars, rettv)
13814 typval_T *argvars;
13815 typval_T *rettv;
13816{
13817 char_u *str;
13818 char_u buf[NUMBUFLEN];
13819
13820 str = get_tv_string_buf(&argvars[0], buf);
13821 do_luaeval(str, argvars + 1, rettv);
13822}
13823#endif
13824
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 * "map()" function
13827 */
13828 static void
13829f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013830 typval_T *argvars;
13831 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013832{
13833 filter_map(argvars, rettv, TRUE);
13834}
13835
13836/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013837 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 */
13839 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013840f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013841 typval_T *argvars;
13842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013844 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845}
13846
13847/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 */
13850 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013851f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 typval_T *argvars;
13853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013855 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856}
13857
Bram Moolenaar33570922005-01-25 22:26:29 +000013858static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859
13860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013862 typval_T *argvars;
13863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 int type;
13865{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866 char_u *str = NULL;
13867 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 char_u *pat;
13869 regmatch_T regmatch;
13870 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013871 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 char_u *save_cpo;
13873 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013874 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013875 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013876 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 list_T *l = NULL;
13878 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013879 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013880 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881
13882 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13883 save_cpo = p_cpo;
13884 p_cpo = (char_u *)"";
13885
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013886 rettv->vval.v_number = -1;
13887 if (type == 3)
13888 {
13889 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013890 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013891 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013892 }
13893 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013895 rettv->v_type = VAR_STRING;
13896 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013899 if (argvars[0].v_type == VAR_LIST)
13900 {
13901 if ((l = argvars[0].vval.v_list) == NULL)
13902 goto theend;
13903 li = l->lv_first;
13904 }
13905 else
13906 expr = str = get_tv_string(&argvars[0]);
13907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13909 if (pat == NULL)
13910 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013911
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013912 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 int error = FALSE;
13915
13916 start = get_tv_number_chk(&argvars[2], &error);
13917 if (error)
13918 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013919 if (l != NULL)
13920 {
13921 li = list_find(l, start);
13922 if (li == NULL)
13923 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013924 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013925 }
13926 else
13927 {
13928 if (start < 0)
13929 start = 0;
13930 if (start > (long)STRLEN(str))
13931 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013932 /* When "count" argument is there ignore matches before "start",
13933 * otherwise skip part of the string. Differs when pattern is "^"
13934 * or "\<". */
13935 if (argvars[3].v_type != VAR_UNKNOWN)
13936 startcol = start;
13937 else
13938 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013940
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013941 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013942 nth = get_tv_number_chk(&argvars[3], &error);
13943 if (error)
13944 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 }
13946
13947 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13948 if (regmatch.regprog != NULL)
13949 {
13950 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013951
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013952 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013953 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013954 if (l != NULL)
13955 {
13956 if (li == NULL)
13957 {
13958 match = FALSE;
13959 break;
13960 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013961 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013962 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013963 if (str == NULL)
13964 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 }
13966
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013967 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013969 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013970 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971 if (l == NULL && !match)
13972 break;
13973
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013974 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013975 if (l != NULL)
13976 {
13977 li = li->li_next;
13978 ++idx;
13979 }
13980 else
13981 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013982#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013983 startcol = (colnr_T)(regmatch.startp[0]
13984 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013985#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013986 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013987#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013988 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013989 }
13990
13991 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013993 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013994 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013995 int i;
13996
13997 /* return list with matched string and submatches */
13998 for (i = 0; i < NSUBEXP; ++i)
13999 {
14000 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014001 {
14002 if (list_append_string(rettv->vval.v_list,
14003 (char_u *)"", 0) == FAIL)
14004 break;
14005 }
14006 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014007 regmatch.startp[i],
14008 (int)(regmatch.endp[i] - regmatch.startp[i]))
14009 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014010 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014011 }
14012 }
14013 else if (type == 2)
14014 {
14015 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016 if (l != NULL)
14017 copy_tv(&li->li_tv, rettv);
14018 else
14019 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014021 }
14022 else if (l != NULL)
14023 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 else
14025 {
14026 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 (varnumber_T)(regmatch.startp[0] - str);
14029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014032 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 }
14034 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014035 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 }
14037
14038theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014039 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 p_cpo = save_cpo;
14041}
14042
14043/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044 * "match()" function
14045 */
14046 static void
14047f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 typval_T *argvars;
14049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014050{
14051 find_some_match(argvars, rettv, 1);
14052}
14053
14054/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014055 * "matchadd()" function
14056 */
14057 static void
14058f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014059 typval_T *argvars UNUSED;
14060 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014061{
14062#ifdef FEAT_SEARCH_EXTRA
14063 char_u buf[NUMBUFLEN];
14064 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14065 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14066 int prio = 10; /* default priority */
14067 int id = -1;
14068 int error = FALSE;
14069
14070 rettv->vval.v_number = -1;
14071
14072 if (grp == NULL || pat == NULL)
14073 return;
14074 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014075 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014076 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014077 if (argvars[3].v_type != VAR_UNKNOWN)
14078 id = get_tv_number_chk(&argvars[3], &error);
14079 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014080 if (error == TRUE)
14081 return;
14082 if (id >= 1 && id <= 3)
14083 {
14084 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14085 return;
14086 }
14087
14088 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14089#endif
14090}
14091
14092/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014093 * "matcharg()" function
14094 */
14095 static void
14096f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014097 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014098 typval_T *rettv;
14099{
14100 if (rettv_list_alloc(rettv) == OK)
14101 {
14102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014103 int id = get_tv_number(&argvars[0]);
14104 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014105
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014106 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014107 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014108 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14109 {
14110 list_append_string(rettv->vval.v_list,
14111 syn_id2name(m->hlg_id), -1);
14112 list_append_string(rettv->vval.v_list, m->pattern, -1);
14113 }
14114 else
14115 {
14116 list_append_string(rettv->vval.v_list, NUL, -1);
14117 list_append_string(rettv->vval.v_list, NUL, -1);
14118 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014119 }
14120#endif
14121 }
14122}
14123
14124/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014125 * "matchdelete()" function
14126 */
14127 static void
14128f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014129 typval_T *argvars UNUSED;
14130 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014131{
14132#ifdef FEAT_SEARCH_EXTRA
14133 rettv->vval.v_number = match_delete(curwin,
14134 (int)get_tv_number(&argvars[0]), TRUE);
14135#endif
14136}
14137
14138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139 * "matchend()" function
14140 */
14141 static void
14142f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014145{
14146 find_some_match(argvars, rettv, 0);
14147}
14148
14149/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014150 * "matchlist()" function
14151 */
14152 static void
14153f_matchlist(argvars, rettv)
14154 typval_T *argvars;
14155 typval_T *rettv;
14156{
14157 find_some_match(argvars, rettv, 3);
14158}
14159
14160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161 * "matchstr()" function
14162 */
14163 static void
14164f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *argvars;
14166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014167{
14168 find_some_match(argvars, rettv, 2);
14169}
14170
Bram Moolenaar33570922005-01-25 22:26:29 +000014171static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014172
14173 static void
14174max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 typval_T *argvars;
14176 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014177 int domax;
14178{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014179 long n = 0;
14180 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014181 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014182
14183 if (argvars[0].v_type == VAR_LIST)
14184 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 list_T *l;
14186 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014187
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014188 l = argvars[0].vval.v_list;
14189 if (l != NULL)
14190 {
14191 li = l->lv_first;
14192 if (li != NULL)
14193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014195 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014196 {
14197 li = li->li_next;
14198 if (li == NULL)
14199 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014201 if (domax ? i > n : i < n)
14202 n = i;
14203 }
14204 }
14205 }
14206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014207 else if (argvars[0].v_type == VAR_DICT)
14208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014210 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014212 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014213
14214 d = argvars[0].vval.v_dict;
14215 if (d != NULL)
14216 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014217 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014220 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014222 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014224 if (first)
14225 {
14226 n = i;
14227 first = FALSE;
14228 }
14229 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014230 n = i;
14231 }
14232 }
14233 }
14234 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014235 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014236 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014238}
14239
14240/*
14241 * "max()" function
14242 */
14243 static void
14244f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014245 typval_T *argvars;
14246 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014247{
14248 max_min(argvars, rettv, TRUE);
14249}
14250
14251/*
14252 * "min()" function
14253 */
14254 static void
14255f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014256 typval_T *argvars;
14257 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014258{
14259 max_min(argvars, rettv, FALSE);
14260}
14261
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014262static int mkdir_recurse __ARGS((char_u *dir, int prot));
14263
14264/*
14265 * Create the directory in which "dir" is located, and higher levels when
14266 * needed.
14267 */
14268 static int
14269mkdir_recurse(dir, prot)
14270 char_u *dir;
14271 int prot;
14272{
14273 char_u *p;
14274 char_u *updir;
14275 int r = FAIL;
14276
14277 /* Get end of directory name in "dir".
14278 * We're done when it's "/" or "c:/". */
14279 p = gettail_sep(dir);
14280 if (p <= get_past_head(dir))
14281 return OK;
14282
14283 /* If the directory exists we're done. Otherwise: create it.*/
14284 updir = vim_strnsave(dir, (int)(p - dir));
14285 if (updir == NULL)
14286 return FAIL;
14287 if (mch_isdir(updir))
14288 r = OK;
14289 else if (mkdir_recurse(updir, prot) == OK)
14290 r = vim_mkdir_emsg(updir, prot);
14291 vim_free(updir);
14292 return r;
14293}
14294
14295#ifdef vim_mkdir
14296/*
14297 * "mkdir()" function
14298 */
14299 static void
14300f_mkdir(argvars, rettv)
14301 typval_T *argvars;
14302 typval_T *rettv;
14303{
14304 char_u *dir;
14305 char_u buf[NUMBUFLEN];
14306 int prot = 0755;
14307
14308 rettv->vval.v_number = FAIL;
14309 if (check_restricted() || check_secure())
14310 return;
14311
14312 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014313 if (*dir == NUL)
14314 rettv->vval.v_number = FAIL;
14315 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014316 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014317 if (*gettail(dir) == NUL)
14318 /* remove trailing slashes */
14319 *gettail_sep(dir) = NUL;
14320
14321 if (argvars[1].v_type != VAR_UNKNOWN)
14322 {
14323 if (argvars[2].v_type != VAR_UNKNOWN)
14324 prot = get_tv_number_chk(&argvars[2], NULL);
14325 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14326 mkdir_recurse(dir, prot);
14327 }
14328 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014329 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014330}
14331#endif
14332
Bram Moolenaar0d660222005-01-07 21:51:51 +000014333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 * "mode()" function
14335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 typval_T *argvars;
14339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014341 char_u buf[3];
14342
14343 buf[1] = NUL;
14344 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345
14346#ifdef FEAT_VISUAL
14347 if (VIsual_active)
14348 {
14349 if (VIsual_select)
14350 buf[0] = VIsual_mode + 's' - 'v';
14351 else
14352 buf[0] = VIsual_mode;
14353 }
14354 else
14355#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014356 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14357 || State == CONFIRM)
14358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014360 if (State == ASKMORE)
14361 buf[1] = 'm';
14362 else if (State == CONFIRM)
14363 buf[1] = '?';
14364 }
14365 else if (State == EXTERNCMD)
14366 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 else if (State & INSERT)
14368 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014369#ifdef FEAT_VREPLACE
14370 if (State & VREPLACE_FLAG)
14371 {
14372 buf[0] = 'R';
14373 buf[1] = 'v';
14374 }
14375 else
14376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 if (State & REPLACE_FLAG)
14378 buf[0] = 'R';
14379 else
14380 buf[0] = 'i';
14381 }
14382 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014385 if (exmode_active)
14386 buf[1] = 'v';
14387 }
14388 else if (exmode_active)
14389 {
14390 buf[0] = 'c';
14391 buf[1] = 'e';
14392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014396 if (finish_op)
14397 buf[1] = 'o';
14398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014400 /* Clear out the minor mode when the argument is not a non-zero number or
14401 * non-empty string. */
14402 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014403 buf[1] = NUL;
14404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405 rettv->vval.v_string = vim_strsave(buf);
14406 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407}
14408
Bram Moolenaar429fa852013-04-15 12:27:36 +020014409#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014410/*
14411 * "mzeval()" function
14412 */
14413 static void
14414f_mzeval(argvars, rettv)
14415 typval_T *argvars;
14416 typval_T *rettv;
14417{
14418 char_u *str;
14419 char_u buf[NUMBUFLEN];
14420
14421 str = get_tv_string_buf(&argvars[0], buf);
14422 do_mzeval(str, rettv);
14423}
Bram Moolenaar75676462013-01-30 14:55:42 +010014424
14425 void
14426mzscheme_call_vim(name, args, rettv)
14427 char_u *name;
14428 typval_T *args;
14429 typval_T *rettv;
14430{
14431 typval_T argvars[3];
14432
14433 argvars[0].v_type = VAR_STRING;
14434 argvars[0].vval.v_string = name;
14435 copy_tv(args, &argvars[1]);
14436 argvars[2].v_type = VAR_UNKNOWN;
14437 f_call(argvars, rettv);
14438 clear_tv(&argvars[1]);
14439}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014440#endif
14441
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014443 * "nextnonblank()" function
14444 */
14445 static void
14446f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 typval_T *argvars;
14448 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014449{
14450 linenr_T lnum;
14451
14452 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455 {
14456 lnum = 0;
14457 break;
14458 }
14459 if (*skipwhite(ml_get(lnum)) != NUL)
14460 break;
14461 }
14462 rettv->vval.v_number = lnum;
14463}
14464
14465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466 * "nr2char()" function
14467 */
14468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *argvars;
14471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472{
14473 char_u buf[NUMBUFLEN];
14474
14475#ifdef FEAT_MBYTE
14476 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014477 {
14478 int utf8 = 0;
14479
14480 if (argvars[1].v_type != VAR_UNKNOWN)
14481 utf8 = get_tv_number_chk(&argvars[1], NULL);
14482 if (utf8)
14483 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14484 else
14485 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 else
14488#endif
14489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 buf[1] = NUL;
14492 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014493 rettv->v_type = VAR_STRING;
14494 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014495}
14496
14497/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014498 * "or(expr, expr)" function
14499 */
14500 static void
14501f_or(argvars, rettv)
14502 typval_T *argvars;
14503 typval_T *rettv;
14504{
14505 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14506 | get_tv_number_chk(&argvars[1], NULL);
14507}
14508
14509/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014510 * "pathshorten()" function
14511 */
14512 static void
14513f_pathshorten(argvars, rettv)
14514 typval_T *argvars;
14515 typval_T *rettv;
14516{
14517 char_u *p;
14518
14519 rettv->v_type = VAR_STRING;
14520 p = get_tv_string_chk(&argvars[0]);
14521 if (p == NULL)
14522 rettv->vval.v_string = NULL;
14523 else
14524 {
14525 p = vim_strsave(p);
14526 rettv->vval.v_string = p;
14527 if (p != NULL)
14528 shorten_dir(p);
14529 }
14530}
14531
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014532#ifdef FEAT_FLOAT
14533/*
14534 * "pow()" function
14535 */
14536 static void
14537f_pow(argvars, rettv)
14538 typval_T *argvars;
14539 typval_T *rettv;
14540{
14541 float_T fx, fy;
14542
14543 rettv->v_type = VAR_FLOAT;
14544 if (get_float_arg(argvars, &fx) == OK
14545 && get_float_arg(&argvars[1], &fy) == OK)
14546 rettv->vval.v_float = pow(fx, fy);
14547 else
14548 rettv->vval.v_float = 0.0;
14549}
14550#endif
14551
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553 * "prevnonblank()" function
14554 */
14555 static void
14556f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 typval_T *argvars;
14558 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559{
14560 linenr_T lnum;
14561
14562 lnum = get_tv_lnum(argvars);
14563 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14564 lnum = 0;
14565 else
14566 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14567 --lnum;
14568 rettv->vval.v_number = lnum;
14569}
14570
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014571#ifdef HAVE_STDARG_H
14572/* This dummy va_list is here because:
14573 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14574 * - locally in the function results in a "used before set" warning
14575 * - using va_start() to initialize it gives "function with fixed args" error */
14576static va_list ap;
14577#endif
14578
Bram Moolenaar8c711452005-01-14 21:53:12 +000014579/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014580 * "printf()" function
14581 */
14582 static void
14583f_printf(argvars, rettv)
14584 typval_T *argvars;
14585 typval_T *rettv;
14586{
14587 rettv->v_type = VAR_STRING;
14588 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014589#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014590 {
14591 char_u buf[NUMBUFLEN];
14592 int len;
14593 char_u *s;
14594 int saved_did_emsg = did_emsg;
14595 char *fmt;
14596
14597 /* Get the required length, allocate the buffer and do it for real. */
14598 did_emsg = FALSE;
14599 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014600 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014601 if (!did_emsg)
14602 {
14603 s = alloc(len + 1);
14604 if (s != NULL)
14605 {
14606 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014607 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014608 }
14609 }
14610 did_emsg |= saved_did_emsg;
14611 }
14612#endif
14613}
14614
14615/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014616 * "pumvisible()" function
14617 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014618 static void
14619f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014620 typval_T *argvars UNUSED;
14621 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014622{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014623#ifdef FEAT_INS_EXPAND
14624 if (pum_visible())
14625 rettv->vval.v_number = 1;
14626#endif
14627}
14628
Bram Moolenaardb913952012-06-29 12:54:53 +020014629#ifdef FEAT_PYTHON3
14630/*
14631 * "py3eval()" function
14632 */
14633 static void
14634f_py3eval(argvars, rettv)
14635 typval_T *argvars;
14636 typval_T *rettv;
14637{
14638 char_u *str;
14639 char_u buf[NUMBUFLEN];
14640
14641 str = get_tv_string_buf(&argvars[0], buf);
14642 do_py3eval(str, rettv);
14643}
14644#endif
14645
14646#ifdef FEAT_PYTHON
14647/*
14648 * "pyeval()" function
14649 */
14650 static void
14651f_pyeval(argvars, rettv)
14652 typval_T *argvars;
14653 typval_T *rettv;
14654{
14655 char_u *str;
14656 char_u buf[NUMBUFLEN];
14657
14658 str = get_tv_string_buf(&argvars[0], buf);
14659 do_pyeval(str, rettv);
14660}
14661#endif
14662
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014663/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014664 * "range()" function
14665 */
14666 static void
14667f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 typval_T *argvars;
14669 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014670{
14671 long start;
14672 long end;
14673 long stride = 1;
14674 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014678 if (argvars[1].v_type == VAR_UNKNOWN)
14679 {
14680 end = start - 1;
14681 start = 0;
14682 }
14683 else
14684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014685 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014686 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014687 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014688 }
14689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 if (error)
14691 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014692 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014693 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014694 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014695 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014696 else
14697 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014698 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014699 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014700 if (list_append_number(rettv->vval.v_list,
14701 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014702 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014703 }
14704}
14705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706/*
14707 * "readfile()" function
14708 */
14709 static void
14710f_readfile(argvars, rettv)
14711 typval_T *argvars;
14712 typval_T *rettv;
14713{
14714 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716 char_u *fname;
14717 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014718 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14719 int io_size = sizeof(buf);
14720 int readlen; /* size of last fread() */
14721 char_u *prev = NULL; /* previously read bytes, if any */
14722 long prevlen = 0; /* length of data in prev */
14723 long prevsize = 0; /* size of prev buffer */
14724 long maxline = MAXLNUM;
14725 long cnt = 0;
14726 char_u *p; /* position in buf */
14727 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014729 if (argvars[1].v_type != VAR_UNKNOWN)
14730 {
14731 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14732 binary = TRUE;
14733 if (argvars[2].v_type != VAR_UNKNOWN)
14734 maxline = get_tv_number(&argvars[2]);
14735 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014737 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739
14740 /* Always open the file in binary mode, library functions have a mind of
14741 * their own about CR-LF conversion. */
14742 fname = get_tv_string(&argvars[0]);
14743 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14744 {
14745 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14746 return;
14747 }
14748
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014749 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014751 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 /* This for loop processes what was read, but is also entered at end
14754 * of file so that either:
14755 * - an incomplete line gets written
14756 * - a "binary" file gets an empty line at the end if it ends in a
14757 * newline. */
14758 for (p = buf, start = buf;
14759 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14760 ++p)
14761 {
14762 if (*p == '\n' || readlen <= 0)
14763 {
14764 listitem_T *li;
14765 char_u *s = NULL;
14766 long_u len = p - start;
14767
14768 /* Finished a line. Remove CRs before NL. */
14769 if (readlen > 0 && !binary)
14770 {
14771 while (len > 0 && start[len - 1] == '\r')
14772 --len;
14773 /* removal may cross back to the "prev" string */
14774 if (len == 0)
14775 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14776 --prevlen;
14777 }
14778 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014779 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014780 else
14781 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014782 /* Change "prev" buffer to be the right size. This way
14783 * the bytes are only copied once, and very long lines are
14784 * allocated only once. */
14785 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014787 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014788 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014789 prev = NULL; /* the list will own the string */
14790 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 }
14792 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014793 if (s == NULL)
14794 {
14795 do_outofmem_msg((long_u) prevlen + len + 1);
14796 failed = TRUE;
14797 break;
14798 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014799
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014800 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014801 {
14802 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014803 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014804 break;
14805 }
14806 li->li_tv.v_type = VAR_STRING;
14807 li->li_tv.v_lock = 0;
14808 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014809 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014810
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014811 start = p + 1; /* step over newline */
14812 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014813 break;
14814 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014815 else if (*p == NUL)
14816 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014817#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014818 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14819 * when finding the BF and check the previous two bytes. */
14820 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014821 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14823 * + 1, these may be in the "prev" string. */
14824 char_u back1 = p >= buf + 1 ? p[-1]
14825 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14826 char_u back2 = p >= buf + 2 ? p[-2]
14827 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14828 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014829
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014831 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014832 char_u *dest = p - 2;
14833
14834 /* Usually a BOM is at the beginning of a file, and so at
14835 * the beginning of a line; then we can just step over it.
14836 */
14837 if (start == dest)
14838 start = p + 1;
14839 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014840 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014841 /* have to shuffle buf to close gap */
14842 int adjust_prevlen = 0;
14843
14844 if (dest < buf)
14845 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014846 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014847 dest = buf;
14848 }
14849 if (readlen > p - buf + 1)
14850 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14851 readlen -= 3 - adjust_prevlen;
14852 prevlen -= adjust_prevlen;
14853 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014854 }
14855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014856 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014857#endif
14858 } /* for */
14859
14860 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14861 break;
14862 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014863 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014864 /* There's part of a line in buf, store it in "prev". */
14865 if (p - start + prevlen >= prevsize)
14866 {
14867 /* need bigger "prev" buffer */
14868 char_u *newprev;
14869
14870 /* A common use case is ordinary text files and "prev" gets a
14871 * fragment of a line, so the first allocation is made
14872 * small, to avoid repeatedly 'allocing' large and
14873 * 'reallocing' small. */
14874 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014875 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014876 else
14877 {
14878 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014879 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014880 prevsize = grow50pc > growmin ? grow50pc : growmin;
14881 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014882 newprev = prev == NULL ? alloc(prevsize)
14883 : vim_realloc(prev, prevsize);
14884 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014885 {
14886 do_outofmem_msg((long_u)prevsize);
14887 failed = TRUE;
14888 break;
14889 }
14890 prev = newprev;
14891 }
14892 /* Add the line part to end of "prev". */
14893 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014894 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014895 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014896 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014897
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014898 /*
14899 * For a negative line count use only the lines at the end of the file,
14900 * free the rest.
14901 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014902 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014903 while (cnt > -maxline)
14904 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014905 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014906 --cnt;
14907 }
14908
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014909 if (failed)
14910 {
14911 list_free(rettv->vval.v_list, TRUE);
14912 /* readfile doc says an empty list is returned on error */
14913 rettv->vval.v_list = list_alloc();
14914 }
14915
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014916 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014917 fclose(fd);
14918}
14919
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014920#if defined(FEAT_RELTIME)
14921static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14922
14923/*
14924 * Convert a List to proftime_T.
14925 * Return FAIL when there is something wrong.
14926 */
14927 static int
14928list2proftime(arg, tm)
14929 typval_T *arg;
14930 proftime_T *tm;
14931{
14932 long n1, n2;
14933 int error = FALSE;
14934
14935 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14936 || arg->vval.v_list->lv_len != 2)
14937 return FAIL;
14938 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14939 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14940# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014941 tm->HighPart = n1;
14942 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014943# else
14944 tm->tv_sec = n1;
14945 tm->tv_usec = n2;
14946# endif
14947 return error ? FAIL : OK;
14948}
14949#endif /* FEAT_RELTIME */
14950
14951/*
14952 * "reltime()" function
14953 */
14954 static void
14955f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014956 typval_T *argvars UNUSED;
14957 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014958{
14959#ifdef FEAT_RELTIME
14960 proftime_T res;
14961 proftime_T start;
14962
14963 if (argvars[0].v_type == VAR_UNKNOWN)
14964 {
14965 /* No arguments: get current time. */
14966 profile_start(&res);
14967 }
14968 else if (argvars[1].v_type == VAR_UNKNOWN)
14969 {
14970 if (list2proftime(&argvars[0], &res) == FAIL)
14971 return;
14972 profile_end(&res);
14973 }
14974 else
14975 {
14976 /* Two arguments: compute the difference. */
14977 if (list2proftime(&argvars[0], &start) == FAIL
14978 || list2proftime(&argvars[1], &res) == FAIL)
14979 return;
14980 profile_sub(&res, &start);
14981 }
14982
14983 if (rettv_list_alloc(rettv) == OK)
14984 {
14985 long n1, n2;
14986
14987# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014988 n1 = res.HighPart;
14989 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014990# else
14991 n1 = res.tv_sec;
14992 n2 = res.tv_usec;
14993# endif
14994 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14995 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14996 }
14997#endif
14998}
14999
15000/*
15001 * "reltimestr()" function
15002 */
15003 static void
15004f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015005 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015006 typval_T *rettv;
15007{
15008#ifdef FEAT_RELTIME
15009 proftime_T tm;
15010#endif
15011
15012 rettv->v_type = VAR_STRING;
15013 rettv->vval.v_string = NULL;
15014#ifdef FEAT_RELTIME
15015 if (list2proftime(&argvars[0], &tm) == OK)
15016 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15017#endif
15018}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015019
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15021static void make_connection __ARGS((void));
15022static int check_connection __ARGS((void));
15023
15024 static void
15025make_connection()
15026{
15027 if (X_DISPLAY == NULL
15028# ifdef FEAT_GUI
15029 && !gui.in_use
15030# endif
15031 )
15032 {
15033 x_force_connect = TRUE;
15034 setup_term_clip();
15035 x_force_connect = FALSE;
15036 }
15037}
15038
15039 static int
15040check_connection()
15041{
15042 make_connection();
15043 if (X_DISPLAY == NULL)
15044 {
15045 EMSG(_("E240: No connection to Vim server"));
15046 return FAIL;
15047 }
15048 return OK;
15049}
15050#endif
15051
15052#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015053static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054
15055 static void
15056remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 typval_T *argvars;
15058 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059 int expr;
15060{
15061 char_u *server_name;
15062 char_u *keys;
15063 char_u *r = NULL;
15064 char_u buf[NUMBUFLEN];
15065# ifdef WIN32
15066 HWND w;
15067# else
15068 Window w;
15069# endif
15070
15071 if (check_restricted() || check_secure())
15072 return;
15073
15074# ifdef FEAT_X11
15075 if (check_connection() == FAIL)
15076 return;
15077# endif
15078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015079 server_name = get_tv_string_chk(&argvars[0]);
15080 if (server_name == NULL)
15081 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 keys = get_tv_string_buf(&argvars[1], buf);
15083# ifdef WIN32
15084 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15085# else
15086 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15087 < 0)
15088# endif
15089 {
15090 if (r != NULL)
15091 EMSG(r); /* sending worked but evaluation failed */
15092 else
15093 EMSG2(_("E241: Unable to send to %s"), server_name);
15094 return;
15095 }
15096
15097 rettv->vval.v_string = r;
15098
15099 if (argvars[2].v_type != VAR_UNKNOWN)
15100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015102 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015105 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 v.di_tv.v_type = VAR_STRING;
15107 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108 idvar = get_tv_string_chk(&argvars[2]);
15109 if (idvar != NULL)
15110 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 }
15113}
15114#endif
15115
15116/*
15117 * "remote_expr()" function
15118 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 static void
15120f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123{
15124 rettv->v_type = VAR_STRING;
15125 rettv->vval.v_string = NULL;
15126#ifdef FEAT_CLIENTSERVER
15127 remote_common(argvars, rettv, TRUE);
15128#endif
15129}
15130
15131/*
15132 * "remote_foreground()" function
15133 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134 static void
15135f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015136 typval_T *argvars UNUSED;
15137 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139#ifdef FEAT_CLIENTSERVER
15140# ifdef WIN32
15141 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 {
15143 char_u *server_name = get_tv_string_chk(&argvars[0]);
15144
15145 if (server_name != NULL)
15146 serverForeground(server_name);
15147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148# else
15149 /* Send a foreground() expression to the server. */
15150 argvars[1].v_type = VAR_STRING;
15151 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15152 argvars[2].v_type = VAR_UNKNOWN;
15153 remote_common(argvars, rettv, TRUE);
15154 vim_free(argvars[1].vval.v_string);
15155# endif
15156#endif
15157}
15158
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 static void
15160f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015161 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163{
15164#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166 char_u *s = NULL;
15167# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015168 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171
15172 if (check_restricted() || check_secure())
15173 {
15174 rettv->vval.v_number = -1;
15175 return;
15176 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015177 serverid = get_tv_string_chk(&argvars[0]);
15178 if (serverid == NULL)
15179 {
15180 rettv->vval.v_number = -1;
15181 return; /* type error; errmsg already given */
15182 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015184 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185 if (n == 0)
15186 rettv->vval.v_number = -1;
15187 else
15188 {
15189 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15190 rettv->vval.v_number = (s != NULL);
15191 }
15192# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193 if (check_connection() == FAIL)
15194 return;
15195
15196 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198# endif
15199
15200 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015202 char_u *retvar;
15203
Bram Moolenaar33570922005-01-25 22:26:29 +000015204 v.di_tv.v_type = VAR_STRING;
15205 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015206 retvar = get_tv_string_chk(&argvars[1]);
15207 if (retvar != NULL)
15208 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210 }
15211#else
15212 rettv->vval.v_number = -1;
15213#endif
15214}
15215
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216 static void
15217f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015218 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015220{
15221 char_u *r = NULL;
15222
15223#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 char_u *serverid = get_tv_string_chk(&argvars[0]);
15225
15226 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227 {
15228# ifdef WIN32
15229 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015230 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015232 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233 if (n != 0)
15234 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15235 if (r == NULL)
15236# else
15237 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239# endif
15240 EMSG(_("E277: Unable to read a server reply"));
15241 }
15242#endif
15243 rettv->v_type = VAR_STRING;
15244 rettv->vval.v_string = r;
15245}
15246
15247/*
15248 * "remote_send()" function
15249 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250 static void
15251f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015252 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015253 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015254{
15255 rettv->v_type = VAR_STRING;
15256 rettv->vval.v_string = NULL;
15257#ifdef FEAT_CLIENTSERVER
15258 remote_common(argvars, rettv, FALSE);
15259#endif
15260}
15261
15262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015263 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015264 */
15265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015267 typval_T *argvars;
15268 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015269{
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 list_T *l;
15271 listitem_T *item, *item2;
15272 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015274 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015275 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015276 dict_T *d;
15277 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015278 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279
Bram Moolenaar8c711452005-01-14 21:53:12 +000015280 if (argvars[0].v_type == VAR_DICT)
15281 {
15282 if (argvars[2].v_type != VAR_UNKNOWN)
15283 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015284 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015285 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015287 key = get_tv_string_chk(&argvars[1]);
15288 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 di = dict_find(d, key, -1);
15291 if (di == NULL)
15292 EMSG2(_(e_dictkey), key);
15293 else
15294 {
15295 *rettv = di->di_tv;
15296 init_tv(&di->di_tv);
15297 dictitem_remove(d, di);
15298 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015300 }
15301 }
15302 else if (argvars[0].v_type != VAR_LIST)
15303 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015304 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015305 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 int error = FALSE;
15308
15309 idx = get_tv_number_chk(&argvars[1], &error);
15310 if (error)
15311 ; /* type error: do nothing, errmsg already given */
15312 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015313 EMSGN(_(e_listidx), idx);
15314 else
15315 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015316 if (argvars[2].v_type == VAR_UNKNOWN)
15317 {
15318 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015319 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320 *rettv = item->li_tv;
15321 vim_free(item);
15322 }
15323 else
15324 {
15325 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 end = get_tv_number_chk(&argvars[2], &error);
15327 if (error)
15328 ; /* type error: do nothing */
15329 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015330 EMSGN(_(e_listidx), end);
15331 else
15332 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015333 int cnt = 0;
15334
15335 for (li = item; li != NULL; li = li->li_next)
15336 {
15337 ++cnt;
15338 if (li == item2)
15339 break;
15340 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015341 if (li == NULL) /* didn't find "item2" after "item" */
15342 EMSG(_(e_invrange));
15343 else
15344 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015345 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015346 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015347 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015348 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015349 l->lv_first = item;
15350 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015351 item->li_prev = NULL;
15352 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015353 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015354 }
15355 }
15356 }
15357 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015358 }
15359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360}
15361
15362/*
15363 * "rename({from}, {to})" function
15364 */
15365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015367 typval_T *argvars;
15368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369{
15370 char_u buf[NUMBUFLEN];
15371
15372 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15376 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377}
15378
15379/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015380 * "repeat()" function
15381 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015382 static void
15383f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015384 typval_T *argvars;
15385 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015386{
15387 char_u *p;
15388 int n;
15389 int slen;
15390 int len;
15391 char_u *r;
15392 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015393
15394 n = get_tv_number(&argvars[1]);
15395 if (argvars[0].v_type == VAR_LIST)
15396 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015397 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015398 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015399 if (list_extend(rettv->vval.v_list,
15400 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015401 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015402 }
15403 else
15404 {
15405 p = get_tv_string(&argvars[0]);
15406 rettv->v_type = VAR_STRING;
15407 rettv->vval.v_string = NULL;
15408
15409 slen = (int)STRLEN(p);
15410 len = slen * n;
15411 if (len <= 0)
15412 return;
15413
15414 r = alloc(len + 1);
15415 if (r != NULL)
15416 {
15417 for (i = 0; i < n; i++)
15418 mch_memmove(r + i * slen, p, (size_t)slen);
15419 r[len] = NUL;
15420 }
15421
15422 rettv->vval.v_string = r;
15423 }
15424}
15425
15426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 * "resolve()" function
15428 */
15429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015430f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015431 typval_T *argvars;
15432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433{
15434 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015435#ifdef HAVE_READLINK
15436 char_u *buf = NULL;
15437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440#ifdef FEAT_SHORTCUT
15441 {
15442 char_u *v = NULL;
15443
15444 v = mch_resolve_shortcut(p);
15445 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 }
15450#else
15451# ifdef HAVE_READLINK
15452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 char_u *cpy;
15454 int len;
15455 char_u *remain = NULL;
15456 char_u *q;
15457 int is_relative_to_current = FALSE;
15458 int has_trailing_pathsep = FALSE;
15459 int limit = 100;
15460
15461 p = vim_strsave(p);
15462
15463 if (p[0] == '.' && (vim_ispathsep(p[1])
15464 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15465 is_relative_to_current = TRUE;
15466
15467 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015468 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015471 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473
15474 q = getnextcomp(p);
15475 if (*q != NUL)
15476 {
15477 /* Separate the first path component in "p", and keep the
15478 * remainder (beginning with the path separator). */
15479 remain = vim_strsave(q - 1);
15480 q[-1] = NUL;
15481 }
15482
Bram Moolenaard9462e32011-04-11 21:35:11 +020015483 buf = alloc(MAXPATHL + 1);
15484 if (buf == NULL)
15485 goto fail;
15486
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 for (;;)
15488 {
15489 for (;;)
15490 {
15491 len = readlink((char *)p, (char *)buf, MAXPATHL);
15492 if (len <= 0)
15493 break;
15494 buf[len] = NUL;
15495
15496 if (limit-- == 0)
15497 {
15498 vim_free(p);
15499 vim_free(remain);
15500 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 goto fail;
15503 }
15504
15505 /* Ensure that the result will have a trailing path separator
15506 * if the argument has one. */
15507 if (remain == NULL && has_trailing_pathsep)
15508 add_pathsep(buf);
15509
15510 /* Separate the first path component in the link value and
15511 * concatenate the remainders. */
15512 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15513 if (*q != NUL)
15514 {
15515 if (remain == NULL)
15516 remain = vim_strsave(q - 1);
15517 else
15518 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015519 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520 if (cpy != NULL)
15521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 vim_free(remain);
15523 remain = cpy;
15524 }
15525 }
15526 q[-1] = NUL;
15527 }
15528
15529 q = gettail(p);
15530 if (q > p && *q == NUL)
15531 {
15532 /* Ignore trailing path separator. */
15533 q[-1] = NUL;
15534 q = gettail(p);
15535 }
15536 if (q > p && !mch_isFullName(buf))
15537 {
15538 /* symlink is relative to directory of argument */
15539 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15540 if (cpy != NULL)
15541 {
15542 STRCPY(cpy, p);
15543 STRCPY(gettail(cpy), buf);
15544 vim_free(p);
15545 p = cpy;
15546 }
15547 }
15548 else
15549 {
15550 vim_free(p);
15551 p = vim_strsave(buf);
15552 }
15553 }
15554
15555 if (remain == NULL)
15556 break;
15557
15558 /* Append the first path component of "remain" to "p". */
15559 q = getnextcomp(remain + 1);
15560 len = q - remain - (*q != NUL);
15561 cpy = vim_strnsave(p, STRLEN(p) + len);
15562 if (cpy != NULL)
15563 {
15564 STRNCAT(cpy, remain, len);
15565 vim_free(p);
15566 p = cpy;
15567 }
15568 /* Shorten "remain". */
15569 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015570 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 else
15572 {
15573 vim_free(remain);
15574 remain = NULL;
15575 }
15576 }
15577
15578 /* If the result is a relative path name, make it explicitly relative to
15579 * the current directory if and only if the argument had this form. */
15580 if (!vim_ispathsep(*p))
15581 {
15582 if (is_relative_to_current
15583 && *p != NUL
15584 && !(p[0] == '.'
15585 && (p[1] == NUL
15586 || vim_ispathsep(p[1])
15587 || (p[1] == '.'
15588 && (p[2] == NUL
15589 || vim_ispathsep(p[2]))))))
15590 {
15591 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015592 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 if (cpy != NULL)
15594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 vim_free(p);
15596 p = cpy;
15597 }
15598 }
15599 else if (!is_relative_to_current)
15600 {
15601 /* Strip leading "./". */
15602 q = p;
15603 while (q[0] == '.' && vim_ispathsep(q[1]))
15604 q += 2;
15605 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015606 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 }
15608 }
15609
15610 /* Ensure that the result will have no trailing path separator
15611 * if the argument had none. But keep "/" or "//". */
15612 if (!has_trailing_pathsep)
15613 {
15614 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015615 if (after_pathsep(p, q))
15616 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 }
15618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 }
15621# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015622 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623# endif
15624#endif
15625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627
15628#ifdef HAVE_READLINK
15629fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015630 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015632 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633}
15634
15635/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 */
15638 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015639f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015640 typval_T *argvars;
15641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642{
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 list_T *l;
15644 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645
Bram Moolenaar0d660222005-01-07 21:51:51 +000015646 if (argvars[0].v_type != VAR_LIST)
15647 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015648 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015649 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015650 {
15651 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015652 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015653 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654 while (li != NULL)
15655 {
15656 ni = li->li_prev;
15657 list_append(l, li);
15658 li = ni;
15659 }
15660 rettv->vval.v_list = l;
15661 rettv->v_type = VAR_LIST;
15662 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015663 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665}
15666
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015667#define SP_NOMOVE 0x01 /* don't move cursor */
15668#define SP_REPEAT 0x02 /* repeat to find outer pair */
15669#define SP_RETCOUNT 0x04 /* return matchcount */
15670#define SP_SETPCMARK 0x08 /* set previous context mark */
15671#define SP_START 0x10 /* accept match at start position */
15672#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15673#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015674
Bram Moolenaar33570922005-01-25 22:26:29 +000015675static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676
15677/*
15678 * Get flags for a search function.
15679 * Possibly sets "p_ws".
15680 * Returns BACKWARD, FORWARD or zero (for an error).
15681 */
15682 static int
15683get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015684 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685 int *flagsp;
15686{
15687 int dir = FORWARD;
15688 char_u *flags;
15689 char_u nbuf[NUMBUFLEN];
15690 int mask;
15691
15692 if (varp->v_type != VAR_UNKNOWN)
15693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015694 flags = get_tv_string_buf_chk(varp, nbuf);
15695 if (flags == NULL)
15696 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697 while (*flags != NUL)
15698 {
15699 switch (*flags)
15700 {
15701 case 'b': dir = BACKWARD; break;
15702 case 'w': p_ws = TRUE; break;
15703 case 'W': p_ws = FALSE; break;
15704 default: mask = 0;
15705 if (flagsp != NULL)
15706 switch (*flags)
15707 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015708 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015709 case 'e': mask = SP_END; break;
15710 case 'm': mask = SP_RETCOUNT; break;
15711 case 'n': mask = SP_NOMOVE; break;
15712 case 'p': mask = SP_SUBPAT; break;
15713 case 'r': mask = SP_REPEAT; break;
15714 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015715 }
15716 if (mask == 0)
15717 {
15718 EMSG2(_(e_invarg2), flags);
15719 dir = 0;
15720 }
15721 else
15722 *flagsp |= mask;
15723 }
15724 if (dir == 0)
15725 break;
15726 ++flags;
15727 }
15728 }
15729 return dir;
15730}
15731
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015733 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015735 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015736search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015737 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015738 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015739 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015741 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 char_u *pat;
15743 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015744 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 int save_p_ws = p_ws;
15746 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015747 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015748 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015749 proftime_T tm;
15750#ifdef FEAT_RELTIME
15751 long time_limit = 0;
15752#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015753 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015754 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015757 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015758 if (dir == 0)
15759 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015760 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015761 if (flags & SP_START)
15762 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015763 if (flags & SP_END)
15764 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015765
Bram Moolenaar76929292008-01-06 19:07:36 +000015766 /* Optional arguments: line number to stop searching and timeout. */
15767 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015768 {
15769 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15770 if (lnum_stop < 0)
15771 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015772#ifdef FEAT_RELTIME
15773 if (argvars[3].v_type != VAR_UNKNOWN)
15774 {
15775 time_limit = get_tv_number_chk(&argvars[3], NULL);
15776 if (time_limit < 0)
15777 goto theend;
15778 }
15779#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015780 }
15781
Bram Moolenaar76929292008-01-06 19:07:36 +000015782#ifdef FEAT_RELTIME
15783 /* Set the time limit, if there is one. */
15784 profile_setlimit(time_limit, &tm);
15785#endif
15786
Bram Moolenaar231334e2005-07-25 20:46:57 +000015787 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015788 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015789 * Check to make sure only those flags are set.
15790 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15791 * flags cannot be set. Check for that condition also.
15792 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015793 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015794 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015796 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015797 goto theend;
15798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015800 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015801 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015802 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015803 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015805 if (flags & SP_SUBPAT)
15806 retval = subpatnum;
15807 else
15808 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015809 if (flags & SP_SETPCMARK)
15810 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015812 if (match_pos != NULL)
15813 {
15814 /* Store the match cursor position */
15815 match_pos->lnum = pos.lnum;
15816 match_pos->col = pos.col + 1;
15817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 /* "/$" will put the cursor after the end of the line, may need to
15819 * correct that here */
15820 check_cursor();
15821 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015822
15823 /* If 'n' flag is used: restore cursor position. */
15824 if (flags & SP_NOMOVE)
15825 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015826 else
15827 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015828theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015830
15831 return retval;
15832}
15833
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015834#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015835
15836/*
15837 * round() is not in C90, use ceil() or floor() instead.
15838 */
15839 float_T
15840vim_round(f)
15841 float_T f;
15842{
15843 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15844}
15845
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015846/*
15847 * "round({float})" function
15848 */
15849 static void
15850f_round(argvars, rettv)
15851 typval_T *argvars;
15852 typval_T *rettv;
15853{
15854 float_T f;
15855
15856 rettv->v_type = VAR_FLOAT;
15857 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015858 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015859 else
15860 rettv->vval.v_float = 0.0;
15861}
15862#endif
15863
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015864/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015865 * "screenattr()" function
15866 */
15867 static void
15868f_screenattr(argvars, rettv)
15869 typval_T *argvars UNUSED;
15870 typval_T *rettv;
15871{
15872 int row;
15873 int col;
15874 int c;
15875
15876 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15877 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15878 if (row < 0 || row >= screen_Rows
15879 || col < 0 || col >= screen_Columns)
15880 c = -1;
15881 else
15882 c = ScreenAttrs[LineOffset[row] + col];
15883 rettv->vval.v_number = c;
15884}
15885
15886/*
15887 * "screenchar()" function
15888 */
15889 static void
15890f_screenchar(argvars, rettv)
15891 typval_T *argvars UNUSED;
15892 typval_T *rettv;
15893{
15894 int row;
15895 int col;
15896 int off;
15897 int c;
15898
15899 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15900 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15901 if (row < 0 || row >= screen_Rows
15902 || col < 0 || col >= screen_Columns)
15903 c = -1;
15904 else
15905 {
15906 off = LineOffset[row] + col;
15907#ifdef FEAT_MBYTE
15908 if (enc_utf8 && ScreenLinesUC[off] != 0)
15909 c = ScreenLinesUC[off];
15910 else
15911#endif
15912 c = ScreenLines[off];
15913 }
15914 rettv->vval.v_number = c;
15915}
15916
15917/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015918 * "screencol()" function
15919 *
15920 * First column is 1 to be consistent with virtcol().
15921 */
15922 static void
15923f_screencol(argvars, rettv)
15924 typval_T *argvars UNUSED;
15925 typval_T *rettv;
15926{
15927 rettv->vval.v_number = screen_screencol() + 1;
15928}
15929
15930/*
15931 * "screenrow()" function
15932 */
15933 static void
15934f_screenrow(argvars, rettv)
15935 typval_T *argvars UNUSED;
15936 typval_T *rettv;
15937{
15938 rettv->vval.v_number = screen_screenrow() + 1;
15939}
15940
15941/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015942 * "search()" function
15943 */
15944 static void
15945f_search(argvars, rettv)
15946 typval_T *argvars;
15947 typval_T *rettv;
15948{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015949 int flags = 0;
15950
15951 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952}
15953
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015955 * "searchdecl()" function
15956 */
15957 static void
15958f_searchdecl(argvars, rettv)
15959 typval_T *argvars;
15960 typval_T *rettv;
15961{
15962 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015963 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015964 int error = FALSE;
15965 char_u *name;
15966
15967 rettv->vval.v_number = 1; /* default: FAIL */
15968
15969 name = get_tv_string_chk(&argvars[0]);
15970 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015971 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015972 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015973 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15974 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15975 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015976 if (!error && name != NULL)
15977 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015978 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015979}
15980
15981/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015982 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015984 static int
15985searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015986 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015987 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988{
15989 char_u *spat, *mpat, *epat;
15990 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 int dir;
15993 int flags = 0;
15994 char_u nbuf1[NUMBUFLEN];
15995 char_u nbuf2[NUMBUFLEN];
15996 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015997 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015998 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015999 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016002 spat = get_tv_string_chk(&argvars[0]);
16003 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16004 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16005 if (spat == NULL || mpat == NULL || epat == NULL)
16006 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 /* Handle the optional fourth argument: flags */
16009 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016010 if (dir == 0)
16011 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016012
16013 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016014 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16015 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016016 if ((flags & (SP_END | SP_SUBPAT)) != 0
16017 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016018 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016019 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016020 goto theend;
16021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016023 /* Using 'r' implies 'W', otherwise it doesn't work. */
16024 if (flags & SP_REPEAT)
16025 p_ws = FALSE;
16026
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016027 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016028 if (argvars[3].v_type == VAR_UNKNOWN
16029 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 skip = (char_u *)"";
16031 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016033 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016034 if (argvars[5].v_type != VAR_UNKNOWN)
16035 {
16036 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16037 if (lnum_stop < 0)
16038 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016039#ifdef FEAT_RELTIME
16040 if (argvars[6].v_type != VAR_UNKNOWN)
16041 {
16042 time_limit = get_tv_number_chk(&argvars[6], NULL);
16043 if (time_limit < 0)
16044 goto theend;
16045 }
16046#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016047 }
16048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016049 if (skip == NULL)
16050 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016052 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016053 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016054
16055theend:
16056 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016057
16058 return retval;
16059}
16060
16061/*
16062 * "searchpair()" function
16063 */
16064 static void
16065f_searchpair(argvars, rettv)
16066 typval_T *argvars;
16067 typval_T *rettv;
16068{
16069 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16070}
16071
16072/*
16073 * "searchpairpos()" function
16074 */
16075 static void
16076f_searchpairpos(argvars, rettv)
16077 typval_T *argvars;
16078 typval_T *rettv;
16079{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016080 pos_T match_pos;
16081 int lnum = 0;
16082 int col = 0;
16083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016084 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016085 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016086
16087 if (searchpair_cmn(argvars, &match_pos) > 0)
16088 {
16089 lnum = match_pos.lnum;
16090 col = match_pos.col;
16091 }
16092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016093 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16094 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016095}
16096
16097/*
16098 * Search for a start/middle/end thing.
16099 * Used by searchpair(), see its documentation for the details.
16100 * Returns 0 or -1 for no match,
16101 */
16102 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016103do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16104 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016105 char_u *spat; /* start pattern */
16106 char_u *mpat; /* middle pattern */
16107 char_u *epat; /* end pattern */
16108 int dir; /* BACKWARD or FORWARD */
16109 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016110 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016111 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016112 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016113 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016114{
16115 char_u *save_cpo;
16116 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16117 long retval = 0;
16118 pos_T pos;
16119 pos_T firstpos;
16120 pos_T foundpos;
16121 pos_T save_cursor;
16122 pos_T save_pos;
16123 int n;
16124 int r;
16125 int nest = 1;
16126 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016127 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016128 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016129
16130 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16131 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016132 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016133
Bram Moolenaar76929292008-01-06 19:07:36 +000016134#ifdef FEAT_RELTIME
16135 /* Set the time limit, if there is one. */
16136 profile_setlimit(time_limit, &tm);
16137#endif
16138
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016139 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16140 * start/middle/end (pat3, for the top pair). */
16141 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16142 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16143 if (pat2 == NULL || pat3 == NULL)
16144 goto theend;
16145 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16146 if (*mpat == NUL)
16147 STRCPY(pat3, pat2);
16148 else
16149 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16150 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016151 if (flags & SP_START)
16152 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016153
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 save_cursor = curwin->w_cursor;
16155 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016156 clearpos(&firstpos);
16157 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 pat = pat3;
16159 for (;;)
16160 {
16161 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016162 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16164 /* didn't find it or found the first match again: FAIL */
16165 break;
16166
16167 if (firstpos.lnum == 0)
16168 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016169 if (equalpos(pos, foundpos))
16170 {
16171 /* Found the same position again. Can happen with a pattern that
16172 * has "\zs" at the end and searching backwards. Advance one
16173 * character and try again. */
16174 if (dir == BACKWARD)
16175 decl(&pos);
16176 else
16177 incl(&pos);
16178 }
16179 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016181 /* clear the start flag to avoid getting stuck here */
16182 options &= ~SEARCH_START;
16183
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 /* If the skip pattern matches, ignore this match. */
16185 if (*skip != NUL)
16186 {
16187 save_pos = curwin->w_cursor;
16188 curwin->w_cursor = pos;
16189 r = eval_to_bool(skip, &err, NULL, FALSE);
16190 curwin->w_cursor = save_pos;
16191 if (err)
16192 {
16193 /* Evaluating {skip} caused an error, break here. */
16194 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016195 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 break;
16197 }
16198 if (r)
16199 continue;
16200 }
16201
16202 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16203 {
16204 /* Found end when searching backwards or start when searching
16205 * forward: nested pair. */
16206 ++nest;
16207 pat = pat2; /* nested, don't search for middle */
16208 }
16209 else
16210 {
16211 /* Found end when searching forward or start when searching
16212 * backward: end of (nested) pair; or found middle in outer pair. */
16213 if (--nest == 1)
16214 pat = pat3; /* outer level, search for middle */
16215 }
16216
16217 if (nest == 0)
16218 {
16219 /* Found the match: return matchcount or line number. */
16220 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016221 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016223 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016224 if (flags & SP_SETPCMARK)
16225 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 curwin->w_cursor = pos;
16227 if (!(flags & SP_REPEAT))
16228 break;
16229 nest = 1; /* search for next unmatched */
16230 }
16231 }
16232
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016233 if (match_pos != NULL)
16234 {
16235 /* Store the match cursor position */
16236 match_pos->lnum = curwin->w_cursor.lnum;
16237 match_pos->col = curwin->w_cursor.col + 1;
16238 }
16239
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016241 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 curwin->w_cursor = save_cursor;
16243
16244theend:
16245 vim_free(pat2);
16246 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016247 if (p_cpo == empty_option)
16248 p_cpo = save_cpo;
16249 else
16250 /* Darn, evaluating the {skip} expression changed the value. */
16251 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016252
16253 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016256/*
16257 * "searchpos()" function
16258 */
16259 static void
16260f_searchpos(argvars, rettv)
16261 typval_T *argvars;
16262 typval_T *rettv;
16263{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016264 pos_T match_pos;
16265 int lnum = 0;
16266 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016267 int n;
16268 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016269
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016270 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016271 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016272
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016273 n = search_cmn(argvars, &match_pos, &flags);
16274 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016275 {
16276 lnum = match_pos.lnum;
16277 col = match_pos.col;
16278 }
16279
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016280 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16281 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016282 if (flags & SP_SUBPAT)
16283 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016284}
16285
16286
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 static void
16288f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292#ifdef FEAT_CLIENTSERVER
16293 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 char_u *server = get_tv_string_chk(&argvars[0]);
16295 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016298 if (server == NULL || reply == NULL)
16299 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300 if (check_restricted() || check_secure())
16301 return;
16302# ifdef FEAT_X11
16303 if (check_connection() == FAIL)
16304 return;
16305# endif
16306
16307 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309 EMSG(_("E258: Unable to send to client"));
16310 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 rettv->vval.v_number = 0;
16313#else
16314 rettv->vval.v_number = -1;
16315#endif
16316}
16317
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318 static void
16319f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016322{
16323 char_u *r = NULL;
16324
16325#ifdef FEAT_CLIENTSERVER
16326# ifdef WIN32
16327 r = serverGetVimNames();
16328# else
16329 make_connection();
16330 if (X_DISPLAY != NULL)
16331 r = serverGetVimNames(X_DISPLAY);
16332# endif
16333#endif
16334 rettv->v_type = VAR_STRING;
16335 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336}
16337
16338/*
16339 * "setbufvar()" function
16340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016342f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016344 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345{
16346 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 char_u nbuf[NUMBUFLEN];
16351
16352 if (check_restricted() || check_secure())
16353 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016354 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16355 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016356 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 varp = &argvars[2];
16358
16359 if (buf != NULL && varname != NULL && varp != NULL)
16360 {
16361 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363
16364 if (*varname == '&')
16365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 long numval;
16367 char_u *strval;
16368 int error = FALSE;
16369
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371 numval = get_tv_number_chk(varp, &error);
16372 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016373 if (!error && strval != NULL)
16374 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 }
16376 else
16377 {
16378 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16379 if (bufvarname != NULL)
16380 {
16381 STRCPY(bufvarname, "b:");
16382 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016383 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 vim_free(bufvarname);
16385 }
16386 }
16387
16388 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391}
16392
16393/*
16394 * "setcmdpos()" function
16395 */
16396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016397f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 typval_T *argvars;
16399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016401 int pos = (int)get_tv_number(&argvars[0]) - 1;
16402
16403 if (pos >= 0)
16404 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405}
16406
16407/*
16408 * "setline()" function
16409 */
16410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016412 typval_T *argvars;
16413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414{
16415 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016416 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016417 list_T *l = NULL;
16418 listitem_T *li = NULL;
16419 long added = 0;
16420 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016422 lnum = get_tv_lnum(&argvars[0]);
16423 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016425 l = argvars[1].vval.v_list;
16426 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016428 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016430
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016431 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016432 for (;;)
16433 {
16434 if (l != NULL)
16435 {
16436 /* list argument, get next string */
16437 if (li == NULL)
16438 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016440 li = li->li_next;
16441 }
16442
16443 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016445 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016446
16447 /* When coming here from Insert mode, sync undo, so that this can be
16448 * undone separately from what was previously inserted. */
16449 if (u_sync_once == 2)
16450 {
16451 u_sync_once = 1; /* notify that u_sync() was called */
16452 u_sync(TRUE);
16453 }
16454
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016455 if (lnum <= curbuf->b_ml.ml_line_count)
16456 {
16457 /* existing line, replace it */
16458 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16459 {
16460 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016461 if (lnum == curwin->w_cursor.lnum)
16462 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016463 rettv->vval.v_number = 0; /* OK */
16464 }
16465 }
16466 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16467 {
16468 /* lnum is one past the last line, append the line */
16469 ++added;
16470 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16471 rettv->vval.v_number = 0; /* OK */
16472 }
16473
16474 if (l == NULL) /* only one string argument */
16475 break;
16476 ++lnum;
16477 }
16478
16479 if (added > 0)
16480 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481}
16482
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016483static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16484
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016486 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016487 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016488 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016489set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016490 win_T *wp UNUSED;
16491 typval_T *list_arg UNUSED;
16492 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016493 typval_T *rettv;
16494{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016495#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016496 char_u *act;
16497 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016498#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016499
Bram Moolenaar2641f772005-03-25 21:58:17 +000016500 rettv->vval.v_number = -1;
16501
16502#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016503 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016504 EMSG(_(e_listreq));
16505 else
16506 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016507 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016508
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016509 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016510 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016511 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016512 if (act == NULL)
16513 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016514 if (*act == 'a' || *act == 'r')
16515 action = *act;
16516 }
16517
Bram Moolenaar81484f42012-12-05 15:16:47 +010016518 if (l != NULL && set_errorlist(wp, l, action,
16519 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016520 rettv->vval.v_number = 0;
16521 }
16522#endif
16523}
16524
16525/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016526 * "setloclist()" function
16527 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016528 static void
16529f_setloclist(argvars, rettv)
16530 typval_T *argvars;
16531 typval_T *rettv;
16532{
16533 win_T *win;
16534
16535 rettv->vval.v_number = -1;
16536
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016537 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016538 if (win != NULL)
16539 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16540}
16541
16542/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016543 * "setmatches()" function
16544 */
16545 static void
16546f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016547 typval_T *argvars UNUSED;
16548 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016549{
16550#ifdef FEAT_SEARCH_EXTRA
16551 list_T *l;
16552 listitem_T *li;
16553 dict_T *d;
16554
16555 rettv->vval.v_number = -1;
16556 if (argvars[0].v_type != VAR_LIST)
16557 {
16558 EMSG(_(e_listreq));
16559 return;
16560 }
16561 if ((l = argvars[0].vval.v_list) != NULL)
16562 {
16563
16564 /* To some extent make sure that we are dealing with a list from
16565 * "getmatches()". */
16566 li = l->lv_first;
16567 while (li != NULL)
16568 {
16569 if (li->li_tv.v_type != VAR_DICT
16570 || (d = li->li_tv.vval.v_dict) == NULL)
16571 {
16572 EMSG(_(e_invarg));
16573 return;
16574 }
16575 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16576 && dict_find(d, (char_u *)"pattern", -1) != NULL
16577 && dict_find(d, (char_u *)"priority", -1) != NULL
16578 && dict_find(d, (char_u *)"id", -1) != NULL))
16579 {
16580 EMSG(_(e_invarg));
16581 return;
16582 }
16583 li = li->li_next;
16584 }
16585
16586 clear_matches(curwin);
16587 li = l->lv_first;
16588 while (li != NULL)
16589 {
16590 d = li->li_tv.vval.v_dict;
16591 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16592 get_dict_string(d, (char_u *)"pattern", FALSE),
16593 (int)get_dict_number(d, (char_u *)"priority"),
16594 (int)get_dict_number(d, (char_u *)"id"));
16595 li = li->li_next;
16596 }
16597 rettv->vval.v_number = 0;
16598 }
16599#endif
16600}
16601
16602/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016603 * "setpos()" function
16604 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016605 static void
16606f_setpos(argvars, rettv)
16607 typval_T *argvars;
16608 typval_T *rettv;
16609{
16610 pos_T pos;
16611 int fnum;
16612 char_u *name;
16613
Bram Moolenaar08250432008-02-13 11:42:46 +000016614 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016615 name = get_tv_string_chk(argvars);
16616 if (name != NULL)
16617 {
16618 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16619 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016620 if (--pos.col < 0)
16621 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016622 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016623 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016624 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016625 if (fnum == curbuf->b_fnum)
16626 {
16627 curwin->w_cursor = pos;
16628 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016629 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016630 }
16631 else
16632 EMSG(_(e_invarg));
16633 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016634 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16635 {
16636 /* set mark */
16637 if (setmark_pos(name[1], &pos, fnum) == OK)
16638 rettv->vval.v_number = 0;
16639 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016640 else
16641 EMSG(_(e_invarg));
16642 }
16643 }
16644}
16645
16646/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016647 * "setqflist()" function
16648 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016649 static void
16650f_setqflist(argvars, rettv)
16651 typval_T *argvars;
16652 typval_T *rettv;
16653{
16654 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16655}
16656
16657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 * "setreg()" function
16659 */
16660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 typval_T *argvars;
16663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664{
16665 int regname;
16666 char_u *strregname;
16667 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016668 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 int append;
16670 char_u yank_type;
16671 long block_len;
16672
16673 block_len = -1;
16674 yank_type = MAUTO;
16675 append = FALSE;
16676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016677 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 if (strregname == NULL)
16681 return; /* type error; errmsg already given */
16682 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683 if (regname == 0 || regname == '@')
16684 regname = '"';
16685 else if (regname == '=')
16686 return;
16687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016688 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016690 stropt = get_tv_string_chk(&argvars[2]);
16691 if (stropt == NULL)
16692 return; /* type error */
16693 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 switch (*stropt)
16695 {
16696 case 'a': case 'A': /* append */
16697 append = TRUE;
16698 break;
16699 case 'v': case 'c': /* character-wise selection */
16700 yank_type = MCHAR;
16701 break;
16702 case 'V': case 'l': /* line-wise selection */
16703 yank_type = MLINE;
16704 break;
16705#ifdef FEAT_VISUAL
16706 case 'b': case Ctrl_V: /* block-wise selection */
16707 yank_type = MBLOCK;
16708 if (VIM_ISDIGIT(stropt[1]))
16709 {
16710 ++stropt;
16711 block_len = getdigits(&stropt) - 1;
16712 --stropt;
16713 }
16714 break;
16715#endif
16716 }
16717 }
16718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016719 strval = get_tv_string_chk(&argvars[1]);
16720 if (strval != NULL)
16721 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016723 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724}
16725
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016726/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016727 * "settabvar()" function
16728 */
16729 static void
16730f_settabvar(argvars, rettv)
16731 typval_T *argvars;
16732 typval_T *rettv;
16733{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016734#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016735 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016736 tabpage_T *tp;
16737#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016738 char_u *varname, *tabvarname;
16739 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016740
16741 rettv->vval.v_number = 0;
16742
16743 if (check_restricted() || check_secure())
16744 return;
16745
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016746#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016747 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016748#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016749 varname = get_tv_string_chk(&argvars[1]);
16750 varp = &argvars[2];
16751
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016752 if (varname != NULL && varp != NULL
16753#ifdef FEAT_WINDOWS
16754 && tp != NULL
16755#endif
16756 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016757 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016758#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016759 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016760 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016761#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016762
16763 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16764 if (tabvarname != NULL)
16765 {
16766 STRCPY(tabvarname, "t:");
16767 STRCPY(tabvarname + 2, varname);
16768 set_var(tabvarname, varp, TRUE);
16769 vim_free(tabvarname);
16770 }
16771
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016772#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016773 /* Restore current tabpage */
16774 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016775 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016776#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016777 }
16778}
16779
16780/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016781 * "settabwinvar()" function
16782 */
16783 static void
16784f_settabwinvar(argvars, rettv)
16785 typval_T *argvars;
16786 typval_T *rettv;
16787{
16788 setwinvar(argvars, rettv, 1);
16789}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790
16791/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016792 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016795f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016796 typval_T *argvars;
16797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016799 setwinvar(argvars, rettv, 0);
16800}
16801
16802/*
16803 * "setwinvar()" and "settabwinvar()" functions
16804 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016805
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016806 static void
16807setwinvar(argvars, rettv, off)
16808 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016809 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016810 int off;
16811{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 win_T *win;
16813#ifdef FEAT_WINDOWS
16814 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016815 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816#endif
16817 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016818 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016820 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821
16822 if (check_restricted() || check_secure())
16823 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016824
16825#ifdef FEAT_WINDOWS
16826 if (off == 1)
16827 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16828 else
16829 tp = curtab;
16830#endif
16831 win = find_win_by_nr(&argvars[off], tp);
16832 varname = get_tv_string_chk(&argvars[off + 1]);
16833 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834
16835 if (win != NULL && varname != NULL && varp != NULL)
16836 {
16837#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016838 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016839 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840#endif
16841
16842 if (*varname == '&')
16843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016844 long numval;
16845 char_u *strval;
16846 int error = FALSE;
16847
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016849 numval = get_tv_number_chk(varp, &error);
16850 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016851 if (!error && strval != NULL)
16852 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 }
16854 else
16855 {
16856 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16857 if (winvarname != NULL)
16858 {
16859 STRCPY(winvarname, "w:");
16860 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016861 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 vim_free(winvarname);
16863 }
16864 }
16865
16866#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016867 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868#endif
16869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870}
16871
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016872#ifdef FEAT_CRYPT
16873/*
16874 * "sha256({string})" function
16875 */
16876 static void
16877f_sha256(argvars, rettv)
16878 typval_T *argvars;
16879 typval_T *rettv;
16880{
16881 char_u *p;
16882
16883 p = get_tv_string(&argvars[0]);
16884 rettv->vval.v_string = vim_strsave(
16885 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16886 rettv->v_type = VAR_STRING;
16887}
16888#endif /* FEAT_CRYPT */
16889
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016891 * "shellescape({string})" function
16892 */
16893 static void
16894f_shellescape(argvars, rettv)
16895 typval_T *argvars;
16896 typval_T *rettv;
16897{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016898 rettv->vval.v_string = vim_strsave_shellescape(
16899 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016900 rettv->v_type = VAR_STRING;
16901}
16902
16903/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016904 * shiftwidth() function
16905 */
16906 static void
16907f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016908 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016909 typval_T *rettv;
16910{
16911 rettv->vval.v_number = get_sw_value();
16912}
16913
16914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 */
16917 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016918f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 typval_T *argvars;
16920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 p = get_tv_string(&argvars[0]);
16925 rettv->vval.v_string = vim_strsave(p);
16926 simplify_filename(rettv->vval.v_string); /* simplify in place */
16927 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928}
16929
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016930#ifdef FEAT_FLOAT
16931/*
16932 * "sin()" function
16933 */
16934 static void
16935f_sin(argvars, rettv)
16936 typval_T *argvars;
16937 typval_T *rettv;
16938{
16939 float_T f;
16940
16941 rettv->v_type = VAR_FLOAT;
16942 if (get_float_arg(argvars, &f) == OK)
16943 rettv->vval.v_float = sin(f);
16944 else
16945 rettv->vval.v_float = 0.0;
16946}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016947
16948/*
16949 * "sinh()" function
16950 */
16951 static void
16952f_sinh(argvars, rettv)
16953 typval_T *argvars;
16954 typval_T *rettv;
16955{
16956 float_T f;
16957
16958 rettv->v_type = VAR_FLOAT;
16959 if (get_float_arg(argvars, &f) == OK)
16960 rettv->vval.v_float = sinh(f);
16961 else
16962 rettv->vval.v_float = 0.0;
16963}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016964#endif
16965
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966static int
16967#ifdef __BORLANDC__
16968 _RTLENTRYF
16969#endif
16970 item_compare __ARGS((const void *s1, const void *s2));
16971static int
16972#ifdef __BORLANDC__
16973 _RTLENTRYF
16974#endif
16975 item_compare2 __ARGS((const void *s1, const void *s2));
16976
16977static int item_compare_ic;
16978static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016979static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981#define ITEM_COMPARE_FAIL 999
16982
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 static int
16987#ifdef __BORLANDC__
16988_RTLENTRYF
16989#endif
16990item_compare(s1, s2)
16991 const void *s1;
16992 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 char_u *p1, *p2;
16995 char_u *tofree1, *tofree2;
16996 int res;
16997 char_u numbuf1[NUMBUFLEN];
16998 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017000 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17001 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017002 if (p1 == NULL)
17003 p1 = (char_u *)"";
17004 if (p2 == NULL)
17005 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 if (item_compare_ic)
17007 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 res = STRCMP(p1, p2);
17010 vim_free(tofree1);
17011 vim_free(tofree2);
17012 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013}
17014
17015 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016#ifdef __BORLANDC__
17017_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019item_compare2(s1, s2)
17020 const void *s1;
17021 const void *s2;
17022{
17023 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017024 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017025 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 /* shortcut after failure in previous call; compare all items equal */
17029 if (item_compare_func_err)
17030 return 0;
17031
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017032 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17033 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017034 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17035 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017036
17037 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017038 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017039 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17040 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041 clear_tv(&argv[0]);
17042 clear_tv(&argv[1]);
17043
17044 if (res == FAIL)
17045 res = ITEM_COMPARE_FAIL;
17046 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17048 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017049 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050 clear_tv(&rettv);
17051 return res;
17052}
17053
17054/*
17055 * "sort({list})" function
17056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017059 typval_T *argvars;
17060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061{
Bram Moolenaar33570922005-01-25 22:26:29 +000017062 list_T *l;
17063 listitem_T *li;
17064 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065 long len;
17066 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068 if (argvars[0].v_type != VAR_LIST)
17069 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 else
17071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017073 if (l == NULL || tv_check_lock(l->lv_lock,
17074 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075 return;
17076 rettv->vval.v_list = l;
17077 rettv->v_type = VAR_LIST;
17078 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080 len = list_len(l);
17081 if (len <= 1)
17082 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084 item_compare_ic = FALSE;
17085 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017086 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087 if (argvars[1].v_type != VAR_UNKNOWN)
17088 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017089 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017091 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 else
17093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017094 int error = FALSE;
17095
17096 i = get_tv_number_chk(&argvars[1], &error);
17097 if (error)
17098 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 if (i == 1)
17100 item_compare_ic = TRUE;
17101 else
17102 item_compare_func = get_tv_string(&argvars[1]);
17103 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017104
17105 if (argvars[2].v_type != VAR_UNKNOWN)
17106 {
17107 /* optional third argument: {dict} */
17108 if (argvars[2].v_type != VAR_DICT)
17109 {
17110 EMSG(_(e_dictreq));
17111 return;
17112 }
17113 item_compare_selfdict = argvars[2].vval.v_dict;
17114 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119 if (ptrs == NULL)
17120 return;
17121 i = 0;
17122 for (li = l->lv_first; li != NULL; li = li->li_next)
17123 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017125 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 /* test the compare function */
17127 if (item_compare_func != NULL
17128 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17129 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017130 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132 {
17133 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017134 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 item_compare_func == NULL ? item_compare : item_compare2);
17136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 if (!item_compare_func_err)
17138 {
17139 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017140 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 l->lv_len = 0;
17142 for (i = 0; i < len; ++i)
17143 list_append(l, ptrs[i]);
17144 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 }
17146
17147 vim_free(ptrs);
17148 }
17149}
17150
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017151/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017152 * "soundfold({word})" function
17153 */
17154 static void
17155f_soundfold(argvars, rettv)
17156 typval_T *argvars;
17157 typval_T *rettv;
17158{
17159 char_u *s;
17160
17161 rettv->v_type = VAR_STRING;
17162 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017163#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017164 rettv->vval.v_string = eval_soundfold(s);
17165#else
17166 rettv->vval.v_string = vim_strsave(s);
17167#endif
17168}
17169
17170/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017171 * "spellbadword()" function
17172 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017173 static void
17174f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017175 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017176 typval_T *rettv;
17177{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017178 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017179 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017180 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017181
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017182 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017183 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017184
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017185#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017186 if (argvars[0].v_type == VAR_UNKNOWN)
17187 {
17188 /* Find the start and length of the badly spelled word. */
17189 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17190 if (len != 0)
17191 word = ml_get_cursor();
17192 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017193 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017194 {
17195 char_u *str = get_tv_string_chk(&argvars[0]);
17196 int capcol = -1;
17197
17198 if (str != NULL)
17199 {
17200 /* Check the argument for spelling. */
17201 while (*str != NUL)
17202 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017203 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017204 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017205 {
17206 word = str;
17207 break;
17208 }
17209 str += len;
17210 }
17211 }
17212 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017213#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017214
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017215 list_append_string(rettv->vval.v_list, word, len);
17216 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017217 attr == HLF_SPB ? "bad" :
17218 attr == HLF_SPR ? "rare" :
17219 attr == HLF_SPL ? "local" :
17220 attr == HLF_SPC ? "caps" :
17221 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017222}
17223
17224/*
17225 * "spellsuggest()" function
17226 */
17227 static void
17228f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017229 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017230 typval_T *rettv;
17231{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017232#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017233 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017234 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017235 int maxcount;
17236 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017237 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017238 listitem_T *li;
17239 int need_capital = FALSE;
17240#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017241
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017242 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017243 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017244
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017245#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017246 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017247 {
17248 str = get_tv_string(&argvars[0]);
17249 if (argvars[1].v_type != VAR_UNKNOWN)
17250 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017251 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017252 if (maxcount <= 0)
17253 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017254 if (argvars[2].v_type != VAR_UNKNOWN)
17255 {
17256 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17257 if (typeerr)
17258 return;
17259 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017260 }
17261 else
17262 maxcount = 25;
17263
Bram Moolenaar4770d092006-01-12 23:22:24 +000017264 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017265
17266 for (i = 0; i < ga.ga_len; ++i)
17267 {
17268 str = ((char_u **)ga.ga_data)[i];
17269
17270 li = listitem_alloc();
17271 if (li == NULL)
17272 vim_free(str);
17273 else
17274 {
17275 li->li_tv.v_type = VAR_STRING;
17276 li->li_tv.v_lock = 0;
17277 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017278 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017279 }
17280 }
17281 ga_clear(&ga);
17282 }
17283#endif
17284}
17285
Bram Moolenaar0d660222005-01-07 21:51:51 +000017286 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017287f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017288 typval_T *argvars;
17289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017290{
17291 char_u *str;
17292 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017293 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017294 regmatch_T regmatch;
17295 char_u patbuf[NUMBUFLEN];
17296 char_u *save_cpo;
17297 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017299 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301
17302 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17303 save_cpo = p_cpo;
17304 p_cpo = (char_u *)"";
17305
17306 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017307 if (argvars[1].v_type != VAR_UNKNOWN)
17308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017309 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17310 if (pat == NULL)
17311 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017312 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017313 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017314 }
17315 if (pat == NULL || *pat == NUL)
17316 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017318 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017320 if (typeerr)
17321 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322
Bram Moolenaar0d660222005-01-07 21:51:51 +000017323 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17324 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017326 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017327 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017328 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017329 if (*str == NUL)
17330 match = FALSE; /* empty item at the end */
17331 else
17332 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333 if (match)
17334 end = regmatch.startp[0];
17335 else
17336 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017337 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17338 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017340 if (list_append_string(rettv->vval.v_list, str,
17341 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017342 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343 }
17344 if (!match)
17345 break;
17346 /* Advance to just after the match. */
17347 if (regmatch.endp[0] > str)
17348 col = 0;
17349 else
17350 {
17351 /* Don't get stuck at the same match. */
17352#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017353 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354#else
17355 col = 1;
17356#endif
17357 }
17358 str = regmatch.endp[0];
17359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360
Bram Moolenaar473de612013-06-08 18:19:48 +020017361 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365}
17366
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017367#ifdef FEAT_FLOAT
17368/*
17369 * "sqrt()" function
17370 */
17371 static void
17372f_sqrt(argvars, rettv)
17373 typval_T *argvars;
17374 typval_T *rettv;
17375{
17376 float_T f;
17377
17378 rettv->v_type = VAR_FLOAT;
17379 if (get_float_arg(argvars, &f) == OK)
17380 rettv->vval.v_float = sqrt(f);
17381 else
17382 rettv->vval.v_float = 0.0;
17383}
17384
17385/*
17386 * "str2float()" function
17387 */
17388 static void
17389f_str2float(argvars, rettv)
17390 typval_T *argvars;
17391 typval_T *rettv;
17392{
17393 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17394
17395 if (*p == '+')
17396 p = skipwhite(p + 1);
17397 (void)string2float(p, &rettv->vval.v_float);
17398 rettv->v_type = VAR_FLOAT;
17399}
17400#endif
17401
Bram Moolenaar2c932302006-03-18 21:42:09 +000017402/*
17403 * "str2nr()" function
17404 */
17405 static void
17406f_str2nr(argvars, rettv)
17407 typval_T *argvars;
17408 typval_T *rettv;
17409{
17410 int base = 10;
17411 char_u *p;
17412 long n;
17413
17414 if (argvars[1].v_type != VAR_UNKNOWN)
17415 {
17416 base = get_tv_number(&argvars[1]);
17417 if (base != 8 && base != 10 && base != 16)
17418 {
17419 EMSG(_(e_invarg));
17420 return;
17421 }
17422 }
17423
17424 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017425 if (*p == '+')
17426 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017427 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17428 rettv->vval.v_number = n;
17429}
17430
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431#ifdef HAVE_STRFTIME
17432/*
17433 * "strftime({format}[, {time}])" function
17434 */
17435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 typval_T *argvars;
17438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439{
17440 char_u result_buf[256];
17441 struct tm *curtime;
17442 time_t seconds;
17443 char_u *p;
17444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017445 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017447 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017448 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449 seconds = time(NULL);
17450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017451 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 curtime = localtime(&seconds);
17453 /* MSVC returns NULL for an invalid value of seconds. */
17454 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017455 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 else
17457 {
17458# ifdef FEAT_MBYTE
17459 vimconv_T conv;
17460 char_u *enc;
17461
17462 conv.vc_type = CONV_NONE;
17463 enc = enc_locale();
17464 convert_setup(&conv, p_enc, enc);
17465 if (conv.vc_type != CONV_NONE)
17466 p = string_convert(&conv, p, NULL);
17467# endif
17468 if (p != NULL)
17469 (void)strftime((char *)result_buf, sizeof(result_buf),
17470 (char *)p, curtime);
17471 else
17472 result_buf[0] = NUL;
17473
17474# ifdef FEAT_MBYTE
17475 if (conv.vc_type != CONV_NONE)
17476 vim_free(p);
17477 convert_setup(&conv, enc, p_enc);
17478 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 else
17481# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017482 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483
17484# ifdef FEAT_MBYTE
17485 /* Release conversion descriptors */
17486 convert_setup(&conv, NULL, NULL);
17487 vim_free(enc);
17488# endif
17489 }
17490}
17491#endif
17492
17493/*
17494 * "stridx()" function
17495 */
17496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 typval_T *argvars;
17499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500{
17501 char_u buf[NUMBUFLEN];
17502 char_u *needle;
17503 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017508 needle = get_tv_string_chk(&argvars[1]);
17509 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017511 if (needle == NULL || haystack == NULL)
17512 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513
Bram Moolenaar33570922005-01-25 22:26:29 +000017514 if (argvars[2].v_type != VAR_UNKNOWN)
17515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 int error = FALSE;
17517
17518 start_idx = get_tv_number_chk(&argvars[2], &error);
17519 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017521 if (start_idx >= 0)
17522 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017523 }
17524
17525 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17526 if (pos != NULL)
17527 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528}
17529
17530/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017531 * "string()" function
17532 */
17533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017535 typval_T *argvars;
17536 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017537{
17538 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017539 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017541 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017542 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017543 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017544 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546}
17547
17548/*
17549 * "strlen()" function
17550 */
17551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017553 typval_T *argvars;
17554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017556 rettv->vval.v_number = (varnumber_T)(STRLEN(
17557 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558}
17559
17560/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017561 * "strchars()" function
17562 */
17563 static void
17564f_strchars(argvars, rettv)
17565 typval_T *argvars;
17566 typval_T *rettv;
17567{
17568 char_u *s = get_tv_string(&argvars[0]);
17569#ifdef FEAT_MBYTE
17570 varnumber_T len = 0;
17571
17572 while (*s != NUL)
17573 {
17574 mb_cptr2char_adv(&s);
17575 ++len;
17576 }
17577 rettv->vval.v_number = len;
17578#else
17579 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17580#endif
17581}
17582
17583/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017584 * "strdisplaywidth()" function
17585 */
17586 static void
17587f_strdisplaywidth(argvars, rettv)
17588 typval_T *argvars;
17589 typval_T *rettv;
17590{
17591 char_u *s = get_tv_string(&argvars[0]);
17592 int col = 0;
17593
17594 if (argvars[1].v_type != VAR_UNKNOWN)
17595 col = get_tv_number(&argvars[1]);
17596
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017597 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017598}
17599
17600/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017601 * "strwidth()" function
17602 */
17603 static void
17604f_strwidth(argvars, rettv)
17605 typval_T *argvars;
17606 typval_T *rettv;
17607{
17608 char_u *s = get_tv_string(&argvars[0]);
17609
17610 rettv->vval.v_number = (varnumber_T)(
17611#ifdef FEAT_MBYTE
17612 mb_string2cells(s, -1)
17613#else
17614 STRLEN(s)
17615#endif
17616 );
17617}
17618
17619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 * "strpart()" function
17621 */
17622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 typval_T *argvars;
17625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
17627 char_u *p;
17628 int n;
17629 int len;
17630 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017631 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017633 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 slen = (int)STRLEN(p);
17635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017636 n = get_tv_number_chk(&argvars[1], &error);
17637 if (error)
17638 len = 0;
17639 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017640 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 else
17642 len = slen - n; /* default len: all bytes that are available. */
17643
17644 /*
17645 * Only return the overlap between the specified part and the actual
17646 * string.
17647 */
17648 if (n < 0)
17649 {
17650 len += n;
17651 n = 0;
17652 }
17653 else if (n > slen)
17654 n = slen;
17655 if (len < 0)
17656 len = 0;
17657 else if (n + len > slen)
17658 len = slen - n;
17659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017660 rettv->v_type = VAR_STRING;
17661 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662}
17663
17664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017665 * "strridx()" function
17666 */
17667 static void
17668f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 typval_T *argvars;
17670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017671{
17672 char_u buf[NUMBUFLEN];
17673 char_u *needle;
17674 char_u *haystack;
17675 char_u *rest;
17676 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017677 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 needle = get_tv_string_chk(&argvars[1]);
17680 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681
17682 rettv->vval.v_number = -1;
17683 if (needle == NULL || haystack == NULL)
17684 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017685
17686 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017687 if (argvars[2].v_type != VAR_UNKNOWN)
17688 {
17689 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017690 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017691 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017692 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017693 }
17694 else
17695 end_idx = haystack_len;
17696
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017698 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017699 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017700 lastmatch = haystack + end_idx;
17701 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017703 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017704 for (rest = haystack; *rest != '\0'; ++rest)
17705 {
17706 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017707 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708 break;
17709 lastmatch = rest;
17710 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017711 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712
17713 if (lastmatch == NULL)
17714 rettv->vval.v_number = -1;
17715 else
17716 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17717}
17718
17719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 * "strtrans()" function
17721 */
17722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017723f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017724 typval_T *argvars;
17725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727 rettv->v_type = VAR_STRING;
17728 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729}
17730
17731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732 * "submatch()" function
17733 */
17734 static void
17735f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 typval_T *argvars;
17737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738{
17739 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017740 rettv->vval.v_string =
17741 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742}
17743
17744/*
17745 * "substitute()" function
17746 */
17747 static void
17748f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017749 typval_T *argvars;
17750 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017751{
17752 char_u patbuf[NUMBUFLEN];
17753 char_u subbuf[NUMBUFLEN];
17754 char_u flagsbuf[NUMBUFLEN];
17755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 char_u *str = get_tv_string_chk(&argvars[0]);
17757 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17758 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17759 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17760
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017762 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17763 rettv->vval.v_string = NULL;
17764 else
17765 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766}
17767
17768/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017769 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017773 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775{
17776 int id = 0;
17777#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017778 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 long col;
17780 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017781 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017783 lnum = get_tv_lnum(argvars); /* -1 on type error */
17784 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17785 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017788 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017789 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790#endif
17791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793}
17794
17795/*
17796 * "synIDattr(id, what [, mode])" function
17797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017799f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802{
17803 char_u *p = NULL;
17804#ifdef FEAT_SYN_HL
17805 int id;
17806 char_u *what;
17807 char_u *mode;
17808 char_u modebuf[NUMBUFLEN];
17809 int modec;
17810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017811 id = get_tv_number(&argvars[0]);
17812 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017813 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017815 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017817 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 modec = 0; /* replace invalid with current */
17819 }
17820 else
17821 {
17822#ifdef FEAT_GUI
17823 if (gui.in_use)
17824 modec = 'g';
17825 else
17826#endif
17827 if (t_colors > 1)
17828 modec = 'c';
17829 else
17830 modec = 't';
17831 }
17832
17833
17834 switch (TOLOWER_ASC(what[0]))
17835 {
17836 case 'b':
17837 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17838 p = highlight_color(id, what, modec);
17839 else /* bold */
17840 p = highlight_has_attr(id, HL_BOLD, modec);
17841 break;
17842
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017843 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 p = highlight_color(id, what, modec);
17845 break;
17846
17847 case 'i':
17848 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17849 p = highlight_has_attr(id, HL_INVERSE, modec);
17850 else /* italic */
17851 p = highlight_has_attr(id, HL_ITALIC, modec);
17852 break;
17853
17854 case 'n': /* name */
17855 p = get_highlight_name(NULL, id - 1);
17856 break;
17857
17858 case 'r': /* reverse */
17859 p = highlight_has_attr(id, HL_INVERSE, modec);
17860 break;
17861
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017862 case 's':
17863 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17864 p = highlight_color(id, what, modec);
17865 else /* standout */
17866 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 break;
17868
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017869 case 'u':
17870 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17871 /* underline */
17872 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17873 else
17874 /* undercurl */
17875 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 break;
17877 }
17878
17879 if (p != NULL)
17880 p = vim_strsave(p);
17881#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017882 rettv->v_type = VAR_STRING;
17883 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884}
17885
17886/*
17887 * "synIDtrans(id)" function
17888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017891 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893{
17894 int id;
17895
17896#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017897 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898
17899 if (id > 0)
17900 id = syn_get_final_id(id);
17901 else
17902#endif
17903 id = 0;
17904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017905 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906}
17907
17908/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017909 * "synconcealed(lnum, col)" function
17910 */
17911 static void
17912f_synconcealed(argvars, rettv)
17913 typval_T *argvars UNUSED;
17914 typval_T *rettv;
17915{
17916#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17917 long lnum;
17918 long col;
17919 int syntax_flags = 0;
17920 int cchar;
17921 int matchid = 0;
17922 char_u str[NUMBUFLEN];
17923#endif
17924
17925 rettv->v_type = VAR_LIST;
17926 rettv->vval.v_list = NULL;
17927
17928#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17929 lnum = get_tv_lnum(argvars); /* -1 on type error */
17930 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17931
17932 vim_memset(str, NUL, sizeof(str));
17933
17934 if (rettv_list_alloc(rettv) != FAIL)
17935 {
17936 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17937 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17938 && curwin->w_p_cole > 0)
17939 {
17940 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17941 syntax_flags = get_syntax_info(&matchid);
17942
17943 /* get the conceal character */
17944 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17945 {
17946 cchar = syn_get_sub_char();
17947 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17948 cchar = lcs_conceal;
17949 if (cchar != NUL)
17950 {
17951# ifdef FEAT_MBYTE
17952 if (has_mbyte)
17953 (*mb_char2bytes)(cchar, str);
17954 else
17955# endif
17956 str[0] = cchar;
17957 }
17958 }
17959 }
17960
17961 list_append_number(rettv->vval.v_list,
17962 (syntax_flags & HL_CONCEAL) != 0);
17963 /* -1 to auto-determine strlen */
17964 list_append_string(rettv->vval.v_list, str, -1);
17965 list_append_number(rettv->vval.v_list, matchid);
17966 }
17967#endif
17968}
17969
17970/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017971 * "synstack(lnum, col)" function
17972 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017973 static void
17974f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017975 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017976 typval_T *rettv;
17977{
17978#ifdef FEAT_SYN_HL
17979 long lnum;
17980 long col;
17981 int i;
17982 int id;
17983#endif
17984
17985 rettv->v_type = VAR_LIST;
17986 rettv->vval.v_list = NULL;
17987
17988#ifdef FEAT_SYN_HL
17989 lnum = get_tv_lnum(argvars); /* -1 on type error */
17990 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17991
17992 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017993 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017994 && rettv_list_alloc(rettv) != FAIL)
17995 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017996 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017997 for (i = 0; ; ++i)
17998 {
17999 id = syn_get_stack_item(i);
18000 if (id < 0)
18001 break;
18002 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18003 break;
18004 }
18005 }
18006#endif
18007}
18008
18009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 * "system()" function
18011 */
18012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018013f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018014 typval_T *argvars;
18015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018017 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018019 char_u *infile = NULL;
18020 char_u buf[NUMBUFLEN];
18021 int err = FALSE;
18022 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018024 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018025 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018027 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018028 {
18029 /*
18030 * Write the string to a temp file, to be used for input of the shell
18031 * command.
18032 */
18033 if ((infile = vim_tempname('i')) == NULL)
18034 {
18035 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018036 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018037 }
18038
18039 fd = mch_fopen((char *)infile, WRITEBIN);
18040 if (fd == NULL)
18041 {
18042 EMSG2(_(e_notopen), infile);
18043 goto done;
18044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018045 p = get_tv_string_buf_chk(&argvars[1], buf);
18046 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018047 {
18048 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018049 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018050 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018051 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18052 err = TRUE;
18053 if (fclose(fd) != 0)
18054 err = TRUE;
18055 if (err)
18056 {
18057 EMSG(_("E677: Error writing temp file"));
18058 goto done;
18059 }
18060 }
18061
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018062 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18063 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018064
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065#ifdef USE_CR
18066 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018067 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 {
18069 char_u *s;
18070
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018071 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 {
18073 if (*s == CAR)
18074 *s = NL;
18075 }
18076 }
18077#else
18078# ifdef USE_CRNL
18079 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018080 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 {
18082 char_u *s, *d;
18083
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018084 d = res;
18085 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 {
18087 if (s[0] == CAR && s[1] == NL)
18088 ++s;
18089 *d++ = *s;
18090 }
18091 *d = NUL;
18092 }
18093# endif
18094#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018095
18096done:
18097 if (infile != NULL)
18098 {
18099 mch_remove(infile);
18100 vim_free(infile);
18101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018102 rettv->v_type = VAR_STRING;
18103 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104}
18105
18106/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018107 * "tabpagebuflist()" function
18108 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018109 static void
18110f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018111 typval_T *argvars UNUSED;
18112 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018113{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018114#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018115 tabpage_T *tp;
18116 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018117
18118 if (argvars[0].v_type == VAR_UNKNOWN)
18119 wp = firstwin;
18120 else
18121 {
18122 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18123 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018124 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018125 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018126 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018127 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018128 for (; wp != NULL; wp = wp->w_next)
18129 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018130 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018131 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018132 }
18133#endif
18134}
18135
18136
18137/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018138 * "tabpagenr()" function
18139 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018140 static void
18141f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018142 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018143 typval_T *rettv;
18144{
18145 int nr = 1;
18146#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018147 char_u *arg;
18148
18149 if (argvars[0].v_type != VAR_UNKNOWN)
18150 {
18151 arg = get_tv_string_chk(&argvars[0]);
18152 nr = 0;
18153 if (arg != NULL)
18154 {
18155 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018156 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018157 else
18158 EMSG2(_(e_invexpr2), arg);
18159 }
18160 }
18161 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018162 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018163#endif
18164 rettv->vval.v_number = nr;
18165}
18166
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018167
18168#ifdef FEAT_WINDOWS
18169static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18170
18171/*
18172 * Common code for tabpagewinnr() and winnr().
18173 */
18174 static int
18175get_winnr(tp, argvar)
18176 tabpage_T *tp;
18177 typval_T *argvar;
18178{
18179 win_T *twin;
18180 int nr = 1;
18181 win_T *wp;
18182 char_u *arg;
18183
18184 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18185 if (argvar->v_type != VAR_UNKNOWN)
18186 {
18187 arg = get_tv_string_chk(argvar);
18188 if (arg == NULL)
18189 nr = 0; /* type error; errmsg already given */
18190 else if (STRCMP(arg, "$") == 0)
18191 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18192 else if (STRCMP(arg, "#") == 0)
18193 {
18194 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18195 if (twin == NULL)
18196 nr = 0;
18197 }
18198 else
18199 {
18200 EMSG2(_(e_invexpr2), arg);
18201 nr = 0;
18202 }
18203 }
18204
18205 if (nr > 0)
18206 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18207 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018208 {
18209 if (wp == NULL)
18210 {
18211 /* didn't find it in this tabpage */
18212 nr = 0;
18213 break;
18214 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018215 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018216 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018217 return nr;
18218}
18219#endif
18220
18221/*
18222 * "tabpagewinnr()" function
18223 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018224 static void
18225f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018226 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018227 typval_T *rettv;
18228{
18229 int nr = 1;
18230#ifdef FEAT_WINDOWS
18231 tabpage_T *tp;
18232
18233 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18234 if (tp == NULL)
18235 nr = 0;
18236 else
18237 nr = get_winnr(tp, &argvars[1]);
18238#endif
18239 rettv->vval.v_number = nr;
18240}
18241
18242
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018243/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018244 * "tagfiles()" function
18245 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018246 static void
18247f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018248 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018249 typval_T *rettv;
18250{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018251 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018252 tagname_T tn;
18253 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018254
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018255 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018256 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018257 fname = alloc(MAXPATHL);
18258 if (fname == NULL)
18259 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018260
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018261 for (first = TRUE; ; first = FALSE)
18262 if (get_tagfname(&tn, first, fname) == FAIL
18263 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018264 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018265 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018266 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018267}
18268
18269/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018270 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018271 */
18272 static void
18273f_taglist(argvars, rettv)
18274 typval_T *argvars;
18275 typval_T *rettv;
18276{
18277 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018278
18279 tag_pattern = get_tv_string(&argvars[0]);
18280
18281 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018282 if (*tag_pattern == NUL)
18283 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018285 if (rettv_list_alloc(rettv) == OK)
18286 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018287}
18288
18289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 * "tempname()" function
18291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296{
18297 static int x = 'A';
18298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 rettv->v_type = VAR_STRING;
18300 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301
18302 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18303 * names. Skip 'I' and 'O', they are used for shell redirection. */
18304 do
18305 {
18306 if (x == 'Z')
18307 x = '0';
18308 else if (x == '9')
18309 x = 'A';
18310 else
18311 {
18312#ifdef EBCDIC
18313 if (x == 'I')
18314 x = 'J';
18315 else if (x == 'R')
18316 x = 'S';
18317 else
18318#endif
18319 ++x;
18320 }
18321 } while (x == 'I' || x == 'O');
18322}
18323
18324/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018325 * "test(list)" function: Just checking the walls...
18326 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018327 static void
18328f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018329 typval_T *argvars UNUSED;
18330 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018331{
18332 /* Used for unit testing. Change the code below to your liking. */
18333#if 0
18334 listitem_T *li;
18335 list_T *l;
18336 char_u *bad, *good;
18337
18338 if (argvars[0].v_type != VAR_LIST)
18339 return;
18340 l = argvars[0].vval.v_list;
18341 if (l == NULL)
18342 return;
18343 li = l->lv_first;
18344 if (li == NULL)
18345 return;
18346 bad = get_tv_string(&li->li_tv);
18347 li = li->li_next;
18348 if (li == NULL)
18349 return;
18350 good = get_tv_string(&li->li_tv);
18351 rettv->vval.v_number = test_edit_score(bad, good);
18352#endif
18353}
18354
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018355#ifdef FEAT_FLOAT
18356/*
18357 * "tan()" function
18358 */
18359 static void
18360f_tan(argvars, rettv)
18361 typval_T *argvars;
18362 typval_T *rettv;
18363{
18364 float_T f;
18365
18366 rettv->v_type = VAR_FLOAT;
18367 if (get_float_arg(argvars, &f) == OK)
18368 rettv->vval.v_float = tan(f);
18369 else
18370 rettv->vval.v_float = 0.0;
18371}
18372
18373/*
18374 * "tanh()" function
18375 */
18376 static void
18377f_tanh(argvars, rettv)
18378 typval_T *argvars;
18379 typval_T *rettv;
18380{
18381 float_T f;
18382
18383 rettv->v_type = VAR_FLOAT;
18384 if (get_float_arg(argvars, &f) == OK)
18385 rettv->vval.v_float = tanh(f);
18386 else
18387 rettv->vval.v_float = 0.0;
18388}
18389#endif
18390
Bram Moolenaard52d9742005-08-21 22:20:28 +000018391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 * "tolower(string)" function
18393 */
18394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018396 typval_T *argvars;
18397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398{
18399 char_u *p;
18400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 p = vim_strsave(get_tv_string(&argvars[0]));
18402 rettv->v_type = VAR_STRING;
18403 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404
18405 if (p != NULL)
18406 while (*p != NUL)
18407 {
18408#ifdef FEAT_MBYTE
18409 int l;
18410
18411 if (enc_utf8)
18412 {
18413 int c, lc;
18414
18415 c = utf_ptr2char(p);
18416 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018417 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 /* TODO: reallocate string when byte count changes. */
18419 if (utf_char2len(lc) == l)
18420 utf_char2bytes(lc, p);
18421 p += l;
18422 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018423 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 p += l; /* skip multi-byte character */
18425 else
18426#endif
18427 {
18428 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18429 ++p;
18430 }
18431 }
18432}
18433
18434/*
18435 * "toupper(string)" function
18436 */
18437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018438f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018439 typval_T *argvars;
18440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018442 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018443 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444}
18445
18446/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018447 * "tr(string, fromstr, tostr)" function
18448 */
18449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018450f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 typval_T *argvars;
18452 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018453{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018454 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018455 char_u *fromstr;
18456 char_u *tostr;
18457 char_u *p;
18458#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018459 int inlen;
18460 int fromlen;
18461 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018462 int idx;
18463 char_u *cpstr;
18464 int cplen;
18465 int first = TRUE;
18466#endif
18467 char_u buf[NUMBUFLEN];
18468 char_u buf2[NUMBUFLEN];
18469 garray_T ga;
18470
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018471 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018472 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18473 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018474
18475 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->v_type = VAR_STRING;
18477 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018478 if (fromstr == NULL || tostr == NULL)
18479 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018480 ga_init2(&ga, (int)sizeof(char), 80);
18481
18482#ifdef FEAT_MBYTE
18483 if (!has_mbyte)
18484#endif
18485 /* not multi-byte: fromstr and tostr must be the same length */
18486 if (STRLEN(fromstr) != STRLEN(tostr))
18487 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018488#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018489error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018490#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018491 EMSG2(_(e_invarg2), fromstr);
18492 ga_clear(&ga);
18493 return;
18494 }
18495
18496 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018497 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018498 {
18499#ifdef FEAT_MBYTE
18500 if (has_mbyte)
18501 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018502 inlen = (*mb_ptr2len)(in_str);
18503 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018504 cplen = inlen;
18505 idx = 0;
18506 for (p = fromstr; *p != NUL; p += fromlen)
18507 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018508 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018509 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018510 {
18511 for (p = tostr; *p != NUL; p += tolen)
18512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018513 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018514 if (idx-- == 0)
18515 {
18516 cplen = tolen;
18517 cpstr = p;
18518 break;
18519 }
18520 }
18521 if (*p == NUL) /* tostr is shorter than fromstr */
18522 goto error;
18523 break;
18524 }
18525 ++idx;
18526 }
18527
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018528 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018529 {
18530 /* Check that fromstr and tostr have the same number of
18531 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018532 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018533 first = FALSE;
18534 for (p = tostr; *p != NUL; p += tolen)
18535 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018536 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018537 --idx;
18538 }
18539 if (idx != 0)
18540 goto error;
18541 }
18542
18543 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018544 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018545 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018546
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018547 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018548 }
18549 else
18550#endif
18551 {
18552 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018553 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018554 if (p != NULL)
18555 ga_append(&ga, tostr[p - fromstr]);
18556 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018557 ga_append(&ga, *in_str);
18558 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018559 }
18560 }
18561
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018562 /* add a terminating NUL */
18563 ga_grow(&ga, 1);
18564 ga_append(&ga, NUL);
18565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018567}
18568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018569#ifdef FEAT_FLOAT
18570/*
18571 * "trunc({float})" function
18572 */
18573 static void
18574f_trunc(argvars, rettv)
18575 typval_T *argvars;
18576 typval_T *rettv;
18577{
18578 float_T f;
18579
18580 rettv->v_type = VAR_FLOAT;
18581 if (get_float_arg(argvars, &f) == OK)
18582 /* trunc() is not in C90, use floor() or ceil() instead. */
18583 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18584 else
18585 rettv->vval.v_float = 0.0;
18586}
18587#endif
18588
Bram Moolenaar8299df92004-07-10 09:47:34 +000018589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 * "type(expr)" function
18591 */
18592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018593f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018594 typval_T *argvars;
18595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018597 int n;
18598
18599 switch (argvars[0].v_type)
18600 {
18601 case VAR_NUMBER: n = 0; break;
18602 case VAR_STRING: n = 1; break;
18603 case VAR_FUNC: n = 2; break;
18604 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018605 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018606#ifdef FEAT_FLOAT
18607 case VAR_FLOAT: n = 5; break;
18608#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018609 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18610 }
18611 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612}
18613
18614/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018615 * "undofile(name)" function
18616 */
18617 static void
18618f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018619 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018620 typval_T *rettv;
18621{
18622 rettv->v_type = VAR_STRING;
18623#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018624 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018625 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018626
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018627 if (*fname == NUL)
18628 {
18629 /* If there is no file name there will be no undo file. */
18630 rettv->vval.v_string = NULL;
18631 }
18632 else
18633 {
18634 char_u *ffname = FullName_save(fname, FALSE);
18635
18636 if (ffname != NULL)
18637 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18638 vim_free(ffname);
18639 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018640 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018641#else
18642 rettv->vval.v_string = NULL;
18643#endif
18644}
18645
18646/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018647 * "undotree()" function
18648 */
18649 static void
18650f_undotree(argvars, rettv)
18651 typval_T *argvars UNUSED;
18652 typval_T *rettv;
18653{
18654 if (rettv_dict_alloc(rettv) == OK)
18655 {
18656 dict_T *dict = rettv->vval.v_dict;
18657 list_T *list;
18658
Bram Moolenaar730cde92010-06-27 05:18:54 +020018659 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018660 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018661 dict_add_nr_str(dict, "save_last",
18662 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018663 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18664 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018665 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018666
18667 list = list_alloc();
18668 if (list != NULL)
18669 {
18670 u_eval_tree(curbuf->b_u_oldhead, list);
18671 dict_add_list(dict, "entries", list);
18672 }
18673 }
18674}
18675
18676/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018677 * "values(dict)" function
18678 */
18679 static void
18680f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018681 typval_T *argvars;
18682 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018683{
18684 dict_list(argvars, rettv, 1);
18685}
18686
18687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 * "virtcol(string)" function
18689 */
18690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018691f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018692 typval_T *argvars;
18693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694{
18695 colnr_T vcol = 0;
18696 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018697 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018699 fp = var2fpos(&argvars[0], FALSE, &fnum);
18700 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18701 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 {
18703 getvvcol(curwin, fp, NULL, NULL, &vcol);
18704 ++vcol;
18705 }
18706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018707 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708}
18709
18710/*
18711 * "visualmode()" function
18712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018715 typval_T *argvars UNUSED;
18716 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717{
18718#ifdef FEAT_VISUAL
18719 char_u str[2];
18720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 str[0] = curbuf->b_visual_mode_eval;
18723 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018724 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725
18726 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018727 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729#endif
18730}
18731
18732/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018733 * "wildmenumode()" function
18734 */
18735 static void
18736f_wildmenumode(argvars, rettv)
18737 typval_T *argvars UNUSED;
18738 typval_T *rettv UNUSED;
18739{
18740#ifdef FEAT_WILDMENU
18741 if (wild_menu_showing)
18742 rettv->vval.v_number = 1;
18743#endif
18744}
18745
18746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 * "winbufnr(nr)" function
18748 */
18749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 typval_T *argvars;
18752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753{
18754 win_T *wp;
18755
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018756 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018760 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761}
18762
18763/*
18764 * "wincol()" function
18765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018767f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018768 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770{
18771 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018772 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773}
18774
18775/*
18776 * "winheight(nr)" function
18777 */
18778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018780 typval_T *argvars;
18781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782{
18783 win_T *wp;
18784
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018785 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018789 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790}
18791
18792/*
18793 * "winline()" function
18794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018796f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018797 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799{
18800 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802}
18803
18804/*
18805 * "winnr()" function
18806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811{
18812 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018813
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018815 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018817 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818}
18819
18820/*
18821 * "winrestcmd()" function
18822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827{
18828#ifdef FEAT_WINDOWS
18829 win_T *wp;
18830 int winnr = 1;
18831 garray_T ga;
18832 char_u buf[50];
18833
18834 ga_init2(&ga, (int)sizeof(char), 70);
18835 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18836 {
18837 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18838 ga_concat(&ga, buf);
18839# ifdef FEAT_VERTSPLIT
18840 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18841 ga_concat(&ga, buf);
18842# endif
18843 ++winnr;
18844 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018845 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018847 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852}
18853
18854/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018855 * "winrestview()" function
18856 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018857 static void
18858f_winrestview(argvars, rettv)
18859 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018860 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018861{
18862 dict_T *dict;
18863
18864 if (argvars[0].v_type != VAR_DICT
18865 || (dict = argvars[0].vval.v_dict) == NULL)
18866 EMSG(_(e_invarg));
18867 else
18868 {
18869 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18870 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18871#ifdef FEAT_VIRTUALEDIT
18872 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18873#endif
18874 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018875 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018876
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018877 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018878#ifdef FEAT_DIFF
18879 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18880#endif
18881 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18882 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18883
18884 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018885 win_new_height(curwin, curwin->w_height);
18886# ifdef FEAT_VERTSPLIT
18887 win_new_width(curwin, W_WIDTH(curwin));
18888# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018889 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018890
18891 if (curwin->w_topline == 0)
18892 curwin->w_topline = 1;
18893 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18894 curwin->w_topline = curbuf->b_ml.ml_line_count;
18895#ifdef FEAT_DIFF
18896 check_topfill(curwin, TRUE);
18897#endif
18898 }
18899}
18900
18901/*
18902 * "winsaveview()" function
18903 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018904 static void
18905f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018906 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018907 typval_T *rettv;
18908{
18909 dict_T *dict;
18910
Bram Moolenaara800b422010-06-27 01:15:55 +020018911 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018912 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018913 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018914
18915 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18916 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18917#ifdef FEAT_VIRTUALEDIT
18918 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18919#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018920 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018921 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18922
18923 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18924#ifdef FEAT_DIFF
18925 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18926#endif
18927 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18928 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18929}
18930
18931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 * "winwidth(nr)" function
18933 */
18934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018935f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 typval_T *argvars;
18937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938{
18939 win_T *wp;
18940
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018941 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 else
18945#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018946 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018948 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949#endif
18950}
18951
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018953 * "writefile()" function
18954 */
18955 static void
18956f_writefile(argvars, rettv)
18957 typval_T *argvars;
18958 typval_T *rettv;
18959{
18960 int binary = FALSE;
18961 char_u *fname;
18962 FILE *fd;
18963 listitem_T *li;
18964 char_u *s;
18965 int ret = 0;
18966 int c;
18967
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018968 if (check_restricted() || check_secure())
18969 return;
18970
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018971 if (argvars[0].v_type != VAR_LIST)
18972 {
18973 EMSG2(_(e_listarg), "writefile()");
18974 return;
18975 }
18976 if (argvars[0].vval.v_list == NULL)
18977 return;
18978
18979 if (argvars[2].v_type != VAR_UNKNOWN
18980 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18981 binary = TRUE;
18982
18983 /* Always open the file in binary mode, library functions have a mind of
18984 * their own about CR-LF conversion. */
18985 fname = get_tv_string(&argvars[1]);
18986 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18987 {
18988 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18989 ret = -1;
18990 }
18991 else
18992 {
18993 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18994 li = li->li_next)
18995 {
18996 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18997 {
18998 if (*s == '\n')
18999 c = putc(NUL, fd);
19000 else
19001 c = putc(*s, fd);
19002 if (c == EOF)
19003 {
19004 ret = -1;
19005 break;
19006 }
19007 }
19008 if (!binary || li->li_next != NULL)
19009 if (putc('\n', fd) == EOF)
19010 {
19011 ret = -1;
19012 break;
19013 }
19014 if (ret < 0)
19015 {
19016 EMSG(_(e_write));
19017 break;
19018 }
19019 }
19020 fclose(fd);
19021 }
19022
19023 rettv->vval.v_number = ret;
19024}
19025
19026/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019027 * "xor(expr, expr)" function
19028 */
19029 static void
19030f_xor(argvars, rettv)
19031 typval_T *argvars;
19032 typval_T *rettv;
19033{
19034 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19035 ^ get_tv_number_chk(&argvars[1], NULL);
19036}
19037
19038
19039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019041 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 */
19043 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019044var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019045 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019046 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019047 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019049 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019051 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
Bram Moolenaara5525202006-03-02 22:52:09 +000019053 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019054 if (varp->v_type == VAR_LIST)
19055 {
19056 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019057 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019058 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019059 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019060
19061 l = varp->vval.v_list;
19062 if (l == NULL)
19063 return NULL;
19064
19065 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019066 pos.lnum = list_find_nr(l, 0L, &error);
19067 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019068 return NULL; /* invalid line number */
19069
19070 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019071 pos.col = list_find_nr(l, 1L, &error);
19072 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019073 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019074 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019075
19076 /* We accept "$" for the column number: last column. */
19077 li = list_find(l, 1L);
19078 if (li != NULL && li->li_tv.v_type == VAR_STRING
19079 && li->li_tv.vval.v_string != NULL
19080 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19081 pos.col = len + 1;
19082
Bram Moolenaara5525202006-03-02 22:52:09 +000019083 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019084 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019085 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019086 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019087
Bram Moolenaara5525202006-03-02 22:52:09 +000019088#ifdef FEAT_VIRTUALEDIT
19089 /* Get the virtual offset. Defaults to zero. */
19090 pos.coladd = list_find_nr(l, 2L, &error);
19091 if (error)
19092 pos.coladd = 0;
19093#endif
19094
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019095 return &pos;
19096 }
19097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019098 name = get_tv_string_chk(varp);
19099 if (name == NULL)
19100 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019101 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019103#ifdef FEAT_VISUAL
19104 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19105 {
19106 if (VIsual_active)
19107 return &VIsual;
19108 return &curwin->w_cursor;
19109 }
19110#endif
19111 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019113 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19115 return NULL;
19116 return pp;
19117 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019118
19119#ifdef FEAT_VIRTUALEDIT
19120 pos.coladd = 0;
19121#endif
19122
Bram Moolenaar477933c2007-07-17 14:32:23 +000019123 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019124 {
19125 pos.col = 0;
19126 if (name[1] == '0') /* "w0": first visible line */
19127 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019128 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019129 pos.lnum = curwin->w_topline;
19130 return &pos;
19131 }
19132 else if (name[1] == '$') /* "w$": last visible line */
19133 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019134 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019135 pos.lnum = curwin->w_botline - 1;
19136 return &pos;
19137 }
19138 }
19139 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019141 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 {
19143 pos.lnum = curbuf->b_ml.ml_line_count;
19144 pos.col = 0;
19145 }
19146 else
19147 {
19148 pos.lnum = curwin->w_cursor.lnum;
19149 pos.col = (colnr_T)STRLEN(ml_get_curline());
19150 }
19151 return &pos;
19152 }
19153 return NULL;
19154}
19155
19156/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019157 * Convert list in "arg" into a position and optional file number.
19158 * When "fnump" is NULL there is no file number, only 3 items.
19159 * Note that the column is passed on as-is, the caller may want to decrement
19160 * it to use 1 for the first column.
19161 * Return FAIL when conversion is not possible, doesn't check the position for
19162 * validity.
19163 */
19164 static int
19165list2fpos(arg, posp, fnump)
19166 typval_T *arg;
19167 pos_T *posp;
19168 int *fnump;
19169{
19170 list_T *l = arg->vval.v_list;
19171 long i = 0;
19172 long n;
19173
Bram Moolenaarbde35262006-07-23 20:12:24 +000019174 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19175 * when "fnump" isn't NULL and "coladd" is optional. */
19176 if (arg->v_type != VAR_LIST
19177 || l == NULL
19178 || l->lv_len < (fnump == NULL ? 2 : 3)
19179 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019180 return FAIL;
19181
19182 if (fnump != NULL)
19183 {
19184 n = list_find_nr(l, i++, NULL); /* fnum */
19185 if (n < 0)
19186 return FAIL;
19187 if (n == 0)
19188 n = curbuf->b_fnum; /* current buffer */
19189 *fnump = n;
19190 }
19191
19192 n = list_find_nr(l, i++, NULL); /* lnum */
19193 if (n < 0)
19194 return FAIL;
19195 posp->lnum = n;
19196
19197 n = list_find_nr(l, i++, NULL); /* col */
19198 if (n < 0)
19199 return FAIL;
19200 posp->col = n;
19201
19202#ifdef FEAT_VIRTUALEDIT
19203 n = list_find_nr(l, i, NULL);
19204 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019205 posp->coladd = 0;
19206 else
19207 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019208#endif
19209
19210 return OK;
19211}
19212
19213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 * Get the length of an environment variable name.
19215 * Advance "arg" to the first character after the name.
19216 * Return 0 for error.
19217 */
19218 static int
19219get_env_len(arg)
19220 char_u **arg;
19221{
19222 char_u *p;
19223 int len;
19224
19225 for (p = *arg; vim_isIDc(*p); ++p)
19226 ;
19227 if (p == *arg) /* no name found */
19228 return 0;
19229
19230 len = (int)(p - *arg);
19231 *arg = p;
19232 return len;
19233}
19234
19235/*
19236 * Get the length of the name of a function or internal variable.
19237 * "arg" is advanced to the first non-white character after the name.
19238 * Return 0 if something is wrong.
19239 */
19240 static int
19241get_id_len(arg)
19242 char_u **arg;
19243{
19244 char_u *p;
19245 int len;
19246
19247 /* Find the end of the name. */
19248 for (p = *arg; eval_isnamec(*p); ++p)
19249 ;
19250 if (p == *arg) /* no name found */
19251 return 0;
19252
19253 len = (int)(p - *arg);
19254 *arg = skipwhite(p);
19255
19256 return len;
19257}
19258
19259/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019260 * Get the length of the name of a variable or function.
19261 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019263 * Return -1 if curly braces expansion failed.
19264 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 * If the name contains 'magic' {}'s, expand them and return the
19266 * expanded name in an allocated string via 'alias' - caller must free.
19267 */
19268 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019269get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270 char_u **arg;
19271 char_u **alias;
19272 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019273 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274{
19275 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 char_u *p;
19277 char_u *expr_start;
19278 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279
19280 *alias = NULL; /* default to no alias */
19281
19282 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19283 && (*arg)[2] == (int)KE_SNR)
19284 {
19285 /* hard coded <SNR>, already translated */
19286 *arg += 3;
19287 return get_id_len(arg) + 3;
19288 }
19289 len = eval_fname_script(*arg);
19290 if (len > 0)
19291 {
19292 /* literal "<SID>", "s:" or "<SNR>" */
19293 *arg += len;
19294 }
19295
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019297 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019299 p = find_name_end(*arg, &expr_start, &expr_end,
19300 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 if (expr_start != NULL)
19302 {
19303 char_u *temp_string;
19304
19305 if (!evaluate)
19306 {
19307 len += (int)(p - *arg);
19308 *arg = skipwhite(p);
19309 return len;
19310 }
19311
19312 /*
19313 * Include any <SID> etc in the expanded string:
19314 * Thus the -len here.
19315 */
19316 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19317 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019318 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 *alias = temp_string;
19320 *arg = skipwhite(p);
19321 return (int)STRLEN(temp_string);
19322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323
19324 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019325 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 EMSG2(_(e_invexpr2), *arg);
19327
19328 return len;
19329}
19330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331/*
19332 * Find the end of a variable or function name, taking care of magic braces.
19333 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19334 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019335 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336 * Return a pointer to just after the name. Equal to "arg" if there is no
19337 * valid name.
19338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019340find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 char_u *arg;
19342 char_u **expr_start;
19343 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019344 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019346 int mb_nest = 0;
19347 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 char_u *p;
19349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 *expr_start = NULL;
19353 *expr_end = NULL;
19354 }
19355
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019356 /* Quick check for valid starting character. */
19357 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19358 return arg;
19359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019360 for (p = arg; *p != NUL
19361 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019362 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019363 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019364 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019365 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019367 if (*p == '\'')
19368 {
19369 /* skip over 'string' to avoid counting [ and ] inside it. */
19370 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19371 ;
19372 if (*p == NUL)
19373 break;
19374 }
19375 else if (*p == '"')
19376 {
19377 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19378 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19379 if (*p == '\\' && p[1] != NUL)
19380 ++p;
19381 if (*p == NUL)
19382 break;
19383 }
19384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019385 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387 if (*p == '[')
19388 ++br_nest;
19389 else if (*p == ']')
19390 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019395 if (*p == '{')
19396 {
19397 mb_nest++;
19398 if (expr_start != NULL && *expr_start == NULL)
19399 *expr_start = p;
19400 }
19401 else if (*p == '}')
19402 {
19403 mb_nest--;
19404 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19405 *expr_end = p;
19406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 }
19409
19410 return p;
19411}
19412
19413/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019414 * Expands out the 'magic' {}'s in a variable/function name.
19415 * Note that this can call itself recursively, to deal with
19416 * constructs like foo{bar}{baz}{bam}
19417 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19418 * "in_start" ^
19419 * "expr_start" ^
19420 * "expr_end" ^
19421 * "in_end" ^
19422 *
19423 * Returns a new allocated string, which the caller must free.
19424 * Returns NULL for failure.
19425 */
19426 static char_u *
19427make_expanded_name(in_start, expr_start, expr_end, in_end)
19428 char_u *in_start;
19429 char_u *expr_start;
19430 char_u *expr_end;
19431 char_u *in_end;
19432{
19433 char_u c1;
19434 char_u *retval = NULL;
19435 char_u *temp_result;
19436 char_u *nextcmd = NULL;
19437
19438 if (expr_end == NULL || in_end == NULL)
19439 return NULL;
19440 *expr_start = NUL;
19441 *expr_end = NUL;
19442 c1 = *in_end;
19443 *in_end = NUL;
19444
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019445 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019446 if (temp_result != NULL && nextcmd == NULL)
19447 {
19448 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19449 + (in_end - expr_end) + 1));
19450 if (retval != NULL)
19451 {
19452 STRCPY(retval, in_start);
19453 STRCAT(retval, temp_result);
19454 STRCAT(retval, expr_end + 1);
19455 }
19456 }
19457 vim_free(temp_result);
19458
19459 *in_end = c1; /* put char back for error messages */
19460 *expr_start = '{';
19461 *expr_end = '}';
19462
19463 if (retval != NULL)
19464 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019465 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019466 if (expr_start != NULL)
19467 {
19468 /* Further expansion! */
19469 temp_result = make_expanded_name(retval, expr_start,
19470 expr_end, temp_result);
19471 vim_free(retval);
19472 retval = temp_result;
19473 }
19474 }
19475
19476 return retval;
19477}
19478
19479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019481 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 */
19483 static int
19484eval_isnamec(c)
19485 int c;
19486{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019487 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19488}
19489
19490/*
19491 * Return TRUE if character "c" can be used as the first character in a
19492 * variable or function name (excluding '{' and '}').
19493 */
19494 static int
19495eval_isnamec1(c)
19496 int c;
19497{
19498 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499}
19500
19501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 * Set number v: variable to "val".
19503 */
19504 void
19505set_vim_var_nr(idx, val)
19506 int idx;
19507 long val;
19508{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019509 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510}
19511
19512/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019513 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 */
19515 long
19516get_vim_var_nr(idx)
19517 int idx;
19518{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019519 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520}
19521
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019522/*
19523 * Get string v: variable value. Uses a static buffer, can only be used once.
19524 */
19525 char_u *
19526get_vim_var_str(idx)
19527 int idx;
19528{
19529 return get_tv_string(&vimvars[idx].vv_tv);
19530}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019531
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019533 * Get List v: variable value. Caller must take care of reference count when
19534 * needed.
19535 */
19536 list_T *
19537get_vim_var_list(idx)
19538 int idx;
19539{
19540 return vimvars[idx].vv_list;
19541}
19542
19543/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019544 * Set v:char to character "c".
19545 */
19546 void
19547set_vim_var_char(c)
19548 int c;
19549{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019550 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019551
19552#ifdef FEAT_MBYTE
19553 if (has_mbyte)
19554 buf[(*mb_char2bytes)(c, buf)] = NUL;
19555 else
19556#endif
19557 {
19558 buf[0] = c;
19559 buf[1] = NUL;
19560 }
19561 set_vim_var_string(VV_CHAR, buf, -1);
19562}
19563
19564/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019565 * Set v:count to "count" and v:count1 to "count1".
19566 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 */
19568 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019569set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 long count;
19571 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019572 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019574 if (set_prevcount)
19575 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019576 vimvars[VV_COUNT].vv_nr = count;
19577 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578}
19579
19580/*
19581 * Set string v: variable to a copy of "val".
19582 */
19583 void
19584set_vim_var_string(idx, val, len)
19585 int idx;
19586 char_u *val;
19587 int len; /* length of "val" to use or -1 (whole string) */
19588{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019589 /* Need to do this (at least) once, since we can't initialize a union.
19590 * Will always be invoked when "v:progname" is set. */
19591 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19592
Bram Moolenaare9a41262005-01-15 22:18:47 +000019593 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019595 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019597 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019599 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601
19602/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019603 * Set List v: variable to "val".
19604 */
19605 void
19606set_vim_var_list(idx, val)
19607 int idx;
19608 list_T *val;
19609{
19610 list_unref(vimvars[idx].vv_list);
19611 vimvars[idx].vv_list = val;
19612 if (val != NULL)
19613 ++val->lv_refcount;
19614}
19615
19616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 * Set v:register if needed.
19618 */
19619 void
19620set_reg_var(c)
19621 int c;
19622{
19623 char_u regname;
19624
19625 if (c == 0 || c == ' ')
19626 regname = '"';
19627 else
19628 regname = c;
19629 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019630 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 set_vim_var_string(VV_REG, &regname, 1);
19632}
19633
19634/*
19635 * Get or set v:exception. If "oldval" == NULL, return the current value.
19636 * Otherwise, restore the value to "oldval" and return NULL.
19637 * Must always be called in pairs to save and restore v:exception! Does not
19638 * take care of memory allocations.
19639 */
19640 char_u *
19641v_exception(oldval)
19642 char_u *oldval;
19643{
19644 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019645 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646
Bram Moolenaare9a41262005-01-15 22:18:47 +000019647 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 return NULL;
19649}
19650
19651/*
19652 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19653 * Otherwise, restore the value to "oldval" and return NULL.
19654 * Must always be called in pairs to save and restore v:throwpoint! Does not
19655 * take care of memory allocations.
19656 */
19657 char_u *
19658v_throwpoint(oldval)
19659 char_u *oldval;
19660{
19661 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019662 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663
Bram Moolenaare9a41262005-01-15 22:18:47 +000019664 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 return NULL;
19666}
19667
19668#if defined(FEAT_AUTOCMD) || defined(PROTO)
19669/*
19670 * Set v:cmdarg.
19671 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19672 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19673 * Must always be called in pairs!
19674 */
19675 char_u *
19676set_cmdarg(eap, oldarg)
19677 exarg_T *eap;
19678 char_u *oldarg;
19679{
19680 char_u *oldval;
19681 char_u *newval;
19682 unsigned len;
19683
Bram Moolenaare9a41262005-01-15 22:18:47 +000019684 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019685 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019687 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019688 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019689 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 }
19691
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019692 if (eap->force_bin == FORCE_BIN)
19693 len = 6;
19694 else if (eap->force_bin == FORCE_NOBIN)
19695 len = 8;
19696 else
19697 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019698
19699 if (eap->read_edit)
19700 len += 7;
19701
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019702 if (eap->force_ff != 0)
19703 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19704# ifdef FEAT_MBYTE
19705 if (eap->force_enc != 0)
19706 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019707 if (eap->bad_char != 0)
19708 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019709# endif
19710
19711 newval = alloc(len + 1);
19712 if (newval == NULL)
19713 return NULL;
19714
19715 if (eap->force_bin == FORCE_BIN)
19716 sprintf((char *)newval, " ++bin");
19717 else if (eap->force_bin == FORCE_NOBIN)
19718 sprintf((char *)newval, " ++nobin");
19719 else
19720 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019721
19722 if (eap->read_edit)
19723 STRCAT(newval, " ++edit");
19724
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019725 if (eap->force_ff != 0)
19726 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19727 eap->cmd + eap->force_ff);
19728# ifdef FEAT_MBYTE
19729 if (eap->force_enc != 0)
19730 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19731 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019732 if (eap->bad_char == BAD_KEEP)
19733 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19734 else if (eap->bad_char == BAD_DROP)
19735 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19736 else if (eap->bad_char != 0)
19737 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019738# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019739 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019740 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741}
19742#endif
19743
19744/*
19745 * Get the value of internal variable "name".
19746 * Return OK or FAIL.
19747 */
19748 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019749get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 char_u *name;
19751 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019753 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754{
19755 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 typval_T *tv = NULL;
19757 typval_T atv;
19758 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760
19761 /* truncate the name, so that we can use strcmp() */
19762 cc = name[len];
19763 name[len] = NUL;
19764
19765 /*
19766 * Check for "b:changedtick".
19767 */
19768 if (STRCMP(name, "b:changedtick") == 0)
19769 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019770 atv.v_type = VAR_NUMBER;
19771 atv.vval.v_number = curbuf->b_changedtick;
19772 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 }
19774
19775 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 * Check for user-defined variables.
19777 */
19778 else
19779 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019780 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 }
19784
Bram Moolenaare9a41262005-01-15 22:18:47 +000019785 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019787 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019788 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 ret = FAIL;
19790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019791 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019792 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793
19794 name[len] = cc;
19795
19796 return ret;
19797}
19798
19799/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019800 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19801 * Also handle function call with Funcref variable: func(expr)
19802 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19803 */
19804 static int
19805handle_subscript(arg, rettv, evaluate, verbose)
19806 char_u **arg;
19807 typval_T *rettv;
19808 int evaluate; /* do more than finding the end */
19809 int verbose; /* give error messages */
19810{
19811 int ret = OK;
19812 dict_T *selfdict = NULL;
19813 char_u *s;
19814 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019815 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019816
19817 while (ret == OK
19818 && (**arg == '['
19819 || (**arg == '.' && rettv->v_type == VAR_DICT)
19820 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19821 && !vim_iswhite(*(*arg - 1)))
19822 {
19823 if (**arg == '(')
19824 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019825 /* need to copy the funcref so that we can clear rettv */
19826 functv = *rettv;
19827 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019828
19829 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019830 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019831 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019832 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19833 &len, evaluate, selfdict);
19834
19835 /* Clear the funcref afterwards, so that deleting it while
19836 * evaluating the arguments is possible (see test55). */
19837 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019838
19839 /* Stop the expression evaluation when immediately aborting on
19840 * error, or when an interrupt occurred or an exception was thrown
19841 * but not caught. */
19842 if (aborting())
19843 {
19844 if (ret == OK)
19845 clear_tv(rettv);
19846 ret = FAIL;
19847 }
19848 dict_unref(selfdict);
19849 selfdict = NULL;
19850 }
19851 else /* **arg == '[' || **arg == '.' */
19852 {
19853 dict_unref(selfdict);
19854 if (rettv->v_type == VAR_DICT)
19855 {
19856 selfdict = rettv->vval.v_dict;
19857 if (selfdict != NULL)
19858 ++selfdict->dv_refcount;
19859 }
19860 else
19861 selfdict = NULL;
19862 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19863 {
19864 clear_tv(rettv);
19865 ret = FAIL;
19866 }
19867 }
19868 }
19869 dict_unref(selfdict);
19870 return ret;
19871}
19872
19873/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019874 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 * value).
19876 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019879{
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019881}
19882
19883/*
19884 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 * The string "s" must have been allocated, it is consumed.
19886 * Return NULL for out of memory, the variable otherwise.
19887 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 char_u *s;
19891{
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019894 rettv = alloc_tv();
19895 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019897 rettv->v_type = VAR_STRING;
19898 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 }
19900 else
19901 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019902 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903}
19904
19905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019908 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019909free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019910 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911{
19912 if (varp != NULL)
19913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019914 switch (varp->v_type)
19915 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019916 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019917 func_unref(varp->vval.v_string);
19918 /*FALLTHROUGH*/
19919 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019920 vim_free(varp->vval.v_string);
19921 break;
19922 case VAR_LIST:
19923 list_unref(varp->vval.v_list);
19924 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019925 case VAR_DICT:
19926 dict_unref(varp->vval.v_dict);
19927 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019928 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019929#ifdef FEAT_FLOAT
19930 case VAR_FLOAT:
19931#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019932 case VAR_UNKNOWN:
19933 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019934 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019935 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019936 break;
19937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 vim_free(varp);
19939 }
19940}
19941
19942/*
19943 * Free the memory for a variable value and set the value to NULL or 0.
19944 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019945 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019946clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948{
19949 if (varp != NULL)
19950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019951 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019953 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019954 func_unref(varp->vval.v_string);
19955 /*FALLTHROUGH*/
19956 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019957 vim_free(varp->vval.v_string);
19958 varp->vval.v_string = NULL;
19959 break;
19960 case VAR_LIST:
19961 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019962 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019964 case VAR_DICT:
19965 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019966 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019967 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019969 varp->vval.v_number = 0;
19970 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019971#ifdef FEAT_FLOAT
19972 case VAR_FLOAT:
19973 varp->vval.v_float = 0.0;
19974 break;
19975#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 case VAR_UNKNOWN:
19977 break;
19978 default:
19979 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019981 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 }
19983}
19984
19985/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 * Set the value of a variable to NULL without freeing items.
19987 */
19988 static void
19989init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991{
19992 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019993 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994}
19995
19996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 * Get the number value of a variable.
19998 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 * For incompatible types, return 0.
20000 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20001 * caller of incompatible types: it sets *denote to TRUE if "denote"
20002 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 */
20004 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020006 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020008 int error = FALSE;
20009
20010 return get_tv_number_chk(varp, &error); /* return 0L on error */
20011}
20012
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020013 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020014get_tv_number_chk(varp, denote)
20015 typval_T *varp;
20016 int *denote;
20017{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020020 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020022 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020023 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020024#ifdef FEAT_FLOAT
20025 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020026 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020027 break;
20028#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020029 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020030 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 break;
20032 case VAR_STRING:
20033 if (varp->vval.v_string != NULL)
20034 vim_str2nr(varp->vval.v_string, NULL, NULL,
20035 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020036 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020037 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020038 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020039 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020040 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020041 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020042 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020043 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020044 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020045 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 if (denote == NULL) /* useful for values that must be unsigned */
20048 n = -1;
20049 else
20050 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020051 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052}
20053
20054/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020055 * Get the lnum from the first argument.
20056 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020057 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 */
20059 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020061 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062{
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 linenr_T lnum;
20065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020066 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 if (lnum == 0) /* no valid number, try using line() */
20068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020069 rettv.v_type = VAR_NUMBER;
20070 f_line(argvars, &rettv);
20071 lnum = rettv.vval.v_number;
20072 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 }
20074 return lnum;
20075}
20076
20077/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020078 * Get the lnum from the first argument.
20079 * Also accepts "$", then "buf" is used.
20080 * Returns 0 on error.
20081 */
20082 static linenr_T
20083get_tv_lnum_buf(argvars, buf)
20084 typval_T *argvars;
20085 buf_T *buf;
20086{
20087 if (argvars[0].v_type == VAR_STRING
20088 && argvars[0].vval.v_string != NULL
20089 && argvars[0].vval.v_string[0] == '$'
20090 && buf != NULL)
20091 return buf->b_ml.ml_line_count;
20092 return get_tv_number_chk(&argvars[0], NULL);
20093}
20094
20095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 * Get the string value of a variable.
20097 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020098 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20099 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 * If the String variable has never been set, return an empty string.
20101 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020102 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20103 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 */
20105 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108{
20109 static char_u mybuf[NUMBUFLEN];
20110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112}
20113
20114 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020116 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020117 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020119 char_u *res = get_tv_string_buf_chk(varp, buf);
20120
20121 return res != NULL ? res : (char_u *)"";
20122}
20123
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020124 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020125get_tv_string_chk(varp)
20126 typval_T *varp;
20127{
20128 static char_u mybuf[NUMBUFLEN];
20129
20130 return get_tv_string_buf_chk(varp, mybuf);
20131}
20132
20133 static char_u *
20134get_tv_string_buf_chk(varp, buf)
20135 typval_T *varp;
20136 char_u *buf;
20137{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020138 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020140 case VAR_NUMBER:
20141 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20142 return buf;
20143 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020144 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020145 break;
20146 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020147 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020148 break;
20149 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020150 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020151 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020152#ifdef FEAT_FLOAT
20153 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020154 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020155 break;
20156#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020157 case VAR_STRING:
20158 if (varp->vval.v_string != NULL)
20159 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020160 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020161 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020165 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166}
20167
20168/*
20169 * Find variable "name" in the list of variables.
20170 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020171 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020172 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020176find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182
Bram Moolenaara7043832005-01-21 11:56:39 +000020183 ht = find_var_ht(name, &varname);
20184 if (htp != NULL)
20185 *htp = ht;
20186 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020188 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189}
20190
20191/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020192 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020193 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020195 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020196find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020197 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020198 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020199 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020200 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020201{
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 hashitem_T *hi;
20203
20204 if (*varname == NUL)
20205 {
20206 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020207 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020208 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020209 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020210 case 'g': return &globvars_var;
20211 case 'v': return &vimvars_var;
20212 case 'b': return &curbuf->b_bufvar;
20213 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020214#ifdef FEAT_WINDOWS
20215 case 't': return &curtab->tp_winvar;
20216#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020217 case 'l': return current_funccal == NULL
20218 ? NULL : &current_funccal->l_vars_var;
20219 case 'a': return current_funccal == NULL
20220 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 }
20222 return NULL;
20223 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020224
20225 hi = hash_find(ht, varname);
20226 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020227 {
20228 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020229 * worked find the variable again. Don't auto-load a script if it was
20230 * loaded already, otherwise it would be loaded every time when
20231 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020232 if (ht == &globvarht && !writing)
20233 {
20234 /* Note: script_autoload() may make "hi" invalid. It must either
20235 * be obtained again or not used. */
20236 if (!script_autoload(varname, FALSE) || aborting())
20237 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020238 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020239 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020240 if (HASHITEM_EMPTY(hi))
20241 return NULL;
20242 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020244}
20245
20246/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020248 * Set "varname" to the start of name without ':'.
20249 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020250 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020251find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 char_u *name;
20253 char_u **varname;
20254{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020255 hashitem_T *hi;
20256
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 if (name[1] != ':')
20258 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020259 /* The name must not start with a colon or #. */
20260 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 return NULL;
20262 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020263
20264 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020265 hi = hash_find(&compat_hashtab, name);
20266 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020267 return &compat_hashtab;
20268
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 return &globvarht; /* global variable */
20271 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 }
20273 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020274 if (*name == 'g') /* global variable */
20275 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020276 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20277 */
20278 if (vim_strchr(name + 2, ':') != NULL
20279 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020280 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020282 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020284 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020285#ifdef FEAT_WINDOWS
20286 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020287 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020288#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020289 if (*name == 'v') /* v: variable */
20290 return &vimvarht;
20291 if (*name == 'a' && current_funccal != NULL) /* function argument */
20292 return &current_funccal->l_avars.dv_hashtab;
20293 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20294 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 if (*name == 's' /* script variable */
20296 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20297 return &SCRIPT_VARS(current_SID);
20298 return NULL;
20299}
20300
20301/*
20302 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020303 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 * Returns NULL when it doesn't exist.
20305 */
20306 char_u *
20307get_var_value(name)
20308 char_u *name;
20309{
Bram Moolenaar33570922005-01-25 22:26:29 +000020310 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311
Bram Moolenaara7043832005-01-21 11:56:39 +000020312 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 if (v == NULL)
20314 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316}
20317
20318/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 * sourcing this script and when executing functions defined in the script.
20321 */
20322 void
20323new_script_vars(id)
20324 scid_T id;
20325{
Bram Moolenaara7043832005-01-21 11:56:39 +000020326 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020327 hashtab_T *ht;
20328 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020329
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20331 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020332 /* Re-allocating ga_data means that an ht_array pointing to
20333 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020334 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020335 for (i = 1; i <= ga_scripts.ga_len; ++i)
20336 {
20337 ht = &SCRIPT_VARS(i);
20338 if (ht->ht_mask == HT_INIT_SIZE - 1)
20339 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020340 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020341 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020342 }
20343
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 while (ga_scripts.ga_len < id)
20345 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020346 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020347 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020348 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 }
20351 }
20352}
20353
20354/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020355 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20356 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 */
20358 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020359init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 dict_T *dict;
20361 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020362 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363{
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020365 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020366 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020367 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020368 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 dict_var->di_tv.vval.v_dict = dict;
20370 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020371 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020372 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20373 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374}
20375
20376/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020377 * Unreference a dictionary initialized by init_var_dict().
20378 */
20379 void
20380unref_var_dict(dict)
20381 dict_T *dict;
20382{
20383 /* Now the dict needs to be freed if no one else is using it, go back to
20384 * normal reference counting. */
20385 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20386 dict_unref(dict);
20387}
20388
20389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020391 * Frees all allocated variables and the value they contain.
20392 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 */
20394 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020395vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020396 hashtab_T *ht;
20397{
20398 vars_clear_ext(ht, TRUE);
20399}
20400
20401/*
20402 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20403 */
20404 static void
20405vars_clear_ext(ht, free_val)
20406 hashtab_T *ht;
20407 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408{
Bram Moolenaara7043832005-01-21 11:56:39 +000020409 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 hashitem_T *hi;
20411 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412
Bram Moolenaar33570922005-01-25 22:26:29 +000020413 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020414 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020415 for (hi = ht->ht_array; todo > 0; ++hi)
20416 {
20417 if (!HASHITEM_EMPTY(hi))
20418 {
20419 --todo;
20420
Bram Moolenaar33570922005-01-25 22:26:29 +000020421 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020422 * ht_array might change then. hash_clear() takes care of it
20423 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 v = HI2DI(hi);
20425 if (free_val)
20426 clear_tv(&v->di_tv);
20427 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20428 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020429 }
20430 }
20431 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020432 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433}
20434
Bram Moolenaara7043832005-01-21 11:56:39 +000020435/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020436 * Delete a variable from hashtab "ht" at item "hi".
20437 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020440delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020441 hashtab_T *ht;
20442 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443{
Bram Moolenaar33570922005-01-25 22:26:29 +000020444 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020445
20446 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 clear_tv(&di->di_tv);
20448 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449}
20450
20451/*
20452 * List the value of one internal variable.
20453 */
20454 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020455list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020456 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020458 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020460 char_u *tofree;
20461 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020462 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020463
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020464 current_copyID += COPYID_INC;
20465 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020466 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020467 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020468 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469}
20470
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020472list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 char_u *prefix;
20474 char_u *name;
20475 int type;
20476 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020477 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478{
Bram Moolenaar31859182007-08-14 20:41:13 +000020479 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20480 msg_start();
20481 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 if (name != NULL) /* "a:" vars don't have a name stored */
20483 msg_puts(name);
20484 msg_putchar(' ');
20485 msg_advance(22);
20486 if (type == VAR_NUMBER)
20487 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020488 else if (type == VAR_FUNC)
20489 msg_putchar('*');
20490 else if (type == VAR_LIST)
20491 {
20492 msg_putchar('[');
20493 if (*string == '[')
20494 ++string;
20495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020496 else if (type == VAR_DICT)
20497 {
20498 msg_putchar('{');
20499 if (*string == '{')
20500 ++string;
20501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 else
20503 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020504
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020506
20507 if (type == VAR_FUNC)
20508 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020509 if (*first)
20510 {
20511 msg_clr_eos();
20512 *first = FALSE;
20513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514}
20515
20516/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020517 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 * If the variable already exists, the value is updated.
20519 * Otherwise the variable is created.
20520 */
20521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020522set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020525 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526{
Bram Moolenaar33570922005-01-25 22:26:29 +000020527 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020529 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020531 ht = find_var_ht(name, &varname);
20532 if (ht == NULL || *varname == NUL)
20533 {
20534 EMSG2(_(e_illvar), name);
20535 return;
20536 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020537 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020538
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020539 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20540 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020541
Bram Moolenaar33570922005-01-25 22:26:29 +000020542 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020544 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020545 if (var_check_ro(v->di_flags, name)
20546 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020547 return;
20548 if (v->di_tv.v_type != tv->v_type
20549 && !((v->di_tv.v_type == VAR_STRING
20550 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020552 || tv->v_type == VAR_NUMBER))
20553#ifdef FEAT_FLOAT
20554 && !((v->di_tv.v_type == VAR_NUMBER
20555 || v->di_tv.v_type == VAR_FLOAT)
20556 && (tv->v_type == VAR_NUMBER
20557 || tv->v_type == VAR_FLOAT))
20558#endif
20559 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020560 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020561 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020562 return;
20563 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020564
20565 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020566 * Handle setting internal v: variables separately: we don't change
20567 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 */
20569 if (ht == &vimvarht)
20570 {
20571 if (v->di_tv.v_type == VAR_STRING)
20572 {
20573 vim_free(v->di_tv.vval.v_string);
20574 if (copy || tv->v_type != VAR_STRING)
20575 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20576 else
20577 {
20578 /* Take over the string to avoid an extra alloc/free. */
20579 v->di_tv.vval.v_string = tv->vval.v_string;
20580 tv->vval.v_string = NULL;
20581 }
20582 }
20583 else if (v->di_tv.v_type != VAR_NUMBER)
20584 EMSG2(_(e_intern2), "set_var()");
20585 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020586 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020587 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020588 if (STRCMP(varname, "searchforward") == 0)
20589 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20590 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 return;
20592 }
20593
20594 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 }
20596 else /* add a new variable */
20597 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020598 /* Can't add "v:" variable. */
20599 if (ht == &vimvarht)
20600 {
20601 EMSG2(_(e_illvar), name);
20602 return;
20603 }
20604
Bram Moolenaar92124a32005-06-17 22:03:40 +000020605 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020606 if (!valid_varname(varname))
20607 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020608
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020609 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20610 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020611 if (v == NULL)
20612 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020613 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020616 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 return;
20618 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020619 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020621
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020622 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020623 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020624 else
20625 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020626 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020627 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020628 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630}
20631
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020632/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020633 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020634 * Also give an error message.
20635 */
20636 static int
20637var_check_ro(flags, name)
20638 int flags;
20639 char_u *name;
20640{
20641 if (flags & DI_FLAGS_RO)
20642 {
20643 EMSG2(_(e_readonlyvar), name);
20644 return TRUE;
20645 }
20646 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20647 {
20648 EMSG2(_(e_readonlysbx), name);
20649 return TRUE;
20650 }
20651 return FALSE;
20652}
20653
20654/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020655 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20656 * Also give an error message.
20657 */
20658 static int
20659var_check_fixed(flags, name)
20660 int flags;
20661 char_u *name;
20662{
20663 if (flags & DI_FLAGS_FIX)
20664 {
20665 EMSG2(_("E795: Cannot delete variable %s"), name);
20666 return TRUE;
20667 }
20668 return FALSE;
20669}
20670
20671/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020672 * Check if a funcref is assigned to a valid variable name.
20673 * Return TRUE and give an error if not.
20674 */
20675 static int
20676var_check_func_name(name, new_var)
20677 char_u *name; /* points to start of variable name */
20678 int new_var; /* TRUE when creating the variable */
20679{
20680 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20681 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20682 ? name[2] : name[0]))
20683 {
20684 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20685 name);
20686 return TRUE;
20687 }
20688 /* Don't allow hiding a function. When "v" is not NULL we might be
20689 * assigning another function to the same var, the type is checked
20690 * below. */
20691 if (new_var && function_exists(name))
20692 {
20693 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20694 name);
20695 return TRUE;
20696 }
20697 return FALSE;
20698}
20699
20700/*
20701 * Check if a variable name is valid.
20702 * Return FALSE and give an error if not.
20703 */
20704 static int
20705valid_varname(varname)
20706 char_u *varname;
20707{
20708 char_u *p;
20709
20710 for (p = varname; *p != NUL; ++p)
20711 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20712 && *p != AUTOLOAD_CHAR)
20713 {
20714 EMSG2(_(e_illvar), varname);
20715 return FALSE;
20716 }
20717 return TRUE;
20718}
20719
20720/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020721 * Return TRUE if typeval "tv" is set to be locked (immutable).
20722 * Also give an error message, using "name".
20723 */
20724 static int
20725tv_check_lock(lock, name)
20726 int lock;
20727 char_u *name;
20728{
20729 if (lock & VAR_LOCKED)
20730 {
20731 EMSG2(_("E741: Value is locked: %s"),
20732 name == NULL ? (char_u *)_("Unknown") : name);
20733 return TRUE;
20734 }
20735 if (lock & VAR_FIXED)
20736 {
20737 EMSG2(_("E742: Cannot change value of %s"),
20738 name == NULL ? (char_u *)_("Unknown") : name);
20739 return TRUE;
20740 }
20741 return FALSE;
20742}
20743
20744/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020745 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020746 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020747 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020748 * It is OK for "from" and "to" to point to the same item. This is used to
20749 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020750 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020751 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020752copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020753 typval_T *from;
20754 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020756 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020757 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020758 switch (from->v_type)
20759 {
20760 case VAR_NUMBER:
20761 to->vval.v_number = from->vval.v_number;
20762 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020763#ifdef FEAT_FLOAT
20764 case VAR_FLOAT:
20765 to->vval.v_float = from->vval.v_float;
20766 break;
20767#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020768 case VAR_STRING:
20769 case VAR_FUNC:
20770 if (from->vval.v_string == NULL)
20771 to->vval.v_string = NULL;
20772 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020773 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020774 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020775 if (from->v_type == VAR_FUNC)
20776 func_ref(to->vval.v_string);
20777 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020778 break;
20779 case VAR_LIST:
20780 if (from->vval.v_list == NULL)
20781 to->vval.v_list = NULL;
20782 else
20783 {
20784 to->vval.v_list = from->vval.v_list;
20785 ++to->vval.v_list->lv_refcount;
20786 }
20787 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020788 case VAR_DICT:
20789 if (from->vval.v_dict == NULL)
20790 to->vval.v_dict = NULL;
20791 else
20792 {
20793 to->vval.v_dict = from->vval.v_dict;
20794 ++to->vval.v_dict->dv_refcount;
20795 }
20796 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020797 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020798 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020799 break;
20800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801}
20802
20803/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020804 * Make a copy of an item.
20805 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020806 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20807 * reference to an already copied list/dict can be used.
20808 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020809 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020810 static int
20811item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020812 typval_T *from;
20813 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020814 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020815 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020816{
20817 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020818 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020819
Bram Moolenaar33570922005-01-25 22:26:29 +000020820 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020821 {
20822 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020823 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020824 }
20825 ++recurse;
20826
20827 switch (from->v_type)
20828 {
20829 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020830#ifdef FEAT_FLOAT
20831 case VAR_FLOAT:
20832#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020833 case VAR_STRING:
20834 case VAR_FUNC:
20835 copy_tv(from, to);
20836 break;
20837 case VAR_LIST:
20838 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020839 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020840 if (from->vval.v_list == NULL)
20841 to->vval.v_list = NULL;
20842 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20843 {
20844 /* use the copy made earlier */
20845 to->vval.v_list = from->vval.v_list->lv_copylist;
20846 ++to->vval.v_list->lv_refcount;
20847 }
20848 else
20849 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20850 if (to->vval.v_list == NULL)
20851 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020852 break;
20853 case VAR_DICT:
20854 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020855 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020856 if (from->vval.v_dict == NULL)
20857 to->vval.v_dict = NULL;
20858 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20859 {
20860 /* use the copy made earlier */
20861 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20862 ++to->vval.v_dict->dv_refcount;
20863 }
20864 else
20865 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20866 if (to->vval.v_dict == NULL)
20867 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020868 break;
20869 default:
20870 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020871 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020872 }
20873 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020874 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020875}
20876
20877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 * ":echo expr1 ..." print each argument separated with a space, add a
20879 * newline at the end.
20880 * ":echon expr1 ..." print each argument plain.
20881 */
20882 void
20883ex_echo(eap)
20884 exarg_T *eap;
20885{
20886 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020888 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 char_u *p;
20890 int needclr = TRUE;
20891 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020892 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893
20894 if (eap->skip)
20895 ++emsg_skip;
20896 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20897 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020898 /* If eval1() causes an error message the text from the command may
20899 * still need to be cleared. E.g., "echo 22,44". */
20900 need_clr_eos = needclr;
20901
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 {
20905 /*
20906 * Report the invalid expression unless the expression evaluation
20907 * has been cancelled due to an aborting error, an interrupt, or an
20908 * exception.
20909 */
20910 if (!aborting())
20911 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020912 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 break;
20914 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020915 need_clr_eos = FALSE;
20916
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 if (!eap->skip)
20918 {
20919 if (atstart)
20920 {
20921 atstart = FALSE;
20922 /* Call msg_start() after eval1(), evaluating the expression
20923 * may cause a message to appear. */
20924 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020925 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020926 /* Mark the saved text as finishing the line, so that what
20927 * follows is displayed on a new line when scrolling back
20928 * at the more prompt. */
20929 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 }
20933 else if (eap->cmdidx == CMD_echo)
20934 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020935 current_copyID += COPYID_INC;
20936 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020937 if (p != NULL)
20938 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020940 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020942 if (*p != TAB && needclr)
20943 {
20944 /* remove any text still there from the command */
20945 msg_clr_eos();
20946 needclr = FALSE;
20947 }
20948 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 }
20950 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020951 {
20952#ifdef FEAT_MBYTE
20953 if (has_mbyte)
20954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020955 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020956
20957 (void)msg_outtrans_len_attr(p, i, echo_attr);
20958 p += i - 1;
20959 }
20960 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020962 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020965 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020967 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 arg = skipwhite(arg);
20969 }
20970 eap->nextcmd = check_nextcmd(arg);
20971
20972 if (eap->skip)
20973 --emsg_skip;
20974 else
20975 {
20976 /* remove text that may still be there from the command */
20977 if (needclr)
20978 msg_clr_eos();
20979 if (eap->cmdidx == CMD_echo)
20980 msg_end();
20981 }
20982}
20983
20984/*
20985 * ":echohl {name}".
20986 */
20987 void
20988ex_echohl(eap)
20989 exarg_T *eap;
20990{
20991 int id;
20992
20993 id = syn_name2id(eap->arg);
20994 if (id == 0)
20995 echo_attr = 0;
20996 else
20997 echo_attr = syn_id2attr(id);
20998}
20999
21000/*
21001 * ":execute expr1 ..." execute the result of an expression.
21002 * ":echomsg expr1 ..." Print a message
21003 * ":echoerr expr1 ..." Print an error
21004 * Each gets spaces around each argument and a newline at the end for
21005 * echo commands
21006 */
21007 void
21008ex_execute(eap)
21009 exarg_T *eap;
21010{
21011 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021012 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 int ret = OK;
21014 char_u *p;
21015 garray_T ga;
21016 int len;
21017 int save_did_emsg;
21018
21019 ga_init2(&ga, 1, 80);
21020
21021 if (eap->skip)
21022 ++emsg_skip;
21023 while (*arg != NUL && *arg != '|' && *arg != '\n')
21024 {
21025 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021026 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 {
21028 /*
21029 * Report the invalid expression unless the expression evaluation
21030 * has been cancelled due to an aborting error, an interrupt, or an
21031 * exception.
21032 */
21033 if (!aborting())
21034 EMSG2(_(e_invexpr2), p);
21035 ret = FAIL;
21036 break;
21037 }
21038
21039 if (!eap->skip)
21040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021041 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 len = (int)STRLEN(p);
21043 if (ga_grow(&ga, len + 2) == FAIL)
21044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 ret = FAIL;
21047 break;
21048 }
21049 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 ga.ga_len += len;
21053 }
21054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021055 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 arg = skipwhite(arg);
21057 }
21058
21059 if (ret != FAIL && ga.ga_data != NULL)
21060 {
21061 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021064 out_flush();
21065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 else if (eap->cmdidx == CMD_echoerr)
21067 {
21068 /* We don't want to abort following commands, restore did_emsg. */
21069 save_did_emsg = did_emsg;
21070 EMSG((char_u *)ga.ga_data);
21071 if (!force_abort)
21072 did_emsg = save_did_emsg;
21073 }
21074 else if (eap->cmdidx == CMD_execute)
21075 do_cmdline((char_u *)ga.ga_data,
21076 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21077 }
21078
21079 ga_clear(&ga);
21080
21081 if (eap->skip)
21082 --emsg_skip;
21083
21084 eap->nextcmd = check_nextcmd(arg);
21085}
21086
21087/*
21088 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21089 * "arg" points to the "&" or '+' when called, to "option" when returning.
21090 * Returns NULL when no option name found. Otherwise pointer to the char
21091 * after the option name.
21092 */
21093 static char_u *
21094find_option_end(arg, opt_flags)
21095 char_u **arg;
21096 int *opt_flags;
21097{
21098 char_u *p = *arg;
21099
21100 ++p;
21101 if (*p == 'g' && p[1] == ':')
21102 {
21103 *opt_flags = OPT_GLOBAL;
21104 p += 2;
21105 }
21106 else if (*p == 'l' && p[1] == ':')
21107 {
21108 *opt_flags = OPT_LOCAL;
21109 p += 2;
21110 }
21111 else
21112 *opt_flags = 0;
21113
21114 if (!ASCII_ISALPHA(*p))
21115 return NULL;
21116 *arg = p;
21117
21118 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21119 p += 4; /* termcap option */
21120 else
21121 while (ASCII_ISALPHA(*p))
21122 ++p;
21123 return p;
21124}
21125
21126/*
21127 * ":function"
21128 */
21129 void
21130ex_function(eap)
21131 exarg_T *eap;
21132{
21133 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021134 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 int j;
21136 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021138 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 char_u *name = NULL;
21140 char_u *p;
21141 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021142 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 garray_T newargs;
21144 garray_T newlines;
21145 int varargs = FALSE;
21146 int mustend = FALSE;
21147 int flags = 0;
21148 ufunc_T *fp;
21149 int indent;
21150 int nesting;
21151 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021152 dictitem_T *v;
21153 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021154 static int func_nr = 0; /* number for nameless function */
21155 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021156 hashtab_T *ht;
21157 int todo;
21158 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021159 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160
21161 /*
21162 * ":function" without argument: list functions.
21163 */
21164 if (ends_excmd(*eap->arg))
21165 {
21166 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021167 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021168 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021169 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021170 {
21171 if (!HASHITEM_EMPTY(hi))
21172 {
21173 --todo;
21174 fp = HI2UF(hi);
21175 if (!isdigit(*fp->uf_name))
21176 list_func_head(fp, FALSE);
21177 }
21178 }
21179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 eap->nextcmd = check_nextcmd(eap->arg);
21181 return;
21182 }
21183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021184 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021185 * ":function /pat": list functions matching pattern.
21186 */
21187 if (*eap->arg == '/')
21188 {
21189 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21190 if (!eap->skip)
21191 {
21192 regmatch_T regmatch;
21193
21194 c = *p;
21195 *p = NUL;
21196 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21197 *p = c;
21198 if (regmatch.regprog != NULL)
21199 {
21200 regmatch.rm_ic = p_ic;
21201
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021202 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021203 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21204 {
21205 if (!HASHITEM_EMPTY(hi))
21206 {
21207 --todo;
21208 fp = HI2UF(hi);
21209 if (!isdigit(*fp->uf_name)
21210 && vim_regexec(&regmatch, fp->uf_name, 0))
21211 list_func_head(fp, FALSE);
21212 }
21213 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021214 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021215 }
21216 }
21217 if (*p == '/')
21218 ++p;
21219 eap->nextcmd = check_nextcmd(p);
21220 return;
21221 }
21222
21223 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021224 * Get the function name. There are these situations:
21225 * func normal function name
21226 * "name" == func, "fudi.fd_dict" == NULL
21227 * dict.func new dictionary entry
21228 * "name" == NULL, "fudi.fd_dict" set,
21229 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21230 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021231 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021232 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21233 * dict.func existing dict entry that's not a Funcref
21234 * "name" == NULL, "fudi.fd_dict" set,
21235 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 name = trans_function_name(&p, eap->skip, 0, &fudi);
21239 paren = (vim_strchr(p, '(') != NULL);
21240 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 {
21242 /*
21243 * Return on an invalid expression in braces, unless the expression
21244 * evaluation has been cancelled due to an aborting error, an
21245 * interrupt, or an exception.
21246 */
21247 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021248 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021249 if (!eap->skip && fudi.fd_newkey != NULL)
21250 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021251 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 else
21255 eap->skip = TRUE;
21256 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021257
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 /* An error in a function call during evaluation of an expression in magic
21259 * braces should not cause the function not to be defined. */
21260 saved_did_emsg = did_emsg;
21261 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262
21263 /*
21264 * ":function func" with only function name: list function.
21265 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021266 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 {
21268 if (!ends_excmd(*skipwhite(p)))
21269 {
21270 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021271 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 }
21273 eap->nextcmd = check_nextcmd(p);
21274 if (eap->nextcmd != NULL)
21275 *p = NUL;
21276 if (!eap->skip && !got_int)
21277 {
21278 fp = find_func(name);
21279 if (fp != NULL)
21280 {
21281 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021282 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021284 if (FUNCLINE(fp, j) == NULL)
21285 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 msg_putchar('\n');
21287 msg_outnum((long)(j + 1));
21288 if (j < 9)
21289 msg_putchar(' ');
21290 if (j < 99)
21291 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021292 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 out_flush(); /* show a line at a time */
21294 ui_breakcheck();
21295 }
21296 if (!got_int)
21297 {
21298 msg_putchar('\n');
21299 msg_puts((char_u *)" endfunction");
21300 }
21301 }
21302 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021303 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021305 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 }
21307
21308 /*
21309 * ":function name(arg1, arg2)" Define function.
21310 */
21311 p = skipwhite(p);
21312 if (*p != '(')
21313 {
21314 if (!eap->skip)
21315 {
21316 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021317 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 }
21319 /* attempt to continue by skipping some text */
21320 if (vim_strchr(p, '(') != NULL)
21321 p = vim_strchr(p, '(');
21322 }
21323 p = skipwhite(p + 1);
21324
21325 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21326 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21327
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021328 if (!eap->skip)
21329 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021330 /* Check the name of the function. Unless it's a dictionary function
21331 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021332 if (name != NULL)
21333 arg = name;
21334 else
21335 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021336 if (arg != NULL && (fudi.fd_di == NULL
21337 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021338 {
21339 if (*arg == K_SPECIAL)
21340 j = 3;
21341 else
21342 j = 0;
21343 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21344 : eval_isnamec(arg[j])))
21345 ++j;
21346 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021347 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021348 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021349 /* Disallow using the g: dict. */
21350 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21351 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021352 }
21353
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 /*
21355 * Isolate the arguments: "arg1, arg2, ...)"
21356 */
21357 while (*p != ')')
21358 {
21359 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21360 {
21361 varargs = TRUE;
21362 p += 3;
21363 mustend = TRUE;
21364 }
21365 else
21366 {
21367 arg = p;
21368 while (ASCII_ISALNUM(*p) || *p == '_')
21369 ++p;
21370 if (arg == p || isdigit(*arg)
21371 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21372 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21373 {
21374 if (!eap->skip)
21375 EMSG2(_("E125: Illegal argument: %s"), arg);
21376 break;
21377 }
21378 if (ga_grow(&newargs, 1) == FAIL)
21379 goto erret;
21380 c = *p;
21381 *p = NUL;
21382 arg = vim_strsave(arg);
21383 if (arg == NULL)
21384 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021385
21386 /* Check for duplicate argument name. */
21387 for (i = 0; i < newargs.ga_len; ++i)
21388 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21389 {
21390 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21391 goto erret;
21392 }
21393
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21395 *p = c;
21396 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 if (*p == ',')
21398 ++p;
21399 else
21400 mustend = TRUE;
21401 }
21402 p = skipwhite(p);
21403 if (mustend && *p != ')')
21404 {
21405 if (!eap->skip)
21406 EMSG2(_(e_invarg2), eap->arg);
21407 break;
21408 }
21409 }
21410 ++p; /* skip the ')' */
21411
Bram Moolenaare9a41262005-01-15 22:18:47 +000021412 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 for (;;)
21414 {
21415 p = skipwhite(p);
21416 if (STRNCMP(p, "range", 5) == 0)
21417 {
21418 flags |= FC_RANGE;
21419 p += 5;
21420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021421 else if (STRNCMP(p, "dict", 4) == 0)
21422 {
21423 flags |= FC_DICT;
21424 p += 4;
21425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 else if (STRNCMP(p, "abort", 5) == 0)
21427 {
21428 flags |= FC_ABORT;
21429 p += 5;
21430 }
21431 else
21432 break;
21433 }
21434
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021435 /* When there is a line break use what follows for the function body.
21436 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21437 if (*p == '\n')
21438 line_arg = p + 1;
21439 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 EMSG(_(e_trailing));
21441
21442 /*
21443 * Read the body of the function, until ":endfunction" is found.
21444 */
21445 if (KeyTyped)
21446 {
21447 /* Check if the function already exists, don't let the user type the
21448 * whole function before telling him it doesn't work! For a script we
21449 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021450 if (!eap->skip && !eap->forceit)
21451 {
21452 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21453 EMSG(_(e_funcdict));
21454 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021455 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021458 if (!eap->skip && did_emsg)
21459 goto erret;
21460
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 msg_putchar('\n'); /* don't overwrite the function name */
21462 cmdline_row = msg_row;
21463 }
21464
21465 indent = 2;
21466 nesting = 0;
21467 for (;;)
21468 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021469 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021470 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021471 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021472 saved_wait_return = FALSE;
21473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021475 sourcing_lnum_off = sourcing_lnum;
21476
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021477 if (line_arg != NULL)
21478 {
21479 /* Use eap->arg, split up in parts by line breaks. */
21480 theline = line_arg;
21481 p = vim_strchr(theline, '\n');
21482 if (p == NULL)
21483 line_arg += STRLEN(line_arg);
21484 else
21485 {
21486 *p = NUL;
21487 line_arg = p + 1;
21488 }
21489 }
21490 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 theline = getcmdline(':', 0L, indent);
21492 else
21493 theline = eap->getline(':', eap->cookie, indent);
21494 if (KeyTyped)
21495 lines_left = Rows - 1;
21496 if (theline == NULL)
21497 {
21498 EMSG(_("E126: Missing :endfunction"));
21499 goto erret;
21500 }
21501
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021502 /* Detect line continuation: sourcing_lnum increased more than one. */
21503 if (sourcing_lnum > sourcing_lnum_off + 1)
21504 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21505 else
21506 sourcing_lnum_off = 0;
21507
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 if (skip_until != NULL)
21509 {
21510 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21511 * don't check for ":endfunc". */
21512 if (STRCMP(theline, skip_until) == 0)
21513 {
21514 vim_free(skip_until);
21515 skip_until = NULL;
21516 }
21517 }
21518 else
21519 {
21520 /* skip ':' and blanks*/
21521 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21522 ;
21523
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021524 /* Check for "endfunction". */
21525 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021527 if (line_arg == NULL)
21528 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 break;
21530 }
21531
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021532 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 * at "end". */
21534 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21535 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021536 else if (STRNCMP(p, "if", 2) == 0
21537 || STRNCMP(p, "wh", 2) == 0
21538 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 || STRNCMP(p, "try", 3) == 0)
21540 indent += 2;
21541
21542 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021543 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021545 if (*p == '!')
21546 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 p += eval_fname_script(p);
21548 if (ASCII_ISALPHA(*p))
21549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021550 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 if (*skipwhite(p) == '(')
21552 {
21553 ++nesting;
21554 indent += 2;
21555 }
21556 }
21557 }
21558
21559 /* Check for ":append" or ":insert". */
21560 p = skip_range(p, NULL);
21561 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21562 || (p[0] == 'i'
21563 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21564 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21565 skip_until = vim_strsave((char_u *)".");
21566
21567 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21568 arg = skipwhite(skiptowhite(p));
21569 if (arg[0] == '<' && arg[1] =='<'
21570 && ((p[0] == 'p' && p[1] == 'y'
21571 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21572 || (p[0] == 'p' && p[1] == 'e'
21573 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21574 || (p[0] == 't' && p[1] == 'c'
21575 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021576 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21577 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21579 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021580 || (p[0] == 'm' && p[1] == 'z'
21581 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 ))
21583 {
21584 /* ":python <<" continues until a dot, like ":append" */
21585 p = skipwhite(arg + 2);
21586 if (*p == NUL)
21587 skip_until = vim_strsave((char_u *)".");
21588 else
21589 skip_until = vim_strsave(p);
21590 }
21591 }
21592
21593 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021594 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021595 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021596 if (line_arg == NULL)
21597 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021599 }
21600
21601 /* Copy the line to newly allocated memory. get_one_sourceline()
21602 * allocates 250 bytes per line, this saves 80% on average. The cost
21603 * is an extra alloc/free. */
21604 p = vim_strsave(theline);
21605 if (p != NULL)
21606 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021607 if (line_arg == NULL)
21608 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021609 theline = p;
21610 }
21611
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021612 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21613
21614 /* Add NULL lines for continuation lines, so that the line count is
21615 * equal to the index in the growarray. */
21616 while (sourcing_lnum_off-- > 0)
21617 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021618
21619 /* Check for end of eap->arg. */
21620 if (line_arg != NULL && *line_arg == NUL)
21621 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 }
21623
21624 /* Don't define the function when skipping commands or when an error was
21625 * detected. */
21626 if (eap->skip || did_emsg)
21627 goto erret;
21628
21629 /*
21630 * If there are no errors, add the function
21631 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021633 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021634 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021636 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021637 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021638 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021639 goto erret;
21640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021641
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642 fp = find_func(name);
21643 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021645 if (!eap->forceit)
21646 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021647 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021648 goto erret;
21649 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021652 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021653 name);
21654 goto erret;
21655 }
21656 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021657 ga_clear_strings(&(fp->uf_args));
21658 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021659 vim_free(name);
21660 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 }
21663 else
21664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021665 char numbuf[20];
21666
21667 fp = NULL;
21668 if (fudi.fd_newkey == NULL && !eap->forceit)
21669 {
21670 EMSG(_(e_funcdict));
21671 goto erret;
21672 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021673 if (fudi.fd_di == NULL)
21674 {
21675 /* Can't add a function to a locked dictionary */
21676 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21677 goto erret;
21678 }
21679 /* Can't change an existing function if it is locked */
21680 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21681 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021682
21683 /* Give the function a sequential number. Can only be used with a
21684 * Funcref! */
21685 vim_free(name);
21686 sprintf(numbuf, "%d", ++func_nr);
21687 name = vim_strsave((char_u *)numbuf);
21688 if (name == NULL)
21689 goto erret;
21690 }
21691
21692 if (fp == NULL)
21693 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021694 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021695 {
21696 int slen, plen;
21697 char_u *scriptname;
21698
21699 /* Check that the autoload name matches the script name. */
21700 j = FAIL;
21701 if (sourcing_name != NULL)
21702 {
21703 scriptname = autoload_name(name);
21704 if (scriptname != NULL)
21705 {
21706 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021707 plen = (int)STRLEN(p);
21708 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021709 if (slen > plen && fnamecmp(p,
21710 sourcing_name + slen - plen) == 0)
21711 j = OK;
21712 vim_free(scriptname);
21713 }
21714 }
21715 if (j == FAIL)
21716 {
21717 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21718 goto erret;
21719 }
21720 }
21721
21722 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 if (fp == NULL)
21724 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021725
21726 if (fudi.fd_dict != NULL)
21727 {
21728 if (fudi.fd_di == NULL)
21729 {
21730 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021731 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021732 if (fudi.fd_di == NULL)
21733 {
21734 vim_free(fp);
21735 goto erret;
21736 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021737 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21738 {
21739 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021740 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021741 goto erret;
21742 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021743 }
21744 else
21745 /* overwrite existing dict entry */
21746 clear_tv(&fudi.fd_di->di_tv);
21747 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021748 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021749 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021750 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021751
21752 /* behave like "dict" was used */
21753 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021754 }
21755
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021757 STRCPY(fp->uf_name, name);
21758 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021760 fp->uf_args = newargs;
21761 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021762#ifdef FEAT_PROFILE
21763 fp->uf_tml_count = NULL;
21764 fp->uf_tml_total = NULL;
21765 fp->uf_tml_self = NULL;
21766 fp->uf_profiling = FALSE;
21767 if (prof_def_func())
21768 func_do_profile(fp);
21769#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021770 fp->uf_varargs = varargs;
21771 fp->uf_flags = flags;
21772 fp->uf_calls = 0;
21773 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021774 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775
21776erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 ga_clear_strings(&newargs);
21778 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021779ret_free:
21780 vim_free(skip_until);
21781 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021784 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785}
21786
21787/*
21788 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021789 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021791 * flags:
21792 * TFN_INT: internal function name OK
21793 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 * Advances "pp" to just after the function name (if no error).
21795 */
21796 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021797trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 char_u **pp;
21799 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021803 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 char_u *start;
21805 char_u *end;
21806 int lead;
21807 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021810
21811 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021814
21815 /* Check for hard coded <SNR>: already translated function ID (from a user
21816 * command). */
21817 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21818 && (*pp)[2] == (int)KE_SNR)
21819 {
21820 *pp += 3;
21821 len = get_id_len(pp) + 3;
21822 return vim_strnsave(start, len);
21823 }
21824
21825 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21826 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021828 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021830
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021831 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21832 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021833 if (end == start)
21834 {
21835 if (!skip)
21836 EMSG(_("E129: Function name required"));
21837 goto theend;
21838 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021839 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021840 {
21841 /*
21842 * Report an invalid expression in braces, unless the expression
21843 * evaluation has been cancelled due to an aborting error, an
21844 * interrupt, or an exception.
21845 */
21846 if (!aborting())
21847 {
21848 if (end != NULL)
21849 EMSG2(_(e_invarg2), start);
21850 }
21851 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021852 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021853 goto theend;
21854 }
21855
21856 if (lv.ll_tv != NULL)
21857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021858 if (fdp != NULL)
21859 {
21860 fdp->fd_dict = lv.ll_dict;
21861 fdp->fd_newkey = lv.ll_newkey;
21862 lv.ll_newkey = NULL;
21863 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021864 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021865 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21866 {
21867 name = vim_strsave(lv.ll_tv->vval.v_string);
21868 *pp = end;
21869 }
21870 else
21871 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021872 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21873 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021874 EMSG(_(e_funcref));
21875 else
21876 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021877 name = NULL;
21878 }
21879 goto theend;
21880 }
21881
21882 if (lv.ll_name == NULL)
21883 {
21884 /* Error found, but continue after the function name. */
21885 *pp = end;
21886 goto theend;
21887 }
21888
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021889 /* Check if the name is a Funcref. If so, use the value. */
21890 if (lv.ll_exp_name != NULL)
21891 {
21892 len = (int)STRLEN(lv.ll_exp_name);
21893 name = deref_func_name(lv.ll_exp_name, &len);
21894 if (name == lv.ll_exp_name)
21895 name = NULL;
21896 }
21897 else
21898 {
21899 len = (int)(end - *pp);
21900 name = deref_func_name(*pp, &len);
21901 if (name == *pp)
21902 name = NULL;
21903 }
21904 if (name != NULL)
21905 {
21906 name = vim_strsave(name);
21907 *pp = end;
21908 goto theend;
21909 }
21910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021911 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021912 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021913 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021914 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21915 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21916 {
21917 /* When there was "s:" already or the name expanded to get a
21918 * leading "s:" then remove it. */
21919 lv.ll_name += 2;
21920 len -= 2;
21921 lead = 2;
21922 }
21923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021924 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021925 {
21926 if (lead == 2) /* skip over "s:" */
21927 lv.ll_name += 2;
21928 len = (int)(end - lv.ll_name);
21929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021930
21931 /*
21932 * Copy the function name to allocated memory.
21933 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21934 * Accept <SNR>123_name() outside a script.
21935 */
21936 if (skip)
21937 lead = 0; /* do nothing */
21938 else if (lead > 0)
21939 {
21940 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021941 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21942 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021943 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021944 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021945 if (current_SID <= 0)
21946 {
21947 EMSG(_(e_usingsid));
21948 goto theend;
21949 }
21950 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21951 lead += (int)STRLEN(sid_buf);
21952 }
21953 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021954 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021955 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021956 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021957 goto theend;
21958 }
21959 name = alloc((unsigned)(len + lead + 1));
21960 if (name != NULL)
21961 {
21962 if (lead > 0)
21963 {
21964 name[0] = K_SPECIAL;
21965 name[1] = KS_EXTRA;
21966 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021967 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021968 STRCPY(name + 3, sid_buf);
21969 }
21970 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21971 name[len + lead] = NUL;
21972 }
21973 *pp = end;
21974
21975theend:
21976 clear_lval(&lv);
21977 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978}
21979
21980/*
21981 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21982 * Return 2 if "p" starts with "s:".
21983 * Return 0 otherwise.
21984 */
21985 static int
21986eval_fname_script(p)
21987 char_u *p;
21988{
21989 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21990 || STRNICMP(p + 1, "SNR>", 4) == 0))
21991 return 5;
21992 if (p[0] == 's' && p[1] == ':')
21993 return 2;
21994 return 0;
21995}
21996
21997/*
21998 * Return TRUE if "p" starts with "<SID>" or "s:".
21999 * Only works if eval_fname_script() returned non-zero for "p"!
22000 */
22001 static int
22002eval_fname_sid(p)
22003 char_u *p;
22004{
22005 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22006}
22007
22008/*
22009 * List the head of the function: "name(arg1, arg2)".
22010 */
22011 static void
22012list_func_head(fp, indent)
22013 ufunc_T *fp;
22014 int indent;
22015{
22016 int j;
22017
22018 msg_start();
22019 if (indent)
22020 MSG_PUTS(" ");
22021 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022022 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 {
22024 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022025 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 }
22027 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022028 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022030 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 {
22032 if (j)
22033 MSG_PUTS(", ");
22034 msg_puts(FUNCARG(fp, j));
22035 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022036 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 {
22038 if (j)
22039 MSG_PUTS(", ");
22040 MSG_PUTS("...");
22041 }
22042 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022043 if (fp->uf_flags & FC_ABORT)
22044 MSG_PUTS(" abort");
22045 if (fp->uf_flags & FC_RANGE)
22046 MSG_PUTS(" range");
22047 if (fp->uf_flags & FC_DICT)
22048 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022049 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022050 if (p_verbose > 0)
22051 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052}
22053
22054/*
22055 * Find a function by name, return pointer to it in ufuncs.
22056 * Return NULL for unknown function.
22057 */
22058 static ufunc_T *
22059find_func(name)
22060 char_u *name;
22061{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022062 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022064 hi = hash_find(&func_hashtab, name);
22065 if (!HASHITEM_EMPTY(hi))
22066 return HI2UF(hi);
22067 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068}
22069
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022070#if defined(EXITFREE) || defined(PROTO)
22071 void
22072free_all_functions()
22073{
22074 hashitem_T *hi;
22075
22076 /* Need to start all over every time, because func_free() may change the
22077 * hash table. */
22078 while (func_hashtab.ht_used > 0)
22079 for (hi = func_hashtab.ht_array; ; ++hi)
22080 if (!HASHITEM_EMPTY(hi))
22081 {
22082 func_free(HI2UF(hi));
22083 break;
22084 }
22085}
22086#endif
22087
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022088 int
22089translated_function_exists(name)
22090 char_u *name;
22091{
22092 if (builtin_function(name))
22093 return find_internal_func(name) >= 0;
22094 return find_func(name) != NULL;
22095}
22096
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022097/*
22098 * Return TRUE if a function "name" exists.
22099 */
22100 static int
22101function_exists(name)
22102 char_u *name;
22103{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022104 char_u *nm = name;
22105 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106 int n = FALSE;
22107
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022108 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022109 nm = skipwhite(nm);
22110
22111 /* Only accept "funcname", "funcname ", "funcname (..." and
22112 * "funcname(...", not "funcname!...". */
22113 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022114 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022115 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022116 return n;
22117}
22118
Bram Moolenaara1544c02013-05-30 12:35:52 +020022119 char_u *
22120get_expanded_name(name, check)
22121 char_u *name;
22122 int check;
22123{
22124 char_u *nm = name;
22125 char_u *p;
22126
22127 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22128
22129 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022130 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022131 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022132
Bram Moolenaara1544c02013-05-30 12:35:52 +020022133 vim_free(p);
22134 return NULL;
22135}
22136
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022137/*
22138 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022139 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022140 */
22141 static int
22142builtin_function(name)
22143 char_u *name;
22144{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022145 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22146 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022147}
22148
Bram Moolenaar05159a02005-02-26 23:04:13 +000022149#if defined(FEAT_PROFILE) || defined(PROTO)
22150/*
22151 * Start profiling function "fp".
22152 */
22153 static void
22154func_do_profile(fp)
22155 ufunc_T *fp;
22156{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022157 int len = fp->uf_lines.ga_len;
22158
22159 if (len == 0)
22160 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022161 fp->uf_tm_count = 0;
22162 profile_zero(&fp->uf_tm_self);
22163 profile_zero(&fp->uf_tm_total);
22164 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022165 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022166 if (fp->uf_tml_total == NULL)
22167 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022168 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022169 if (fp->uf_tml_self == NULL)
22170 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022171 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022172 fp->uf_tml_idx = -1;
22173 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22174 || fp->uf_tml_self == NULL)
22175 return; /* out of memory */
22176
22177 fp->uf_profiling = TRUE;
22178}
22179
22180/*
22181 * Dump the profiling results for all functions in file "fd".
22182 */
22183 void
22184func_dump_profile(fd)
22185 FILE *fd;
22186{
22187 hashitem_T *hi;
22188 int todo;
22189 ufunc_T *fp;
22190 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022191 ufunc_T **sorttab;
22192 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022193
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022194 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022195 if (todo == 0)
22196 return; /* nothing to dump */
22197
Bram Moolenaar73830342005-02-28 22:48:19 +000022198 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22199
Bram Moolenaar05159a02005-02-26 23:04:13 +000022200 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22201 {
22202 if (!HASHITEM_EMPTY(hi))
22203 {
22204 --todo;
22205 fp = HI2UF(hi);
22206 if (fp->uf_profiling)
22207 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022208 if (sorttab != NULL)
22209 sorttab[st_len++] = fp;
22210
Bram Moolenaar05159a02005-02-26 23:04:13 +000022211 if (fp->uf_name[0] == K_SPECIAL)
22212 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22213 else
22214 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22215 if (fp->uf_tm_count == 1)
22216 fprintf(fd, "Called 1 time\n");
22217 else
22218 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22219 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22220 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22221 fprintf(fd, "\n");
22222 fprintf(fd, "count total (s) self (s)\n");
22223
22224 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22225 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022226 if (FUNCLINE(fp, i) == NULL)
22227 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022228 prof_func_line(fd, fp->uf_tml_count[i],
22229 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022230 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22231 }
22232 fprintf(fd, "\n");
22233 }
22234 }
22235 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022236
22237 if (sorttab != NULL && st_len > 0)
22238 {
22239 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22240 prof_total_cmp);
22241 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22242 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22243 prof_self_cmp);
22244 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22245 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022246
22247 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022248}
Bram Moolenaar73830342005-02-28 22:48:19 +000022249
22250 static void
22251prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22252 FILE *fd;
22253 ufunc_T **sorttab;
22254 int st_len;
22255 char *title;
22256 int prefer_self; /* when equal print only self time */
22257{
22258 int i;
22259 ufunc_T *fp;
22260
22261 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22262 fprintf(fd, "count total (s) self (s) function\n");
22263 for (i = 0; i < 20 && i < st_len; ++i)
22264 {
22265 fp = sorttab[i];
22266 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22267 prefer_self);
22268 if (fp->uf_name[0] == K_SPECIAL)
22269 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22270 else
22271 fprintf(fd, " %s()\n", fp->uf_name);
22272 }
22273 fprintf(fd, "\n");
22274}
22275
22276/*
22277 * Print the count and times for one function or function line.
22278 */
22279 static void
22280prof_func_line(fd, count, total, self, prefer_self)
22281 FILE *fd;
22282 int count;
22283 proftime_T *total;
22284 proftime_T *self;
22285 int prefer_self; /* when equal print only self time */
22286{
22287 if (count > 0)
22288 {
22289 fprintf(fd, "%5d ", count);
22290 if (prefer_self && profile_equal(total, self))
22291 fprintf(fd, " ");
22292 else
22293 fprintf(fd, "%s ", profile_msg(total));
22294 if (!prefer_self && profile_equal(total, self))
22295 fprintf(fd, " ");
22296 else
22297 fprintf(fd, "%s ", profile_msg(self));
22298 }
22299 else
22300 fprintf(fd, " ");
22301}
22302
22303/*
22304 * Compare function for total time sorting.
22305 */
22306 static int
22307#ifdef __BORLANDC__
22308_RTLENTRYF
22309#endif
22310prof_total_cmp(s1, s2)
22311 const void *s1;
22312 const void *s2;
22313{
22314 ufunc_T *p1, *p2;
22315
22316 p1 = *(ufunc_T **)s1;
22317 p2 = *(ufunc_T **)s2;
22318 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22319}
22320
22321/*
22322 * Compare function for self time sorting.
22323 */
22324 static int
22325#ifdef __BORLANDC__
22326_RTLENTRYF
22327#endif
22328prof_self_cmp(s1, s2)
22329 const void *s1;
22330 const void *s2;
22331{
22332 ufunc_T *p1, *p2;
22333
22334 p1 = *(ufunc_T **)s1;
22335 p2 = *(ufunc_T **)s2;
22336 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22337}
22338
Bram Moolenaar05159a02005-02-26 23:04:13 +000022339#endif
22340
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022341/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022342 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022343 * Return TRUE if a package was loaded.
22344 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022345 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022346script_autoload(name, reload)
22347 char_u *name;
22348 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022349{
22350 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022351 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022352 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022353 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022354
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022355 /* Return quickly when autoload disabled. */
22356 if (no_autoload)
22357 return FALSE;
22358
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022359 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022360 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022361 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022362 return FALSE;
22363
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022364 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022365
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022366 /* Find the name in the list of previously loaded package names. Skip
22367 * "autoload/", it's always the same. */
22368 for (i = 0; i < ga_loaded.ga_len; ++i)
22369 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22370 break;
22371 if (!reload && i < ga_loaded.ga_len)
22372 ret = FALSE; /* was loaded already */
22373 else
22374 {
22375 /* Remember the name if it wasn't loaded already. */
22376 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22377 {
22378 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22379 tofree = NULL;
22380 }
22381
22382 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022383 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022384 ret = TRUE;
22385 }
22386
22387 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022388 return ret;
22389}
22390
22391/*
22392 * Return the autoload script name for a function or variable name.
22393 * Returns NULL when out of memory.
22394 */
22395 static char_u *
22396autoload_name(name)
22397 char_u *name;
22398{
22399 char_u *p;
22400 char_u *scriptname;
22401
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022402 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022403 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22404 if (scriptname == NULL)
22405 return FALSE;
22406 STRCPY(scriptname, "autoload/");
22407 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022408 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022409 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022410 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022411 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022412 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022413}
22414
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22416
22417/*
22418 * Function given to ExpandGeneric() to obtain the list of user defined
22419 * function names.
22420 */
22421 char_u *
22422get_user_func_name(xp, idx)
22423 expand_T *xp;
22424 int idx;
22425{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022426 static long_u done;
22427 static hashitem_T *hi;
22428 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429
22430 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022432 done = 0;
22433 hi = func_hashtab.ht_array;
22434 }
22435 if (done < func_hashtab.ht_used)
22436 {
22437 if (done++ > 0)
22438 ++hi;
22439 while (HASHITEM_EMPTY(hi))
22440 ++hi;
22441 fp = HI2UF(hi);
22442
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022443 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022444 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022445
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022446 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22447 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448
22449 cat_func_name(IObuff, fp);
22450 if (xp->xp_context != EXPAND_USER_FUNC)
22451 {
22452 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022453 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 STRCAT(IObuff, ")");
22455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 return IObuff;
22457 }
22458 return NULL;
22459}
22460
22461#endif /* FEAT_CMDL_COMPL */
22462
22463/*
22464 * Copy the function name of "fp" to buffer "buf".
22465 * "buf" must be able to hold the function name plus three bytes.
22466 * Takes care of script-local function names.
22467 */
22468 static void
22469cat_func_name(buf, fp)
22470 char_u *buf;
22471 ufunc_T *fp;
22472{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022473 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 {
22475 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022476 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 }
22478 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022479 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480}
22481
22482/*
22483 * ":delfunction {name}"
22484 */
22485 void
22486ex_delfunction(eap)
22487 exarg_T *eap;
22488{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 char_u *p;
22491 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493
22494 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022495 name = trans_function_name(&p, eap->skip, 0, &fudi);
22496 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 {
22499 if (fudi.fd_dict != NULL && !eap->skip)
22500 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 if (!ends_excmd(*skipwhite(p)))
22504 {
22505 vim_free(name);
22506 EMSG(_(e_trailing));
22507 return;
22508 }
22509 eap->nextcmd = check_nextcmd(p);
22510 if (eap->nextcmd != NULL)
22511 *p = NUL;
22512
22513 if (!eap->skip)
22514 fp = find_func(name);
22515 vim_free(name);
22516
22517 if (!eap->skip)
22518 {
22519 if (fp == NULL)
22520 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022521 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 return;
22523 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022524 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 {
22526 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22527 return;
22528 }
22529
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022530 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022532 /* Delete the dict item that refers to the function, it will
22533 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022534 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022536 else
22537 func_free(fp);
22538 }
22539}
22540
22541/*
22542 * Free a function and remove it from the list of functions.
22543 */
22544 static void
22545func_free(fp)
22546 ufunc_T *fp;
22547{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022548 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022549
22550 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022551 ga_clear_strings(&(fp->uf_args));
22552 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022553#ifdef FEAT_PROFILE
22554 vim_free(fp->uf_tml_count);
22555 vim_free(fp->uf_tml_total);
22556 vim_free(fp->uf_tml_self);
22557#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022558
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022559 /* remove the function from the function hashtable */
22560 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22561 if (HASHITEM_EMPTY(hi))
22562 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022563 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022564 hash_remove(&func_hashtab, hi);
22565
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022566 vim_free(fp);
22567}
22568
22569/*
22570 * Unreference a Function: decrement the reference count and free it when it
22571 * becomes zero. Only for numbered functions.
22572 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022573 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022574func_unref(name)
22575 char_u *name;
22576{
22577 ufunc_T *fp;
22578
22579 if (name != NULL && isdigit(*name))
22580 {
22581 fp = find_func(name);
22582 if (fp == NULL)
22583 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022584 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022585 {
22586 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022587 * when "uf_calls" becomes zero. */
22588 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022589 func_free(fp);
22590 }
22591 }
22592}
22593
22594/*
22595 * Count a reference to a Function.
22596 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022597 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022598func_ref(name)
22599 char_u *name;
22600{
22601 ufunc_T *fp;
22602
22603 if (name != NULL && isdigit(*name))
22604 {
22605 fp = find_func(name);
22606 if (fp == NULL)
22607 EMSG2(_(e_intern2), "func_ref()");
22608 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022609 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 }
22611}
22612
22613/*
22614 * Call a user function.
22615 */
22616 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022617call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 ufunc_T *fp; /* pointer to function */
22619 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 typval_T *argvars; /* arguments */
22621 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 linenr_T firstline; /* first line of range */
22623 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625{
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 char_u *save_sourcing_name;
22627 linenr_T save_sourcing_lnum;
22628 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022629 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022630 int save_did_emsg;
22631 static int depth = 0;
22632 dictitem_T *v;
22633 int fixvar_idx = 0; /* index in fixvar[] */
22634 int i;
22635 int ai;
22636 char_u numbuf[NUMBUFLEN];
22637 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022638#ifdef FEAT_PROFILE
22639 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022640 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642
22643 /* If depth of calling is getting too high, don't execute the function */
22644 if (depth >= p_mfd)
22645 {
22646 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022647 rettv->v_type = VAR_NUMBER;
22648 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 return;
22650 }
22651 ++depth;
22652
22653 line_breakcheck(); /* check for CTRL-C hit */
22654
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022655 fc = (funccall_T *)alloc(sizeof(funccall_T));
22656 fc->caller = current_funccal;
22657 current_funccal = fc;
22658 fc->func = fp;
22659 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022660 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022661 fc->linenr = 0;
22662 fc->returned = FALSE;
22663 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022665 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22666 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667
Bram Moolenaar33570922005-01-25 22:26:29 +000022668 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022669 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22671 * each argument variable and saves a lot of time.
22672 */
22673 /*
22674 * Init l: variables.
22675 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022676 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022677 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022678 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022679 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22680 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022681 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022682 name = v->di_key;
22683 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022684 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022685 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022686 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022687 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 v->di_tv.vval.v_dict = selfdict;
22689 ++selfdict->dv_refcount;
22690 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022691
Bram Moolenaar33570922005-01-25 22:26:29 +000022692 /*
22693 * Init a: variables.
22694 * Set a:0 to "argcount".
22695 * Set a:000 to a list with room for the "..." arguments.
22696 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022697 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022698 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022699 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022700 /* Use "name" to avoid a warning from some compiler that checks the
22701 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022702 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022703 name = v->di_key;
22704 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022705 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022706 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022707 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022708 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022709 v->di_tv.vval.v_list = &fc->l_varlist;
22710 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22711 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22712 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022713
22714 /*
22715 * Set a:firstline to "firstline" and a:lastline to "lastline".
22716 * Set a:name to named arguments.
22717 * Set a:N to the "..." arguments.
22718 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022719 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022720 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022721 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022722 (varnumber_T)lastline);
22723 for (i = 0; i < argcount; ++i)
22724 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022725 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 if (ai < 0)
22727 /* named argument a:name */
22728 name = FUNCARG(fp, i);
22729 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022730 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 /* "..." argument a:1, a:2, etc. */
22732 sprintf((char *)numbuf, "%d", ai + 1);
22733 name = numbuf;
22734 }
22735 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22736 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022737 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22739 }
22740 else
22741 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022742 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22743 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 if (v == NULL)
22745 break;
22746 v->di_flags = DI_FLAGS_RO;
22747 }
22748 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022749 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022750
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022751 /* Note: the values are copied directly to avoid alloc/free.
22752 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022754 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022755
22756 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22757 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022758 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22759 fc->l_listitems[ai].li_tv = argvars[i];
22760 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022761 }
22762 }
22763
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 /* Don't redraw while executing the function. */
22765 ++RedrawingDisabled;
22766 save_sourcing_name = sourcing_name;
22767 save_sourcing_lnum = sourcing_lnum;
22768 sourcing_lnum = 1;
22769 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022770 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 if (sourcing_name != NULL)
22772 {
22773 if (save_sourcing_name != NULL
22774 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22775 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22776 else
22777 STRCPY(sourcing_name, "function ");
22778 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22779
22780 if (p_verbose >= 12)
22781 {
22782 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022783 verbose_enter_scroll();
22784
Bram Moolenaar555b2802005-05-19 21:08:39 +000022785 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 if (p_verbose >= 14)
22787 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022789 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022790 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022791 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792
22793 msg_puts((char_u *)"(");
22794 for (i = 0; i < argcount; ++i)
22795 {
22796 if (i > 0)
22797 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022798 if (argvars[i].v_type == VAR_NUMBER)
22799 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 else
22801 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022802 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22803 if (s != NULL)
22804 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022805 if (vim_strsize(s) > MSG_BUF_CLEN)
22806 {
22807 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22808 s = buf;
22809 }
22810 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022811 vim_free(tofree);
22812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 }
22814 }
22815 msg_puts((char_u *)")");
22816 }
22817 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022818
22819 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 --no_wait_return;
22821 }
22822 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022823#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022824 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022825 {
22826 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22827 func_do_profile(fp);
22828 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022829 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022830 {
22831 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022832 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022833 profile_zero(&fp->uf_tm_children);
22834 }
22835 script_prof_save(&wait_start);
22836 }
22837#endif
22838
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022840 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 save_did_emsg = did_emsg;
22842 did_emsg = FALSE;
22843
22844 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022845 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22847
22848 --RedrawingDisabled;
22849
22850 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022851 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022853 clear_tv(rettv);
22854 rettv->v_type = VAR_NUMBER;
22855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 }
22857
Bram Moolenaar05159a02005-02-26 23:04:13 +000022858#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022859 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022860 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022861 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022862 profile_end(&call_start);
22863 profile_sub_wait(&wait_start, &call_start);
22864 profile_add(&fp->uf_tm_total, &call_start);
22865 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022866 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022867 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022868 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22869 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022870 }
22871 }
22872#endif
22873
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 /* when being verbose, mention the return value */
22875 if (p_verbose >= 12)
22876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022878 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022881 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022882 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022883 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022884 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022887 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022888 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022889 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022890 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022891
Bram Moolenaar555b2802005-05-19 21:08:39 +000022892 /* The value may be very long. Skip the middle part, so that we
22893 * have some idea how it starts and ends. smsg() would always
22894 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022895 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022896 if (s != NULL)
22897 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022898 if (vim_strsize(s) > MSG_BUF_CLEN)
22899 {
22900 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22901 s = buf;
22902 }
22903 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022904 vim_free(tofree);
22905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 }
22907 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022908
22909 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 --no_wait_return;
22911 }
22912
22913 vim_free(sourcing_name);
22914 sourcing_name = save_sourcing_name;
22915 sourcing_lnum = save_sourcing_lnum;
22916 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022917#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022918 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022919 script_prof_restore(&wait_start);
22920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921
22922 if (p_verbose >= 12 && sourcing_name != NULL)
22923 {
22924 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022925 verbose_enter_scroll();
22926
Bram Moolenaar555b2802005-05-19 21:08:39 +000022927 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022929
22930 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 --no_wait_return;
22932 }
22933
22934 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022935 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022937
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022938 /* If the a:000 list and the l: and a: dicts are not referenced we can
22939 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022940 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22941 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22942 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22943 {
22944 free_funccal(fc, FALSE);
22945 }
22946 else
22947 {
22948 hashitem_T *hi;
22949 listitem_T *li;
22950 int todo;
22951
22952 /* "fc" is still in use. This can happen when returning "a:000" or
22953 * assigning "l:" to a global variable.
22954 * Link "fc" in the list for garbage collection later. */
22955 fc->caller = previous_funccal;
22956 previous_funccal = fc;
22957
22958 /* Make a copy of the a: variables, since we didn't do that above. */
22959 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22960 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22961 {
22962 if (!HASHITEM_EMPTY(hi))
22963 {
22964 --todo;
22965 v = HI2DI(hi);
22966 copy_tv(&v->di_tv, &v->di_tv);
22967 }
22968 }
22969
22970 /* Make a copy of the a:000 items, since we didn't do that above. */
22971 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22972 copy_tv(&li->li_tv, &li->li_tv);
22973 }
22974}
22975
22976/*
22977 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022978 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022979 */
22980 static int
22981can_free_funccal(fc, copyID)
22982 funccall_T *fc;
22983 int copyID;
22984{
22985 return (fc->l_varlist.lv_copyID != copyID
22986 && fc->l_vars.dv_copyID != copyID
22987 && fc->l_avars.dv_copyID != copyID);
22988}
22989
22990/*
22991 * Free "fc" and what it contains.
22992 */
22993 static void
22994free_funccal(fc, free_val)
22995 funccall_T *fc;
22996 int free_val; /* a: vars were allocated */
22997{
22998 listitem_T *li;
22999
23000 /* The a: variables typevals may not have been allocated, only free the
23001 * allocated variables. */
23002 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23003
23004 /* free all l: variables */
23005 vars_clear(&fc->l_vars.dv_hashtab);
23006
23007 /* Free the a:000 variables if they were allocated. */
23008 if (free_val)
23009 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23010 clear_tv(&li->li_tv);
23011
23012 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013}
23014
23015/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023016 * Add a number variable "name" to dict "dp" with value "nr".
23017 */
23018 static void
23019add_nr_var(dp, v, name, nr)
23020 dict_T *dp;
23021 dictitem_T *v;
23022 char *name;
23023 varnumber_T nr;
23024{
23025 STRCPY(v->di_key, name);
23026 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23027 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23028 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023029 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023030 v->di_tv.vval.v_number = nr;
23031}
23032
23033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 * ":return [expr]"
23035 */
23036 void
23037ex_return(eap)
23038 exarg_T *eap;
23039{
23040 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 int returning = FALSE;
23043
23044 if (current_funccal == NULL)
23045 {
23046 EMSG(_("E133: :return not inside a function"));
23047 return;
23048 }
23049
23050 if (eap->skip)
23051 ++emsg_skip;
23052
23053 eap->nextcmd = NULL;
23054 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023055 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 {
23057 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023058 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023060 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 }
23062 /* It's safer to return also on error. */
23063 else if (!eap->skip)
23064 {
23065 /*
23066 * Return unless the expression evaluation has been cancelled due to an
23067 * aborting error, an interrupt, or an exception.
23068 */
23069 if (!aborting())
23070 returning = do_return(eap, FALSE, TRUE, NULL);
23071 }
23072
23073 /* When skipping or the return gets pending, advance to the next command
23074 * in this line (!returning). Otherwise, ignore the rest of the line.
23075 * Following lines will be ignored by get_func_line(). */
23076 if (returning)
23077 eap->nextcmd = NULL;
23078 else if (eap->nextcmd == NULL) /* no argument */
23079 eap->nextcmd = check_nextcmd(arg);
23080
23081 if (eap->skip)
23082 --emsg_skip;
23083}
23084
23085/*
23086 * Return from a function. Possibly makes the return pending. Also called
23087 * for a pending return at the ":endtry" or after returning from an extra
23088 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023089 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023090 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 * FALSE when the return gets pending.
23092 */
23093 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023094do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 exarg_T *eap;
23096 int reanimate;
23097 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023098 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099{
23100 int idx;
23101 struct condstack *cstack = eap->cstack;
23102
23103 if (reanimate)
23104 /* Undo the return. */
23105 current_funccal->returned = FALSE;
23106
23107 /*
23108 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23109 * not in its finally clause (which then is to be executed next) is found.
23110 * In this case, make the ":return" pending for execution at the ":endtry".
23111 * Otherwise, return normally.
23112 */
23113 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23114 if (idx >= 0)
23115 {
23116 cstack->cs_pending[idx] = CSTP_RETURN;
23117
23118 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023119 /* A pending return again gets pending. "rettv" points to an
23120 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023122 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 else
23124 {
23125 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023126 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023128 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023130 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 {
23132 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023133 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 else
23136 EMSG(_(e_outofmem));
23137 }
23138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023139 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140
23141 if (reanimate)
23142 {
23143 /* The pending return value could be overwritten by a ":return"
23144 * without argument in a finally clause; reset the default
23145 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023146 current_funccal->rettv->v_type = VAR_NUMBER;
23147 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 }
23149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 }
23152 else
23153 {
23154 current_funccal->returned = TRUE;
23155
23156 /* If the return is carried out now, store the return value. For
23157 * a return immediately after reanimation, the value is already
23158 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023159 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023161 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023164 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 }
23166 }
23167
23168 return idx < 0;
23169}
23170
23171/*
23172 * Free the variable with a pending return value.
23173 */
23174 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023175discard_pending_return(rettv)
23176 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177{
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179}
23180
23181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023182 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 * is an allocated string. Used by report_pending() for verbose messages.
23184 */
23185 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023186get_return_cmd(rettv)
23187 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023189 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023190 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023191 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023193 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023194 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023195 if (s == NULL)
23196 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023197
23198 STRCPY(IObuff, ":return ");
23199 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23200 if (STRLEN(s) + 8 >= IOSIZE)
23201 STRCPY(IObuff + IOSIZE - 4, "...");
23202 vim_free(tofree);
23203 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204}
23205
23206/*
23207 * Get next function line.
23208 * Called by do_cmdline() to get the next line.
23209 * Returns allocated string, or NULL for end of function.
23210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 char_u *
23212get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023213 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023215 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216{
Bram Moolenaar33570922005-01-25 22:26:29 +000023217 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023218 ufunc_T *fp = fcp->func;
23219 char_u *retval;
23220 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221
23222 /* If breakpoints have been added/deleted need to check for it. */
23223 if (fcp->dbg_tick != debug_tick)
23224 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023225 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 sourcing_lnum);
23227 fcp->dbg_tick = debug_tick;
23228 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023229#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023230 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023231 func_line_end(cookie);
23232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233
Bram Moolenaar05159a02005-02-26 23:04:13 +000023234 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023235 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23236 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 retval = NULL;
23238 else
23239 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023240 /* Skip NULL lines (continuation lines). */
23241 while (fcp->linenr < gap->ga_len
23242 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23243 ++fcp->linenr;
23244 if (fcp->linenr >= gap->ga_len)
23245 retval = NULL;
23246 else
23247 {
23248 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23249 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023250#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023251 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023252 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023253#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 }
23256
23257 /* Did we encounter a breakpoint? */
23258 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23259 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023260 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023262 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 sourcing_lnum);
23264 fcp->dbg_tick = debug_tick;
23265 }
23266
23267 return retval;
23268}
23269
Bram Moolenaar05159a02005-02-26 23:04:13 +000023270#if defined(FEAT_PROFILE) || defined(PROTO)
23271/*
23272 * Called when starting to read a function line.
23273 * "sourcing_lnum" must be correct!
23274 * When skipping lines it may not actually be executed, but we won't find out
23275 * until later and we need to store the time now.
23276 */
23277 void
23278func_line_start(cookie)
23279 void *cookie;
23280{
23281 funccall_T *fcp = (funccall_T *)cookie;
23282 ufunc_T *fp = fcp->func;
23283
23284 if (fp->uf_profiling && sourcing_lnum >= 1
23285 && sourcing_lnum <= fp->uf_lines.ga_len)
23286 {
23287 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023288 /* Skip continuation lines. */
23289 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23290 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023291 fp->uf_tml_execed = FALSE;
23292 profile_start(&fp->uf_tml_start);
23293 profile_zero(&fp->uf_tml_children);
23294 profile_get_wait(&fp->uf_tml_wait);
23295 }
23296}
23297
23298/*
23299 * Called when actually executing a function line.
23300 */
23301 void
23302func_line_exec(cookie)
23303 void *cookie;
23304{
23305 funccall_T *fcp = (funccall_T *)cookie;
23306 ufunc_T *fp = fcp->func;
23307
23308 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23309 fp->uf_tml_execed = TRUE;
23310}
23311
23312/*
23313 * Called when done with a function line.
23314 */
23315 void
23316func_line_end(cookie)
23317 void *cookie;
23318{
23319 funccall_T *fcp = (funccall_T *)cookie;
23320 ufunc_T *fp = fcp->func;
23321
23322 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23323 {
23324 if (fp->uf_tml_execed)
23325 {
23326 ++fp->uf_tml_count[fp->uf_tml_idx];
23327 profile_end(&fp->uf_tml_start);
23328 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023329 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023330 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23331 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023332 }
23333 fp->uf_tml_idx = -1;
23334 }
23335}
23336#endif
23337
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338/*
23339 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023340 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 */
23342 int
23343func_has_ended(cookie)
23344 void *cookie;
23345{
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347
23348 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23349 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023350 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 || fcp->returned);
23352}
23353
23354/*
23355 * return TRUE if cookie indicates a function which "abort"s on errors.
23356 */
23357 int
23358func_has_abort(cookie)
23359 void *cookie;
23360{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023361 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362}
23363
23364#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23365typedef enum
23366{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023367 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23368 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23369 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370} var_flavour_T;
23371
23372static var_flavour_T var_flavour __ARGS((char_u *varname));
23373
23374 static var_flavour_T
23375var_flavour(varname)
23376 char_u *varname;
23377{
23378 char_u *p = varname;
23379
23380 if (ASCII_ISUPPER(*p))
23381 {
23382 while (*(++p))
23383 if (ASCII_ISLOWER(*p))
23384 return VAR_FLAVOUR_SESSION;
23385 return VAR_FLAVOUR_VIMINFO;
23386 }
23387 else
23388 return VAR_FLAVOUR_DEFAULT;
23389}
23390#endif
23391
23392#if defined(FEAT_VIMINFO) || defined(PROTO)
23393/*
23394 * Restore global vars that start with a capital from the viminfo file
23395 */
23396 int
23397read_viminfo_varlist(virp, writing)
23398 vir_T *virp;
23399 int writing;
23400{
23401 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023402 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023403 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404
23405 if (!writing && (find_viminfo_parameter('!') != NULL))
23406 {
23407 tab = vim_strchr(virp->vir_line + 1, '\t');
23408 if (tab != NULL)
23409 {
23410 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023411 switch (*tab)
23412 {
23413 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023414#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023415 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023416#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023417 case 'D': type = VAR_DICT; break;
23418 case 'L': type = VAR_LIST; break;
23419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420
23421 tab = vim_strchr(tab, '\t');
23422 if (tab != NULL)
23423 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023424 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023425 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023426 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023427 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023428#ifdef FEAT_FLOAT
23429 else if (type == VAR_FLOAT)
23430 (void)string2float(tab + 1, &tv.vval.v_float);
23431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023433 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023434 if (type == VAR_DICT || type == VAR_LIST)
23435 {
23436 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23437
23438 if (etv == NULL)
23439 /* Failed to parse back the dict or list, use it as a
23440 * string. */
23441 tv.v_type = VAR_STRING;
23442 else
23443 {
23444 vim_free(tv.vval.v_string);
23445 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023446 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023447 }
23448 }
23449
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023450 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023451
23452 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023453 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023454 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23455 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 }
23457 }
23458 }
23459
23460 return viminfo_readline(virp);
23461}
23462
23463/*
23464 * Write global vars that start with a capital to the viminfo file
23465 */
23466 void
23467write_viminfo_varlist(fp)
23468 FILE *fp;
23469{
Bram Moolenaar33570922005-01-25 22:26:29 +000023470 hashitem_T *hi;
23471 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023472 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023473 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023474 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023475 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023476 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477
23478 if (find_viminfo_parameter('!') == NULL)
23479 return;
23480
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023481 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023482
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023483 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023484 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023486 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023488 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023489 this_var = HI2DI(hi);
23490 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023491 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023493 {
23494 case VAR_STRING: s = "STR"; break;
23495 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023496#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023497 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023498#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023499 case VAR_DICT: s = "DIC"; break;
23500 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023501 default: continue;
23502 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023503 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023504 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023505 if (p != NULL)
23506 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023507 vim_free(tofree);
23508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 }
23510 }
23511}
23512#endif
23513
23514#if defined(FEAT_SESSION) || defined(PROTO)
23515 int
23516store_session_globals(fd)
23517 FILE *fd;
23518{
Bram Moolenaar33570922005-01-25 22:26:29 +000023519 hashitem_T *hi;
23520 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023521 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 char_u *p, *t;
23523
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023524 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023525 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023527 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023529 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023530 this_var = HI2DI(hi);
23531 if ((this_var->di_tv.v_type == VAR_NUMBER
23532 || this_var->di_tv.v_type == VAR_STRING)
23533 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023534 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023535 /* Escape special characters with a backslash. Turn a LF and
23536 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023537 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023538 (char_u *)"\\\"\n\r");
23539 if (p == NULL) /* out of memory */
23540 break;
23541 for (t = p; *t != NUL; ++t)
23542 if (*t == '\n')
23543 *t = 'n';
23544 else if (*t == '\r')
23545 *t = 'r';
23546 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 this_var->di_key,
23548 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23549 : ' ',
23550 p,
23551 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23552 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023553 || put_eol(fd) == FAIL)
23554 {
23555 vim_free(p);
23556 return FAIL;
23557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 vim_free(p);
23559 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023560#ifdef FEAT_FLOAT
23561 else if (this_var->di_tv.v_type == VAR_FLOAT
23562 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23563 {
23564 float_T f = this_var->di_tv.vval.v_float;
23565 int sign = ' ';
23566
23567 if (f < 0)
23568 {
23569 f = -f;
23570 sign = '-';
23571 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023572 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023573 this_var->di_key, sign, f) < 0)
23574 || put_eol(fd) == FAIL)
23575 return FAIL;
23576 }
23577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 }
23579 }
23580 return OK;
23581}
23582#endif
23583
Bram Moolenaar661b1822005-07-28 22:36:45 +000023584/*
23585 * Display script name where an item was last set.
23586 * Should only be invoked when 'verbose' is non-zero.
23587 */
23588 void
23589last_set_msg(scriptID)
23590 scid_T scriptID;
23591{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023592 char_u *p;
23593
Bram Moolenaar661b1822005-07-28 22:36:45 +000023594 if (scriptID != 0)
23595 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023596 p = home_replace_save(NULL, get_scriptname(scriptID));
23597 if (p != NULL)
23598 {
23599 verbose_enter();
23600 MSG_PUTS(_("\n\tLast set from "));
23601 MSG_PUTS(p);
23602 vim_free(p);
23603 verbose_leave();
23604 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023605 }
23606}
23607
Bram Moolenaard812df62008-11-09 12:46:09 +000023608/*
23609 * List v:oldfiles in a nice way.
23610 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023611 void
23612ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023613 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023614{
23615 list_T *l = vimvars[VV_OLDFILES].vv_list;
23616 listitem_T *li;
23617 int nr = 0;
23618
23619 if (l == NULL)
23620 msg((char_u *)_("No old files"));
23621 else
23622 {
23623 msg_start();
23624 msg_scroll = TRUE;
23625 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23626 {
23627 msg_outnum((long)++nr);
23628 MSG_PUTS(": ");
23629 msg_outtrans(get_tv_string(&li->li_tv));
23630 msg_putchar('\n');
23631 out_flush(); /* output one line at a time */
23632 ui_breakcheck();
23633 }
23634 /* Assume "got_int" was set to truncate the listing. */
23635 got_int = FALSE;
23636
23637#ifdef FEAT_BROWSE_CMD
23638 if (cmdmod.browse)
23639 {
23640 quit_more = FALSE;
23641 nr = prompt_for_number(FALSE);
23642 msg_starthere();
23643 if (nr > 0)
23644 {
23645 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23646 (long)nr);
23647
23648 if (p != NULL)
23649 {
23650 p = expand_env_save(p);
23651 eap->arg = p;
23652 eap->cmdidx = CMD_edit;
23653 cmdmod.browse = FALSE;
23654 do_exedit(eap, NULL);
23655 vim_free(p);
23656 }
23657 }
23658 }
23659#endif
23660 }
23661}
23662
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663#endif /* FEAT_EVAL */
23664
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023666#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667
23668#ifdef WIN3264
23669/*
23670 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23671 */
23672static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23673static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23674static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23675
23676/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023677 * Get the short path (8.3) for the filename in "fnamep".
23678 * Only works for a valid file name.
23679 * When the path gets longer "fnamep" is changed and the allocated buffer
23680 * is put in "bufp".
23681 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23682 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 */
23684 static int
23685get_short_pathname(fnamep, bufp, fnamelen)
23686 char_u **fnamep;
23687 char_u **bufp;
23688 int *fnamelen;
23689{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023690 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 char_u *newbuf;
23692
23693 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023694 l = GetShortPathName(*fnamep, *fnamep, len);
23695 if (l > len - 1)
23696 {
23697 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023698 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 newbuf = vim_strnsave(*fnamep, l);
23700 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023701 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702
23703 vim_free(*bufp);
23704 *fnamep = *bufp = newbuf;
23705
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023706 /* Really should always succeed, as the buffer is big enough. */
23707 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 }
23709
23710 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023711 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712}
23713
23714/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023715 * Get the short path (8.3) for the filename in "fname". The converted
23716 * path is returned in "bufp".
23717 *
23718 * Some of the directories specified in "fname" may not exist. This function
23719 * will shorten the existing directories at the beginning of the path and then
23720 * append the remaining non-existing path.
23721 *
23722 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023723 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023724 * bufp - Pointer to an allocated buffer for the filename.
23725 * fnamelen - Length of the filename pointed to by fname
23726 *
23727 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 */
23729 static int
23730shortpath_for_invalid_fname(fname, bufp, fnamelen)
23731 char_u **fname;
23732 char_u **bufp;
23733 int *fnamelen;
23734{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023735 char_u *short_fname, *save_fname, *pbuf_unused;
23736 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023738 int old_len, len;
23739 int new_len, sfx_len;
23740 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741
23742 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023743 old_len = *fnamelen;
23744 save_fname = vim_strnsave(*fname, old_len);
23745 pbuf_unused = NULL;
23746 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023748 endp = save_fname + old_len - 1; /* Find the end of the copy */
23749 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023751 /*
23752 * Try shortening the supplied path till it succeeds by removing one
23753 * directory at a time from the tail of the path.
23754 */
23755 len = 0;
23756 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023758 /* go back one path-separator */
23759 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23760 --endp;
23761 if (endp <= save_fname)
23762 break; /* processed the complete path */
23763
23764 /*
23765 * Replace the path separator with a NUL and try to shorten the
23766 * resulting path.
23767 */
23768 ch = *endp;
23769 *endp = 0;
23770 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023771 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023772 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23773 {
23774 retval = FAIL;
23775 goto theend;
23776 }
23777 *endp = ch; /* preserve the string */
23778
23779 if (len > 0)
23780 break; /* successfully shortened the path */
23781
23782 /* failed to shorten the path. Skip the path separator */
23783 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 }
23785
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023786 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023788 /*
23789 * Succeeded in shortening the path. Now concatenate the shortened
23790 * path with the remaining path at the tail.
23791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023793 /* Compute the length of the new path. */
23794 sfx_len = (int)(save_endp - endp) + 1;
23795 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023797 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023799 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023801 /* There is not enough space in the currently allocated string,
23802 * copy it to a buffer big enough. */
23803 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023805 {
23806 retval = FAIL;
23807 goto theend;
23808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 }
23810 else
23811 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023812 /* Transfer short_fname to the main buffer (it's big enough),
23813 * unless get_short_pathname() did its work in-place. */
23814 *fname = *bufp = save_fname;
23815 if (short_fname != save_fname)
23816 vim_strncpy(save_fname, short_fname, len);
23817 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023819
23820 /* concat the not-shortened part of the path */
23821 vim_strncpy(*fname + len, endp, sfx_len);
23822 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023824
23825theend:
23826 vim_free(pbuf_unused);
23827 vim_free(save_fname);
23828
23829 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830}
23831
23832/*
23833 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023834 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 */
23836 static int
23837shortpath_for_partial(fnamep, bufp, fnamelen)
23838 char_u **fnamep;
23839 char_u **bufp;
23840 int *fnamelen;
23841{
23842 int sepcount, len, tflen;
23843 char_u *p;
23844 char_u *pbuf, *tfname;
23845 int hasTilde;
23846
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023847 /* Count up the path separators from the RHS.. so we know which part
23848 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023850 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 if (vim_ispathsep(*p))
23852 ++sepcount;
23853
23854 /* Need full path first (use expand_env() to remove a "~/") */
23855 hasTilde = (**fnamep == '~');
23856 if (hasTilde)
23857 pbuf = tfname = expand_env_save(*fnamep);
23858 else
23859 pbuf = tfname = FullName_save(*fnamep, FALSE);
23860
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023861 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023863 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23864 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865
23866 if (len == 0)
23867 {
23868 /* Don't have a valid filename, so shorten the rest of the
23869 * path if we can. This CAN give us invalid 8.3 filenames, but
23870 * there's not a lot of point in guessing what it might be.
23871 */
23872 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023873 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23874 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 }
23876
23877 /* Count the paths backward to find the beginning of the desired string. */
23878 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023879 {
23880#ifdef FEAT_MBYTE
23881 if (has_mbyte)
23882 p -= mb_head_off(tfname, p);
23883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 if (vim_ispathsep(*p))
23885 {
23886 if (sepcount == 0 || (hasTilde && sepcount == 1))
23887 break;
23888 else
23889 sepcount --;
23890 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 if (hasTilde)
23893 {
23894 --p;
23895 if (p >= tfname)
23896 *p = '~';
23897 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023898 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 }
23900 else
23901 ++p;
23902
23903 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23904 vim_free(*bufp);
23905 *fnamelen = (int)STRLEN(p);
23906 *bufp = pbuf;
23907 *fnamep = p;
23908
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023909 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910}
23911#endif /* WIN3264 */
23912
23913/*
23914 * Adjust a filename, according to a string of modifiers.
23915 * *fnamep must be NUL terminated when called. When returning, the length is
23916 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023917 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 * When there is an error, *fnamep is set to NULL.
23919 */
23920 int
23921modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23922 char_u *src; /* string with modifiers */
23923 int *usedlen; /* characters after src that are used */
23924 char_u **fnamep; /* file name so far */
23925 char_u **bufp; /* buffer for allocated file name or NULL */
23926 int *fnamelen; /* length of fnamep */
23927{
23928 int valid = 0;
23929 char_u *tail;
23930 char_u *s, *p, *pbuf;
23931 char_u dirname[MAXPATHL];
23932 int c;
23933 int has_fullname = 0;
23934#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023935 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 int has_shortname = 0;
23937#endif
23938
23939repeat:
23940 /* ":p" - full path/file_name */
23941 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23942 {
23943 has_fullname = 1;
23944
23945 valid |= VALID_PATH;
23946 *usedlen += 2;
23947
23948 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23949 if ((*fnamep)[0] == '~'
23950#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23951 && ((*fnamep)[1] == '/'
23952# ifdef BACKSLASH_IN_FILENAME
23953 || (*fnamep)[1] == '\\'
23954# endif
23955 || (*fnamep)[1] == NUL)
23956
23957#endif
23958 )
23959 {
23960 *fnamep = expand_env_save(*fnamep);
23961 vim_free(*bufp); /* free any allocated file name */
23962 *bufp = *fnamep;
23963 if (*fnamep == NULL)
23964 return -1;
23965 }
23966
23967 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023968 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 {
23970 if (vim_ispathsep(*p)
23971 && p[1] == '.'
23972 && (p[2] == NUL
23973 || vim_ispathsep(p[2])
23974 || (p[2] == '.'
23975 && (p[3] == NUL || vim_ispathsep(p[3])))))
23976 break;
23977 }
23978
23979 /* FullName_save() is slow, don't use it when not needed. */
23980 if (*p != NUL || !vim_isAbsName(*fnamep))
23981 {
23982 *fnamep = FullName_save(*fnamep, *p != NUL);
23983 vim_free(*bufp); /* free any allocated file name */
23984 *bufp = *fnamep;
23985 if (*fnamep == NULL)
23986 return -1;
23987 }
23988
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023989#ifdef WIN3264
23990# if _WIN32_WINNT >= 0x0500
23991 if (vim_strchr(*fnamep, '~') != NULL)
23992 {
23993 /* Expand 8.3 filename to full path. Needed to make sure the same
23994 * file does not have two different names.
23995 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23996 p = alloc(_MAX_PATH + 1);
23997 if (p != NULL)
23998 {
23999 if (GetLongPathName(*fnamep, p, MAXPATHL))
24000 {
24001 vim_free(*bufp);
24002 *bufp = *fnamep = p;
24003 }
24004 else
24005 vim_free(p);
24006 }
24007 }
24008# endif
24009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 /* Append a path separator to a directory. */
24011 if (mch_isdir(*fnamep))
24012 {
24013 /* Make room for one or two extra characters. */
24014 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24015 vim_free(*bufp); /* free any allocated file name */
24016 *bufp = *fnamep;
24017 if (*fnamep == NULL)
24018 return -1;
24019 add_pathsep(*fnamep);
24020 }
24021 }
24022
24023 /* ":." - path relative to the current directory */
24024 /* ":~" - path relative to the home directory */
24025 /* ":8" - shortname path - postponed till after */
24026 while (src[*usedlen] == ':'
24027 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24028 {
24029 *usedlen += 2;
24030 if (c == '8')
24031 {
24032#ifdef WIN3264
24033 has_shortname = 1; /* Postpone this. */
24034#endif
24035 continue;
24036 }
24037 pbuf = NULL;
24038 /* Need full path first (use expand_env() to remove a "~/") */
24039 if (!has_fullname)
24040 {
24041 if (c == '.' && **fnamep == '~')
24042 p = pbuf = expand_env_save(*fnamep);
24043 else
24044 p = pbuf = FullName_save(*fnamep, FALSE);
24045 }
24046 else
24047 p = *fnamep;
24048
24049 has_fullname = 0;
24050
24051 if (p != NULL)
24052 {
24053 if (c == '.')
24054 {
24055 mch_dirname(dirname, MAXPATHL);
24056 s = shorten_fname(p, dirname);
24057 if (s != NULL)
24058 {
24059 *fnamep = s;
24060 if (pbuf != NULL)
24061 {
24062 vim_free(*bufp); /* free any allocated file name */
24063 *bufp = pbuf;
24064 pbuf = NULL;
24065 }
24066 }
24067 }
24068 else
24069 {
24070 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24071 /* Only replace it when it starts with '~' */
24072 if (*dirname == '~')
24073 {
24074 s = vim_strsave(dirname);
24075 if (s != NULL)
24076 {
24077 *fnamep = s;
24078 vim_free(*bufp);
24079 *bufp = s;
24080 }
24081 }
24082 }
24083 vim_free(pbuf);
24084 }
24085 }
24086
24087 tail = gettail(*fnamep);
24088 *fnamelen = (int)STRLEN(*fnamep);
24089
24090 /* ":h" - head, remove "/file_name", can be repeated */
24091 /* Don't remove the first "/" or "c:\" */
24092 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24093 {
24094 valid |= VALID_HEAD;
24095 *usedlen += 2;
24096 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024097 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024098 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 *fnamelen = (int)(tail - *fnamep);
24100#ifdef VMS
24101 if (*fnamelen > 0)
24102 *fnamelen += 1; /* the path separator is part of the path */
24103#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024104 if (*fnamelen == 0)
24105 {
24106 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24107 p = vim_strsave((char_u *)".");
24108 if (p == NULL)
24109 return -1;
24110 vim_free(*bufp);
24111 *bufp = *fnamep = tail = p;
24112 *fnamelen = 1;
24113 }
24114 else
24115 {
24116 while (tail > s && !after_pathsep(s, tail))
24117 mb_ptr_back(*fnamep, tail);
24118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 }
24120
24121 /* ":8" - shortname */
24122 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24123 {
24124 *usedlen += 2;
24125#ifdef WIN3264
24126 has_shortname = 1;
24127#endif
24128 }
24129
24130#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024131 /*
24132 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 */
24134 if (has_shortname)
24135 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024136 /* Copy the string if it is shortened by :h and when it wasn't copied
24137 * yet, because we are going to change it in place. Avoids changing
24138 * the buffer name for "%:8". */
24139 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 {
24141 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024142 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 return -1;
24144 vim_free(*bufp);
24145 *bufp = *fnamep = p;
24146 }
24147
24148 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024149 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 if (!has_fullname && !vim_isAbsName(*fnamep))
24151 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024152 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 return -1;
24154 }
24155 else
24156 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024157 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158
Bram Moolenaardc935552011-08-17 15:23:23 +020024159 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024161 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 return -1;
24163
24164 if (l == 0)
24165 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024166 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024168 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 return -1;
24170 }
24171 *fnamelen = l;
24172 }
24173 }
24174#endif /* WIN3264 */
24175
24176 /* ":t" - tail, just the basename */
24177 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24178 {
24179 *usedlen += 2;
24180 *fnamelen -= (int)(tail - *fnamep);
24181 *fnamep = tail;
24182 }
24183
24184 /* ":e" - extension, can be repeated */
24185 /* ":r" - root, without extension, can be repeated */
24186 while (src[*usedlen] == ':'
24187 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24188 {
24189 /* find a '.' in the tail:
24190 * - for second :e: before the current fname
24191 * - otherwise: The last '.'
24192 */
24193 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24194 s = *fnamep - 2;
24195 else
24196 s = *fnamep + *fnamelen - 1;
24197 for ( ; s > tail; --s)
24198 if (s[0] == '.')
24199 break;
24200 if (src[*usedlen + 1] == 'e') /* :e */
24201 {
24202 if (s > tail)
24203 {
24204 *fnamelen += (int)(*fnamep - (s + 1));
24205 *fnamep = s + 1;
24206#ifdef VMS
24207 /* cut version from the extension */
24208 s = *fnamep + *fnamelen - 1;
24209 for ( ; s > *fnamep; --s)
24210 if (s[0] == ';')
24211 break;
24212 if (s > *fnamep)
24213 *fnamelen = s - *fnamep;
24214#endif
24215 }
24216 else if (*fnamep <= tail)
24217 *fnamelen = 0;
24218 }
24219 else /* :r */
24220 {
24221 if (s > tail) /* remove one extension */
24222 *fnamelen = (int)(s - *fnamep);
24223 }
24224 *usedlen += 2;
24225 }
24226
24227 /* ":s?pat?foo?" - substitute */
24228 /* ":gs?pat?foo?" - global substitute */
24229 if (src[*usedlen] == ':'
24230 && (src[*usedlen + 1] == 's'
24231 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24232 {
24233 char_u *str;
24234 char_u *pat;
24235 char_u *sub;
24236 int sep;
24237 char_u *flags;
24238 int didit = FALSE;
24239
24240 flags = (char_u *)"";
24241 s = src + *usedlen + 2;
24242 if (src[*usedlen + 1] == 'g')
24243 {
24244 flags = (char_u *)"g";
24245 ++s;
24246 }
24247
24248 sep = *s++;
24249 if (sep)
24250 {
24251 /* find end of pattern */
24252 p = vim_strchr(s, sep);
24253 if (p != NULL)
24254 {
24255 pat = vim_strnsave(s, (int)(p - s));
24256 if (pat != NULL)
24257 {
24258 s = p + 1;
24259 /* find end of substitution */
24260 p = vim_strchr(s, sep);
24261 if (p != NULL)
24262 {
24263 sub = vim_strnsave(s, (int)(p - s));
24264 str = vim_strnsave(*fnamep, *fnamelen);
24265 if (sub != NULL && str != NULL)
24266 {
24267 *usedlen = (int)(p + 1 - src);
24268 s = do_string_sub(str, pat, sub, flags);
24269 if (s != NULL)
24270 {
24271 *fnamep = s;
24272 *fnamelen = (int)STRLEN(s);
24273 vim_free(*bufp);
24274 *bufp = s;
24275 didit = TRUE;
24276 }
24277 }
24278 vim_free(sub);
24279 vim_free(str);
24280 }
24281 vim_free(pat);
24282 }
24283 }
24284 /* after using ":s", repeat all the modifiers */
24285 if (didit)
24286 goto repeat;
24287 }
24288 }
24289
24290 return valid;
24291}
24292
24293/*
24294 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24295 * "flags" can be "g" to do a global substitute.
24296 * Returns an allocated string, NULL for error.
24297 */
24298 char_u *
24299do_string_sub(str, pat, sub, flags)
24300 char_u *str;
24301 char_u *pat;
24302 char_u *sub;
24303 char_u *flags;
24304{
24305 int sublen;
24306 regmatch_T regmatch;
24307 int i;
24308 int do_all;
24309 char_u *tail;
24310 garray_T ga;
24311 char_u *ret;
24312 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024313 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314
24315 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24316 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024317 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318
24319 ga_init2(&ga, 1, 200);
24320
24321 do_all = (flags[0] == 'g');
24322
24323 regmatch.rm_ic = p_ic;
24324 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24325 if (regmatch.regprog != NULL)
24326 {
24327 tail = str;
24328 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24329 {
24330 /*
24331 * Get some space for a temporary buffer to do the substitution
24332 * into. It will contain:
24333 * - The text up to where the match is.
24334 * - The substituted text.
24335 * - The text after the match.
24336 */
24337 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24338 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24339 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24340 {
24341 ga_clear(&ga);
24342 break;
24343 }
24344
24345 /* copy the text up to where the match is */
24346 i = (int)(regmatch.startp[0] - tail);
24347 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24348 /* add the substituted text */
24349 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24350 + ga.ga_len + i, TRUE, TRUE, FALSE);
24351 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024352 zero_width = (tail == regmatch.endp[0]
24353 || regmatch.startp[0] == regmatch.endp[0]);
24354 tail = regmatch.endp[0];
24355 if (*tail == NUL)
24356 break;
24357 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024359 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24361 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 if (!do_all)
24364 break;
24365 }
24366
24367 if (ga.ga_data != NULL)
24368 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24369
Bram Moolenaar473de612013-06-08 18:19:48 +020024370 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 }
24372
24373 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24374 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024375 if (p_cpo == empty_option)
24376 p_cpo = save_cpo;
24377 else
24378 /* Darn, evaluating {sub} expression changed the value. */
24379 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380
24381 return ret;
24382}
24383
24384#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */