blob: 9a590c74911ed2c84fbb53dd339d41f16416a3b8 [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 Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000371};
372
373/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_type vv_di.di_tv.v_type
375#define vv_nr vv_di.di_tv.vval.v_number
376#define vv_float vv_di.di_tv.vval.v_float
377#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000378#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200379#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000381
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200382static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000383#define vimvarht vimvardict.dv_hashtab
384
Bram Moolenaara40058a2005-07-11 22:42:07 +0000385static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
386static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
388static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
389static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
391static void list_glob_vars __ARGS((int *first));
392static void list_buf_vars __ARGS((int *first));
393static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000395static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_vim_vars __ARGS((int *first));
398static void list_script_vars __ARGS((int *first));
399static void list_func_vars __ARGS((int *first));
400static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000401static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
402static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100403static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404static void clear_lval __ARGS((lval_T *lp));
405static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
406static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
408static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
409static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
410static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
411static void item_lock __ARGS((typval_T *tv, int deep, int lock));
412static int tv_islocked __ARGS((typval_T *tv));
413
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
415static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000420static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
421static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000423static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000428static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100430static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
431static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
432static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000435static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100440static 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 +0000441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000442static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200443static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100458static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static 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 +0200460static 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 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __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_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200473static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100489static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100491static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200510static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
515static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200525static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200527#ifdef FEAT_FLOAT
528static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
529#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000532static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
539static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000543static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000552static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000554static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200558static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000561static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200562static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200572static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000573static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000574static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200577static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000578static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100584static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000587static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000601static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100606static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200624#ifdef FEAT_LUA
625static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000631static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200632static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100649static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200657#ifdef FEAT_PYTHON3
658static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
660#ifdef FEAT_PYTHON
661static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000665static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#ifdef FEAT_FLOAT
678static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
679#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200680static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100682static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000685static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000687static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200692static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000695static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000696static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000697static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000698static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200700static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000701static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100703#ifdef FEAT_CRYPT
704static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
705#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000706static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200707static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
710static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200711static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000712#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000714static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000715static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
719static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
721#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000722static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200723static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724#ifdef HAVE_STRFTIME
725static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
727static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200733static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200734static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000740static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200741static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200743static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000744static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000745static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000746static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000747static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000748static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000750static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200751#ifdef FEAT_FLOAT
752static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
754#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#ifdef FEAT_FLOAT
759static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200762static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200763static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100764static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100768static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000775static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100779static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780
Bram Moolenaar493c1782014-05-28 14:34:46 +0200781static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000782static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static int get_env_len __ARGS((char_u **arg));
784static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000786static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
787#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
788#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
789 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000790static 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 +0000791static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200793static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000794static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static typval_T *alloc_tv __ARGS((void));
796static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void init_tv __ARGS((typval_T *varp));
798static long get_tv_number __ARGS((typval_T *varp));
799static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000800static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static char_u *get_tv_string __ARGS((typval_T *varp));
802static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000803static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100804static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
805static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000806static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
807static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
808static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000809static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
810static 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 +0000811static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200812static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
813static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200814static int var_check_func_name __ARGS((char_u *name, int new_var));
815static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000817static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
819static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
820static int eval_fname_script __ARGS((char_u *p));
821static int eval_fname_sid __ARGS((char_u *p));
822static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static ufunc_T *find_func __ARGS((char_u *name));
824static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200825static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
827static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000828static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
829static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
830static int
831# ifdef __BORLANDC__
832 _RTLENTRYF
833# endif
834 prof_total_cmp __ARGS((const void *s1, const void *s2));
835static int
836# ifdef __BORLANDC__
837 _RTLENTRYF
838# endif
839 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200841static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000842static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000843static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000844static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000845static 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 +0000846static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
847static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000849static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
850static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000851static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000852static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200854static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200855static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200857
858#ifdef EBCDIC
859static int compare_func_name __ARGS((const void *s1, const void *s2));
860static void sortFunctions __ARGS(());
861#endif
862
Bram Moolenaar33570922005-01-25 22:26:29 +0000863/*
864 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000865 */
866 void
867eval_init()
868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 int i;
870 struct vimvar *p;
871
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200872 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
873 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200874 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
878 for (i = 0; i < VV_LEN; ++i)
879 {
880 p = &vimvars[i];
881 STRCPY(p->vv_di.di_key, p->vv_name);
882 if (p->vv_flags & VV_RO)
883 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
884 else if (p->vv_flags & VV_RO_SBX)
885 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
886 else
887 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000888
889 /* add to v: scope dict, unless the value is not always available */
890 if (p->vv_type != VAR_UNKNOWN)
891 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000893 /* add to compat scope dict */
894 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000896 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100897 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200898 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200899 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
902 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100903 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904 */
905 sortFunctions();
906#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000907}
908
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909#if defined(EXITFREE) || defined(PROTO)
910 void
911eval_clear()
912{
913 int i;
914 struct vimvar *p;
915
916 for (i = 0; i < VV_LEN; ++i)
917 {
918 p = &vimvars[i];
919 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000921 vim_free(p->vv_str);
922 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000923 }
924 else if (p->vv_di.di_tv.v_type == VAR_LIST)
925 {
926 list_unref(p->vv_list);
927 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000928 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929 }
930 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000931 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932 hash_clear(&compat_hashtab);
933
Bram Moolenaard9fba312005-06-26 22:34:35 +0000934 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200936 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100937# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938
939 /* global variables */
940 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000941
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000942 /* autoloaded script names */
943 ga_clear_strings(&ga_loaded);
944
Bram Moolenaarcca74132013-09-25 21:00:28 +0200945 /* Script-local variables. First clear all the variables and in a second
946 * loop free the scriptvar_T, because a variable in one script might hold
947 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200948 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200949 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200950 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200951 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200952 ga_clear(&ga_scripts);
953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000954 /* unreferenced lists and dicts */
955 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000956
957 /* functions */
958 free_all_functions();
959 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000960}
961#endif
962
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 * Return the name of the executed function.
965 */
966 char_u *
967func_name(cookie)
968 void *cookie;
969{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000970 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Return the address holding the next breakpoint line for a funccall cookie.
975 */
976 linenr_T *
977func_breakpoint(cookie)
978 void *cookie;
979{
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983/*
984 * Return the address holding the debug tick for a funccall cookie.
985 */
986 int *
987func_dbg_tick(cookie)
988 void *cookie;
989{
Bram Moolenaar33570922005-01-25 22:26:29 +0000990 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
993/*
994 * Return the nesting level for a funccall cookie.
995 */
996 int
997func_level(cookie)
998 void *cookie;
999{
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001004funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001006/* pointer to list of previously used funccal, still around because some
1007 * item in it is still being used. */
1008funccall_T *previous_funccal = NULL;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/*
1011 * Return TRUE when a function was ended by a ":return" command.
1012 */
1013 int
1014current_func_returned()
1015{
1016 return current_funccal->returned;
1017}
1018
1019
1020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Set an internal variable to a string value. Creates the variable if it does
1022 * not already exist.
1023 */
1024 void
1025set_internal_string_var(name, value)
1026 char_u *name;
1027 char_u *value;
1028{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001029 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 val = vim_strsave(value);
1033 if (val != NULL)
1034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001038 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 }
1042}
1043
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001045static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046static char_u *redir_endp = NULL;
1047static char_u *redir_varname = NULL;
1048
1049/*
1050 * Start recording command output to a variable
1051 * Returns OK if successfully completed the setup. FAIL otherwise.
1052 */
1053 int
1054var_redir_start(name, append)
1055 char_u *name;
1056 int append; /* append to an existing variable */
1057{
1058 int save_emsg;
1059 int err;
1060 typval_T tv;
1061
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001063 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 {
1065 EMSG(_(e_invarg));
1066 return FAIL;
1067 }
1068
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 redir_varname = vim_strsave(name);
1071 if (redir_varname == NULL)
1072 return FAIL;
1073
1074 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1075 if (redir_lval == NULL)
1076 {
1077 var_redir_stop();
1078 return FAIL;
1079 }
1080
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001081 /* The output is stored in growarray "redir_ga" until redirection ends. */
1082 ga_init2(&redir_ga, (int)sizeof(char), 500);
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001085 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1088 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_endp != NULL && *redir_endp != NUL)
1091 /* Trailing characters are present after the variable name */
1092 EMSG(_(e_trailing));
1093 else
1094 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 var_redir_stop();
1097 return FAIL;
1098 }
1099
1100 /* check if we can write to the variable: set it to or append an empty
1101 * string */
1102 save_emsg = did_emsg;
1103 did_emsg = FALSE;
1104 tv.v_type = VAR_STRING;
1105 tv.vval.v_string = (char_u *)"";
1106 if (append)
1107 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1108 else
1109 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001110 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001112 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (err)
1114 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 var_redir_stop();
1117 return FAIL;
1118 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
1120 return OK;
1121}
1122
1123/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 * Append "value[value_len]" to the variable set by var_redir_start().
1125 * The actual appending is postponed until redirection ends, because the value
1126 * appended may in fact be the string we write to, changing it may cause freed
1127 * memory to be used:
1128 * :redir => foo
1129 * :let foo
1130 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
1139 if (redir_lval == NULL)
1140 return;
1141
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001147 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 {
1149 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 }
1152 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154}
1155
1156/*
1157 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 */
1160 void
1161var_redir_stop()
1162{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 typval_T tv;
1164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 if (redir_lval != NULL)
1166 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* If there was no error: assign the text to the variable. */
1168 if (redir_endp != NULL)
1169 {
1170 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1171 tv.v_type = VAR_STRING;
1172 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001173 /* Call get_lval() again, if it's inside a Dict or List it may
1174 * have changed. */
1175 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1178 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1179 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001181
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 /* free the collected output */
1183 vim_free(redir_ga.ga_data);
1184 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 vim_free(redir_lval);
1187 redir_lval = NULL;
1188 }
1189 vim_free(redir_varname);
1190 redir_varname = NULL;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193# if defined(FEAT_MBYTE) || defined(PROTO)
1194 int
1195eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1196 char_u *enc_from;
1197 char_u *enc_to;
1198 char_u *fname_from;
1199 char_u *fname_to;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1204 set_vim_var_string(VV_CC_TO, enc_to, -1);
1205 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1206 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1207 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_CC_FROM, NULL, -1);
1210 set_vim_var_string(VV_CC_TO, NULL, -1);
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1213
1214 if (err)
1215 return FAIL;
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1221 int
1222eval_printexpr(fname, args)
1223 char_u *fname;
1224 char_u *args;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, fname, -1);
1229 set_vim_var_string(VV_CMDARG, args, -1);
1230 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1231 err = TRUE;
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_CMDARG, NULL, -1);
1234
1235 if (err)
1236 {
1237 mch_remove(fname);
1238 return FAIL;
1239 }
1240 return OK;
1241}
1242# endif
1243
1244# if defined(FEAT_DIFF) || defined(PROTO)
1245 void
1246eval_diff(origfile, newfile, outfile)
1247 char_u *origfile;
1248 char_u *newfile;
1249 char_u *outfile;
1250{
1251 int err = FALSE;
1252
1253 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1254 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1255 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1256 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260}
1261
1262 void
1263eval_patch(origfile, difffile, outfile)
1264 char_u *origfile;
1265 char_u *difffile;
1266 char_u *outfile;
1267{
1268 int err;
1269
1270 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1271 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1272 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1273 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1276 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1277}
1278# endif
1279
1280/*
1281 * Top level evaluation function, returning a boolean.
1282 * Sets "error" to TRUE if there was an error.
1283 * Return TRUE or FALSE.
1284 */
1285 int
1286eval_to_bool(arg, error, nextcmd, skip)
1287 char_u *arg;
1288 int *error;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int retval = FALSE;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 else
1300 {
1301 *error = FALSE;
1302 if (!skip)
1303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001304 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 }
1308 if (skip)
1309 --emsg_skip;
1310
1311 return retval;
1312}
1313
1314/*
1315 * Top level evaluation function, returning a string. If "skip" is TRUE,
1316 * only parsing to "nextcmd" is done, without reporting errors. Return
1317 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1318 */
1319 char_u *
1320eval_to_string_skip(arg, nextcmd, skip)
1321 char_u *arg;
1322 char_u **nextcmd;
1323 int skip; /* only parse, don't execute */
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *retval;
1327
1328 if (skip)
1329 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 retval = NULL;
1332 else
1333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 retval = vim_strsave(get_tv_string(&tv));
1335 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 if (skip)
1338 --emsg_skip;
1339
1340 return retval;
1341}
1342
1343/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344 * Skip over an expression at "*pp".
1345 * Return FAIL for an error, OK otherwise.
1346 */
1347 int
1348skip_expr(pp)
1349 char_u **pp;
1350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352
1353 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001355}
1356
1357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 * When "convert" is TRUE convert a List into a sequence of lines and convert
1360 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 * Return pointer to allocated memory, or NULL for failure.
1362 */
1363 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *arg;
1366 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001372#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 {
1382 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001383 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001384 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001385 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001386 if (tv.vval.v_list->lv_len > 0)
1387 ga_append(&ga, NL);
1388 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 ga_append(&ga, NUL);
1390 retval = (char_u *)ga.ga_data;
1391 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001392#ifdef FEAT_FLOAT
1393 else if (convert && tv.v_type == VAR_FLOAT)
1394 {
1395 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1396 retval = vim_strsave(numbuf);
1397 }
1398#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 else
1400 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403
1404 return retval;
1405}
1406
1407/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001408 * Call eval_to_string() without using current local variables and using
1409 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 */
1411 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *arg;
1414 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 char_u *retval;
1418 void *save_funccalp;
1419
1420 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 if (use_sandbox)
1422 ++sandbox;
1423 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001425 if (use_sandbox)
1426 --sandbox;
1427 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 restore_funccal(save_funccalp);
1429 return retval;
1430}
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432/*
1433 * Top level evaluation function, returning a number.
1434 * Evaluates "expr" silently.
1435 * Returns -1 for an error.
1436 */
1437 int
1438eval_to_number(expr)
1439 char_u *expr;
1440{
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001443 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 ++emsg_off;
1446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = -1;
1449 else
1450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454 --emsg_off;
1455
1456 return retval;
1457}
1458
Bram Moolenaara40058a2005-07-11 22:42:07 +00001459/*
1460 * Prepare v: variable "idx" to be used.
1461 * Save the current typeval in "save_tv".
1462 * When not used yet add the variable to the v: hashtable.
1463 */
1464 static void
1465prepare_vimvar(idx, save_tv)
1466 int idx;
1467 typval_T *save_tv;
1468{
1469 *save_tv = vimvars[idx].vv_tv;
1470 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1471 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1472}
1473
1474/*
1475 * Restore v: variable "idx" to typeval "save_tv".
1476 * When no longer defined, remove the variable from the v: hashtable.
1477 */
1478 static void
1479restore_vimvar(idx, save_tv)
1480 int idx;
1481 typval_T *save_tv;
1482{
1483 hashitem_T *hi;
1484
Bram Moolenaara40058a2005-07-11 22:42:07 +00001485 vimvars[idx].vv_tv = *save_tv;
1486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1487 {
1488 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1489 if (HASHITEM_EMPTY(hi))
1490 EMSG2(_(e_intern2), "restore_vimvar()");
1491 else
1492 hash_remove(&vimvarht, hi);
1493 }
1494}
1495
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001496#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497/*
1498 * Evaluate an expression to a list with suggestions.
1499 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001500 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 */
1502 list_T *
1503eval_spell_expr(badword, expr)
1504 char_u *badword;
1505 char_u *expr;
1506{
1507 typval_T save_val;
1508 typval_T rettv;
1509 list_T *list = NULL;
1510 char_u *p = skipwhite(expr);
1511
1512 /* Set "v:val" to the bad word. */
1513 prepare_vimvar(VV_VAL, &save_val);
1514 vimvars[VV_VAL].vv_type = VAR_STRING;
1515 vimvars[VV_VAL].vv_str = badword;
1516 if (p_verbose == 0)
1517 ++emsg_off;
1518
1519 if (eval1(&p, &rettv, TRUE) == OK)
1520 {
1521 if (rettv.v_type != VAR_LIST)
1522 clear_tv(&rettv);
1523 else
1524 list = rettv.vval.v_list;
1525 }
1526
1527 if (p_verbose == 0)
1528 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001529 restore_vimvar(VV_VAL, &save_val);
1530
1531 return list;
1532}
1533
1534/*
1535 * "list" is supposed to contain two items: a word and a number. Return the
1536 * word in "pp" and the number as the return value.
1537 * Return -1 if anything isn't right.
1538 * Used to get the good word and score from the eval_spell_expr() result.
1539 */
1540 int
1541get_spellword(list, pp)
1542 list_T *list;
1543 char_u **pp;
1544{
1545 listitem_T *li;
1546
1547 li = list->lv_first;
1548 if (li == NULL)
1549 return -1;
1550 *pp = get_tv_string(&li->li_tv);
1551
1552 li = li->li_next;
1553 if (li == NULL)
1554 return -1;
1555 return get_tv_number(&li->li_tv);
1556}
1557#endif
1558
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001559/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 * Top level evaluation function.
1561 * Returns an allocated typval_T with the result.
1562 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563 */
1564 typval_T *
1565eval_expr(arg, nextcmd)
1566 char_u *arg;
1567 char_u **nextcmd;
1568{
1569 typval_T *tv;
1570
1571 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001572 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001573 {
1574 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001575 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001576 }
1577
1578 return tv;
1579}
1580
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 * Uses argv[argc] for the function arguments. Only Number and String
1585 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001588 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 char_u *func;
1591 int argc;
1592 char_u **argv;
1593 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar33570922005-01-25 22:26:29 +00001597 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 long n;
1599 int len;
1600 int i;
1601 int doesrange;
1602 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001605 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 for (i = 0; i < argc; i++)
1610 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001611 /* Pass a NULL or empty argument as an empty string */
1612 if (argv[i] == NULL || *argv[i] == NUL)
1613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001614 argvars[i].v_type = VAR_STRING;
1615 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001616 continue;
1617 }
1618
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001619 if (str_arg_only)
1620 len = 0;
1621 else
1622 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001623 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (len != 0 && len == (int)STRLEN(argv[i]))
1625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001626 argvars[i].v_type = VAR_NUMBER;
1627 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 else
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_STRING;
1632 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
1635
1636 if (safe)
1637 {
1638 save_funccalp = save_funccal();
1639 ++sandbox;
1640 }
1641
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1643 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (safe)
1647 {
1648 --sandbox;
1649 restore_funccal(save_funccalp);
1650 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 vim_free(argvars);
1652
1653 if (ret == FAIL)
1654 clear_tv(rettv);
1655
1656 return ret;
1657}
1658
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001659/*
1660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1676 return -1;
1677
1678 retval = get_tv_number_chk(&rettv, NULL);
1679 clear_tv(&rettv);
1680 return retval;
1681}
1682
1683#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1684 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1685
Bram Moolenaar4f688582007-07-24 12:34:30 +00001686# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001688 * Call vimL function "func" and return the result as a string.
1689 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 * Uses argv[argc] for the function arguments.
1691 */
1692 void *
1693call_func_retstr(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001700 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001702 /* All arguments are passed as strings, no conversion to number. */
1703 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001704 return NULL;
1705
1706 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 return retval;
1709}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001712/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001713 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001715 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 */
1717 void *
1718call_func_retlist(func, argc, argv, safe)
1719 char_u *func;
1720 int argc;
1721 char_u **argv;
1722 int safe; /* use the sandbox */
1723{
1724 typval_T rettv;
1725
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001726 /* All arguments are passed as strings, no conversion to number. */
1727 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 return NULL;
1729
1730 if (rettv.v_type != VAR_LIST)
1731 {
1732 clear_tv(&rettv);
1733 return NULL;
1734 }
1735
1736 return rettv.vval.v_list;
1737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
1739
1740/*
1741 * Save the current function call pointer, and set it to NULL.
1742 * Used when executing autocommands and for ":source".
1743 */
1744 void *
1745save_funccal()
1746{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 current_funccal = NULL;
1750 return (void *)fc;
1751}
1752
1753 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754restore_funccal(vfc)
1755 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757 funccall_T *fc = (funccall_T *)vfc;
1758
1759 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762#if defined(FEAT_PROFILE) || defined(PROTO)
1763/*
1764 * Prepare profiling for entering a child or something else that is not
1765 * counted for the script/function itself.
1766 * Should always be called in pair with prof_child_exit().
1767 */
1768 void
1769prof_child_enter(tm)
1770 proftime_T *tm; /* place to store waittime */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 profile_start(&fc->prof_child);
1776 script_prof_save(tm);
1777}
1778
1779/*
1780 * Take care of time spent in a child.
1781 * Should always be called after prof_child_enter().
1782 */
1783 void
1784prof_child_exit(tm)
1785 proftime_T *tm; /* where waittime was stored */
1786{
1787 funccall_T *fc = current_funccal;
1788
1789 if (fc != NULL && fc->func->uf_profiling)
1790 {
1791 profile_end(&fc->prof_child);
1792 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1793 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1794 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1795 }
1796 script_prof_restore(tm);
1797}
1798#endif
1799
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#ifdef FEAT_FOLDING
1802/*
1803 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1804 * it in "*cp". Doesn't give error messages.
1805 */
1806 int
1807eval_foldexpr(arg, cp)
1808 char_u *arg;
1809 int *cp;
1810{
Bram Moolenaar33570922005-01-25 22:26:29 +00001811 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 int retval;
1813 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001814 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1815 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 ++sandbox;
1820 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 retval = 0;
1824 else
1825 {
1826 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 if (tv.v_type == VAR_NUMBER)
1828 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001829 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a string, check if there is a non-digit before
1834 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (!VIM_ISDIGIT(*s) && *s != '-')
1837 *cp = *s++;
1838 retval = atol((char *)s);
1839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001843 if (use_sandbox)
1844 --sandbox;
1845 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 return retval;
1848}
1849#endif
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 * ":let" list all variable values
1853 * ":let var1 var2" list variable values
1854 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 * ":let var += expr" assignment command.
1856 * ":let var -= expr" assignment command.
1857 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
1860 void
1861ex_let(eap)
1862 exarg_T *eap;
1863{
1864 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 int var_count = 0;
1869 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001871 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 argend = skip_var_list(arg, &var_count, &semicolon);
1875 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001877 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1878 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001879 expr = skipwhite(argend);
1880 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1881 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001883 /*
1884 * ":let" without "=": list variables
1885 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 if (*arg == '[')
1887 EMSG(_(e_invarg));
1888 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001894 list_glob_vars(&first);
1895 list_buf_vars(&first);
1896 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001897#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001899#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_script_vars(&first);
1901 list_func_vars(&first);
1902 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 eap->nextcmd = check_nextcmd(arg);
1905 }
1906 else
1907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 op[0] = '=';
1909 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1913 op[0] = *expr; /* +=, -= or .= */
1914 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 else
1917 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (eap->skip)
1920 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 {
1924 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 --emsg_skip;
1927 }
1928 else if (i != FAIL)
1929 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 }
1935}
1936
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937/*
1938 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1939 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 * When "nextchars" is not NULL it points to a string with characters that
1941 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1942 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 * Returns OK or FAIL;
1944 */
1945 static int
1946ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1947 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 int copy; /* copy values from "tv", don't move */
1950 int semicolon; /* from skip_var_list() */
1951 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953{
1954 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 listitem_T *item;
1958 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959
1960 if (*arg != '[')
1961 {
1962 /*
1963 * ":let var = expr" or ":for var in list"
1964 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 return OK;
1968 }
1969
1970 /*
1971 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1972 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001973 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 {
1975 EMSG(_(e_listreq));
1976 return FAIL;
1977 }
1978
1979 i = list_len(l);
1980 if (semicolon == 0 && var_count < i)
1981 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001982 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return FAIL;
1984 }
1985 if (var_count - semicolon > i)
1986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001987 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 return FAIL;
1989 }
1990
1991 item = l->lv_first;
1992 while (*arg != ']')
1993 {
1994 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 item = item->li_next;
1997 if (arg == NULL)
1998 return FAIL;
1999
2000 arg = skipwhite(arg);
2001 if (*arg == ';')
2002 {
2003 /* Put the rest of the list (may be empty) in the var after ';'.
2004 * Create a new list for this. */
2005 l = list_alloc();
2006 if (l == NULL)
2007 return FAIL;
2008 while (item != NULL)
2009 {
2010 list_append_tv(l, &item->li_tv);
2011 item = item->li_next;
2012 }
2013
2014 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002015 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 ltv.vval.v_list = l;
2017 l->lv_refcount = 1;
2018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2020 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 clear_tv(&ltv);
2022 if (arg == NULL)
2023 return FAIL;
2024 break;
2025 }
2026 else if (*arg != ',' && *arg != ']')
2027 {
2028 EMSG2(_(e_intern2), "ex_let_vars()");
2029 return FAIL;
2030 }
2031 }
2032
2033 return OK;
2034}
2035
2036/*
2037 * Skip over assignable variable "var" or list of variables "[var, var]".
2038 * Used for ":let varvar = expr" and ":for varvar in expr".
2039 * For "[var, var]" increment "*var_count" for each variable.
2040 * for "[var, var; var]" set "semicolon".
2041 * Return NULL for an error.
2042 */
2043 static char_u *
2044skip_var_list(arg, var_count, semicolon)
2045 char_u *arg;
2046 int *var_count;
2047 int *semicolon;
2048{
2049 char_u *p, *s;
2050
2051 if (*arg == '[')
2052 {
2053 /* "[var, var]": find the matching ']'. */
2054 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002055 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2058 s = skip_var_one(p);
2059 if (s == p)
2060 {
2061 EMSG2(_(e_invarg2), p);
2062 return NULL;
2063 }
2064 ++*var_count;
2065
2066 p = skipwhite(s);
2067 if (*p == ']')
2068 break;
2069 else if (*p == ';')
2070 {
2071 if (*semicolon == 1)
2072 {
2073 EMSG(_("Double ; in list of variables"));
2074 return NULL;
2075 }
2076 *semicolon = 1;
2077 }
2078 else if (*p != ',')
2079 {
2080 EMSG2(_(e_invarg2), p);
2081 return NULL;
2082 }
2083 }
2084 return p + 1;
2085 }
2086 else
2087 return skip_var_one(arg);
2088}
2089
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002091 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002092 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 static char_u *
2095skip_var_one(arg)
2096 char_u *arg;
2097{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 if (*arg == '@' && arg[1] != NUL)
2099 return arg + 2;
2100 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2101 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102}
2103
Bram Moolenaara7043832005-01-21 11:56:39 +00002104/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 * List variables for hashtab "ht" with prefix "prefix".
2106 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002114{
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 hashitem_T *hi;
2116 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 int todo;
2118
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2121 {
2122 if (!HASHITEM_EMPTY(hi))
2123 {
2124 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 di = HI2DI(hi);
2126 if (empty || di->di_tv.v_type != VAR_STRING
2127 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 }
2130 }
2131}
2132
2133/*
2134 * List global variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_glob_vars(first)
2138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
2143/*
2144 * List buffer variables.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_buf_vars(first)
2148 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_win_vars(first)
2165 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002167 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171#ifdef FEAT_WINDOWS
2172/*
2173 * List tab page variables.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_tab_vars(first)
2177 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181}
2182#endif
2183
Bram Moolenaara7043832005-01-21 11:56:39 +00002184/*
2185 * List Vim variables.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_vim_vars(first)
2189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192}
2193
2194/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195 * List script-local variables, if there is a script.
2196 */
2197 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_script_vars(first)
2199 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200{
2201 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2203 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204}
2205
2206/*
2207 * List function variables, if there is a function.
2208 */
2209 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210list_func_vars(first)
2211 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212{
2213 if (current_funccal != NULL)
2214 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216}
2217
2218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 * List variables in "arg".
2220 */
2221 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 exarg_T *eap;
2224 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226{
2227 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 char_u *name_start;
2231 char_u *arg_subsc;
2232 char_u *tofree;
2233 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234
2235 while (!ends_excmd(*arg) && !got_int)
2236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2241 {
2242 emsg_severe = TRUE;
2243 EMSG(_(e_trailing));
2244 break;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* get_name_len() takes care of expanding curly braces */
2250 name_start = name = arg;
2251 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2252 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* This is mainly to keep test 49 working: when expanding
2255 * curly braces fails overrule the exception error message. */
2256 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 emsg_severe = TRUE;
2259 EMSG2(_(e_invarg2), arg);
2260 break;
2261 }
2262 error = TRUE;
2263 }
2264 else
2265 {
2266 if (tofree != NULL)
2267 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002268 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 else
2271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* handle d.key, l[idx], f(expr) */
2273 arg_subsc = arg;
2274 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 case 'g': list_glob_vars(first); break;
2283 case 'b': list_buf_vars(first); break;
2284 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'v': list_vim_vars(first); break;
2289 case 's': list_script_vars(first); break;
2290 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 default:
2292 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 }
2295 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 {
2297 char_u numbuf[NUMBUFLEN];
2298 char_u *tf;
2299 int c;
2300 char_u *s;
2301
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002302 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 c = *arg;
2304 *arg = NUL;
2305 list_one_var_a((char_u *)"",
2306 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 tv.v_type,
2308 s == NULL ? (char_u *)"" : s,
2309 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 *arg = c;
2311 vim_free(tf);
2312 }
2313 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317
2318 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320
2321 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
2323
2324 return arg;
2325}
2326
2327/*
2328 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2329 * Returns a pointer to the char just after the var name.
2330 * Returns NULL if there is an error.
2331 */
2332 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002335 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002337 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339{
2340 int c1;
2341 char_u *name;
2342 char_u *p;
2343 char_u *arg_end = NULL;
2344 int len;
2345 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347
2348 /*
2349 * ":let $VAR = expr": Set environment variable.
2350 */
2351 if (*arg == '$')
2352 {
2353 /* Find the end of the name. */
2354 ++arg;
2355 name = arg;
2356 len = get_env_len(&arg);
2357 if (len == 0)
2358 EMSG2(_(e_invarg2), name - 1);
2359 else
2360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 if (op != NULL && (*op == '+' || *op == '-'))
2362 EMSG2(_(e_letwrong), op);
2363 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002366 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 {
2368 c1 = name[len];
2369 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 p = get_tv_string_chk(tv);
2371 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 {
2373 int mustfree = FALSE;
2374 char_u *s = vim_getenv(name, &mustfree);
2375
2376 if (s != NULL)
2377 {
2378 p = tofree = concat_str(s, p);
2379 if (mustfree)
2380 vim_free(s);
2381 }
2382 }
2383 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 if (STRICMP(name, "HOME") == 0)
2387 init_homedir();
2388 else if (didset_vim && STRICMP(name, "VIM") == 0)
2389 didset_vim = FALSE;
2390 else if (didset_vimruntime
2391 && STRICMP(name, "VIMRUNTIME") == 0)
2392 didset_vimruntime = FALSE;
2393 arg_end = arg;
2394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
2398 }
2399 }
2400
2401 /*
2402 * ":let &option = expr": Set option value.
2403 * ":let &l:option = expr": Set local option value.
2404 * ":let &g:option = expr": Set global option value.
2405 */
2406 else if (*arg == '&')
2407 {
2408 /* Find the end of the name. */
2409 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002410 if (p == NULL || (endchars != NULL
2411 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 EMSG(_(e_letunexp));
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 long n;
2416 int opt_type;
2417 long numval;
2418 char_u *stringval = NULL;
2419 char_u *s;
2420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 c1 = *p;
2422 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423
2424 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 s = get_tv_string_chk(tv); /* != NULL if number or string */
2426 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 opt_type = get_option_value(arg, &numval,
2429 &stringval, opt_flags);
2430 if ((opt_type == 1 && *op == '.')
2431 || (opt_type == 0 && *op != '.'))
2432 EMSG2(_(e_letwrong), op);
2433 else
2434 {
2435 if (opt_type == 1) /* number */
2436 {
2437 if (*op == '+')
2438 n = numval + n;
2439 else
2440 n = numval - n;
2441 }
2442 else if (opt_type == 0 && stringval != NULL) /* string */
2443 {
2444 s = concat_str(stringval, s);
2445 vim_free(stringval);
2446 stringval = s;
2447 }
2448 }
2449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 if (s != NULL)
2451 {
2452 set_option_value(arg, n, s, opt_flags);
2453 arg_end = p;
2454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 }
2458 }
2459
2460 /*
2461 * ":let @r = expr": Set register contents.
2462 */
2463 else if (*arg == '@')
2464 {
2465 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 if (op != NULL && (*op == '+' || *op == '-'))
2467 EMSG2(_(e_letwrong), op);
2468 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 char_u *s;
2475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 p = get_tv_string_chk(tv);
2477 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002479 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (s != NULL)
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 vim_free(s);
2484 }
2485 }
2486 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 arg_end = arg + 1;
2490 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 }
2493 }
2494
2495 /*
2496 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002499 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002503 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2507 EMSG(_(e_letunexp));
2508 else
2509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 arg_end = p;
2512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516
2517 else
2518 EMSG2(_(e_invarg2), arg);
2519
2520 return arg_end;
2521}
2522
2523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2525 */
2526 static int
2527check_changedtick(arg)
2528 char_u *arg;
2529{
2530 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2531 {
2532 EMSG2(_(e_readonlyvar), arg);
2533 return TRUE;
2534 }
2535 return FALSE;
2536}
2537
2538/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Get an lval: variable, Dict item or List item that can be assigned a value
2540 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2541 * "name.key", "name.key[expr]" etc.
2542 * Indexing only works if "name" is an existing List or Dictionary.
2543 * "name" points to the start of the name.
2544 * If "rettv" is not NULL it points to the value to be assigned.
2545 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2546 * wrong; must end in space or cmd separator.
2547 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 * flags:
2549 * GLV_QUIET: do not give error messages
2550 * GLV_NO_AUTOLOAD: do not use script autoloading
2551 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002553 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 */
2556 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 typval_T *rettv;
2560 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 int unlet;
2562 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002564 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 char_u *expr_start, *expr_end;
2568 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 dictitem_T *v;
2570 typval_T var1;
2571 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002577 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581
2582 if (skip)
2583 {
2584 /* When skipping just find the end of the name. */
2585 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002586 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 }
2588
2589 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (expr_start != NULL)
2592 {
2593 /* Don't expand the name when we already know there is an error. */
2594 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2595 && *p != '[' && *p != '.')
2596 {
2597 EMSG(_(e_trailing));
2598 return NULL;
2599 }
2600
2601 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2602 if (lp->ll_exp_name == NULL)
2603 {
2604 /* Report an invalid expression in braces, unless the
2605 * expression evaluation has been cancelled due to an
2606 * aborting error, an interrupt, or an exception. */
2607 if (!aborting() && !quiet)
2608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002609 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 EMSG2(_(e_invarg2), name);
2611 return NULL;
2612 }
2613 }
2614 lp->ll_name = lp->ll_exp_name;
2615 }
2616 else
2617 lp->ll_name = name;
2618
2619 /* Without [idx] or .key we are done. */
2620 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2621 return p;
2622
2623 cc = *p;
2624 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002625 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (v == NULL && !quiet)
2627 EMSG2(_(e_undefvar), lp->ll_name);
2628 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 if (v == NULL)
2630 return NULL;
2631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /*
2633 * Loop until no more [idx] or .key is following.
2634 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2639 && !(lp->ll_tv->v_type == VAR_DICT
2640 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_("E689: Can only index a List or Dictionary"));
2644 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E708: [:] must come last"));
2650 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 len = -1;
2654 if (*p == '.')
2655 {
2656 key = p + 1;
2657 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2658 ;
2659 if (len == 0)
2660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_(e_emptykey));
2663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = key + len;
2666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 else
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (*p == ':')
2672 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 else
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 empty1 = FALSE;
2676 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 if (get_tv_string_chk(&var1) == NULL)
2679 {
2680 /* not a number or string */
2681 clear_tv(&var1);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /* Optionally get the second index [ :expr]. */
2687 if (*p == ':')
2688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (rettv != NULL && (rettv->v_type != VAR_LIST
2698 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (!empty1)
2703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706 p = skipwhite(p + 1);
2707 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 else
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002718 if (get_tv_string_chk(&var2) == NULL)
2719 {
2720 /* not a number or string */
2721 if (!empty1)
2722 clear_tv(&var1);
2723 clear_tv(&var2);
2724 return NULL;
2725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (!quiet)
2735 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742
2743 /* Skip to past ']'. */
2744 ++p;
2745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
2749 if (len == -1)
2750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*key == NUL)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 lp->ll_list = NULL;
2762 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 /* When assigning to a scope dictionary check that a function and
2766 * variable name is valid (only variable name unless it is l: or
2767 * g: dictionary). Disallow overwriting a builtin function. */
2768 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 int prevval;
2771 int wrong;
2772
2773 if (len != -1)
2774 {
2775 prevval = key[len];
2776 key[len] = NUL;
2777 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002778 else
2779 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2781 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 || !valid_varname(key);
2784 if (len != -1)
2785 key[len] = prevval;
2786 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 return NULL;
2788 }
2789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 /* Can't add "v:" variable. */
2793 if (lp->ll_dict == &vimvardict)
2794 {
2795 EMSG2(_(e_illvar), name);
2796 return NULL;
2797 }
2798
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 p = NULL;
2816 break;
2817 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002819 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 return NULL;
2821
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 else
2827 {
2828 /*
2829 * Get the number and item for the only or first index of the List.
2830 */
2831 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 else
2834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002835 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 clear_tv(&var1);
2837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_list = lp->ll_tv->vval.v_list;
2840 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2841 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002843 if (lp->ll_n1 < 0)
2844 {
2845 lp->ll_n1 = 0;
2846 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2847 }
2848 }
2849 if (lp->ll_li == NULL)
2850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 if (!quiet)
2854 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857
2858 /*
2859 * May need to find the item or absolute index for the second
2860 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 * When no index given: "lp->ll_empty2" is TRUE.
2862 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 {
2873 if (!quiet)
2874 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2881 if (lp->ll_n1 < 0)
2882 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2883 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 {
2885 if (!quiet)
2886 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return p;
2896}
2897
2898/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 */
2901 static void
2902clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904{
2905 vim_free(lp->ll_exp_name);
2906 vim_free(lp->ll_newkey);
2907}
2908
2909/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 listitem_T *ri;
2924 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925
2926 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 cc = *endp;
2931 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002938 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 if ((di == NULL
2942 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2943 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2944 FALSE)))
2945 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 set_var(lp->ll_name, &tv, FALSE);
2947 clear_tv(&tv);
2948 }
2949 }
2950 else
2951 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002955 else if (tv_check_lock(lp->ll_newkey == NULL
2956 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002957 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 else if (lp->ll_range)
2960 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 listitem_T *ll_li = lp->ll_li;
2962 int ll_n1 = lp->ll_n1;
2963
2964 /*
2965 * Check whether any of the list items is locked
2966 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002967 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002970 return;
2971 ri = ri->li_next;
2972 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2973 break;
2974 ll_li = ll_li->li_next;
2975 ++ll_n1;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /*
2979 * Assign the List values to the list items.
2980 */
2981 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2985 else
2986 {
2987 clear_tv(&lp->ll_li->li_tv);
2988 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = ri->li_next;
2991 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2992 break;
2993 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002996 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = NULL;
2999 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 lp->ll_li = lp->ll_li->li_next;
3003 ++lp->ll_n1;
3004 }
3005 if (ri != NULL)
3006 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 else if (lp->ll_empty2
3008 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 : lp->ll_n1 != lp->ll_n2)
3010 EMSG(_("E711: List value has not enough items"));
3011 }
3012 else
3013 {
3014 /*
3015 * Assign to a List or Dictionary item.
3016 */
3017 if (lp->ll_newkey != NULL)
3018 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 if (op != NULL && *op != '=')
3020 {
3021 EMSG2(_(e_letwrong), op);
3022 return;
3023 }
3024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 if (di == NULL)
3028 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3030 {
3031 vim_free(di);
3032 return;
3033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 else if (op != NULL && *op != '=')
3037 {
3038 tv_op(lp->ll_tv, rettv, op);
3039 return;
3040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 /*
3045 * Assign the value to the variable or list item.
3046 */
3047 if (copy)
3048 copy_tv(rettv, lp->ll_tv);
3049 else
3050 {
3051 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003052 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 }
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056}
3057
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3060 * Returns OK or FAIL.
3061 */
3062 static int
3063tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 typval_T *tv1;
3065 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 char_u *op;
3067{
3068 long n;
3069 char_u numbuf[NUMBUFLEN];
3070 char_u *s;
3071
3072 /* Can't do anything with a Funcref or a Dict on the right. */
3073 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3074 {
3075 switch (tv1->v_type)
3076 {
3077 case VAR_DICT:
3078 case VAR_FUNC:
3079 break;
3080
3081 case VAR_LIST:
3082 if (*op != '+' || tv2->v_type != VAR_LIST)
3083 break;
3084 /* List += List */
3085 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3086 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3087 return OK;
3088
3089 case VAR_NUMBER:
3090 case VAR_STRING:
3091 if (tv2->v_type == VAR_LIST)
3092 break;
3093 if (*op == '+' || *op == '-')
3094 {
3095 /* nr += nr or nr -= nr*/
3096 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097#ifdef FEAT_FLOAT
3098 if (tv2->v_type == VAR_FLOAT)
3099 {
3100 float_T f = n;
3101
3102 if (*op == '+')
3103 f += tv2->vval.v_float;
3104 else
3105 f -= tv2->vval.v_float;
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_FLOAT;
3108 tv1->vval.v_float = f;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#endif
3112 {
3113 if (*op == '+')
3114 n += get_tv_number(tv2);
3115 else
3116 n -= get_tv_number(tv2);
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_NUMBER;
3119 tv1->vval.v_number = n;
3120 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 }
3122 else
3123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124 if (tv2->v_type == VAR_FLOAT)
3125 break;
3126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 /* str .= str */
3128 s = get_tv_string(tv1);
3129 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_STRING;
3132 tv1->vval.v_string = s;
3133 }
3134 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135
3136#ifdef FEAT_FLOAT
3137 case VAR_FLOAT:
3138 {
3139 float_T f;
3140
3141 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3142 && tv2->v_type != VAR_NUMBER
3143 && tv2->v_type != VAR_STRING))
3144 break;
3145 if (tv2->v_type == VAR_FLOAT)
3146 f = tv2->vval.v_float;
3147 else
3148 f = get_tv_number(tv2);
3149 if (*op == '+')
3150 tv1->vval.v_float += f;
3151 else
3152 tv1->vval.v_float -= f;
3153 }
3154 return OK;
3155#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 }
3157 }
3158
3159 EMSG2(_(e_letwrong), op);
3160 return FAIL;
3161}
3162
3163/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * Add a watcher to a list.
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
3171 lw->lw_next = l->lv_watch;
3172 l->lv_watch = lw;
3173}
3174
3175/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003176 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 * No warning when it isn't found...
3178 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003179 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 list_T *l;
3182 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 lwp = &l->lv_watch;
3187 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3188 {
3189 if (lw == lwrem)
3190 {
3191 *lwp = lw->lw_next;
3192 break;
3193 }
3194 lwp = &lw->lw_next;
3195 }
3196}
3197
3198/*
3199 * Just before removing an item from a list: advance watchers to the next
3200 * item.
3201 */
3202 static void
3203list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 list_T *l;
3205 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208
3209 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3210 if (lw->lw_item == item)
3211 lw->lw_item = item->li_next;
3212}
3213
3214/*
3215 * Evaluate the expression used in a ":for var in expr" command.
3216 * "arg" points to "var".
3217 * Set "*errp" to TRUE for an error, FALSE otherwise;
3218 * Return a pointer that holds the info. Null when there is an error.
3219 */
3220 void *
3221eval_for_line(arg, errp, nextcmdp, skip)
3222 char_u *arg;
3223 int *errp;
3224 char_u **nextcmdp;
3225 int skip;
3226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 typval_T tv;
3230 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 *errp = TRUE; /* default: there is an error */
3233
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 if (fi == NULL)
3236 return NULL;
3237
3238 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3239 if (expr == NULL)
3240 return fi;
3241
3242 expr = skipwhite(expr);
3243 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3244 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003245 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 return fi;
3247 }
3248
3249 if (skip)
3250 ++emsg_skip;
3251 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3252 {
3253 *errp = FALSE;
3254 if (!skip)
3255 {
3256 l = tv.vval.v_list;
3257 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 clear_tv(&tv);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 else
3263 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003264 /* No need to increment the refcount, it's already set for the
3265 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 fi->fi_list = l;
3267 list_add_watch(l, &fi->fi_lw);
3268 fi->fi_lw.lw_item = l->lv_first;
3269 }
3270 }
3271 }
3272 if (skip)
3273 --emsg_skip;
3274
3275 return fi;
3276}
3277
3278/*
3279 * Use the first item in a ":for" list. Advance to the next.
3280 * Assign the values to the variable (list). "arg" points to the first one.
3281 * Return TRUE when a valid item was found, FALSE when at end of list or
3282 * something wrong.
3283 */
3284 int
3285next_for_item(fi_void, arg)
3286 void *fi_void;
3287 char_u *arg;
3288{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292
3293 item = fi->fi_lw.lw_item;
3294 if (item == NULL)
3295 result = FALSE;
3296 else
3297 {
3298 fi->fi_lw.lw_item = item->li_next;
3299 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3300 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3301 }
3302 return result;
3303}
3304
3305/*
3306 * Free the structure used to store info used by ":for".
3307 */
3308 void
3309free_for_info(fi_void)
3310 void *fi_void;
3311{
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003314 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317 list_unref(fi->fi_list);
3318 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 vim_free(fi);
3320}
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3323
3324 void
3325set_context_for_expression(xp, arg, cmdidx)
3326 expand_T *xp;
3327 char_u *arg;
3328 cmdidx_T cmdidx;
3329{
3330 int got_eq = FALSE;
3331 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 if (cmdidx == CMD_let)
3335 {
3336 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003337 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 {
3339 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (vim_iswhite(*p))
3345 break;
3346 }
3347 return;
3348 }
3349 }
3350 else
3351 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3352 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 while ((xp->xp_pattern = vim_strpbrk(arg,
3354 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3355 {
3356 c = *xp->xp_pattern;
3357 if (c == '&')
3358 {
3359 c = xp->xp_pattern[1];
3360 if (c == '&')
3361 {
3362 ++xp->xp_pattern;
3363 xp->xp_context = cmdidx != CMD_let || got_eq
3364 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3365 }
3366 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003369 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3370 xp->xp_pattern += 2;
3371
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else if (c == '$')
3375 {
3376 /* environment variable */
3377 xp->xp_context = EXPAND_ENV_VARS;
3378 }
3379 else if (c == '=')
3380 {
3381 got_eq = TRUE;
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003384 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 && xp->xp_context == EXPAND_FUNCTIONS
3386 && vim_strchr(xp->xp_pattern, '(') == NULL)
3387 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 break;
3390 }
3391 else if (cmdidx != CMD_let || got_eq)
3392 {
3393 if (c == '"') /* string */
3394 {
3395 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3396 if (c == '\\' && xp->xp_pattern[1] != NUL)
3397 ++xp->xp_pattern;
3398 xp->xp_context = EXPAND_NOTHING;
3399 }
3400 else if (c == '\'') /* literal string */
3401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003402 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3404 /* skip */ ;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '|')
3408 {
3409 if (xp->xp_pattern[1] == '|')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_EXPRESSION;
3413 }
3414 else
3415 xp->xp_context = EXPAND_COMMANDS;
3416 }
3417 else
3418 xp->xp_context = EXPAND_EXPRESSION;
3419 }
3420 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003421 /* Doesn't look like something valid, expand as an expression
3422 * anyway. */
3423 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 arg = xp->xp_pattern;
3425 if (*arg != NUL)
3426 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3427 /* skip */ ;
3428 }
3429 xp->xp_pattern = arg;
3430}
3431
3432#endif /* FEAT_CMDL_COMPL */
3433
3434/*
3435 * ":1,25call func(arg1, arg2)" function call.
3436 */
3437 void
3438ex_call(eap)
3439 exarg_T *eap;
3440{
3441 char_u *arg = eap->arg;
3442 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003444 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 linenr_T lnum;
3448 int doesrange;
3449 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003452 if (eap->skip)
3453 {
3454 /* trans_function_name() doesn't work well when skipping, use eval0()
3455 * instead to skip to any following command, e.g. for:
3456 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003457 ++emsg_skip;
3458 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3459 clear_tv(&rettv);
3460 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 return;
3462 }
3463
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003464 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003465 if (fudi.fd_newkey != NULL)
3466 {
3467 /* Still need to give an error message for missing key. */
3468 EMSG2(_(e_dictkey), fudi.fd_newkey);
3469 vim_free(fudi.fd_newkey);
3470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 if (tofree == NULL)
3472 return;
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 /* Increase refcount on dictionary, it could get deleted when evaluating
3475 * the arguments. */
3476 if (fudi.fd_dict != NULL)
3477 ++fudi.fd_dict->dv_refcount;
3478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003480 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003481 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar532c7802005-01-27 14:44:31 +00003483 /* Skip white space to allow ":call func ()". Not good, but required for
3484 * backward compatibility. */
3485 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (*startarg != '(')
3489 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003490 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 goto end;
3492 }
3493
3494 /*
3495 * When skipping, evaluate the function once, to find the end of the
3496 * arguments.
3497 * When the function takes a range, this is discovered after the first
3498 * call, and the loop is broken.
3499 */
3500 if (eap->skip)
3501 {
3502 ++emsg_skip;
3503 lnum = eap->line2; /* do it once, also with an invalid range */
3504 }
3505 else
3506 lnum = eap->line1;
3507 for ( ; lnum <= eap->line2; ++lnum)
3508 {
3509 if (!eap->skip && eap->addr_count > 0)
3510 {
3511 curwin->w_cursor.lnum = lnum;
3512 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003513#ifdef FEAT_VIRTUALEDIT
3514 curwin->w_cursor.coladd = 0;
3515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003519 eap->line1, eap->line2, &doesrange,
3520 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 failed = TRUE;
3523 break;
3524 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003525
3526 /* Handle a function returning a Funcref, Dictionary or List. */
3527 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3528 {
3529 failed = TRUE;
3530 break;
3531 }
3532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (doesrange || eap->skip)
3535 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (aborting())
3542 break;
3543 }
3544 if (eap->skip)
3545 --emsg_skip;
3546
3547 if (!failed)
3548 {
3549 /* Check for trailing illegal characters and a following command. */
3550 if (!ends_excmd(*arg))
3551 {
3552 emsg_severe = TRUE;
3553 EMSG(_(e_trailing));
3554 }
3555 else
3556 eap->nextcmd = check_nextcmd(arg);
3557 }
3558
3559end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
3564/*
3565 * ":unlet[!] var1 ... " command.
3566 */
3567 void
3568ex_unlet(eap)
3569 exarg_T *eap;
3570{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 ex_unletlock(eap, eap->arg, 0);
3572}
3573
3574/*
3575 * ":lockvar" and ":unlockvar" commands
3576 */
3577 void
3578ex_lockvar(eap)
3579 exarg_T *eap;
3580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
3599ex_unletlock(eap, argstart, deep)
3600 exarg_T *eap;
3601 char_u *argstart;
3602 int deep;
3603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 int forceit;
3658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
3683 int ll_n1 = lp->ll_n1;
3684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003721do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di->di_tv.vval.v_dict;
3743 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hi = hash_find(ht, varname);
3745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003747 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 if (var_check_fixed(di->di_flags, name, FALSE)
3749 || var_check_ro(di->di_flags, name, FALSE)
3750 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 return FAIL;
3752 delete_var(ht, hi);
3753 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 if (forceit)
3757 return OK;
3758 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return FAIL;
3760}
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762/*
3763 * Lock or unlock variable indicated by "lp".
3764 * "deep" is the levels to go (-1 for unlimited);
3765 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3766 */
3767 static int
3768do_lock_var(lp, name_end, deep, lock)
3769 lval_T *lp;
3770 char_u *name_end;
3771 int deep;
3772 int lock;
3773{
3774 int ret = OK;
3775 int cc;
3776 dictitem_T *di;
3777
3778 if (deep == 0) /* nothing to do */
3779 return OK;
3780
3781 if (lp->ll_tv == NULL)
3782 {
3783 cc = *name_end;
3784 *name_end = NUL;
3785
3786 /* Normal name or expanded name. */
3787 if (check_changedtick(lp->ll_name))
3788 ret = FAIL;
3789 else
3790 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003791 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792 if (di == NULL)
3793 ret = FAIL;
3794 else
3795 {
3796 if (lock)
3797 di->di_flags |= DI_FLAGS_LOCK;
3798 else
3799 di->di_flags &= ~DI_FLAGS_LOCK;
3800 item_lock(&di->di_tv, deep, lock);
3801 }
3802 }
3803 *name_end = cc;
3804 }
3805 else if (lp->ll_range)
3806 {
3807 listitem_T *li = lp->ll_li;
3808
3809 /* (un)lock a range of List items. */
3810 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3811 {
3812 item_lock(&li->li_tv, deep, lock);
3813 li = li->li_next;
3814 ++lp->ll_n1;
3815 }
3816 }
3817 else if (lp->ll_list != NULL)
3818 /* (un)lock a List item. */
3819 item_lock(&lp->ll_li->li_tv, deep, lock);
3820 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003821 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 item_lock(&lp->ll_di->di_tv, deep, lock);
3823
3824 return ret;
3825}
3826
3827/*
3828 * Lock or unlock an item. "deep" is nr of levels to go.
3829 */
3830 static void
3831item_lock(tv, deep, lock)
3832 typval_T *tv;
3833 int deep;
3834 int lock;
3835{
3836 static int recurse = 0;
3837 list_T *l;
3838 listitem_T *li;
3839 dict_T *d;
3840 hashitem_T *hi;
3841 int todo;
3842
3843 if (recurse >= DICT_MAXNEST)
3844 {
3845 EMSG(_("E743: variable nested too deep for (un)lock"));
3846 return;
3847 }
3848 if (deep == 0)
3849 return;
3850 ++recurse;
3851
3852 /* lock/unlock the item itself */
3853 if (lock)
3854 tv->v_lock |= VAR_LOCKED;
3855 else
3856 tv->v_lock &= ~VAR_LOCKED;
3857
3858 switch (tv->v_type)
3859 {
3860 case VAR_LIST:
3861 if ((l = tv->vval.v_list) != NULL)
3862 {
3863 if (lock)
3864 l->lv_lock |= VAR_LOCKED;
3865 else
3866 l->lv_lock &= ~VAR_LOCKED;
3867 if (deep < 0 || deep > 1)
3868 /* recursive: lock/unlock the items the List contains */
3869 for (li = l->lv_first; li != NULL; li = li->li_next)
3870 item_lock(&li->li_tv, deep - 1, lock);
3871 }
3872 break;
3873 case VAR_DICT:
3874 if ((d = tv->vval.v_dict) != NULL)
3875 {
3876 if (lock)
3877 d->dv_lock |= VAR_LOCKED;
3878 else
3879 d->dv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 {
3882 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003883 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3885 {
3886 if (!HASHITEM_EMPTY(hi))
3887 {
3888 --todo;
3889 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3890 }
3891 }
3892 }
3893 }
3894 }
3895 --recurse;
3896}
3897
Bram Moolenaara40058a2005-07-11 22:42:07 +00003898/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003899 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3900 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003901 */
3902 static int
3903tv_islocked(tv)
3904 typval_T *tv;
3905{
3906 return (tv->v_lock & VAR_LOCKED)
3907 || (tv->v_type == VAR_LIST
3908 && tv->vval.v_list != NULL
3909 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3910 || (tv->v_type == VAR_DICT
3911 && tv->vval.v_dict != NULL
3912 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3913}
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3916/*
3917 * Delete all "menutrans_" variables.
3918 */
3919 void
3920del_menutrans_vars()
3921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003926 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 {
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3933 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 }
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938#endif
3939
3940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3941
3942/*
3943 * Local string buffer for the next two functions to store a variable name
3944 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3945 * get_user_var_name().
3946 */
3947
3948static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3949
3950static char_u *varnamebuf = NULL;
3951static int varnamebuflen = 0;
3952
3953/*
3954 * Function to concatenate a prefix and a variable name.
3955 */
3956 static char_u *
3957cat_prefix_varname(prefix, name)
3958 int prefix;
3959 char_u *name;
3960{
3961 int len;
3962
3963 len = (int)STRLEN(name) + 3;
3964 if (len > varnamebuflen)
3965 {
3966 vim_free(varnamebuf);
3967 len += 10; /* some additional space */
3968 varnamebuf = alloc(len);
3969 if (varnamebuf == NULL)
3970 {
3971 varnamebuflen = 0;
3972 return NULL;
3973 }
3974 varnamebuflen = len;
3975 }
3976 *varnamebuf = prefix;
3977 varnamebuf[1] = ':';
3978 STRCPY(varnamebuf + 2, name);
3979 return varnamebuf;
3980}
3981
3982/*
3983 * Function given to ExpandGeneric() to obtain the list of user defined
3984 * (global/buffer/window/built-in) variable names.
3985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u *
3987get_user_var_name(xp, idx)
3988 expand_T *xp;
3989 int idx;
3990{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 static long_u gdone;
3992 static long_u bdone;
3993 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994#ifdef FEAT_WINDOWS
3995 static long_u tdone;
3996#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static int vidx;
3998 static hashitem_T *hi;
3999 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 tdone = 0;
4006#endif
4007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004008
4009 /* Global variables */
4010 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 else
4015 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 while (HASHITEM_EMPTY(hi))
4017 ++hi;
4018 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4019 return cat_prefix_varname('g', hi->hi_key);
4020 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022
4023 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004024 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return (char_u *)"b:changedtick";
4039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
4041 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004042 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004045 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 else
4048 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 while (HASHITEM_EMPTY(hi))
4050 ++hi;
4051 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054#ifdef FEAT_WINDOWS
4055 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004056 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 if (tdone < ht->ht_used)
4058 {
4059 if (tdone++ == 0)
4060 hi = ht->ht_array;
4061 else
4062 ++hi;
4063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 return cat_prefix_varname('t', hi->hi_key);
4066 }
4067#endif
4068
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 /* v: variables */
4070 if (vidx < VV_LEN)
4071 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 vim_free(varnamebuf);
4074 varnamebuf = NULL;
4075 varnamebuflen = 0;
4076 return NULL;
4077}
4078
4079#endif /* FEAT_CMDL_COMPL */
4080
4081/*
4082 * types for expressions.
4083 */
4084typedef enum
4085{
4086 TYPE_UNKNOWN = 0
4087 , TYPE_EQUAL /* == */
4088 , TYPE_NEQUAL /* != */
4089 , TYPE_GREATER /* > */
4090 , TYPE_GEQUAL /* >= */
4091 , TYPE_SMALLER /* < */
4092 , TYPE_SEQUAL /* <= */
4093 , TYPE_MATCH /* =~ */
4094 , TYPE_NOMATCH /* !~ */
4095} exptype_T;
4096
4097/*
4098 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4101 */
4102
4103/*
4104 * Handle zero level expression.
4105 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004107 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * Return OK or FAIL.
4109 */
4110 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 char_u **nextcmd;
4115 int evaluate;
4116{
4117 int ret;
4118 char_u *p;
4119
4120 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (ret == FAIL || !ends_excmd(*p))
4123 {
4124 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /*
4127 * Report the invalid expression unless the expression evaluation has
4128 * been cancelled due to an aborting error, an interrupt, or an
4129 * exception.
4130 */
4131 if (!aborting())
4132 EMSG2(_(e_invexpr2), arg);
4133 ret = FAIL;
4134 }
4135 if (nextcmd != NULL)
4136 *nextcmd = check_nextcmd(p);
4137
4138 return ret;
4139}
4140
4141/*
4142 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004143 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004148 * Note: "rettv.v_lock" is not set.
4149 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
4158 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 if ((*arg)[0] == '?')
4168 {
4169 result = FALSE;
4170 if (evaluate)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
4173
4174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 /*
4182 * Get the second variable.
4183 */
4184 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 /*
4189 * Check for the ":".
4190 */
4191 if ((*arg)[0] != ':')
4192 {
4193 EMSG(_("E109: Missing ':' after '?'"));
4194 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197 }
4198
4199 /*
4200 * Get the third variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
4203 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4204 {
4205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208 }
4209 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212
4213 return OK;
4214}
4215
4216/*
4217 * Handle first level expression:
4218 * expr2 || expr2 || expr2 logical OR
4219 *
4220 * "arg" must point to the first non-white of the expression.
4221 * "arg" is advanced to the next non-white after the recognized expression.
4222 *
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int evaluate;
4230{
Bram Moolenaar33570922005-01-25 22:26:29 +00004231 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 long result;
4233 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 /*
4237 * Get the first variable.
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
4241
4242 /*
4243 * Repeat until there is no following "||".
4244 */
4245 first = TRUE;
4246 result = FALSE;
4247 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4248 {
4249 if (evaluate && first)
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (error)
4255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 first = FALSE;
4257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 2);
4263 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4264 return FAIL;
4265
4266 /*
4267 * Compute the result.
4268 */
4269 if (evaluate && !result)
4270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (error)
4275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 if (evaluate)
4278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 rettv->v_type = VAR_NUMBER;
4280 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283
4284 return OK;
4285}
4286
4287/*
4288 * Handle second level expression:
4289 * expr3 && expr3 && expr3 logical AND
4290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int evaluate;
4301{
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 long result;
4304 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /*
4308 * Get the first variable.
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return FAIL;
4312
4313 /*
4314 * Repeat until there is no following "&&".
4315 */
4316 first = TRUE;
4317 result = TRUE;
4318 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4319 {
4320 if (evaluate && first)
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 first = FALSE;
4328 }
4329
4330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 2);
4334 if (eval4(arg, &var2, evaluate && result) == FAIL)
4335 return FAIL;
4336
4337 /*
4338 * Compute the result.
4339 */
4340 if (evaluate && result)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (error)
4346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 if (evaluate)
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 rettv->v_type = VAR_NUMBER;
4351 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355 return OK;
4356}
4357
4358/*
4359 * Handle third level expression:
4360 * var1 == var2
4361 * var1 =~ var2
4362 * var1 != var2
4363 * var1 !~ var2
4364 * var1 > var2
4365 * var1 >= var2
4366 * var1 < var2
4367 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 * var1 is var2
4369 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004434 i = p[len];
4435 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 {
4437 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4438 type_is = TRUE;
4439 }
4440 }
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 if (type != TYPE_UNKNOWN)
4448 {
4449 /* extra question mark appended: ignore case */
4450 if (p[len] == '?')
4451 {
4452 ic = TRUE;
4453 ++len;
4454 }
4455 /* extra '#' appended: match case */
4456 else if (p[len] == '#')
4457 {
4458 ic = FALSE;
4459 ++len;
4460 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
4463 ic = p_ic;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(p + len);
4469 if (eval5(arg, &var2, evaluate) == FAIL)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FAIL;
4473 }
4474
4475 if (evaluate)
4476 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 if (type_is && rettv->v_type != var2.v_type)
4478 {
4479 /* For "is" a different type always means FALSE, for "notis"
4480 * it means TRUE. */
4481 n1 = (type == TYPE_NEQUAL);
4482 }
4483 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4484 {
4485 if (type_is)
4486 {
4487 n1 = (rettv->v_type == var2.v_type
4488 && rettv->vval.v_list == var2.vval.v_list);
4489 if (type == TYPE_NEQUAL)
4490 n1 = !n1;
4491 }
4492 else if (rettv->v_type != var2.v_type
4493 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4494 {
4495 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004496 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004498 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 clear_tv(rettv);
4500 clear_tv(&var2);
4501 return FAIL;
4502 }
4503 else
4504 {
4505 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004506 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4507 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004513 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4514 {
4515 if (type_is)
4516 {
4517 n1 = (rettv->v_type == var2.v_type
4518 && rettv->vval.v_dict == var2.vval.v_dict);
4519 if (type == TYPE_NEQUAL)
4520 n1 = !n1;
4521 }
4522 else if (rettv->v_type != var2.v_type
4523 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4524 {
4525 if (rettv->v_type != var2.v_type)
4526 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4527 else
4528 EMSG(_("E736: Invalid operation for Dictionary"));
4529 clear_tv(rettv);
4530 clear_tv(&var2);
4531 return FAIL;
4532 }
4533 else
4534 {
4535 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004536 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4537 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 }
4542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4544 {
4545 if (rettv->v_type != var2.v_type
4546 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4547 {
4548 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004551 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 clear_tv(rettv);
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 else
4557 {
4558 /* Compare two Funcrefs for being equal or unequal. */
4559 if (rettv->vval.v_string == NULL
4560 || var2.vval.v_string == NULL)
4561 n1 = FALSE;
4562 else
4563 n1 = STRCMP(rettv->vval.v_string,
4564 var2.vval.v_string) == 0;
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 }
4569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570#ifdef FEAT_FLOAT
4571 /*
4572 * If one of the two variables is a float, compare as a float.
4573 * When using "=~" or "!~", always compare as string.
4574 */
4575 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
4578 float_T f1, f2;
4579
4580 if (rettv->v_type == VAR_FLOAT)
4581 f1 = rettv->vval.v_float;
4582 else
4583 f1 = get_tv_number(rettv);
4584 if (var2.v_type == VAR_FLOAT)
4585 f2 = var2.vval.v_float;
4586 else
4587 f2 = get_tv_number(&var2);
4588 n1 = FALSE;
4589 switch (type)
4590 {
4591 case TYPE_EQUAL: n1 = (f1 == f2); break;
4592 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4593 case TYPE_GREATER: n1 = (f1 > f2); break;
4594 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4595 case TYPE_SMALLER: n1 = (f1 < f2); break;
4596 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4597 case TYPE_UNKNOWN:
4598 case TYPE_MATCH:
4599 case TYPE_NOMATCH: break; /* avoid gcc warning */
4600 }
4601 }
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If one of the two variables is a number, compare as a number.
4606 * When using "=~" or "!~", always compare as string.
4607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 n1 = get_tv_number(rettv);
4612 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 switch (type)
4614 {
4615 case TYPE_EQUAL: n1 = (n1 == n2); break;
4616 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4617 case TYPE_GREATER: n1 = (n1 > n2); break;
4618 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4619 case TYPE_SMALLER: n1 = (n1 < n2); break;
4620 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4621 case TYPE_UNKNOWN:
4622 case TYPE_MATCH:
4623 case TYPE_NOMATCH: break; /* avoid gcc warning */
4624 }
4625 }
4626 else
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 s1 = get_tv_string_buf(rettv, buf1);
4629 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4631 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4632 else
4633 i = 0;
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (i == 0); break;
4638 case TYPE_NEQUAL: n1 = (i != 0); break;
4639 case TYPE_GREATER: n1 = (i > 0); break;
4640 case TYPE_GEQUAL: n1 = (i >= 0); break;
4641 case TYPE_SMALLER: n1 = (i < 0); break;
4642 case TYPE_SEQUAL: n1 = (i <= 0); break;
4643
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH:
4646 /* avoid 'l' flag in 'cpoptions' */
4647 save_cpo = p_cpo;
4648 p_cpo = (char_u *)"";
4649 regmatch.regprog = vim_regcomp(s2,
4650 RE_MAGIC + RE_STRING);
4651 regmatch.rm_ic = ic;
4652 if (regmatch.regprog != NULL)
4653 {
4654 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004655 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type == TYPE_NOMATCH)
4657 n1 = !n1;
4658 }
4659 p_cpo = save_cpo;
4660 break;
4661
4662 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4663 }
4664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * Handle fourth level expression:
4677 * + number addition
4678 * - number subtraction
4679 * . string concatenation
4680 *
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4683 *
4684 * Return OK or FAIL.
4685 */
4686 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 int evaluate;
4691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004872eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004876 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005054eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005058 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005137 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
5234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005236 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005350 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005360 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362 if (verbose)
5363 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 return FAIL;
5365 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366#ifdef FEAT_FLOAT
5367 else if (rettv->v_type == VAR_FLOAT)
5368 {
5369 if (verbose)
5370 EMSG(_(e_float_as_string));
5371 return FAIL;
5372 }
5373#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005375 init_tv(&var1);
5376 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 /*
5380 * dict.name
5381 */
5382 key = *arg + 1;
5383 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5384 ;
5385 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 }
5389 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 /*
5392 * something[idx]
5393 *
5394 * Get the (first) variable from inside the [].
5395 */
5396 *arg = skipwhite(*arg + 1);
5397 if (**arg == ':')
5398 empty1 = TRUE;
5399 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5400 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005401 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5402 {
5403 /* not a number or string */
5404 clear_tv(&var1);
5405 return FAIL;
5406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407
5408 /*
5409 * Get the second variable from inside the [:].
5410 */
5411 if (**arg == ':')
5412 {
5413 range = TRUE;
5414 *arg = skipwhite(*arg + 1);
5415 if (**arg == ']')
5416 empty2 = TRUE;
5417 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 if (!empty1)
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
5423 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5424 {
5425 /* not a number or string */
5426 if (!empty1)
5427 clear_tv(&var1);
5428 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 return FAIL;
5430 }
5431 }
5432
5433 /* Check for the ']'. */
5434 if (**arg != ']')
5435 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005436 if (verbose)
5437 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 clear_tv(&var1);
5439 if (range)
5440 clear_tv(&var2);
5441 return FAIL;
5442 }
5443 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445
5446 if (evaluate)
5447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 n1 = 0;
5449 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 n1 = get_tv_number(&var1);
5452 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 }
5454 if (range)
5455 {
5456 if (empty2)
5457 n2 = -1;
5458 else
5459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 n2 = get_tv_number(&var2);
5461 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 }
5464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
5467 case VAR_NUMBER:
5468 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 len = (long)STRLEN(s);
5471 if (range)
5472 {
5473 /* The resulting variable is a substring. If the indexes
5474 * are out of range the result is empty. */
5475 if (n1 < 0)
5476 {
5477 n1 = len + n1;
5478 if (n1 < 0)
5479 n1 = 0;
5480 }
5481 if (n2 < 0)
5482 n2 = len + n2;
5483 else if (n2 >= len)
5484 n2 = len;
5485 if (n1 >= len || n2 < 0 || n1 > n2)
5486 s = NULL;
5487 else
5488 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5489 }
5490 else
5491 {
5492 /* The resulting variable is a string of a single
5493 * character. If the index is too big or negative the
5494 * result is empty. */
5495 if (n1 >= len || n1 < 0)
5496 s = NULL;
5497 else
5498 s = vim_strnsave(s + n1, 1);
5499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 clear_tv(rettv);
5501 rettv->v_type = VAR_STRING;
5502 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 break;
5504
5505 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 if (n1 < 0)
5508 n1 = len + n1;
5509 if (!empty1 && (n1 < 0 || n1 >= len))
5510 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005511 /* For a range we allow invalid values and return an empty
5512 * list. A list index out of range is an error. */
5513 if (!range)
5514 {
5515 if (verbose)
5516 EMSGN(_(e_listidx), n1);
5517 return FAIL;
5518 }
5519 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 if (range)
5522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 list_T *l;
5524 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525
5526 if (n2 < 0)
5527 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005528 else if (n2 >= len)
5529 n2 = len - 1;
5530 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005531 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 l = list_alloc();
5533 if (l == NULL)
5534 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 n1 <= n2; ++n1)
5537 {
5538 if (list_append_tv(l, &item->li_tv) == FAIL)
5539 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005540 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 return FAIL;
5542 }
5543 item = item->li_next;
5544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 clear_tv(rettv);
5546 rettv->v_type = VAR_LIST;
5547 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005548 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 }
5550 else
5551 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005552 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 clear_tv(rettv);
5554 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557
5558 case VAR_DICT:
5559 if (range)
5560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005561 if (verbose)
5562 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 if (len == -1)
5564 clear_tv(&var1);
5565 return FAIL;
5566 }
5567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569
5570 if (len == -1)
5571 {
5572 key = get_tv_string(&var1);
5573 if (*key == NUL)
5574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005575 if (verbose)
5576 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 clear_tv(&var1);
5578 return FAIL;
5579 }
5580 }
5581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005582 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005585 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 if (item == NULL)
5589 return FAIL;
5590
5591 copy_tv(&item->di_tv, &var1);
5592 clear_tv(rettv);
5593 *rettv = var1;
5594 }
5595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 }
5597 }
5598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 return OK;
5600}
5601
5602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 * Get an option value.
5604 * "arg" points to the '&' or '+' before the option name.
5605 * "arg" is advanced to character after the option name.
5606 * Return OK or FAIL.
5607 */
5608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005611 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 int evaluate;
5613{
5614 char_u *option_end;
5615 long numval;
5616 char_u *stringval;
5617 int opt_type;
5618 int c;
5619 int working = (**arg == '+'); /* has("+option") */
5620 int ret = OK;
5621 int opt_flags;
5622
5623 /*
5624 * Isolate the option name and find its value.
5625 */
5626 option_end = find_option_end(arg, &opt_flags);
5627 if (option_end == NULL)
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 EMSG2(_("E112: Option name missing: %s"), *arg);
5631 return FAIL;
5632 }
5633
5634 if (!evaluate)
5635 {
5636 *arg = option_end;
5637 return OK;
5638 }
5639
5640 c = *option_end;
5641 *option_end = NUL;
5642 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645 if (opt_type == -3) /* invalid name */
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 EMSG2(_("E113: Unknown option: %s"), *arg);
5649 ret = FAIL;
5650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 if (opt_type == -2) /* hidden string option */
5654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 rettv->v_type = VAR_STRING;
5656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else if (opt_type == -1) /* hidden number option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_NUMBER;
5661 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == 1) /* number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else /* string option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_STRING;
5671 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 }
5674 else if (working && (opt_type == -2 || opt_type == -1))
5675 ret = FAIL;
5676
5677 *option_end = c; /* put back for error messages */
5678 *arg = option_end;
5679
5680 return ret;
5681}
5682
5683/*
5684 * Allocate a variable for a string constant.
5685 * Return OK or FAIL.
5686 */
5687 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 int evaluate;
5692{
5693 char_u *p;
5694 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 int extra = 0;
5696
5697 /*
5698 * Find the end of the string, skipping backslashed characters.
5699 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 if (*p == '\\' && p[1] != NUL)
5703 {
5704 ++p;
5705 /* A "\<x>" form occupies at least 4 characters, and produces up
5706 * to 6 characters: reserve space for 2 extra */
5707 if (*p == '<')
5708 extra += 2;
5709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711
5712 if (*p != '"')
5713 {
5714 EMSG2(_("E114: Missing quote: %s"), *arg);
5715 return FAIL;
5716 }
5717
5718 /* If only parsing, set *arg and return here */
5719 if (!evaluate)
5720 {
5721 *arg = p + 1;
5722 return OK;
5723 }
5724
5725 /*
5726 * Copy the string into allocated memory, handling backslashed
5727 * characters.
5728 */
5729 name = alloc((unsigned)(p - *arg + extra));
5730 if (name == NULL)
5731 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 rettv->v_type = VAR_STRING;
5733 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
5737 if (*p == '\\')
5738 {
5739 switch (*++p)
5740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 case 'b': *name++ = BS; ++p; break;
5742 case 'e': *name++ = ESC; ++p; break;
5743 case 'f': *name++ = FF; ++p; break;
5744 case 'n': *name++ = NL; ++p; break;
5745 case 'r': *name++ = CAR; ++p; break;
5746 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 case 'X': /* hex: "\x1", "\x12" */
5749 case 'x':
5750 case 'u': /* Unicode: "\u0023" */
5751 case 'U':
5752 if (vim_isxdigit(p[1]))
5753 {
5754 int n, nr;
5755 int c = toupper(*p);
5756
5757 if (c == 'X')
5758 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005759 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005761 else
5762 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 nr = 0;
5764 while (--n >= 0 && vim_isxdigit(p[1]))
5765 {
5766 ++p;
5767 nr = (nr << 4) + hex2nr(*p);
5768 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770#ifdef FEAT_MBYTE
5771 /* For "\u" store the number according to
5772 * 'encoding'. */
5773 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
5776#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 break;
5780
5781 /* octal: "\1", "\12", "\123" */
5782 case '0':
5783 case '1':
5784 case '2':
5785 case '3':
5786 case '4':
5787 case '5':
5788 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 case '7': *name = *p++ - '0';
5790 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *name = (*name << 3) + *p++ - '0';
5793 if (*p >= '0' && *p <= '7')
5794 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798
5799 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 if (extra != 0)
5802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 break;
5805 }
5806 /* FALLTHROUGH */
5807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810 }
5811 }
5812 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 *arg = p + 1;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return OK;
5820}
5821
5822/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * Return OK or FAIL.
5825 */
5826 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005827get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 int evaluate;
5831{
5832 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 int reduce = 0;
5835
5836 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 */
5839 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 break;
5845 ++reduce;
5846 ++p;
5847 }
5848 }
5849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 return FAIL;
5854 }
5855
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 if (!evaluate)
5858 {
5859 *arg = p + 1;
5860 return OK;
5861 }
5862
5863 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 */
5866 str = alloc((unsigned)((p - *arg) - reduce));
5867 if (str == NULL)
5868 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 rettv->v_type = VAR_STRING;
5870 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 break;
5878 ++p;
5879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 *arg = p + 1;
5884
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 return OK;
5886}
5887
5888/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 * Allocate a variable for a List and fill it from "*arg".
5890 * Return OK or FAIL.
5891 */
5892 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005893get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 int evaluate;
5897{
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *l = NULL;
5899 typval_T tv;
5900 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901
5902 if (evaluate)
5903 {
5904 l = list_alloc();
5905 if (l == NULL)
5906 return FAIL;
5907 }
5908
5909 *arg = skipwhite(*arg + 1);
5910 while (**arg != ']' && **arg != NUL)
5911 {
5912 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5913 goto failret;
5914 if (evaluate)
5915 {
5916 item = listitem_alloc();
5917 if (item != NULL)
5918 {
5919 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005920 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 list_append(l, item);
5922 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005923 else
5924 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 }
5926
5927 if (**arg == ']')
5928 break;
5929 if (**arg != ',')
5930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 goto failret;
5933 }
5934 *arg = skipwhite(*arg + 1);
5935 }
5936
5937 if (**arg != ']')
5938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940failret:
5941 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005942 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 return FAIL;
5944 }
5945
5946 *arg = skipwhite(*arg + 1);
5947 if (evaluate)
5948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005949 rettv->v_type = VAR_LIST;
5950 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 ++l->lv_refcount;
5952 }
5953
5954 return OK;
5955}
5956
5957/*
5958 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005959 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005961 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962list_alloc()
5963{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 list_T *l;
5965
5966 l = (list_T *)alloc_clear(sizeof(list_T));
5967 if (l != NULL)
5968 {
5969 /* Prepend the list to the list of lists for garbage collection. */
5970 if (first_list != NULL)
5971 first_list->lv_used_prev = l;
5972 l->lv_used_prev = NULL;
5973 l->lv_used_next = first_list;
5974 first_list = l;
5975 }
5976 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977}
5978
5979/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005980 * Allocate an empty list for a return value.
5981 * Returns OK or FAIL.
5982 */
5983 static int
5984rettv_list_alloc(rettv)
5985 typval_T *rettv;
5986{
5987 list_T *l = list_alloc();
5988
5989 if (l == NULL)
5990 return FAIL;
5991
5992 rettv->vval.v_list = l;
5993 rettv->v_type = VAR_LIST;
5994 ++l->lv_refcount;
5995 return OK;
5996}
5997
5998/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 * Unreference a list: decrement the reference count and free it when it
6000 * becomes zero.
6001 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006002 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006006 if (l != NULL && --l->lv_refcount <= 0)
6007 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008}
6009
6010/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006011 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Ignores the reference count.
6013 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006014 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015list_free(l, recurse)
6016 list_T *l;
6017 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006021 /* Remove the list from the list of lists for garbage collection. */
6022 if (l->lv_used_prev == NULL)
6023 first_list = l->lv_used_next;
6024 else
6025 l->lv_used_prev->lv_used_next = l->lv_used_next;
6026 if (l->lv_used_next != NULL)
6027 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6028
Bram Moolenaard9fba312005-06-26 22:34:35 +00006029 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006031 /* Remove the item before deleting it. */
6032 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006033 if (recurse || (item->li_tv.v_type != VAR_LIST
6034 && item->li_tv.v_type != VAR_DICT))
6035 clear_tv(&item->li_tv);
6036 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 }
6038 vim_free(l);
6039}
6040
6041/*
6042 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006043 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006045 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046listitem_alloc()
6047{
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049}
6050
6051/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006052 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006054 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006058 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 vim_free(item);
6060}
6061
6062/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006063 * Remove a list item from a List and free it. Also clears the value.
6064 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006065 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 list_T *l;
6068 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006070 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071 listitem_free(item);
6072}
6073
6074/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 * Get the number of items in a list.
6076 */
6077 static long
6078list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 if (l == NULL)
6082 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006083 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084}
6085
6086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE when two lists have exactly the same values.
6088 */
6089 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 list_T *l1;
6092 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095{
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006098 if (l1 == NULL || l2 == NULL)
6099 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006100 if (l1 == l2)
6101 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006102 if (list_len(l1) != list_len(l2))
6103 return FALSE;
6104
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105 for (item1 = l1->lv_first, item2 = l2->lv_first;
6106 item1 != NULL && item2 != NULL;
6107 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 return FALSE;
6110 return item1 == NULL && item2 == NULL;
6111}
6112
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006113#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6114 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006115/*
6116 * Return the dictitem that an entry in a hashtable points to.
6117 */
6118 dictitem_T *
6119dict_lookup(hi)
6120 hashitem_T *hi;
6121{
6122 return HI2DI(hi);
6123}
6124#endif
6125
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006127 * Return TRUE when two dictionaries have exactly the same key/values.
6128 */
6129 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006130dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 dict_T *d1;
6132 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 hashitem_T *hi;
6137 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006140 if (d1 == NULL || d2 == NULL)
6141 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006142 if (d1 == d2)
6143 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 if (dict_len(d1) != dict_len(d2))
6145 return FALSE;
6146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006147 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 if (!HASHITEM_EMPTY(hi))
6151 {
6152 item2 = dict_find(d2, hi->hi_key, -1);
6153 if (item2 == NULL)
6154 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006155 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006156 return FALSE;
6157 --todo;
6158 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 }
6160 return TRUE;
6161}
6162
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163static int tv_equal_recurse_limit;
6164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006167 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006168 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006169 */
6170 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 typval_T *tv1;
6173 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 int ic; /* ignore case */
6175 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176{
6177 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006178 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006180 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006185 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186 * recursiveness to a limit. We guess they are equal then.
6187 * A fixed limit has the problem of still taking an awful long time.
6188 * Reduce the limit every time running into it. That should work fine for
6189 * deeply linked structures that are not recursively linked and catch
6190 * recursiveness quickly. */
6191 if (!recursive)
6192 tv_equal_recurse_limit = 1000;
6193 if (recursive_cnt >= tv_equal_recurse_limit)
6194 {
6195 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006196 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 ++recursive_cnt;
6203 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6204 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006205 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206
6207 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006208 ++recursive_cnt;
6209 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6210 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006211 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006212
6213 case VAR_FUNC:
6214 return (tv1->vval.v_string != NULL
6215 && tv2->vval.v_string != NULL
6216 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6217
6218 case VAR_NUMBER:
6219 return tv1->vval.v_number == tv2->vval.v_number;
6220
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006221#ifdef FEAT_FLOAT
6222 case VAR_FLOAT:
6223 return tv1->vval.v_float == tv2->vval.v_float;
6224#endif
6225
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226 case VAR_STRING:
6227 s1 = get_tv_string_buf(tv1, buf1);
6228 s2 = get_tv_string_buf(tv2, buf2);
6229 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231
6232 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006233 return TRUE;
6234}
6235
6236/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 * Locate item with index "n" in list "l" and return it.
6238 * A negative index is counted from the end; -1 is the last item.
6239 * Returns NULL when "n" is out of range.
6240 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 long n;
6245{
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 long idx;
6248
6249 if (l == NULL)
6250 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006251
6252 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006254 n = l->lv_len + n;
6255
6256 /* Check for index out of range. */
6257 if (n < 0 || n >= l->lv_len)
6258 return NULL;
6259
6260 /* When there is a cached index may start search from there. */
6261 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 if (n < l->lv_idx / 2)
6264 {
6265 /* closest to the start of the list */
6266 item = l->lv_first;
6267 idx = 0;
6268 }
6269 else if (n > (l->lv_idx + l->lv_len) / 2)
6270 {
6271 /* closest to the end of the list */
6272 item = l->lv_last;
6273 idx = l->lv_len - 1;
6274 }
6275 else
6276 {
6277 /* closest to the cached index */
6278 item = l->lv_idx_item;
6279 idx = l->lv_idx;
6280 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 }
6282 else
6283 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 if (n < l->lv_len / 2)
6285 {
6286 /* closest to the start of the list */
6287 item = l->lv_first;
6288 idx = 0;
6289 }
6290 else
6291 {
6292 /* closest to the end of the list */
6293 item = l->lv_last;
6294 idx = l->lv_len - 1;
6295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297
6298 while (n > idx)
6299 {
6300 /* search forward */
6301 item = item->li_next;
6302 ++idx;
6303 }
6304 while (n < idx)
6305 {
6306 /* search backward */
6307 item = item->li_prev;
6308 --idx;
6309 }
6310
6311 /* cache the used index */
6312 l->lv_idx = idx;
6313 l->lv_idx_item = item;
6314
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 return item;
6316}
6317
6318/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006319 * Get list item "l[idx]" as a number.
6320 */
6321 static long
6322list_find_nr(l, idx, errorp)
6323 list_T *l;
6324 long idx;
6325 int *errorp; /* set to TRUE when something wrong */
6326{
6327 listitem_T *li;
6328
6329 li = list_find(l, idx);
6330 if (li == NULL)
6331 {
6332 if (errorp != NULL)
6333 *errorp = TRUE;
6334 return -1L;
6335 }
6336 return get_tv_number_chk(&li->li_tv, errorp);
6337}
6338
6339/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006340 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6341 */
6342 char_u *
6343list_find_str(l, idx)
6344 list_T *l;
6345 long idx;
6346{
6347 listitem_T *li;
6348
6349 li = list_find(l, idx - 1);
6350 if (li == NULL)
6351 {
6352 EMSGN(_(e_listidx), idx);
6353 return NULL;
6354 }
6355 return get_tv_string(&li->li_tv);
6356}
6357
6358/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006359 * Locate "item" list "l" and return its index.
6360 * Returns -1 when "item" is not in the list.
6361 */
6362 static long
6363list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 list_T *l;
6365 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366{
6367 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006369
6370 if (l == NULL)
6371 return -1;
6372 idx = 0;
6373 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6374 ++idx;
6375 if (li == NULL)
6376 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006377 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378}
6379
6380/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 * Append item "item" to the end of list "l".
6382 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006383 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 list_T *l;
6386 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387{
6388 if (l->lv_last == NULL)
6389 {
6390 /* empty list */
6391 l->lv_first = item;
6392 l->lv_last = item;
6393 item->li_prev = NULL;
6394 }
6395 else
6396 {
6397 l->lv_last->li_next = item;
6398 item->li_prev = l->lv_last;
6399 l->lv_last = item;
6400 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 item->li_next = NULL;
6403}
6404
6405/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006409 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 list_T *l;
6412 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006414 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415
Bram Moolenaar05159a02005-02-26 23:04:13 +00006416 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006418 copy_tv(tv, &li->li_tv);
6419 list_append(l, li);
6420 return OK;
6421}
6422
6423/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006424 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 * Return FAIL when out of memory.
6426 */
6427 int
6428list_append_dict(list, dict)
6429 list_T *list;
6430 dict_T *dict;
6431{
6432 listitem_T *li = listitem_alloc();
6433
6434 if (li == NULL)
6435 return FAIL;
6436 li->li_tv.v_type = VAR_DICT;
6437 li->li_tv.v_lock = 0;
6438 li->li_tv.vval.v_dict = dict;
6439 list_append(list, li);
6440 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 return OK;
6442}
6443
6444/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006445 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006446 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006447 * Returns FAIL when out of memory.
6448 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006449 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 list_T *l;
6452 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454{
6455 listitem_T *li = listitem_alloc();
6456
6457 if (li == NULL)
6458 return FAIL;
6459 list_append(l, li);
6460 li->li_tv.v_type = VAR_STRING;
6461 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462 if (str == NULL)
6463 li->li_tv.vval.v_string = NULL;
6464 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006465 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 return FAIL;
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006471 * Append "n" to list "l".
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
6475list_append_number(l, n)
6476 list_T *l;
6477 varnumber_T n;
6478{
6479 listitem_T *li;
6480
6481 li = listitem_alloc();
6482 if (li == NULL)
6483 return FAIL;
6484 li->li_tv.v_type = VAR_NUMBER;
6485 li->li_tv.v_lock = 0;
6486 li->li_tv.vval.v_number = n;
6487 list_append(l, li);
6488 return OK;
6489}
6490
6491/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 * If "item" is NULL append at the end.
6494 * Return FAIL when out of memory.
6495 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006496 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 list_T *l;
6499 typval_T *tv;
6500 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501{
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503
6504 if (ni == NULL)
6505 return FAIL;
6506 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006507 list_insert(l, ni, item);
6508 return OK;
6509}
6510
6511 void
6512list_insert(l, ni, item)
6513 list_T *l;
6514 listitem_T *ni;
6515 listitem_T *item;
6516{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 if (item == NULL)
6518 /* Append new item at end of list. */
6519 list_append(l, ni);
6520 else
6521 {
6522 /* Insert new item before existing item. */
6523 ni->li_prev = item->li_prev;
6524 ni->li_next = item;
6525 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 ++l->lv_idx;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 l->lv_idx_item = NULL;
6534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538}
6539
6540/*
6541 * Extend "l1" with "l2".
6542 * If "bef" is NULL append at the end, otherwise insert before this item.
6543 * Returns FAIL when out of memory.
6544 */
6545 static int
6546list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 list_T *l1;
6548 list_T *l2;
6549 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550{
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006552 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006554 /* We also quit the loop when we have inserted the original item count of
6555 * the list, avoid a hang when we extend a list with itself. */
6556 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6558 return FAIL;
6559 return OK;
6560}
6561
6562/*
6563 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6564 * Return FAIL when out of memory.
6565 */
6566 static int
6567list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 list_T *l1;
6569 list_T *l2;
6570 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571{
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006574 if (l1 == NULL || l2 == NULL)
6575 return FAIL;
6576
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 if (l == NULL)
6580 return FAIL;
6581 tv->v_type = VAR_LIST;
6582 tv->vval.v_list = l;
6583
6584 /* append all items from the second list */
6585 return list_extend(l, l2, NULL);
6586}
6587
6588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 * Returns NULL when out of memory.
6593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 list_T *copy;
6601 listitem_T *item;
6602 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 if (orig == NULL)
6605 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606
6607 copy = list_alloc();
6608 if (copy != NULL)
6609 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 if (copyID != 0)
6611 {
6612 /* Do this before adding the items, because one of the items may
6613 * refer back to this list. */
6614 orig->lv_copyID = copyID;
6615 orig->lv_copylist = copy;
6616 }
6617 for (item = orig->lv_first; item != NULL && !got_int;
6618 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 {
6620 ni = listitem_alloc();
6621 if (ni == NULL)
6622 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 {
6625 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6626 {
6627 vim_free(ni);
6628 break;
6629 }
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 list_append(copy, ni);
6634 }
6635 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006636 if (item != NULL)
6637 {
6638 list_unref(copy);
6639 copy = NULL;
6640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 }
6642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return copy;
6644}
6645
6646/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006648 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006649 * This used to be called list_remove, but that conflicts with a Sun header
6650 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006652 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006653vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006654 list_T *l;
6655 listitem_T *item;
6656 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657{
Bram Moolenaar33570922005-01-25 22:26:29 +00006658 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006660 /* notify watchers */
6661 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006663 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 list_fix_watch(l, ip);
6665 if (ip == item2)
6666 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668
6669 if (item2->li_next == NULL)
6670 l->lv_last = item->li_prev;
6671 else
6672 item2->li_next->li_prev = item->li_prev;
6673 if (item->li_prev == NULL)
6674 l->lv_first = item2->li_next;
6675 else
6676 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006677 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678}
6679
6680/*
6681 * Return an allocated string with the string representation of a list.
6682 * May return NULL.
6683 */
6684 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006685list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006686 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688{
6689 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690
6691 if (tv->vval.v_list == NULL)
6692 return NULL;
6693 ga_init2(&ga, (int)sizeof(char), 80);
6694 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006696 {
6697 vim_free(ga.ga_data);
6698 return NULL;
6699 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 ga_append(&ga, ']');
6701 ga_append(&ga, NUL);
6702 return (char_u *)ga.ga_data;
6703}
6704
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006705typedef struct join_S {
6706 char_u *s;
6707 char_u *tofree;
6708} join_T;
6709
6710 static int
6711list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6712 garray_T *gap; /* to store the result in */
6713 list_T *l;
6714 char_u *sep;
6715 int echo_style;
6716 int copyID;
6717 garray_T *join_gap; /* to keep each list item string */
6718{
6719 int i;
6720 join_T *p;
6721 int len;
6722 int sumlen = 0;
6723 int first = TRUE;
6724 char_u *tofree;
6725 char_u numbuf[NUMBUFLEN];
6726 listitem_T *item;
6727 char_u *s;
6728
6729 /* Stringify each item in the list. */
6730 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6731 {
6732 if (echo_style)
6733 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6734 else
6735 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6736 if (s == NULL)
6737 return FAIL;
6738
6739 len = (int)STRLEN(s);
6740 sumlen += len;
6741
Bram Moolenaarcde88542015-08-11 19:14:00 +02006742 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006743 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6744 if (tofree != NULL || s != numbuf)
6745 {
6746 p->s = s;
6747 p->tofree = tofree;
6748 }
6749 else
6750 {
6751 p->s = vim_strnsave(s, len);
6752 p->tofree = p->s;
6753 }
6754
6755 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006756 if (did_echo_string_emsg) /* recursion error, bail out */
6757 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006758 }
6759
6760 /* Allocate result buffer with its total size, avoid re-allocation and
6761 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6762 if (join_gap->ga_len >= 2)
6763 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6764 if (ga_grow(gap, sumlen + 2) == FAIL)
6765 return FAIL;
6766
6767 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6768 {
6769 if (first)
6770 first = FALSE;
6771 else
6772 ga_concat(gap, sep);
6773 p = ((join_T *)join_gap->ga_data) + i;
6774
6775 if (p->s != NULL)
6776 ga_concat(gap, p->s);
6777 line_breakcheck();
6778 }
6779
6780 return OK;
6781}
6782
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006783/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006785 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006786 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006788 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006789list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006793 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006794 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796 garray_T join_ga;
6797 int retval;
6798 join_T *p;
6799 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800
Bram Moolenaard39a7512015-04-16 22:51:22 +02006801 if (l->lv_len < 1)
6802 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006803 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6804 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6805
6806 /* Dispose each item in join_ga. */
6807 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809 p = (join_T *)join_ga.ga_data;
6810 for (i = 0; i < join_ga.ga_len; ++i)
6811 {
6812 vim_free(p->tofree);
6813 ++p;
6814 }
6815 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006817
6818 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819}
6820
6821/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 * Garbage collection for lists and dictionaries.
6823 *
6824 * We use reference counts to be able to free most items right away when they
6825 * are no longer used. But for composite items it's possible that it becomes
6826 * unused while the reference count is > 0: When there is a recursive
6827 * reference. Example:
6828 * :let l = [1, 2, 3]
6829 * :let d = {9: l}
6830 * :let l[1] = d
6831 *
6832 * Since this is quite unusual we handle this with garbage collection: every
6833 * once in a while find out which lists and dicts are not referenced from any
6834 * variable.
6835 *
6836 * Here is a good reference text about garbage collection (refers to Python
6837 * but it applies to all reference-counting mechanisms):
6838 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840
6841/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 * Do garbage collection for lists and dicts.
6843 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 int
6846garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006847{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 buf_T *buf;
6851 win_T *wp;
6852 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006853 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006854 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006856#ifdef FEAT_WINDOWS
6857 tabpage_T *tp;
6858#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006860 /* Only do this once. */
6861 want_garbage_collect = FALSE;
6862 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006863 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006864
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 /* We advance by two because we add one for items referenced through
6866 * previous_funccal. */
6867 current_copyID += COPYID_INC;
6868 copyID = current_copyID;
6869
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 /*
6871 * 1. Go through all accessible variables and mark all lists and dicts
6872 * with copyID.
6873 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874
6875 /* Don't free variables in the previous_funccal list unless they are only
6876 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006877 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6879 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6881 NULL);
6882 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6883 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 }
6885
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 /* script-local variables */
6887 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889
6890 /* buffer-local variables */
6891 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006899#ifdef FEAT_AUTOCMD
6900 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006903#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905#ifdef FEAT_WINDOWS
6906 /* tabpage-local variables */
6907 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6909 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910#endif
6911
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914
6915 /* function-local variables */
6916 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6917 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6919 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 }
6921
Bram Moolenaard812df62008-11-09 12:46:09 +00006922 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006924
Bram Moolenaar1dced572012-04-05 16:54:08 +02006925#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006927#endif
6928
Bram Moolenaardb913952012-06-29 12:54:53 +02006929#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006931#endif
6932
6933#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006935#endif
6936
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 /*
6940 * 2. Free lists and dictionaries that are not referenced.
6941 */
6942 did_free = free_unref_items(copyID);
6943
6944 /*
6945 * 3. Check if any funccal can be freed now.
6946 */
6947 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 if (can_free_funccal(*pfc, copyID))
6950 {
6951 fc = *pfc;
6952 *pfc = fc->caller;
6953 free_funccal(fc, TRUE);
6954 did_free = TRUE;
6955 did_free_funccal = TRUE;
6956 }
6957 else
6958 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 if (did_free_funccal)
6961 /* When a funccal was freed some more items might be garbage
6962 * collected, so run again. */
6963 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 else if (p_verbose > 0)
6966 {
6967 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6968 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969
6970 return did_free;
6971}
6972
6973/*
6974 * Free lists and dictionaries that are no longer referenced.
6975 */
6976 static int
6977free_unref_items(copyID)
6978 int copyID;
6979{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 dict_T *dd, *dd_next;
6981 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 int did_free = FALSE;
6983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 */
6987 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006988 {
6989 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 /* Free the Dictionary and ordinary items it contains, but don't
6993 * recurse into Lists and Dictionaries, they will be in the list
6994 * of dicts or list of lists. */
6995 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 dd = dd_next;
6999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000
7001 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 * Go through the list of lists and free items without the copyID.
7003 * But don't free a list that has a watcher (used in a for loop), these
7004 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 */
7006 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 {
7008 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7010 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007012 /* Free the List and ordinary items it contains, but don't recurse
7013 * into Lists and Dictionaries, they will be in the list of dicts
7014 * or list of lists. */
7015 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007018 ll = ll_next;
7019 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 return did_free;
7021}
7022
7023/*
7024 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 * "list_stack" is used to add lists to be marked. Can be NULL.
7026 *
7027 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 int
7030set_ref_in_ht(ht, copyID, list_stack)
7031 hashtab_T *ht;
7032 int copyID;
7033 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034{
7035 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 hashtab_T *cur_ht;
7039 ht_stack_T *ht_stack = NULL;
7040 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 cur_ht = ht;
7043 for (;;)
7044 {
7045 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 /* Mark each item in the hashtab. If the item contains a hashtab
7048 * it is added to ht_stack, if it contains a list it is added to
7049 * list_stack. */
7050 todo = (int)cur_ht->ht_used;
7051 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7052 if (!HASHITEM_EMPTY(hi))
7053 {
7054 --todo;
7055 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7056 &ht_stack, list_stack);
7057 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059
7060 if (ht_stack == NULL)
7061 break;
7062
7063 /* take an item from the stack */
7064 cur_ht = ht_stack->ht;
7065 tempitem = ht_stack;
7066 ht_stack = ht_stack->prev;
7067 free(tempitem);
7068 }
7069
7070 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071}
7072
7073/*
7074 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7076 *
7077 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 int
7080set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 list_T *l;
7082 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 listitem_T *li;
7086 int abort = FALSE;
7087 list_T *cur_l;
7088 list_stack_T *list_stack = NULL;
7089 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 cur_l = l;
7092 for (;;)
7093 {
7094 if (!abort)
7095 /* Mark each item in the list. If the item contains a hashtab
7096 * it is added to ht_stack, if it contains a list it is added to
7097 * list_stack. */
7098 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7099 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7100 ht_stack, &list_stack);
7101 if (list_stack == NULL)
7102 break;
7103
7104 /* take an item from the stack */
7105 cur_l = list_stack->list;
7106 tempitem = list_stack;
7107 list_stack = list_stack->prev;
7108 free(tempitem);
7109 }
7110
7111 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112}
7113
7114/*
7115 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 * "list_stack" is used to add lists to be marked. Can be NULL.
7117 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7118 *
7119 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007121 int
7122set_ref_in_item(tv, copyID, ht_stack, list_stack)
7123 typval_T *tv;
7124 int copyID;
7125 ht_stack_T **ht_stack;
7126 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127{
7128 dict_T *dd;
7129 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007131
7132 switch (tv->v_type)
7133 {
7134 case VAR_DICT:
7135 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007136 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007138 /* Didn't see this dict yet. */
7139 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 if (ht_stack == NULL)
7141 {
7142 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7143 }
7144 else
7145 {
7146 ht_stack_T *newitem = (ht_stack_T*)malloc(
7147 sizeof(ht_stack_T));
7148 if (newitem == NULL)
7149 abort = TRUE;
7150 else
7151 {
7152 newitem->ht = &dd->dv_hashtab;
7153 newitem->prev = *ht_stack;
7154 *ht_stack = newitem;
7155 }
7156 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007157 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007158 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007159
7160 case VAR_LIST:
7161 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007162 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007163 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 /* Didn't see this list yet. */
7165 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 if (list_stack == NULL)
7167 {
7168 abort = set_ref_in_list(ll, copyID, ht_stack);
7169 }
7170 else
7171 {
7172 list_stack_T *newitem = (list_stack_T*)malloc(
7173 sizeof(list_stack_T));
7174 if (newitem == NULL)
7175 abort = TRUE;
7176 else
7177 {
7178 newitem->list = ll;
7179 newitem->prev = *list_stack;
7180 *list_stack = newitem;
7181 }
7182 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007183 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007185 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007187}
7188
7189/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 * Allocate an empty header for a dictionary.
7191 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007192 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193dict_alloc()
7194{
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007200 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201 if (first_dict != NULL)
7202 first_dict->dv_used_prev = d;
7203 d->dv_used_next = first_dict;
7204 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007205 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007208 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007209 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007210 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007211 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007212 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214}
7215
7216/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007217 * Allocate an empty dict for a return value.
7218 * Returns OK or FAIL.
7219 */
7220 static int
7221rettv_dict_alloc(rettv)
7222 typval_T *rettv;
7223{
7224 dict_T *d = dict_alloc();
7225
7226 if (d == NULL)
7227 return FAIL;
7228
7229 rettv->vval.v_dict = d;
7230 rettv->v_type = VAR_DICT;
7231 ++d->dv_refcount;
7232 return OK;
7233}
7234
7235
7236/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 * Unreference a Dictionary: decrement the reference count and free it when it
7238 * becomes zero.
7239 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007240 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007244 if (d != NULL && --d->dv_refcount <= 0)
7245 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246}
7247
7248/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007249 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 * Ignores the reference count.
7251 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007252 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007253dict_free(d, recurse)
7254 dict_T *d;
7255 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007257 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007259 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007261 /* Remove the dict from the list of dicts for garbage collection. */
7262 if (d->dv_used_prev == NULL)
7263 first_dict = d->dv_used_next;
7264 else
7265 d->dv_used_prev->dv_used_next = d->dv_used_next;
7266 if (d->dv_used_next != NULL)
7267 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7268
7269 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007270 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007271 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 if (!HASHITEM_EMPTY(hi))
7275 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007276 /* Remove the item before deleting it, just in case there is
7277 * something recursive causing trouble. */
7278 di = HI2DI(hi);
7279 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007280 if (recurse || (di->di_tv.v_type != VAR_LIST
7281 && di->di_tv.v_type != VAR_DICT))
7282 clear_tv(&di->di_tv);
7283 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 --todo;
7285 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 vim_free(d);
7289}
7290
7291/*
7292 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 * The "key" is copied to the new item.
7294 * Note that the value of the item "di_tv" still needs to be initialized!
7295 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007297 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298dictitem_alloc(key)
7299 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007307 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007308 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Make a copy of a Dictionary item.
7314 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7322 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 if (di != NULL)
7324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007326 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 copy_tv(&org->di_tv, &di->di_tv);
7328 }
7329 return di;
7330}
7331
7332/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 * Remove item "item" from Dictionary "dict" and free it.
7334 */
7335 static void
7336dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dict_T *dict;
7338 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339{
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 if (HASHITEM_EMPTY(hi))
7344 EMSG2(_(e_intern2), "dictitem_remove()");
7345 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 dictitem_free(item);
7348}
7349
7350/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 * Free a dict item. Also clears the value.
7352 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007353 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007358 if (item->di_flags & DI_FLAGS_ALLOC)
7359 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360}
7361
7362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7364 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007365 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 * Returns NULL when out of memory.
7367 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373{
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dict_T *copy;
7375 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378
7379 if (orig == NULL)
7380 return NULL;
7381
7382 copy = dict_alloc();
7383 if (copy != NULL)
7384 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 if (copyID != 0)
7386 {
7387 orig->dv_copyID = copyID;
7388 orig->dv_copydict = copy;
7389 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007390 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 --todo;
7396
7397 di = dictitem_alloc(hi->hi_key);
7398 if (di == NULL)
7399 break;
7400 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 {
7402 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7403 copyID) == FAIL)
7404 {
7405 vim_free(di);
7406 break;
7407 }
7408 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 else
7410 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7411 if (dict_add(copy, di) == FAIL)
7412 {
7413 dictitem_free(di);
7414 break;
7415 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 if (todo > 0)
7421 {
7422 dict_unref(copy);
7423 copy = NULL;
7424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 }
7426
7427 return copy;
7428}
7429
7430/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007432 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007434 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 dict_T *d;
7437 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438{
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440}
7441
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007443 * Add a number or string entry to dictionary "d".
7444 * When "str" is NULL use number "nr", otherwise use "str".
7445 * Returns FAIL when out of memory and when key already exists.
7446 */
7447 int
7448dict_add_nr_str(d, key, nr, str)
7449 dict_T *d;
7450 char *key;
7451 long nr;
7452 char_u *str;
7453{
7454 dictitem_T *item;
7455
7456 item = dictitem_alloc((char_u *)key);
7457 if (item == NULL)
7458 return FAIL;
7459 item->di_tv.v_lock = 0;
7460 if (str == NULL)
7461 {
7462 item->di_tv.v_type = VAR_NUMBER;
7463 item->di_tv.vval.v_number = nr;
7464 }
7465 else
7466 {
7467 item->di_tv.v_type = VAR_STRING;
7468 item->di_tv.vval.v_string = vim_strsave(str);
7469 }
7470 if (dict_add(d, item) == FAIL)
7471 {
7472 dictitem_free(item);
7473 return FAIL;
7474 }
7475 return OK;
7476}
7477
7478/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007479 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007480 * Returns FAIL when out of memory and when key already exists.
7481 */
7482 int
7483dict_add_list(d, key, list)
7484 dict_T *d;
7485 char *key;
7486 list_T *list;
7487{
7488 dictitem_T *item;
7489
7490 item = dictitem_alloc((char_u *)key);
7491 if (item == NULL)
7492 return FAIL;
7493 item->di_tv.v_lock = 0;
7494 item->di_tv.v_type = VAR_LIST;
7495 item->di_tv.vval.v_list = list;
7496 if (dict_add(d, item) == FAIL)
7497 {
7498 dictitem_free(item);
7499 return FAIL;
7500 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007501 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007502 return OK;
7503}
7504
7505/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 * Get the number of items in a Dictionary.
7507 */
7508 static long
7509dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (d == NULL)
7513 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007514 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515}
7516
7517/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 * Find item "key[len]" in Dictionary "d".
7519 * If "len" is negative use strlen(key).
7520 * Returns NULL when not found.
7521 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007522 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 char_u *key;
7526 int len;
7527{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528#define AKEYLEN 200
7529 char_u buf[AKEYLEN];
7530 char_u *akey;
7531 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 if (len < 0)
7535 akey = key;
7536 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538 tofree = akey = vim_strnsave(key, len);
7539 if (akey == NULL)
7540 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007541 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 else
7543 {
7544 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007545 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007546 akey = buf;
7547 }
7548
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 vim_free(tofree);
7551 if (HASHITEM_EMPTY(hi))
7552 return NULL;
7553 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554}
7555
7556/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007557 * Get a string item from a dictionary.
7558 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007559 * Returns NULL if the entry doesn't exist or out of memory.
7560 */
7561 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563 dict_T *d;
7564 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007565 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007566{
7567 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007568 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007569
7570 di = dict_find(d, key, -1);
7571 if (di == NULL)
7572 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 s = get_tv_string(&di->di_tv);
7574 if (save && s != NULL)
7575 s = vim_strsave(s);
7576 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007577}
7578
7579/*
7580 * Get a number item from a dictionary.
7581 * Returns 0 if the entry doesn't exist or out of memory.
7582 */
7583 long
7584get_dict_number(d, key)
7585 dict_T *d;
7586 char_u *key;
7587{
7588 dictitem_T *di;
7589
7590 di = dict_find(d, key, -1);
7591 if (di == NULL)
7592 return 0;
7593 return get_tv_number(&di->di_tv);
7594}
7595
7596/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 * Return an allocated string with the string representation of a Dictionary.
7598 * May return NULL.
7599 */
7600 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604{
7605 garray_T ga;
7606 int first = TRUE;
7607 char_u *tofree;
7608 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 return NULL;
7616 ga_init2(&ga, (int)sizeof(char), 80);
7617 ga_append(&ga, '{');
7618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007619 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007620 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 --todo;
7625
7626 if (first)
7627 first = FALSE;
7628 else
7629 ga_concat(&ga, (char_u *)", ");
7630
7631 tofree = string_quote(hi->hi_key, FALSE);
7632 if (tofree != NULL)
7633 {
7634 ga_concat(&ga, tofree);
7635 vim_free(tofree);
7636 }
7637 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007638 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 if (s != NULL)
7640 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007642 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007643 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007644 line_breakcheck();
7645
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007648 if (todo > 0)
7649 {
7650 vim_free(ga.ga_data);
7651 return NULL;
7652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653
7654 ga_append(&ga, '}');
7655 ga_append(&ga, NUL);
7656 return (char_u *)ga.ga_data;
7657}
7658
7659/*
7660 * Allocate a variable for a Dictionary and fill it from "*arg".
7661 * Return OK or FAIL. Returns NOTDONE for {expr}.
7662 */
7663 static int
7664get_dict_tv(arg, rettv, evaluate)
7665 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 int evaluate;
7668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 dict_T *d = NULL;
7670 typval_T tvkey;
7671 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007672 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676
7677 /*
7678 * First check if it's not a curly-braces thing: {expr}.
7679 * Must do this without evaluating, otherwise a function may be called
7680 * twice. Unfortunately this means we need to call eval1() twice for the
7681 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 if (*start != '}')
7685 {
7686 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7687 return FAIL;
7688 if (*start == '}')
7689 return NOTDONE;
7690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691
7692 if (evaluate)
7693 {
7694 d = dict_alloc();
7695 if (d == NULL)
7696 return FAIL;
7697 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 tvkey.v_type = VAR_UNKNOWN;
7699 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700
7701 *arg = skipwhite(*arg + 1);
7702 while (**arg != '}' && **arg != NUL)
7703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 goto failret;
7706 if (**arg != ':')
7707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007708 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007709 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007712 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007714 key = get_tv_string_buf_chk(&tvkey, buf);
7715 if (key == NULL || *key == NUL)
7716 {
7717 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7718 if (key != NULL)
7719 EMSG(_(e_emptykey));
7720 clear_tv(&tvkey);
7721 goto failret;
7722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724
7725 *arg = skipwhite(*arg + 1);
7726 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7727 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007728 if (evaluate)
7729 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 goto failret;
7731 }
7732 if (evaluate)
7733 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007734 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 if (item != NULL)
7736 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007737 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 clear_tv(&tv);
7740 goto failret;
7741 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742 item = dictitem_alloc(key);
7743 clear_tv(&tvkey);
7744 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007747 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 if (dict_add(d, item) == FAIL)
7749 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 }
7751 }
7752
7753 if (**arg == '}')
7754 break;
7755 if (**arg != ',')
7756 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007757 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 goto failret;
7759 }
7760 *arg = skipwhite(*arg + 1);
7761 }
7762
7763 if (**arg != '}')
7764 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007765 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766failret:
7767 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007768 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 return FAIL;
7770 }
7771
7772 *arg = skipwhite(*arg + 1);
7773 if (evaluate)
7774 {
7775 rettv->v_type = VAR_DICT;
7776 rettv->vval.v_dict = d;
7777 ++d->dv_refcount;
7778 }
7779
7780 return OK;
7781}
7782
Bram Moolenaar8c711452005-01-14 21:53:12 +00007783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007784 * Return a string with the string representation of a variable.
7785 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007786 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007789 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007790 */
7791 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007792echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007796 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 static int recurse = 0;
7799 char_u *r = NULL;
7800
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007803 if (!did_echo_string_emsg)
7804 {
7805 /* Only give this message once for a recursive call to avoid
7806 * flooding the user with errors. And stop iterating over lists
7807 * and dicts. */
7808 did_echo_string_emsg = TRUE;
7809 EMSG(_("E724: variable nested too deep for displaying"));
7810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007812 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 }
7814 ++recurse;
7815
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816 switch (tv->v_type)
7817 {
7818 case VAR_FUNC:
7819 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 r = tv->vval.v_string;
7821 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007824 if (tv->vval.v_list == NULL)
7825 {
7826 *tofree = NULL;
7827 r = NULL;
7828 }
7829 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7830 {
7831 *tofree = NULL;
7832 r = (char_u *)"[...]";
7833 }
7834 else
7835 {
7836 tv->vval.v_list->lv_copyID = copyID;
7837 *tofree = list2string(tv, copyID);
7838 r = *tofree;
7839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007840 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841
Bram Moolenaar8c711452005-01-14 21:53:12 +00007842 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843 if (tv->vval.v_dict == NULL)
7844 {
7845 *tofree = NULL;
7846 r = NULL;
7847 }
7848 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7849 {
7850 *tofree = NULL;
7851 r = (char_u *)"{...}";
7852 }
7853 else
7854 {
7855 tv->vval.v_dict->dv_copyID = copyID;
7856 *tofree = dict2string(tv, copyID);
7857 r = *tofree;
7858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007861 case VAR_STRING:
7862 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 *tofree = NULL;
7864 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#ifdef FEAT_FLOAT
7868 case VAR_FLOAT:
7869 *tofree = NULL;
7870 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7871 r = numbuf;
7872 break;
7873#endif
7874
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879
Bram Moolenaar8502c702014-06-17 12:51:16 +02007880 if (--recurse == 0)
7881 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883}
7884
7885/*
7886 * Return a string with the string representation of a variable.
7887 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7888 * "numbuf" is used for a number.
7889 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007890 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 */
7892 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007893tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 char_u **tofree;
7896 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007897 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898{
7899 switch (tv->v_type)
7900 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 case VAR_FUNC:
7902 *tofree = string_quote(tv->vval.v_string, TRUE);
7903 return *tofree;
7904 case VAR_STRING:
7905 *tofree = string_quote(tv->vval.v_string, FALSE);
7906 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907#ifdef FEAT_FLOAT
7908 case VAR_FLOAT:
7909 *tofree = NULL;
7910 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7911 return numbuf;
7912#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007915 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007920 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921}
7922
7923/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 * Return string "str" in ' quotes, doubling ' characters.
7925 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007927 */
7928 static char_u *
7929string_quote(str, function)
7930 char_u *str;
7931 int function;
7932{
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007934 char_u *p, *r, *s;
7935
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 len = (function ? 13 : 3);
7937 if (str != NULL)
7938 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007939 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 for (p = str; *p != NUL; mb_ptr_adv(p))
7941 if (*p == '\'')
7942 ++len;
7943 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 s = r = alloc(len);
7945 if (r != NULL)
7946 {
7947 if (function)
7948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 r += 10;
7951 }
7952 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 if (str != NULL)
7955 for (p = str; *p != NUL; )
7956 {
7957 if (*p == '\'')
7958 *r++ = '\'';
7959 MB_COPY_CHAR(p, r);
7960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 if (function)
7963 *r++ = ')';
7964 *r++ = NUL;
7965 }
7966 return s;
7967}
7968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969#ifdef FEAT_FLOAT
7970/*
7971 * Convert the string "text" to a floating point number.
7972 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7973 * this always uses a decimal point.
7974 * Returns the length of the text that was consumed.
7975 */
7976 static int
7977string2float(text, value)
7978 char_u *text;
7979 float_T *value; /* result stored here */
7980{
7981 char *s = (char *)text;
7982 float_T f;
7983
7984 f = strtod(s, &s);
7985 *value = f;
7986 return (int)((char_u *)s - text);
7987}
7988#endif
7989
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 * Get the value of an environment variable.
7992 * "arg" is pointing to the '$'. It is advanced to after the name.
7993 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007994 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */
7996 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 int evaluate;
8001{
8002 char_u *string = NULL;
8003 int len;
8004 int cc;
8005 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008006 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
8008 ++*arg;
8009 name = *arg;
8010 len = get_env_len(arg);
8011 if (evaluate)
8012 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008013 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008014 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008015
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008016 cc = name[len];
8017 name[len] = NUL;
8018 /* first try vim_getenv(), fast for normal environment vars */
8019 string = vim_getenv(name, &mustfree);
8020 if (string != NULL && *string != NUL)
8021 {
8022 if (!mustfree)
8023 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008025 else
8026 {
8027 if (mustfree)
8028 vim_free(string);
8029
8030 /* next try expanding things like $VIM and ${HOME} */
8031 string = expand_env_save(name - 1);
8032 if (string != NULL && *string == '$')
8033 {
8034 vim_free(string);
8035 string = NULL;
8036 }
8037 }
8038 name[len] = cc;
8039
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040 rettv->v_type = VAR_STRING;
8041 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 }
8043
8044 return OK;
8045}
8046
8047/*
8048 * Array with names and number of arguments of all internal functions
8049 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8050 */
8051static struct fst
8052{
8053 char *f_name; /* function name */
8054 char f_min_argc; /* minimal number of arguments */
8055 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008057 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058} functions[] =
8059{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#ifdef FEAT_FLOAT
8061 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008062 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008064 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008065 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"append", 2, 2, f_append},
8067 {"argc", 0, 0, f_argc},
8068 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008069 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008070 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008074 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008077 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"bufexists", 1, 1, f_bufexists},
8079 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8080 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8081 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8082 {"buflisted", 1, 1, f_buflisted},
8083 {"bufloaded", 1, 1, f_bufloaded},
8084 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008085 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"bufwinnr", 1, 1, f_bufwinnr},
8087 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008088 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008089 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008090 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008091#ifdef FEAT_FLOAT
8092 {"ceil", 1, 1, f_ceil},
8093#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008094 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008095 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008097 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008099#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008100 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008101 {"complete_add", 1, 1, f_complete_add},
8102 {"complete_check", 0, 0, f_complete_check},
8103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008105 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#ifdef FEAT_FLOAT
8107 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008108 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008110 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008112 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008113 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"delete", 1, 1, f_delete},
8115 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008116 {"diff_filler", 1, 1, f_diff_filler},
8117 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008118 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008120 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"eventhandler", 0, 0, f_eventhandler},
8122 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008123 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008125#ifdef FEAT_FLOAT
8126 {"exp", 1, 1, f_exp},
8127#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008128 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008130 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8132 {"filereadable", 1, 1, f_filereadable},
8133 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008135 {"finddir", 1, 3, f_finddir},
8136 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#ifdef FEAT_FLOAT
8138 {"float2nr", 1, 1, f_float2nr},
8139 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008140 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008142 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"fnamemodify", 2, 2, f_fnamemodify},
8144 {"foldclosed", 1, 1, f_foldclosed},
8145 {"foldclosedend", 1, 1, f_foldclosedend},
8146 {"foldlevel", 1, 1, f_foldlevel},
8147 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008148 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008150 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008151 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008152 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008153 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008154 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"getchar", 0, 1, f_getchar},
8156 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008157 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"getcmdline", 0, 0, f_getcmdline},
8159 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008160 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008161 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008162 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008164 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008165 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"getfsize", 1, 1, f_getfsize},
8167 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008168 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008170 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008171 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008172 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008173 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008174 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008175 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008177 {"gettabvar", 2, 3, f_gettabvar},
8178 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"getwinposx", 0, 0, f_getwinposx},
8180 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008181 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008182 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008183 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008184 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008187 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008188 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8190 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8191 {"histadd", 2, 2, f_histadd},
8192 {"histdel", 1, 2, f_histdel},
8193 {"histget", 1, 2, f_histget},
8194 {"histnr", 1, 1, f_histnr},
8195 {"hlID", 1, 1, f_hlID},
8196 {"hlexists", 1, 1, f_hlexists},
8197 {"hostname", 0, 0, f_hostname},
8198 {"iconv", 3, 3, f_iconv},
8199 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008200 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008201 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008203 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"inputrestore", 0, 0, f_inputrestore},
8205 {"inputsave", 0, 0, f_inputsave},
8206 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008208 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008210 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008211 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008212 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008213 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"libcall", 3, 3, f_libcall},
8217 {"libcallnr", 3, 3, f_libcallnr},
8218 {"line", 1, 1, f_line},
8219 {"line2byte", 1, 1, f_line2byte},
8220 {"lispindent", 1, 1, f_lispindent},
8221 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008222#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008223 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008224 {"log10", 1, 1, f_log10},
8225#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008226#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008227 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008228#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008230 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008231 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008232 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008233 {"matchadd", 2, 5, f_matchadd},
8234 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008235 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008236 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008237 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008238 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008239 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008240 {"max", 1, 1, f_max},
8241 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008242#ifdef vim_mkdir
8243 {"mkdir", 1, 3, f_mkdir},
8244#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008246#ifdef FEAT_MZSCHEME
8247 {"mzeval", 1, 1, f_mzeval},
8248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008250 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008251 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008252 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008253#ifdef FEAT_FLOAT
8254 {"pow", 2, 2, f_pow},
8255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008257 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008258 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008259#ifdef FEAT_PYTHON3
8260 {"py3eval", 1, 1, f_py3eval},
8261#endif
8262#ifdef FEAT_PYTHON
8263 {"pyeval", 1, 1, f_pyeval},
8264#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008266 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008267 {"reltime", 0, 2, f_reltime},
8268 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"remote_expr", 2, 3, f_remote_expr},
8270 {"remote_foreground", 1, 1, f_remote_foreground},
8271 {"remote_peek", 1, 2, f_remote_peek},
8272 {"remote_read", 1, 1, f_remote_read},
8273 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008274 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008276 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008278 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279#ifdef FEAT_FLOAT
8280 {"round", 1, 1, f_round},
8281#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008282 {"screenattr", 2, 2, f_screenattr},
8283 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008284 {"screencol", 0, 0, f_screencol},
8285 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008286 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008287 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008288 {"searchpair", 3, 7, f_searchpair},
8289 {"searchpairpos", 3, 7, f_searchpairpos},
8290 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"server2client", 2, 2, f_server2client},
8292 {"serverlist", 0, 0, f_serverlist},
8293 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008294 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"setcmdpos", 1, 1, f_setcmdpos},
8296 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008297 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008298 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008299 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008300 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008302 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008303 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008305#ifdef FEAT_CRYPT
8306 {"sha256", 1, 1, f_sha256},
8307#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008308 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008309 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#ifdef FEAT_FLOAT
8312 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008313 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008314#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008315 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008316 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008317 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008318 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008319 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008320#ifdef FEAT_FLOAT
8321 {"sqrt", 1, 1, f_sqrt},
8322 {"str2float", 1, 1, f_str2float},
8323#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008324 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008325 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008326 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327#ifdef HAVE_STRFTIME
8328 {"strftime", 1, 2, f_strftime},
8329#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"strlen", 1, 1, f_strlen},
8333 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008334 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008336 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008337 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"substitute", 4, 4, f_substitute},
8339 {"synID", 3, 3, f_synID},
8340 {"synIDattr", 2, 3, f_synIDattr},
8341 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008342 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008343 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008344 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008345 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008346 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008347 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008348 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008349 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008350 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008351#ifdef FEAT_FLOAT
8352 {"tan", 1, 1, f_tan},
8353 {"tanh", 1, 1, f_tanh},
8354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008356 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"tolower", 1, 1, f_tolower},
8358 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008359 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008360#ifdef FEAT_FLOAT
8361 {"trunc", 1, 1, f_trunc},
8362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008364 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008365 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008366 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008367 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"virtcol", 1, 1, f_virtcol},
8369 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008370 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"winbufnr", 1, 1, f_winbufnr},
8372 {"wincol", 0, 0, f_wincol},
8373 {"winheight", 1, 1, f_winheight},
8374 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008375 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008377 {"winrestview", 1, 1, f_winrestview},
8378 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008380 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008381 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382};
8383
8384#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8385
8386/*
8387 * Function given to ExpandGeneric() to obtain the list of internal
8388 * or user defined function names.
8389 */
8390 char_u *
8391get_function_name(xp, idx)
8392 expand_T *xp;
8393 int idx;
8394{
8395 static int intidx = -1;
8396 char_u *name;
8397
8398 if (idx == 0)
8399 intidx = -1;
8400 if (intidx < 0)
8401 {
8402 name = get_user_func_name(xp, idx);
8403 if (name != NULL)
8404 return name;
8405 }
8406 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8407 {
8408 STRCPY(IObuff, functions[intidx].f_name);
8409 STRCAT(IObuff, "(");
8410 if (functions[intidx].f_max_argc == 0)
8411 STRCAT(IObuff, ")");
8412 return IObuff;
8413 }
8414
8415 return NULL;
8416}
8417
8418/*
8419 * Function given to ExpandGeneric() to obtain the list of internal or
8420 * user defined variable or function names.
8421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 char_u *
8423get_expr_name(xp, idx)
8424 expand_T *xp;
8425 int idx;
8426{
8427 static int intidx = -1;
8428 char_u *name;
8429
8430 if (idx == 0)
8431 intidx = -1;
8432 if (intidx < 0)
8433 {
8434 name = get_function_name(xp, idx);
8435 if (name != NULL)
8436 return name;
8437 }
8438 return get_user_var_name(xp, ++intidx);
8439}
8440
8441#endif /* FEAT_CMDL_COMPL */
8442
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008443#if defined(EBCDIC) || defined(PROTO)
8444/*
8445 * Compare struct fst by function name.
8446 */
8447 static int
8448compare_func_name(s1, s2)
8449 const void *s1;
8450 const void *s2;
8451{
8452 struct fst *p1 = (struct fst *)s1;
8453 struct fst *p2 = (struct fst *)s2;
8454
8455 return STRCMP(p1->f_name, p2->f_name);
8456}
8457
8458/*
8459 * Sort the function table by function name.
8460 * The sorting of the table above is ASCII dependant.
8461 * On machines using EBCDIC we have to sort it.
8462 */
8463 static void
8464sortFunctions()
8465{
8466 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8467
8468 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8469}
8470#endif
8471
8472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473/*
8474 * Find internal function in table above.
8475 * Return index, or -1 if not found
8476 */
8477 static int
8478find_internal_func(name)
8479 char_u *name; /* name of the function */
8480{
8481 int first = 0;
8482 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8483 int cmp;
8484 int x;
8485
8486 /*
8487 * Find the function name in the table. Binary search.
8488 */
8489 while (first <= last)
8490 {
8491 x = first + ((unsigned)(last - first) >> 1);
8492 cmp = STRCMP(name, functions[x].f_name);
8493 if (cmp < 0)
8494 last = x - 1;
8495 else if (cmp > 0)
8496 first = x + 1;
8497 else
8498 return x;
8499 }
8500 return -1;
8501}
8502
8503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8505 * name it contains, otherwise return "name".
8506 */
8507 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008508deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 char_u *name;
8510 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008511 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512{
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 int cc;
8515
8516 cc = name[*lenp];
8517 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008518 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008522 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008523 {
8524 *lenp = 0;
8525 return (char_u *)""; /* just in case */
8526 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008527 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 }
8530
8531 return name;
8532}
8533
8534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 * Allocate a variable for the result of a function.
8536 * Return OK or FAIL.
8537 */
8538 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8540 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 char_u *name; /* name of the function */
8542 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 char_u **arg; /* argument, pointing to the '(' */
8545 linenr_T firstline; /* first line of range */
8546 linenr_T lastline; /* last line of range */
8547 int *doesrange; /* return: function handled range */
8548 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 char_u *argp;
8552 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008553 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 int argcount = 0; /* number of arguments found */
8555
8556 /*
8557 * Get the arguments.
8558 */
8559 argp = *arg;
8560 while (argcount < MAX_FUNC_ARGS)
8561 {
8562 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8563 if (*argp == ')' || *argp == ',' || *argp == NUL)
8564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8566 {
8567 ret = FAIL;
8568 break;
8569 }
8570 ++argcount;
8571 if (*argp != ',')
8572 break;
8573 }
8574 if (*argp == ')')
8575 ++argp;
8576 else
8577 ret = FAIL;
8578
8579 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 {
8584 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008585 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008587 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 *arg = skipwhite(argp);
8594 return ret;
8595}
8596
8597
8598/*
8599 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008600 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008601 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 */
8603 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008604call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008606 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008610 typval_T *argvars; /* vars for arguments, must have "argcount"
8611 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 linenr_T firstline; /* first line of range */
8613 linenr_T lastline; /* last line of range */
8614 int *doesrange; /* return: function handled range */
8615 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617{
8618 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619#define ERROR_UNKNOWN 0
8620#define ERROR_TOOMANY 1
8621#define ERROR_TOOFEW 2
8622#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623#define ERROR_DICT 4
8624#define ERROR_NONE 5
8625#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 int error = ERROR_NONE;
8627 int i;
8628 int llen;
8629 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630#define FLEN_FIXED 40
8631 char_u fname_buf[FLEN_FIXED + 1];
8632 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008633 char_u *name;
8634
8635 /* Make a copy of the name, if it comes from a funcref variable it could
8636 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008637 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008638 if (name == NULL)
8639 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 /*
8642 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8643 * Change <SNR>123_name() to K_SNR 123_name().
8644 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 llen = eval_fname_script(name);
8647 if (llen > 0)
8648 {
8649 fname_buf[0] = K_SPECIAL;
8650 fname_buf[1] = KS_EXTRA;
8651 fname_buf[2] = (int)KE_SNR;
8652 i = 3;
8653 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8654 {
8655 if (current_SID <= 0)
8656 error = ERROR_SCRIPT;
8657 else
8658 {
8659 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8660 i = (int)STRLEN(fname_buf);
8661 }
8662 }
8663 if (i + STRLEN(name + llen) < FLEN_FIXED)
8664 {
8665 STRCPY(fname_buf + i, name + llen);
8666 fname = fname_buf;
8667 }
8668 else
8669 {
8670 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8671 if (fname == NULL)
8672 error = ERROR_OTHER;
8673 else
8674 {
8675 mch_memmove(fname, fname_buf, (size_t)i);
8676 STRCPY(fname + i, name + llen);
8677 }
8678 }
8679 }
8680 else
8681 fname = name;
8682
8683 *doesrange = FALSE;
8684
8685
8686 /* execute the function if no errors detected and executing */
8687 if (evaluate && error == ERROR_NONE)
8688 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008689 char_u *rfname = fname;
8690
8691 /* Ignore "g:" before a function name. */
8692 if (fname[0] == 'g' && fname[1] == ':')
8693 rfname = fname + 2;
8694
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008695 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8696 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 error = ERROR_UNKNOWN;
8698
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {
8701 /*
8702 * User defined function.
8703 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008704 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008707 /* Trigger FuncUndefined event, may load the function. */
8708 if (fp == NULL
8709 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008710 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008711 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008713 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008717 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 {
8720 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008721 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008722 }
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (fp != NULL)
8725 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008726 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008728 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008730 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008733 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 else
8735 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008736 int did_save_redo = FALSE;
8737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 /*
8739 * Call the user function.
8740 * Save and restore search patterns, script variables and
8741 * redo buffer.
8742 */
8743 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008744 if (!ins_compl_active())
8745 {
8746 saveRedobuff();
8747 did_save_redo = TRUE;
8748 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8753 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8754 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008755 /* Function was unreferenced while being used, free it
8756 * now. */
8757 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008758 if (did_save_redo)
8759 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 restore_search_patterns();
8761 error = ERROR_NONE;
8762 }
8763 }
8764 }
8765 else
8766 {
8767 /*
8768 * Find the function name in the table, call its implementation.
8769 */
8770 i = find_internal_func(fname);
8771 if (i >= 0)
8772 {
8773 if (argcount < functions[i].f_min_argc)
8774 error = ERROR_TOOFEW;
8775 else if (argcount > functions[i].f_max_argc)
8776 error = ERROR_TOOMANY;
8777 else
8778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008779 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 error = ERROR_NONE;
8782 }
8783 }
8784 }
8785 /*
8786 * The function call (or "FuncUndefined" autocommand sequence) might
8787 * have been aborted by an error, an interrupt, or an explicitly thrown
8788 * exception that has not been caught so far. This situation can be
8789 * tested for by calling aborting(). For an error in an internal
8790 * function or for the "E132" error in call_user_func(), however, the
8791 * throw point at which the "force_abort" flag (temporarily reset by
8792 * emsg()) is normally updated has not been reached yet. We need to
8793 * update that flag first to make aborting() reliable.
8794 */
8795 update_force_abort();
8796 }
8797 if (error == ERROR_NONE)
8798 ret = OK;
8799
8800 /*
8801 * Report an error unless the argument evaluation or function call has been
8802 * cancelled due to an aborting error, an interrupt, or an exception.
8803 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008804 if (!aborting())
8805 {
8806 switch (error)
8807 {
8808 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008809 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008810 break;
8811 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008812 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 break;
8814 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008815 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008816 name);
8817 break;
8818 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008819 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008820 name);
8821 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008822 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008823 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008824 name);
8825 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008826 }
8827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (fname != name && fname != fname_buf)
8830 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008831 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 return ret;
8834}
8835
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836/*
8837 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008838 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008839 */
8840 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008841emsg_funcname(ermsg, name)
8842 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843 char_u *name;
8844{
8845 char_u *p;
8846
8847 if (*name == K_SPECIAL)
8848 p = concat_str((char_u *)"<SNR>", name + 3);
8849 else
8850 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008851 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008852 if (p != name)
8853 vim_free(p);
8854}
8855
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008856/*
8857 * Return TRUE for a non-zero Number and a non-empty String.
8858 */
8859 static int
8860non_zero_arg(argvars)
8861 typval_T *argvars;
8862{
8863 return ((argvars[0].v_type == VAR_NUMBER
8864 && argvars[0].vval.v_number != 0)
8865 || (argvars[0].v_type == VAR_STRING
8866 && argvars[0].vval.v_string != NULL
8867 && *argvars[0].vval.v_string != NUL));
8868}
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870/*********************************************
8871 * Implementation of the built-in functions
8872 */
8873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008875static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8876
8877/*
8878 * Get the float value of "argvars[0]" into "f".
8879 * Returns FAIL when the argument is not a Number or Float.
8880 */
8881 static int
8882get_float_arg(argvars, f)
8883 typval_T *argvars;
8884 float_T *f;
8885{
8886 if (argvars[0].v_type == VAR_FLOAT)
8887 {
8888 *f = argvars[0].vval.v_float;
8889 return OK;
8890 }
8891 if (argvars[0].v_type == VAR_NUMBER)
8892 {
8893 *f = (float_T)argvars[0].vval.v_number;
8894 return OK;
8895 }
8896 EMSG(_("E808: Number or Float required"));
8897 return FAIL;
8898}
8899
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008900/*
8901 * "abs(expr)" function
8902 */
8903 static void
8904f_abs(argvars, rettv)
8905 typval_T *argvars;
8906 typval_T *rettv;
8907{
8908 if (argvars[0].v_type == VAR_FLOAT)
8909 {
8910 rettv->v_type = VAR_FLOAT;
8911 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8912 }
8913 else
8914 {
8915 varnumber_T n;
8916 int error = FALSE;
8917
8918 n = get_tv_number_chk(&argvars[0], &error);
8919 if (error)
8920 rettv->vval.v_number = -1;
8921 else if (n > 0)
8922 rettv->vval.v_number = n;
8923 else
8924 rettv->vval.v_number = -n;
8925 }
8926}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008927
8928/*
8929 * "acos()" function
8930 */
8931 static void
8932f_acos(argvars, rettv)
8933 typval_T *argvars;
8934 typval_T *rettv;
8935{
8936 float_T f;
8937
8938 rettv->v_type = VAR_FLOAT;
8939 if (get_float_arg(argvars, &f) == OK)
8940 rettv->vval.v_float = acos(f);
8941 else
8942 rettv->vval.v_float = 0.0;
8943}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#endif
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008947 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 */
8949 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008950f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 typval_T *argvars;
8952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
Bram Moolenaar33570922005-01-25 22:26:29 +00008954 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008959 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008960 && !tv_check_lock(l->lv_lock,
8961 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008962 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 }
8965 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008966 EMSG(_(e_listreq));
8967}
8968
8969/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008970 * "and(expr, expr)" function
8971 */
8972 static void
8973f_and(argvars, rettv)
8974 typval_T *argvars;
8975 typval_T *rettv;
8976{
8977 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8978 & get_tv_number_chk(&argvars[1], NULL);
8979}
8980
8981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008982 * "append(lnum, string/list)" function
8983 */
8984 static void
8985f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *argvars;
8987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008988{
8989 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 list_T *l = NULL;
8992 listitem_T *li = NULL;
8993 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 long added = 0;
8995
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008996 /* When coming here from Insert mode, sync undo, so that this can be
8997 * undone separately from what was previously inserted. */
8998 if (u_sync_once == 2)
8999 {
9000 u_sync_once = 1; /* notify that u_sync() was called */
9001 u_sync(TRUE);
9002 }
9003
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 lnum = get_tv_lnum(argvars);
9005 if (lnum >= 0
9006 && lnum <= curbuf->b_ml.ml_line_count
9007 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009009 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011 l = argvars[1].vval.v_list;
9012 if (l == NULL)
9013 return;
9014 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 for (;;)
9017 {
9018 if (l == NULL)
9019 tv = &argvars[1]; /* append a string */
9020 else if (li == NULL)
9021 break; /* end of list */
9022 else
9023 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 line = get_tv_string_chk(tv);
9025 if (line == NULL) /* type error */
9026 {
9027 rettv->vval.v_number = 1; /* Failed */
9028 break;
9029 }
9030 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031 ++added;
9032 if (l == NULL)
9033 break;
9034 li = li->li_next;
9035 }
9036
9037 appended_lines_mark(lnum, added);
9038 if (curwin->w_cursor.lnum > lnum)
9039 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 else
9042 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043}
9044
9045/*
9046 * "argc()" function
9047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009050 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054}
9055
9056/*
9057 * "argidx()" function
9058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065}
9066
9067/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009068 * "arglistid()" function
9069 */
9070 static void
9071f_arglistid(argvars, rettv)
9072 typval_T *argvars UNUSED;
9073 typval_T *rettv;
9074{
9075 win_T *wp;
9076 tabpage_T *tp = NULL;
9077 long n;
9078
9079 rettv->vval.v_number = -1;
9080 if (argvars[0].v_type != VAR_UNKNOWN)
9081 {
9082 if (argvars[1].v_type != VAR_UNKNOWN)
9083 {
9084 n = get_tv_number(&argvars[1]);
9085 if (n >= 0)
9086 tp = find_tabpage(n);
9087 }
9088 else
9089 tp = curtab;
9090
9091 if (tp != NULL)
9092 {
9093 wp = find_win_by_nr(&argvars[0], tp);
9094 if (wp != NULL)
9095 rettv->vval.v_number = wp->w_alist->id;
9096 }
9097 }
9098 else
9099 rettv->vval.v_number = curwin->w_alist->id;
9100}
9101
9102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * "argv(nr)" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110 int idx;
9111
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009112 if (argvars[0].v_type != VAR_UNKNOWN)
9113 {
9114 idx = get_tv_number_chk(&argvars[0], NULL);
9115 if (idx >= 0 && idx < ARGCOUNT)
9116 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9117 else
9118 rettv->vval.v_string = NULL;
9119 rettv->v_type = VAR_STRING;
9120 }
9121 else if (rettv_list_alloc(rettv) == OK)
9122 for (idx = 0; idx < ARGCOUNT; ++idx)
9123 list_append_string(rettv->vval.v_list,
9124 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009127#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009128/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009129 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009130 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009131 static void
9132f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009133 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009134 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009135{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009136 float_T f;
9137
9138 rettv->v_type = VAR_FLOAT;
9139 if (get_float_arg(argvars, &f) == OK)
9140 rettv->vval.v_float = asin(f);
9141 else
9142 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009143}
9144
9145/*
9146 * "atan()" function
9147 */
9148 static void
9149f_atan(argvars, rettv)
9150 typval_T *argvars;
9151 typval_T *rettv;
9152{
9153 float_T f;
9154
9155 rettv->v_type = VAR_FLOAT;
9156 if (get_float_arg(argvars, &f) == OK)
9157 rettv->vval.v_float = atan(f);
9158 else
9159 rettv->vval.v_float = 0.0;
9160}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009161
9162/*
9163 * "atan2()" function
9164 */
9165 static void
9166f_atan2(argvars, rettv)
9167 typval_T *argvars;
9168 typval_T *rettv;
9169{
9170 float_T fx, fy;
9171
9172 rettv->v_type = VAR_FLOAT;
9173 if (get_float_arg(argvars, &fx) == OK
9174 && get_float_arg(&argvars[1], &fy) == OK)
9175 rettv->vval.v_float = atan2(fx, fy);
9176 else
9177 rettv->vval.v_float = 0.0;
9178}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009179#endif
9180
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181/*
9182 * "browse(save, title, initdir, default)" function
9183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189#ifdef FEAT_BROWSE
9190 int save;
9191 char_u *title;
9192 char_u *initdir;
9193 char_u *defname;
9194 char_u buf[NUMBUFLEN];
9195 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 save = get_tv_number_chk(&argvars[0], &error);
9199 title = get_tv_string_chk(&argvars[1]);
9200 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9201 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 if (error || title == NULL || initdir == NULL || defname == NULL)
9204 rettv->vval.v_string = NULL;
9205 else
9206 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009207 do_browse(save ? BROWSE_SAVE : 0,
9208 title, defname, NULL, initdir, NULL, curbuf);
9209#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009213}
9214
9215/*
9216 * "browsedir(title, initdir)" function
9217 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009220 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009221 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009222{
9223#ifdef FEAT_BROWSE
9224 char_u *title;
9225 char_u *initdir;
9226 char_u buf[NUMBUFLEN];
9227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 title = get_tv_string_chk(&argvars[0]);
9229 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 if (title == NULL || initdir == NULL)
9232 rettv->vval.v_string = NULL;
9233 else
9234 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009235 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241
Bram Moolenaar33570922005-01-25 22:26:29 +00009242static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009243
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244/*
9245 * Find a buffer by number or exact name.
9246 */
9247 static buf_T *
9248find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009249 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009253 if (avar->v_type == VAR_NUMBER)
9254 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009255 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009257 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009258 if (buf == NULL)
9259 {
9260 /* No full path name match, try a match with a URL or a "nofile"
9261 * buffer, these don't use the full path. */
9262 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9263 if (buf->b_fname != NULL
9264 && (path_with_url(buf->b_fname)
9265#ifdef FEAT_QUICKFIX
9266 || bt_nofile(buf)
9267#endif
9268 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009269 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009270 break;
9271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 }
9273 return buf;
9274}
9275
9276/*
9277 * "bufexists(expr)" function
9278 */
9279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 typval_T *argvars;
9282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285}
9286
9287/*
9288 * "buflisted(expr)" function
9289 */
9290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009292 typval_T *argvars;
9293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
9295 buf_T *buf;
9296
9297 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299}
9300
9301/*
9302 * "bufloaded(expr)" function
9303 */
9304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 typval_T *argvars;
9307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 buf_T *buf;
9310
9311 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
9314
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009315static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317/*
9318 * Get buffer by number or pattern.
9319 */
9320 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009321get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009322 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009323 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 int save_magic;
9327 char_u *save_cpo;
9328 buf_T *buf;
9329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 if (tv->v_type == VAR_NUMBER)
9331 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009332 if (tv->v_type != VAR_STRING)
9333 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 if (name == NULL || *name == NUL)
9335 return curbuf;
9336 if (name[0] == '$' && name[1] == NUL)
9337 return lastbuf;
9338
9339 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9340 save_magic = p_magic;
9341 p_magic = TRUE;
9342 save_cpo = p_cpo;
9343 p_cpo = (char_u *)"";
9344
9345 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009346 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 p_magic = save_magic;
9349 p_cpo = save_cpo;
9350
9351 /* If not found, try expanding the name, like done for bufexists(). */
9352 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 return buf;
9356}
9357
9358/*
9359 * "bufname(expr)" function
9360 */
9361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 buf_T *buf;
9367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009370 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 --emsg_off;
9377}
9378
9379/*
9380 * "bufnr(expr)" function
9381 */
9382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *argvars;
9385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009388 int error = FALSE;
9389 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009391 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009393 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009394 --emsg_off;
9395
9396 /* If the buffer isn't found and the second argument is not zero create a
9397 * new buffer. */
9398 if (buf == NULL
9399 && argvars[1].v_type != VAR_UNKNOWN
9400 && get_tv_number_chk(&argvars[1], &error) != 0
9401 && !error
9402 && (name = get_tv_string_chk(&argvars[0])) != NULL
9403 && !error)
9404 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410}
9411
9412/*
9413 * "bufwinnr(nr)" function
9414 */
9415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 typval_T *argvars;
9418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_WINDOWS
9421 win_T *wp;
9422 int winnr = 0;
9423#endif
9424 buf_T *buf;
9425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009426 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009428 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#ifdef FEAT_WINDOWS
9430 for (wp = firstwin; wp; wp = wp->w_next)
9431 {
9432 ++winnr;
9433 if (wp->w_buffer == buf)
9434 break;
9435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#endif
9440 --emsg_off;
9441}
9442
9443/*
9444 * "byte2line(byte)" function
9445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#else
9454 long boff = 0;
9455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 (linenr_T)0, &boff);
9462#endif
9463}
9464
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009465 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009466byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009469 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009470{
9471#ifdef FEAT_MBYTE
9472 char_u *t;
9473#endif
9474 char_u *str;
9475 long idx;
9476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 str = get_tv_string_chk(&argvars[0]);
9478 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009481 return;
9482
9483#ifdef FEAT_MBYTE
9484 t = str;
9485 for ( ; idx > 0; idx--)
9486 {
9487 if (*t == NUL) /* EOL reached */
9488 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009489 if (enc_utf8 && comp)
9490 t += utf_ptr2len(t);
9491 else
9492 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009493 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009494 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009495#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009498#endif
9499}
9500
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009501/*
9502 * "byteidx()" function
9503 */
9504 static void
9505f_byteidx(argvars, rettv)
9506 typval_T *argvars;
9507 typval_T *rettv;
9508{
9509 byteidx(argvars, rettv, FALSE);
9510}
9511
9512/*
9513 * "byteidxcomp()" function
9514 */
9515 static void
9516f_byteidxcomp(argvars, rettv)
9517 typval_T *argvars;
9518 typval_T *rettv;
9519{
9520 byteidx(argvars, rettv, TRUE);
9521}
9522
Bram Moolenaardb913952012-06-29 12:54:53 +02009523 int
9524func_call(name, args, selfdict, rettv)
9525 char_u *name;
9526 typval_T *args;
9527 dict_T *selfdict;
9528 typval_T *rettv;
9529{
9530 listitem_T *item;
9531 typval_T argv[MAX_FUNC_ARGS + 1];
9532 int argc = 0;
9533 int dummy;
9534 int r = 0;
9535
9536 for (item = args->vval.v_list->lv_first; item != NULL;
9537 item = item->li_next)
9538 {
9539 if (argc == MAX_FUNC_ARGS)
9540 {
9541 EMSG(_("E699: Too many arguments"));
9542 break;
9543 }
9544 /* Make a copy of each argument. This is needed to be able to set
9545 * v_lock to VAR_FIXED in the copy without changing the original list.
9546 */
9547 copy_tv(&item->li_tv, &argv[argc++]);
9548 }
9549
9550 if (item == NULL)
9551 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9552 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9553 &dummy, TRUE, selfdict);
9554
9555 /* Free the arguments. */
9556 while (argc > 0)
9557 clear_tv(&argv[--argc]);
9558
9559 return r;
9560}
9561
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009563 * "call(func, arglist)" function
9564 */
9565 static void
9566f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009569{
9570 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009571 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009572
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009573 if (argvars[1].v_type != VAR_LIST)
9574 {
9575 EMSG(_(e_listreq));
9576 return;
9577 }
9578 if (argvars[1].vval.v_list == NULL)
9579 return;
9580
9581 if (argvars[0].v_type == VAR_FUNC)
9582 func = argvars[0].vval.v_string;
9583 else
9584 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 if (*func == NUL)
9586 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009587
Bram Moolenaare9a41262005-01-15 22:18:47 +00009588 if (argvars[2].v_type != VAR_UNKNOWN)
9589 {
9590 if (argvars[2].v_type != VAR_DICT)
9591 {
9592 EMSG(_(e_dictreq));
9593 return;
9594 }
9595 selfdict = argvars[2].vval.v_dict;
9596 }
9597
Bram Moolenaardb913952012-06-29 12:54:53 +02009598 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599}
9600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009601#ifdef FEAT_FLOAT
9602/*
9603 * "ceil({float})" function
9604 */
9605 static void
9606f_ceil(argvars, rettv)
9607 typval_T *argvars;
9608 typval_T *rettv;
9609{
9610 float_T f;
9611
9612 rettv->v_type = VAR_FLOAT;
9613 if (get_float_arg(argvars, &f) == OK)
9614 rettv->vval.v_float = ceil(f);
9615 else
9616 rettv->vval.v_float = 0.0;
9617}
9618#endif
9619
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009620/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009621 * "changenr()" function
9622 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009623 static void
9624f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009625 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009626 typval_T *rettv;
9627{
9628 rettv->vval.v_number = curbuf->b_u_seq_cur;
9629}
9630
9631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 * "char2nr(string)" function
9633 */
9634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639#ifdef FEAT_MBYTE
9640 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009641 {
9642 int utf8 = 0;
9643
9644 if (argvars[1].v_type != VAR_UNKNOWN)
9645 utf8 = get_tv_number_chk(&argvars[1], NULL);
9646
9647 if (utf8)
9648 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9649 else
9650 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 else
9653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655}
9656
9657/*
9658 * "cindent(lnum)" function
9659 */
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665#ifdef FEAT_CINDENT
9666 pos_T pos;
9667 linenr_T lnum;
9668
9669 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9672 {
9673 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 curwin->w_cursor = pos;
9676 }
9677 else
9678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681
9682/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009683 * "clearmatches()" function
9684 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009685 static void
9686f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009687 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009688 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009689{
9690#ifdef FEAT_SEARCH_EXTRA
9691 clear_matches(curwin);
9692#endif
9693}
9694
9695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 * "col(string)" function
9697 */
9698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 typval_T *argvars;
9701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 colnr_T col = 0;
9704 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009705 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009707 fp = var2fpos(&argvars[0], FALSE, &fnum);
9708 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 {
9710 if (fp->col == MAXCOL)
9711 {
9712 /* '> can be MAXCOL, get the length of the line then */
9713 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009714 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
9716 col = MAXCOL;
9717 }
9718 else
9719 {
9720 col = fp->col + 1;
9721#ifdef FEAT_VIRTUALEDIT
9722 /* col(".") when the cursor is on the NUL at the end of the line
9723 * because of "coladd" can be seen as an extra column. */
9724 if (virtual_active() && fp == &curwin->w_cursor)
9725 {
9726 char_u *p = ml_get_cursor();
9727
9728 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9729 curwin->w_virtcol - curwin->w_cursor.coladd))
9730 {
9731# ifdef FEAT_MBYTE
9732 int l;
9733
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009734 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 col += l;
9736# else
9737 if (*p != NUL && p[1] == NUL)
9738 ++col;
9739# endif
9740 }
9741 }
9742#endif
9743 }
9744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747
Bram Moolenaar572cb562005-08-05 21:35:02 +00009748#if defined(FEAT_INS_EXPAND)
9749/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009750 * "complete()" function
9751 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009752 static void
9753f_complete(argvars, rettv)
9754 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009755 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009756{
9757 int startcol;
9758
9759 if ((State & INSERT) == 0)
9760 {
9761 EMSG(_("E785: complete() can only be used in Insert mode"));
9762 return;
9763 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009764
9765 /* Check for undo allowed here, because if something was already inserted
9766 * the line was already saved for undo and this check isn't done. */
9767 if (!undo_allowed())
9768 return;
9769
Bram Moolenaarade00832006-03-10 21:46:58 +00009770 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9771 {
9772 EMSG(_(e_invarg));
9773 return;
9774 }
9775
9776 startcol = get_tv_number_chk(&argvars[0], NULL);
9777 if (startcol <= 0)
9778 return;
9779
9780 set_completion(startcol - 1, argvars[1].vval.v_list);
9781}
9782
9783/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009784 * "complete_add()" function
9785 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009786 static void
9787f_complete_add(argvars, rettv)
9788 typval_T *argvars;
9789 typval_T *rettv;
9790{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009791 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009792}
9793
9794/*
9795 * "complete_check()" function
9796 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009797 static void
9798f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009799 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009800 typval_T *rettv;
9801{
9802 int saved = RedrawingDisabled;
9803
9804 RedrawingDisabled = 0;
9805 ins_compl_check_keys(0);
9806 rettv->vval.v_number = compl_interrupted;
9807 RedrawingDisabled = saved;
9808}
9809#endif
9810
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811/*
9812 * "confirm(message, buttons[, default [, type]])" function
9813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009816 typval_T *argvars UNUSED;
9817 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9820 char_u *message;
9821 char_u *buttons = NULL;
9822 char_u buf[NUMBUFLEN];
9823 char_u buf2[NUMBUFLEN];
9824 int def = 1;
9825 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 char_u *typestr;
9827 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 message = get_tv_string_chk(&argvars[0]);
9830 if (message == NULL)
9831 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9835 if (buttons == NULL)
9836 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009840 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9843 if (typestr == NULL)
9844 error = TRUE;
9845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 switch (TOUPPER_ASC(*typestr))
9848 {
9849 case 'E': type = VIM_ERROR; break;
9850 case 'Q': type = VIM_QUESTION; break;
9851 case 'I': type = VIM_INFO; break;
9852 case 'W': type = VIM_WARNING; break;
9853 case 'G': type = VIM_GENERIC; break;
9854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 }
9856 }
9857 }
9858 }
9859
9860 if (buttons == NULL || *buttons == NUL)
9861 buttons = (char_u *)_("&Ok");
9862
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009863 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009865 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#endif
9867}
9868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869/*
9870 * "copy()" function
9871 */
9872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009874 typval_T *argvars;
9875 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009876{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009877 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009880#ifdef FEAT_FLOAT
9881/*
9882 * "cos()" function
9883 */
9884 static void
9885f_cos(argvars, rettv)
9886 typval_T *argvars;
9887 typval_T *rettv;
9888{
9889 float_T f;
9890
9891 rettv->v_type = VAR_FLOAT;
9892 if (get_float_arg(argvars, &f) == OK)
9893 rettv->vval.v_float = cos(f);
9894 else
9895 rettv->vval.v_float = 0.0;
9896}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009897
9898/*
9899 * "cosh()" function
9900 */
9901 static void
9902f_cosh(argvars, rettv)
9903 typval_T *argvars;
9904 typval_T *rettv;
9905{
9906 float_T f;
9907
9908 rettv->v_type = VAR_FLOAT;
9909 if (get_float_arg(argvars, &f) == OK)
9910 rettv->vval.v_float = cosh(f);
9911 else
9912 rettv->vval.v_float = 0.0;
9913}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009914#endif
9915
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917 * "count()" function
9918 */
9919 static void
9920f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009921 typval_T *argvars;
9922 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009923{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009924 long n = 0;
9925 int ic = FALSE;
9926
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009928 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009929 listitem_T *li;
9930 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 if ((l = argvars[0].vval.v_list) != NULL)
9934 {
9935 li = l->lv_first;
9936 if (argvars[2].v_type != VAR_UNKNOWN)
9937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 int error = FALSE;
9939
9940 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009941 if (argvars[3].v_type != VAR_UNKNOWN)
9942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 idx = get_tv_number_chk(&argvars[3], &error);
9944 if (!error)
9945 {
9946 li = list_find(l, idx);
9947 if (li == NULL)
9948 EMSGN(_(e_listidx), idx);
9949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 if (error)
9952 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 }
9954
9955 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009956 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 ++n;
9958 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 else if (argvars[0].v_type == VAR_DICT)
9961 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 int todo;
9963 dict_T *d;
9964 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009965
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009966 if ((d = argvars[0].vval.v_dict) != NULL)
9967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968 int error = FALSE;
9969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 if (argvars[2].v_type != VAR_UNKNOWN)
9971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 if (argvars[3].v_type != VAR_UNKNOWN)
9974 EMSG(_(e_invarg));
9975 }
9976
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009977 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009978 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009979 {
9980 if (!HASHITEM_EMPTY(hi))
9981 {
9982 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009983 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009984 ++n;
9985 }
9986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 }
9988 }
9989 else
9990 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009991 rettv->vval.v_number = n;
9992}
9993
9994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9996 *
9997 * Checks the existence of a cscope connection.
9998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010001 typval_T *argvars UNUSED;
10002 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004#ifdef FEAT_CSCOPE
10005 int num = 0;
10006 char_u *dbpath = NULL;
10007 char_u *prepend = NULL;
10008 char_u buf[NUMBUFLEN];
10009
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010010 if (argvars[0].v_type != VAR_UNKNOWN
10011 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 num = (int)get_tv_number(&argvars[0]);
10014 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 }
10018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020#endif
10021}
10022
10023/*
10024 * "cursor(lnum, col)" function
10025 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010026 * Moves the cursor to the specified line and column.
10027 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010031 typval_T *argvars;
10032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033{
10034 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010035#ifdef FEAT_VIRTUALEDIT
10036 long coladd = 0;
10037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010039 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010040 if (argvars[1].v_type == VAR_UNKNOWN)
10041 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010042 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010043 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010044
Bram Moolenaar493c1782014-05-28 14:34:46 +020010045 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010046 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010047 line = pos.lnum;
10048 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010049#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010050 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010051#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010052 if (curswant >= 0)
10053 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010054 }
10055 else
10056 {
10057 line = get_tv_lnum(argvars);
10058 col = get_tv_number_chk(&argvars[1], NULL);
10059#ifdef FEAT_VIRTUALEDIT
10060 if (argvars[2].v_type != VAR_UNKNOWN)
10061 coladd = get_tv_number_chk(&argvars[2], NULL);
10062#endif
10063 }
10064 if (line < 0 || col < 0
10065#ifdef FEAT_VIRTUALEDIT
10066 || coladd < 0
10067#endif
10068 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (line > 0)
10071 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (col > 0)
10073 curwin->w_cursor.col = col - 1;
10074#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010075 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
10077
10078 /* Make sure the cursor is in a valid position. */
10079 check_cursor();
10080#ifdef FEAT_MBYTE
10081 /* Correct cursor for multi-byte character. */
10082 if (has_mbyte)
10083 mb_adjust_cursor();
10084#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010085
10086 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010087 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088}
10089
10090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 */
10093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010095 typval_T *argvars;
10096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010098 int noref = 0;
10099
10100 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010102 if (noref < 0 || noref > 1)
10103 EMSG(_(e_invarg));
10104 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010105 {
10106 current_copyID += COPYID_INC;
10107 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
10112 * "delete()" function
10113 */
10114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010116 typval_T *argvars;
10117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123}
10124
10125/*
10126 * "did_filetype()" function
10127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010130 typval_T *argvars UNUSED;
10131 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132{
10133#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135#endif
10136}
10137
10138/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010139 * "diff_filler()" function
10140 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010143 typval_T *argvars UNUSED;
10144 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010145{
10146#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010148#endif
10149}
10150
10151/*
10152 * "diff_hlID()" function
10153 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010156 typval_T *argvars UNUSED;
10157 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010158{
10159#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010161 static linenr_T prev_lnum = 0;
10162 static int changedtick = 0;
10163 static int fnum = 0;
10164 static int change_start = 0;
10165 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010166 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010167 int filler_lines;
10168 int col;
10169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 if (lnum < 0) /* ignore type error in {lnum} arg */
10171 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010172 if (lnum != prev_lnum
10173 || changedtick != curbuf->b_changedtick
10174 || fnum != curbuf->b_fnum)
10175 {
10176 /* New line, buffer, change: need to get the values. */
10177 filler_lines = diff_check(curwin, lnum);
10178 if (filler_lines < 0)
10179 {
10180 if (filler_lines == -1)
10181 {
10182 change_start = MAXCOL;
10183 change_end = -1;
10184 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10185 hlID = HLF_ADD; /* added line */
10186 else
10187 hlID = HLF_CHD; /* changed line */
10188 }
10189 else
10190 hlID = HLF_ADD; /* added line */
10191 }
10192 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010193 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010194 prev_lnum = lnum;
10195 changedtick = curbuf->b_changedtick;
10196 fnum = curbuf->b_fnum;
10197 }
10198
10199 if (hlID == HLF_CHD || hlID == HLF_TXD)
10200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010201 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010202 if (col >= change_start && col <= change_end)
10203 hlID = HLF_TXD; /* changed text */
10204 else
10205 hlID = HLF_CHD; /* changed line */
10206 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010207 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010208#endif
10209}
10210
10211/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010212 * "empty({expr})" function
10213 */
10214 static void
10215f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010216 typval_T *argvars;
10217 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010218{
10219 int n;
10220
10221 switch (argvars[0].v_type)
10222 {
10223 case VAR_STRING:
10224 case VAR_FUNC:
10225 n = argvars[0].vval.v_string == NULL
10226 || *argvars[0].vval.v_string == NUL;
10227 break;
10228 case VAR_NUMBER:
10229 n = argvars[0].vval.v_number == 0;
10230 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010231#ifdef FEAT_FLOAT
10232 case VAR_FLOAT:
10233 n = argvars[0].vval.v_float == 0.0;
10234 break;
10235#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010236 case VAR_LIST:
10237 n = argvars[0].vval.v_list == NULL
10238 || argvars[0].vval.v_list->lv_first == NULL;
10239 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 case VAR_DICT:
10241 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010244 default:
10245 EMSG2(_(e_intern2), "f_empty()");
10246 n = 0;
10247 }
10248
10249 rettv->vval.v_number = n;
10250}
10251
10252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 * "escape({string}, {chars})" function
10254 */
10255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010257 typval_T *argvars;
10258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 char_u buf[NUMBUFLEN];
10261
Bram Moolenaar758711c2005-02-02 23:11:38 +000010262 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10263 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265}
10266
10267/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010268 * "eval()" function
10269 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270 static void
10271f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010272 typval_T *argvars;
10273 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010274{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010275 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 s = get_tv_string_chk(&argvars[0]);
10278 if (s != NULL)
10279 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010280
Bram Moolenaar615b9972015-01-14 17:15:05 +010010281 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10283 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010284 if (p != NULL && !aborting())
10285 EMSG2(_(e_invexpr2), p);
10286 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010290 else if (*s != NUL)
10291 EMSG(_(e_trailing));
10292}
10293
10294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 * "eventhandler()" function
10296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303}
10304
10305/*
10306 * "executable()" function
10307 */
10308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 typval_T *argvars;
10311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010313 char_u *name = get_tv_string(&argvars[0]);
10314
10315 /* Check in $PATH and also check directly if there is a directory name. */
10316 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10317 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010318}
10319
10320/*
10321 * "exepath()" function
10322 */
10323 static void
10324f_exepath(argvars, rettv)
10325 typval_T *argvars;
10326 typval_T *rettv;
10327{
10328 char_u *p = NULL;
10329
Bram Moolenaarb5971142015-03-21 17:32:19 +010010330 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010331 rettv->v_type = VAR_STRING;
10332 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
10334
10335/*
10336 * "exists()" function
10337 */
10338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010340 typval_T *argvars;
10341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
10343 char_u *p;
10344 char_u *name;
10345 int n = FALSE;
10346 int len = 0;
10347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (*p == '$') /* environment variable */
10350 {
10351 /* first try "normal" environment variables (fast) */
10352 if (mch_getenv(p + 1) != NULL)
10353 n = TRUE;
10354 else
10355 {
10356 /* try expanding things like $VIM and ${HOME} */
10357 p = expand_env_save(p);
10358 if (p != NULL && *p != '$')
10359 n = TRUE;
10360 vim_free(p);
10361 }
10362 }
10363 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010366 if (*skipwhite(p) != NUL)
10367 n = FALSE; /* trailing garbage */
10368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 else if (*p == '*') /* internal or user defined function */
10370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373 else if (*p == ':')
10374 {
10375 n = cmd_exists(p + 1);
10376 }
10377 else if (*p == '#')
10378 {
10379#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010380 if (p[1] == '#')
10381 n = autocmd_supported(p + 2);
10382 else
10383 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384#endif
10385 }
10386 else /* internal variable */
10387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010388 char_u *tofree;
10389 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010391 /* get_name_len() takes care of expanding curly braces */
10392 name = p;
10393 len = get_name_len(&p, &tofree, TRUE, FALSE);
10394 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010396 if (tofree != NULL)
10397 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010398 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010399 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010401 /* handle d.key, l[idx], f(expr) */
10402 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10403 if (n)
10404 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 }
10406 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010407 if (*p != NUL)
10408 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010410 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 }
10412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414}
10415
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010416#ifdef FEAT_FLOAT
10417/*
10418 * "exp()" function
10419 */
10420 static void
10421f_exp(argvars, rettv)
10422 typval_T *argvars;
10423 typval_T *rettv;
10424{
10425 float_T f;
10426
10427 rettv->v_type = VAR_FLOAT;
10428 if (get_float_arg(argvars, &f) == OK)
10429 rettv->vval.v_float = exp(f);
10430 else
10431 rettv->vval.v_float = 0.0;
10432}
10433#endif
10434
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435/*
10436 * "expand()" function
10437 */
10438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010440 typval_T *argvars;
10441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 char_u *s;
10444 int len;
10445 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010446 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010449 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010452 if (argvars[1].v_type != VAR_UNKNOWN
10453 && argvars[2].v_type != VAR_UNKNOWN
10454 && get_tv_number_chk(&argvars[2], &error)
10455 && !error)
10456 {
10457 rettv->v_type = VAR_LIST;
10458 rettv->vval.v_list = NULL;
10459 }
10460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 if (*s == '%' || *s == '#' || *s == '<')
10463 {
10464 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010465 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010467 if (rettv->v_type == VAR_LIST)
10468 {
10469 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10470 list_append_string(rettv->vval.v_list, result, -1);
10471 else
10472 vim_free(result);
10473 }
10474 else
10475 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 }
10477 else
10478 {
10479 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010480 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 if (argvars[1].v_type != VAR_UNKNOWN
10482 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010483 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 if (!error)
10485 {
10486 ExpandInit(&xpc);
10487 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010488 if (p_wic)
10489 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010490 if (rettv->v_type == VAR_STRING)
10491 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10492 options, WILD_ALL);
10493 else if (rettv_list_alloc(rettv) != FAIL)
10494 {
10495 int i;
10496
10497 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10498 for (i = 0; i < xpc.xp_numfiles; i++)
10499 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10500 ExpandCleanup(&xpc);
10501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 }
10503 else
10504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 }
10506}
10507
10508/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010509 * Go over all entries in "d2" and add them to "d1".
10510 * When "action" is "error" then a duplicate key is an error.
10511 * When "action" is "force" then a duplicate key is overwritten.
10512 * Otherwise duplicate keys are ignored ("action" is "keep").
10513 */
10514 void
10515dict_extend(d1, d2, action)
10516 dict_T *d1;
10517 dict_T *d2;
10518 char_u *action;
10519{
10520 dictitem_T *di1;
10521 hashitem_T *hi2;
10522 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010523 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010524
10525 todo = (int)d2->dv_hashtab.ht_used;
10526 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10527 {
10528 if (!HASHITEM_EMPTY(hi2))
10529 {
10530 --todo;
10531 di1 = dict_find(d1, hi2->hi_key, -1);
10532 if (d1->dv_scope != 0)
10533 {
10534 /* Disallow replacing a builtin function in l: and g:.
10535 * Check the key to be valid when adding to any
10536 * scope. */
10537 if (d1->dv_scope == VAR_DEF_SCOPE
10538 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10539 && var_check_func_name(hi2->hi_key,
10540 di1 == NULL))
10541 break;
10542 if (!valid_varname(hi2->hi_key))
10543 break;
10544 }
10545 if (di1 == NULL)
10546 {
10547 di1 = dictitem_copy(HI2DI(hi2));
10548 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10549 dictitem_free(di1);
10550 }
10551 else if (*action == 'e')
10552 {
10553 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10554 break;
10555 }
10556 else if (*action == 'f' && HI2DI(hi2) != di1)
10557 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010558 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10559 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010560 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010561 clear_tv(&di1->di_tv);
10562 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10563 }
10564 }
10565 }
10566}
10567
10568/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010569 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010571 */
10572 static void
10573f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T *argvars;
10575 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010576{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010577 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010578
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010580 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010581 list_T *l1, *l2;
10582 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010584 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010585
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 l1 = argvars[0].vval.v_list;
10587 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010588 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010589 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 {
10591 if (argvars[2].v_type != VAR_UNKNOWN)
10592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 before = get_tv_number_chk(&argvars[2], &error);
10594 if (error)
10595 return; /* type error; errmsg already given */
10596
Bram Moolenaar758711c2005-02-02 23:11:38 +000010597 if (before == l1->lv_len)
10598 item = NULL;
10599 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010601 item = list_find(l1, before);
10602 if (item == NULL)
10603 {
10604 EMSGN(_(e_listidx), before);
10605 return;
10606 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 }
10608 }
10609 else
10610 item = NULL;
10611 list_extend(l1, l2, item);
10612
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 copy_tv(&argvars[0], rettv);
10614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10617 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010618 dict_T *d1, *d2;
10619 char_u *action;
10620 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010621
10622 d1 = argvars[0].vval.v_dict;
10623 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010624 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010625 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010626 {
10627 /* Check the third argument. */
10628 if (argvars[2].v_type != VAR_UNKNOWN)
10629 {
10630 static char *(av[]) = {"keep", "force", "error"};
10631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 action = get_tv_string_chk(&argvars[2]);
10633 if (action == NULL)
10634 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010635 for (i = 0; i < 3; ++i)
10636 if (STRCMP(action, av[i]) == 0)
10637 break;
10638 if (i == 3)
10639 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010640 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641 return;
10642 }
10643 }
10644 else
10645 action = (char_u *)"force";
10646
Bram Moolenaara9922d62013-05-30 13:01:18 +020010647 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010648
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 copy_tv(&argvars[0], rettv);
10650 }
10651 }
10652 else
10653 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010654}
10655
10656/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010657 * "feedkeys()" function
10658 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010659 static void
10660f_feedkeys(argvars, rettv)
10661 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010662 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663{
10664 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010665 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010666 char_u *keys, *flags;
10667 char_u nbuf[NUMBUFLEN];
10668 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010669 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010670
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010671 /* This is not allowed in the sandbox. If the commands would still be
10672 * executed in the sandbox it would be OK, but it probably happens later,
10673 * when "sandbox" is no longer set. */
10674 if (check_secure())
10675 return;
10676
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010677 keys = get_tv_string(&argvars[0]);
10678 if (*keys != NUL)
10679 {
10680 if (argvars[1].v_type != VAR_UNKNOWN)
10681 {
10682 flags = get_tv_string_buf(&argvars[1], nbuf);
10683 for ( ; *flags != NUL; ++flags)
10684 {
10685 switch (*flags)
10686 {
10687 case 'n': remap = FALSE; break;
10688 case 'm': remap = TRUE; break;
10689 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010690 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010691 }
10692 }
10693 }
10694
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010695 /* Need to escape K_SPECIAL and CSI before putting the string in the
10696 * typeahead buffer. */
10697 keys_esc = vim_strsave_escape_csi(keys);
10698 if (keys_esc != NULL)
10699 {
10700 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010701 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010702 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010703 if (vgetc_busy)
10704 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010705 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010706 }
10707}
10708
10709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 * "filereadable()" function
10711 */
10712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010717 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 char_u *p;
10719 int n;
10720
Bram Moolenaarc236c162008-07-13 17:41:49 +000010721#ifndef O_NONBLOCK
10722# define O_NONBLOCK 0
10723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010725 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10726 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 {
10728 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010729 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 }
10731 else
10732 n = FALSE;
10733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735}
10736
10737/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010738 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 * rights to write into.
10740 */
10741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010743 typval_T *argvars;
10744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010746 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010749static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010750
10751 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010752findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010754 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010755 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010756{
10757#ifdef FEAT_SEARCHPATH
10758 char_u *fname;
10759 char_u *fresult = NULL;
10760 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10761 char_u *p;
10762 char_u pathbuf[NUMBUFLEN];
10763 int count = 1;
10764 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010765 int error = FALSE;
10766#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010767
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010768 rettv->vval.v_string = NULL;
10769 rettv->v_type = VAR_STRING;
10770
10771#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10777 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010778 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 else
10780 {
10781 if (*p != NUL)
10782 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010785 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010786 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010787 }
10788
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010789 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10790 error = TRUE;
10791
10792 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794 do
10795 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010796 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010797 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 fresult = find_file_in_path_option(first ? fname : NULL,
10799 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010800 0, first, path,
10801 find_what,
10802 curbuf->b_ffname,
10803 find_what == FINDFILE_DIR
10804 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010806
10807 if (fresult != NULL && rettv->v_type == VAR_LIST)
10808 list_append_string(rettv->vval.v_list, fresult, -1);
10809
10810 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010812
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010813 if (rettv->v_type == VAR_STRING)
10814 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010815#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010816}
10817
Bram Moolenaar33570922005-01-25 22:26:29 +000010818static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10819static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010820
10821/*
10822 * Implementation of map() and filter().
10823 */
10824 static void
10825filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *argvars;
10827 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010828 int map;
10829{
10830 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 listitem_T *li, *nli;
10833 list_T *l = NULL;
10834 dictitem_T *di;
10835 hashtab_T *ht;
10836 hashitem_T *hi;
10837 dict_T *d = NULL;
10838 typval_T save_val;
10839 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010841 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010842 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010843 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010844 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010845 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010846 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010847
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010849 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010850 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010851 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 return;
10853 }
10854 else if (argvars[0].v_type == VAR_DICT)
10855 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010856 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010857 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 return;
10859 }
10860 else
10861 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010862 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010863 return;
10864 }
10865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 expr = get_tv_string_buf_chk(&argvars[1], buf);
10867 /* On type errors, the preceding call has already displayed an error
10868 * message. Avoid a misleading error message for an empty string that
10869 * was not passed as argument. */
10870 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 prepare_vimvar(VV_VAL, &save_val);
10873 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010874
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010875 /* We reset "did_emsg" to be able to detect whether an error
10876 * occurred during evaluation of the expression. */
10877 save_did_emsg = did_emsg;
10878 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010879
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010880 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 vimvars[VV_KEY].vv_type = VAR_STRING;
10884
10885 ht = &d->dv_hashtab;
10886 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010887 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010888 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 if (!HASHITEM_EMPTY(hi))
10891 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010892 int r;
10893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 --todo;
10895 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010896 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010897 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10898 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 break;
10900 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010901 r = filter_map_one(&di->di_tv, expr, map, &rem);
10902 clear_tv(&vimvars[VV_KEY].vv_tv);
10903 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 break;
10905 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010906 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010907 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10908 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010909 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 }
10913 }
10914 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 }
10916 else
10917 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010918 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 for (li = l->lv_first; li != NULL; li = nli)
10921 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010922 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010923 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010925 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010926 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010927 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010928 break;
10929 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010931 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010932 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010934
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010935 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010937
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010938 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940
10941 copy_tv(&argvars[0], rettv);
10942}
10943
10944 static int
10945filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 char_u *expr;
10948 int map;
10949 int *remp;
10950{
Bram Moolenaar33570922005-01-25 22:26:29 +000010951 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010953 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 s = expr;
10957 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010958 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959 if (*s != NUL) /* check for trailing chars after expr */
10960 {
10961 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010962 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010963 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010964 }
10965 if (map)
10966 {
10967 /* map(): replace the list item value */
10968 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010969 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 *tv = rettv;
10971 }
10972 else
10973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 int error = FALSE;
10975
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 /* On type error, nothing has been removed; return FAIL to stop the
10980 * loop. The error message was given by get_tv_number_chk(). */
10981 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010982 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010983 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010984 retval = OK;
10985theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010987 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010988}
10989
10990/*
10991 * "filter()" function
10992 */
10993 static void
10994f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010995 typval_T *argvars;
10996 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010997{
10998 filter_map(argvars, rettv, FALSE);
10999}
11000
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011002 * "finddir({fname}[, {path}[, {count}]])" function
11003 */
11004 static void
11005f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 typval_T *argvars;
11007 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011009 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011010}
11011
11012/*
11013 * "findfile({fname}[, {path}[, {count}]])" function
11014 */
11015 static void
11016f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011017 typval_T *argvars;
11018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011020 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011021}
11022
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011023#ifdef FEAT_FLOAT
11024/*
11025 * "float2nr({float})" function
11026 */
11027 static void
11028f_float2nr(argvars, rettv)
11029 typval_T *argvars;
11030 typval_T *rettv;
11031{
11032 float_T f;
11033
11034 if (get_float_arg(argvars, &f) == OK)
11035 {
11036 if (f < -0x7fffffff)
11037 rettv->vval.v_number = -0x7fffffff;
11038 else if (f > 0x7fffffff)
11039 rettv->vval.v_number = 0x7fffffff;
11040 else
11041 rettv->vval.v_number = (varnumber_T)f;
11042 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011043}
11044
11045/*
11046 * "floor({float})" function
11047 */
11048 static void
11049f_floor(argvars, rettv)
11050 typval_T *argvars;
11051 typval_T *rettv;
11052{
11053 float_T f;
11054
11055 rettv->v_type = VAR_FLOAT;
11056 if (get_float_arg(argvars, &f) == OK)
11057 rettv->vval.v_float = floor(f);
11058 else
11059 rettv->vval.v_float = 0.0;
11060}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011061
11062/*
11063 * "fmod()" function
11064 */
11065 static void
11066f_fmod(argvars, rettv)
11067 typval_T *argvars;
11068 typval_T *rettv;
11069{
11070 float_T fx, fy;
11071
11072 rettv->v_type = VAR_FLOAT;
11073 if (get_float_arg(argvars, &fx) == OK
11074 && get_float_arg(&argvars[1], &fy) == OK)
11075 rettv->vval.v_float = fmod(fx, fy);
11076 else
11077 rettv->vval.v_float = 0.0;
11078}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011079#endif
11080
Bram Moolenaar0d660222005-01-07 21:51:51 +000011081/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011082 * "fnameescape({string})" function
11083 */
11084 static void
11085f_fnameescape(argvars, rettv)
11086 typval_T *argvars;
11087 typval_T *rettv;
11088{
11089 rettv->vval.v_string = vim_strsave_fnameescape(
11090 get_tv_string(&argvars[0]), FALSE);
11091 rettv->v_type = VAR_STRING;
11092}
11093
11094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 * "fnamemodify({fname}, {mods})" function
11096 */
11097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101{
11102 char_u *fname;
11103 char_u *mods;
11104 int usedlen = 0;
11105 int len;
11106 char_u *fbuf = NULL;
11107 char_u buf[NUMBUFLEN];
11108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 fname = get_tv_string_chk(&argvars[0]);
11110 mods = get_tv_string_buf_chk(&argvars[1], buf);
11111 if (fname == NULL || mods == NULL)
11112 fname = NULL;
11113 else
11114 {
11115 len = (int)STRLEN(fname);
11116 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 vim_free(fbuf);
11125}
11126
Bram Moolenaar33570922005-01-25 22:26:29 +000011127static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
11129/*
11130 * "foldclosed()" function
11131 */
11132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011136 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138#ifdef FEAT_FOLDING
11139 linenr_T lnum;
11140 linenr_T first, last;
11141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11144 {
11145 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11146 {
11147 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 return;
11152 }
11153 }
11154#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156}
11157
11158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 * "foldclosed()" function
11160 */
11161 static void
11162f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165{
11166 foldclosed_both(argvars, rettv, FALSE);
11167}
11168
11169/*
11170 * "foldclosedend()" function
11171 */
11172 static void
11173f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011174 typval_T *argvars;
11175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176{
11177 foldclosed_both(argvars, rettv, TRUE);
11178}
11179
11180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 * "foldlevel()" function
11182 */
11183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011185 typval_T *argvars UNUSED;
11186 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187{
11188#ifdef FEAT_FOLDING
11189 linenr_T lnum;
11190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195}
11196
11197/*
11198 * "foldtext()" function
11199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011202 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
11205#ifdef FEAT_FOLDING
11206 linenr_T lnum;
11207 char_u *s;
11208 char_u *r;
11209 int len;
11210 char *txt;
11211#endif
11212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011216 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11217 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11218 <= curbuf->b_ml.ml_line_count
11219 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 {
11221 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11223 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 {
11225 if (!linewhite(lnum))
11226 break;
11227 ++lnum;
11228 }
11229
11230 /* Find interesting text in this line. */
11231 s = skipwhite(ml_get(lnum));
11232 /* skip C comment-start */
11233 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011236 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011238 {
11239 s = skipwhite(ml_get(lnum + 1));
11240 if (*s == '*')
11241 s = skipwhite(s + 1);
11242 }
11243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 txt = _("+-%s%3ld lines: ");
11245 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 + 20 /* for %3ld */
11248 + STRLEN(s))); /* concatenated */
11249 if (r != NULL)
11250 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011251 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11252 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11253 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 len = (int)STRLEN(r);
11255 STRCAT(r, s);
11256 /* remove 'foldmarker' and 'commentstring' */
11257 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 }
11260 }
11261#endif
11262}
11263
11264/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011265 * "foldtextresult(lnum)" function
11266 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011271{
11272#ifdef FEAT_FOLDING
11273 linenr_T lnum;
11274 char_u *text;
11275 char_u buf[51];
11276 foldinfo_T foldinfo;
11277 int fold_count;
11278#endif
11279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->v_type = VAR_STRING;
11281 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011282#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 /* treat illegal types and illegal string values for {lnum} the same */
11285 if (lnum < 0)
11286 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011287 fold_count = foldedCount(curwin, lnum, &foldinfo);
11288 if (fold_count > 0)
11289 {
11290 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11291 &foldinfo, buf);
11292 if (text == buf)
11293 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011295 }
11296#endif
11297}
11298
11299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 * "foreground()" function
11301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011304 typval_T *argvars UNUSED;
11305 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307#ifdef FEAT_GUI
11308 if (gui.in_use)
11309 gui_mch_set_foreground();
11310#else
11311# ifdef WIN32
11312 win32_set_foreground();
11313# endif
11314#endif
11315}
11316
11317/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011318 * "function()" function
11319 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011324{
11325 char_u *s;
11326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011329 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011330 /* Don't check an autoload name for existence here. */
11331 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011332 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011333 else
11334 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011335 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011336 {
11337 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011338 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011339
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011340 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11341 * also be called from another script. Using trans_function_name()
11342 * would also work, but some plugins depend on the name being
11343 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011345 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011346 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011347 if (rettv->vval.v_string != NULL)
11348 {
11349 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011350 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011351 }
11352 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011353 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011354 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011356 }
11357}
11358
11359/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011360 * "garbagecollect()" function
11361 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011362 static void
11363f_garbagecollect(argvars, rettv)
11364 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011365 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011366{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011367 /* This is postponed until we are back at the toplevel, because we may be
11368 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11369 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011370
11371 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11372 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011373}
11374
11375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376 * "get()" function
11377 */
11378 static void
11379f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382{
Bram Moolenaar33570922005-01-25 22:26:29 +000011383 listitem_T *li;
11384 list_T *l;
11385 dictitem_T *di;
11386 dict_T *d;
11387 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011390 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011391 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 int error = FALSE;
11394
11395 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11396 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011397 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011398 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011400 else if (argvars[0].v_type == VAR_DICT)
11401 {
11402 if ((d = argvars[0].vval.v_dict) != NULL)
11403 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011404 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011405 if (di != NULL)
11406 tv = &di->di_tv;
11407 }
11408 }
11409 else
11410 EMSG2(_(e_listdictarg), "get()");
11411
11412 if (tv == NULL)
11413 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011414 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011415 copy_tv(&argvars[2], rettv);
11416 }
11417 else
11418 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419}
11420
Bram Moolenaar342337a2005-07-21 21:11:17 +000011421static 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 +000011422
11423/*
11424 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011425 * Return a range (from start to end) of lines in rettv from the specified
11426 * buffer.
11427 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428 */
11429 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011430get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011431 buf_T *buf;
11432 linenr_T start;
11433 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011434 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011435 typval_T *rettv;
11436{
11437 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011438
Bram Moolenaar959a1432013-12-14 12:17:38 +010011439 rettv->v_type = VAR_STRING;
11440 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011441 if (retlist && rettv_list_alloc(rettv) == FAIL)
11442 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011443
11444 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11445 return;
11446
11447 if (!retlist)
11448 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011449 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11450 p = ml_get_buf(buf, start, FALSE);
11451 else
11452 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011453 rettv->vval.v_string = vim_strsave(p);
11454 }
11455 else
11456 {
11457 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011458 return;
11459
11460 if (start < 1)
11461 start = 1;
11462 if (end > buf->b_ml.ml_line_count)
11463 end = buf->b_ml.ml_line_count;
11464 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011465 if (list_append_string(rettv->vval.v_list,
11466 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011467 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011468 }
11469}
11470
11471/*
11472 * "getbufline()" function
11473 */
11474 static void
11475f_getbufline(argvars, rettv)
11476 typval_T *argvars;
11477 typval_T *rettv;
11478{
11479 linenr_T lnum;
11480 linenr_T end;
11481 buf_T *buf;
11482
11483 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11484 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011485 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011486 --emsg_off;
11487
Bram Moolenaar661b1822005-07-28 22:36:45 +000011488 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011489 if (argvars[2].v_type == VAR_UNKNOWN)
11490 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011491 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011492 end = get_tv_lnum_buf(&argvars[2], buf);
11493
Bram Moolenaar342337a2005-07-21 21:11:17 +000011494 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011495}
11496
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497/*
11498 * "getbufvar()" function
11499 */
11500 static void
11501f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011504{
11505 buf_T *buf;
11506 buf_T *save_curbuf;
11507 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011509 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11512 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011514 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011515
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011516 rettv->v_type = VAR_STRING;
11517 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011518
11519 if (buf != NULL && varname != NULL)
11520 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011521 /* set curbuf to be our buf, temporarily */
11522 save_curbuf = curbuf;
11523 curbuf = buf;
11524
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011526 {
11527 if (get_option_tv(&varname, rettv, TRUE) == OK)
11528 done = TRUE;
11529 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011530 else if (STRCMP(varname, "changedtick") == 0)
11531 {
11532 rettv->v_type = VAR_NUMBER;
11533 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011534 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011536 else
11537 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011538 /* Look up the variable. */
11539 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11540 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11541 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011545 done = TRUE;
11546 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011548
11549 /* restore previous notion of curbuf */
11550 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 }
11552
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011553 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11554 /* use the default value */
11555 copy_tv(&argvars[2], rettv);
11556
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557 --emsg_off;
11558}
11559
11560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 * "getchar()" function
11562 */
11563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 typval_T *argvars;
11566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567{
11568 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011571 /* Position the cursor. Needed after a message that ends in a space. */
11572 windgoto(msg_row, msg_col);
11573
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 ++no_mapping;
11575 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011576 for (;;)
11577 {
11578 if (argvars[0].v_type == VAR_UNKNOWN)
11579 /* getchar(): blocking wait. */
11580 n = safe_vgetc();
11581 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11582 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011583 n = vpeekc_any();
11584 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011585 /* illegal argument or getchar(0) and no char avail: return zero */
11586 n = 0;
11587 else
11588 /* getchar(0) and char avail: return char */
11589 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011590
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011591 if (n == K_IGNORE)
11592 continue;
11593 break;
11594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 --no_mapping;
11596 --allow_keys;
11597
Bram Moolenaar219b8702006-11-01 14:32:36 +000011598 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11599 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11600 vimvars[VV_MOUSE_COL].vv_nr = 0;
11601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 if (IS_SPECIAL(n) || mod_mask != 0)
11604 {
11605 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11606 int i = 0;
11607
11608 /* Turn a special key into three bytes, plus modifier. */
11609 if (mod_mask != 0)
11610 {
11611 temp[i++] = K_SPECIAL;
11612 temp[i++] = KS_MODIFIER;
11613 temp[i++] = mod_mask;
11614 }
11615 if (IS_SPECIAL(n))
11616 {
11617 temp[i++] = K_SPECIAL;
11618 temp[i++] = K_SECOND(n);
11619 temp[i++] = K_THIRD(n);
11620 }
11621#ifdef FEAT_MBYTE
11622 else if (has_mbyte)
11623 i += (*mb_char2bytes)(n, temp + i);
11624#endif
11625 else
11626 temp[i++] = n;
11627 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->v_type = VAR_STRING;
11629 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011630
11631#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011632 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011633 {
11634 int row = mouse_row;
11635 int col = mouse_col;
11636 win_T *win;
11637 linenr_T lnum;
11638# ifdef FEAT_WINDOWS
11639 win_T *wp;
11640# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011641 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011642
11643 if (row >= 0 && col >= 0)
11644 {
11645 /* Find the window at the mouse coordinates and compute the
11646 * text position. */
11647 win = mouse_find_win(&row, &col);
11648 (void)mouse_comp_pos(win, &row, &col, &lnum);
11649# ifdef FEAT_WINDOWS
11650 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011651 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011652# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011653 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011654 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11655 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11656 }
11657 }
11658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 }
11660}
11661
11662/*
11663 * "getcharmod()" function
11664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671}
11672
11673/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011674 * "getcharsearch()" function
11675 */
11676 static void
11677f_getcharsearch(argvars, rettv)
11678 typval_T *argvars UNUSED;
11679 typval_T *rettv;
11680{
11681 if (rettv_dict_alloc(rettv) != FAIL)
11682 {
11683 dict_T *dict = rettv->vval.v_dict;
11684
11685 dict_add_nr_str(dict, "char", 0L, last_csearch());
11686 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11687 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11688 }
11689}
11690
11691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 * "getcmdline()" function
11693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011696 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->v_type = VAR_STRING;
11700 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701}
11702
11703/*
11704 * "getcmdpos()" function
11705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011708 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712}
11713
11714/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011715 * "getcmdtype()" function
11716 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011717 static void
11718f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011719 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011720 typval_T *rettv;
11721{
11722 rettv->v_type = VAR_STRING;
11723 rettv->vval.v_string = alloc(2);
11724 if (rettv->vval.v_string != NULL)
11725 {
11726 rettv->vval.v_string[0] = get_cmdline_type();
11727 rettv->vval.v_string[1] = NUL;
11728 }
11729}
11730
11731/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011732 * "getcmdwintype()" function
11733 */
11734 static void
11735f_getcmdwintype(argvars, rettv)
11736 typval_T *argvars UNUSED;
11737 typval_T *rettv;
11738{
11739 rettv->v_type = VAR_STRING;
11740 rettv->vval.v_string = NULL;
11741#ifdef FEAT_CMDWIN
11742 rettv->vval.v_string = alloc(2);
11743 if (rettv->vval.v_string != NULL)
11744 {
11745 rettv->vval.v_string[0] = cmdwin_type;
11746 rettv->vval.v_string[1] = NUL;
11747 }
11748#endif
11749}
11750
11751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 * "getcwd()" function
11753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011759 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011762 rettv->vval.v_string = NULL;
11763 cwd = alloc(MAXPATHL);
11764 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011766 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11767 {
11768 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011770 if (rettv->vval.v_string != NULL)
11771 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011773 }
11774 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 }
11776}
11777
11778/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011779 * "getfontname()" function
11780 */
11781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->v_type = VAR_STRING;
11787 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011788#ifdef FEAT_GUI
11789 if (gui.in_use)
11790 {
11791 GuiFont font;
11792 char_u *name = NULL;
11793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011794 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011795 {
11796 /* Get the "Normal" font. Either the name saved by
11797 * hl_set_font_name() or from the font ID. */
11798 font = gui.norm_font;
11799 name = hl_get_font_name();
11800 }
11801 else
11802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011804 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11805 return;
11806 font = gui_mch_get_font(name, FALSE);
11807 if (font == NOFONT)
11808 return; /* Invalid font name, return empty string. */
11809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011812 gui_mch_free_font(font);
11813 }
11814#endif
11815}
11816
11817/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011818 * "getfperm({fname})" function
11819 */
11820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011822 typval_T *argvars;
11823 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011824{
11825 char_u *fname;
11826 struct stat st;
11827 char_u *perm = NULL;
11828 char_u flags[] = "rwx";
11829 int i;
11830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011834 if (mch_stat((char *)fname, &st) >= 0)
11835 {
11836 perm = vim_strsave((char_u *)"---------");
11837 if (perm != NULL)
11838 {
11839 for (i = 0; i < 9; i++)
11840 {
11841 if (st.st_mode & (1 << (8 - i)))
11842 perm[i] = flags[i % 3];
11843 }
11844 }
11845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011847}
11848
11849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * "getfsize({fname})" function
11851 */
11852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011854 typval_T *argvars;
11855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
11857 char_u *fname;
11858 struct stat st;
11859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863
11864 if (mch_stat((char *)fname, &st) >= 0)
11865 {
11866 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011871
11872 /* non-perfect check for overflow */
11873 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11874 rettv->vval.v_number = -2;
11875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 }
11877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879}
11880
11881/*
11882 * "getftime({fname})" function
11883 */
11884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 typval_T *argvars;
11887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888{
11889 char_u *fname;
11890 struct stat st;
11891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893
11894 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898}
11899
11900/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011901 * "getftype({fname})" function
11902 */
11903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011907{
11908 char_u *fname;
11909 struct stat st;
11910 char_u *type = NULL;
11911 char *t;
11912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011916 if (mch_lstat((char *)fname, &st) >= 0)
11917 {
11918#ifdef S_ISREG
11919 if (S_ISREG(st.st_mode))
11920 t = "file";
11921 else if (S_ISDIR(st.st_mode))
11922 t = "dir";
11923# ifdef S_ISLNK
11924 else if (S_ISLNK(st.st_mode))
11925 t = "link";
11926# endif
11927# ifdef S_ISBLK
11928 else if (S_ISBLK(st.st_mode))
11929 t = "bdev";
11930# endif
11931# ifdef S_ISCHR
11932 else if (S_ISCHR(st.st_mode))
11933 t = "cdev";
11934# endif
11935# ifdef S_ISFIFO
11936 else if (S_ISFIFO(st.st_mode))
11937 t = "fifo";
11938# endif
11939# ifdef S_ISSOCK
11940 else if (S_ISSOCK(st.st_mode))
11941 t = "fifo";
11942# endif
11943 else
11944 t = "other";
11945#else
11946# ifdef S_IFMT
11947 switch (st.st_mode & S_IFMT)
11948 {
11949 case S_IFREG: t = "file"; break;
11950 case S_IFDIR: t = "dir"; break;
11951# ifdef S_IFLNK
11952 case S_IFLNK: t = "link"; break;
11953# endif
11954# ifdef S_IFBLK
11955 case S_IFBLK: t = "bdev"; break;
11956# endif
11957# ifdef S_IFCHR
11958 case S_IFCHR: t = "cdev"; break;
11959# endif
11960# ifdef S_IFIFO
11961 case S_IFIFO: t = "fifo"; break;
11962# endif
11963# ifdef S_IFSOCK
11964 case S_IFSOCK: t = "socket"; break;
11965# endif
11966 default: t = "other";
11967 }
11968# else
11969 if (mch_isdir(fname))
11970 t = "dir";
11971 else
11972 t = "file";
11973# endif
11974#endif
11975 type = vim_strsave((char_u *)t);
11976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011978}
11979
11980/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011981 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011982 */
11983 static void
11984f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 typval_T *argvars;
11986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011987{
11988 linenr_T lnum;
11989 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011990 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011991
11992 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011993 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011994 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011995 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011996 retlist = FALSE;
11997 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011998 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011999 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012000 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012001 retlist = TRUE;
12002 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012003
Bram Moolenaar342337a2005-07-21 21:11:17 +000012004 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005}
12006
12007/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012008 * "getmatches()" function
12009 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012010 static void
12011f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012012 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012013 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012014{
12015#ifdef FEAT_SEARCH_EXTRA
12016 dict_T *dict;
12017 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012018 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012019
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012020 if (rettv_list_alloc(rettv) == OK)
12021 {
12022 while (cur != NULL)
12023 {
12024 dict = dict_alloc();
12025 if (dict == NULL)
12026 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012027 if (cur->match.regprog == NULL)
12028 {
12029 /* match added with matchaddpos() */
12030 for (i = 0; i < MAXPOSMATCH; ++i)
12031 {
12032 llpos_T *llpos;
12033 char buf[6];
12034 list_T *l;
12035
12036 llpos = &cur->pos.pos[i];
12037 if (llpos->lnum == 0)
12038 break;
12039 l = list_alloc();
12040 if (l == NULL)
12041 break;
12042 list_append_number(l, (varnumber_T)llpos->lnum);
12043 if (llpos->col > 0)
12044 {
12045 list_append_number(l, (varnumber_T)llpos->col);
12046 list_append_number(l, (varnumber_T)llpos->len);
12047 }
12048 sprintf(buf, "pos%d", i + 1);
12049 dict_add_list(dict, buf, l);
12050 }
12051 }
12052 else
12053 {
12054 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12055 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012056 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012057 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12058 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012059# ifdef FEAT_CONCEAL
12060 if (cur->conceal_char)
12061 {
12062 char_u buf[MB_MAXBYTES + 1];
12063
12064 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12065 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12066 }
12067# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012068 list_append_dict(rettv->vval.v_list, dict);
12069 cur = cur->next;
12070 }
12071 }
12072#endif
12073}
12074
12075/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012076 * "getpid()" function
12077 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012078 static void
12079f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012080 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012081 typval_T *rettv;
12082{
12083 rettv->vval.v_number = mch_get_pid();
12084}
12085
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012086static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12087
12088/*
12089 * "getcurpos()" function
12090 */
12091 static void
12092f_getcurpos(argvars, rettv)
12093 typval_T *argvars;
12094 typval_T *rettv;
12095{
12096 getpos_both(argvars, rettv, TRUE);
12097}
12098
Bram Moolenaar18081e32008-02-20 19:11:07 +000012099/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012100 * "getpos(string)" function
12101 */
12102 static void
12103f_getpos(argvars, rettv)
12104 typval_T *argvars;
12105 typval_T *rettv;
12106{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012107 getpos_both(argvars, rettv, FALSE);
12108}
12109
12110 static void
12111getpos_both(argvars, rettv, getcurpos)
12112 typval_T *argvars;
12113 typval_T *rettv;
12114 int getcurpos;
12115{
Bram Moolenaara5525202006-03-02 22:52:09 +000012116 pos_T *fp;
12117 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012118 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012119
12120 if (rettv_list_alloc(rettv) == OK)
12121 {
12122 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012123 if (getcurpos)
12124 fp = &curwin->w_cursor;
12125 else
12126 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012127 if (fnum != -1)
12128 list_append_number(l, (varnumber_T)fnum);
12129 else
12130 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012131 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12132 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012133 list_append_number(l, (fp != NULL)
12134 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012135 : (varnumber_T)0);
12136 list_append_number(l,
12137#ifdef FEAT_VIRTUALEDIT
12138 (fp != NULL) ? (varnumber_T)fp->coladd :
12139#endif
12140 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012141 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012142 list_append_number(l, curwin->w_curswant == MAXCOL ?
12143 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012144 }
12145 else
12146 rettv->vval.v_number = FALSE;
12147}
12148
12149/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012150 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012151 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012152 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012153f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012154 typval_T *argvars UNUSED;
12155 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012156{
12157#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012158 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012159#endif
12160
Bram Moolenaar2641f772005-03-25 21:58:17 +000012161#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012162 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012163 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012164 wp = NULL;
12165 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12166 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012167 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012168 if (wp == NULL)
12169 return;
12170 }
12171
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012172 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012173 }
12174#endif
12175}
12176
12177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 * "getreg()" function
12179 */
12180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012182 typval_T *argvars;
12183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185 char_u *strregname;
12186 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012187 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012188 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012191 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012193 strregname = get_tv_string_chk(&argvars[0]);
12194 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012195 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012198 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12199 return_list = get_tv_number_chk(&argvars[2], &error);
12200 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012203 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012204
12205 if (error)
12206 return;
12207
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 regname = (strregname == NULL ? '"' : *strregname);
12209 if (regname == 0)
12210 regname = '"';
12211
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012212 if (return_list)
12213 {
12214 rettv->v_type = VAR_LIST;
12215 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12216 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012217 if (rettv->vval.v_list != NULL)
12218 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012219 }
12220 else
12221 {
12222 rettv->v_type = VAR_STRING;
12223 rettv->vval.v_string = get_reg_contents(regname,
12224 arg2 ? GREG_EXPR_SRC : 0);
12225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226}
12227
12228/*
12229 * "getregtype()" function
12230 */
12231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 typval_T *argvars;
12234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
12236 char_u *strregname;
12237 int regname;
12238 char_u buf[NUMBUFLEN + 2];
12239 long reglen = 0;
12240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012241 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012242 {
12243 strregname = get_tv_string_chk(&argvars[0]);
12244 if (strregname == NULL) /* type error; errmsg already given */
12245 {
12246 rettv->v_type = VAR_STRING;
12247 rettv->vval.v_string = NULL;
12248 return;
12249 }
12250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 else
12252 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012253 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254
12255 regname = (strregname == NULL ? '"' : *strregname);
12256 if (regname == 0)
12257 regname = '"';
12258
12259 buf[0] = NUL;
12260 buf[1] = NUL;
12261 switch (get_reg_type(regname, &reglen))
12262 {
12263 case MLINE: buf[0] = 'V'; break;
12264 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 case MBLOCK:
12266 buf[0] = Ctrl_V;
12267 sprintf((char *)buf + 1, "%ld", reglen + 1);
12268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 rettv->v_type = VAR_STRING;
12271 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272}
12273
12274/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012275 * "gettabvar()" function
12276 */
12277 static void
12278f_gettabvar(argvars, rettv)
12279 typval_T *argvars;
12280 typval_T *rettv;
12281{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012282 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012283 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012284 dictitem_T *v;
12285 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012286 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012287
12288 rettv->v_type = VAR_STRING;
12289 rettv->vval.v_string = NULL;
12290
12291 varname = get_tv_string_chk(&argvars[1]);
12292 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12293 if (tp != NULL && varname != NULL)
12294 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012295 /* Set tp to be our tabpage, temporarily. Also set the window to the
12296 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012297 if (switch_win(&oldcurwin, &oldtabpage,
12298 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012299 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012300 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012301 /* look up the variable */
12302 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12303 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12304 if (v != NULL)
12305 {
12306 copy_tv(&v->di_tv, rettv);
12307 done = TRUE;
12308 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012309 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012310
12311 /* restore previous notion of curwin */
12312 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012313 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012314
12315 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012316 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012317}
12318
12319/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012320 * "gettabwinvar()" function
12321 */
12322 static void
12323f_gettabwinvar(argvars, rettv)
12324 typval_T *argvars;
12325 typval_T *rettv;
12326{
12327 getwinvar(argvars, rettv, 1);
12328}
12329
12330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 * "getwinposx()" function
12332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339#ifdef FEAT_GUI
12340 if (gui.in_use)
12341 {
12342 int x, y;
12343
12344 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 }
12347#endif
12348}
12349
12350/*
12351 * "getwinposy()" function
12352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012355 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359#ifdef FEAT_GUI
12360 if (gui.in_use)
12361 {
12362 int x, y;
12363
12364 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 }
12367#endif
12368}
12369
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012370/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012371 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012372 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012373 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012374find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012375 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012376 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012377{
12378#ifdef FEAT_WINDOWS
12379 win_T *wp;
12380#endif
12381 int nr;
12382
12383 nr = get_tv_number_chk(vp, NULL);
12384
12385#ifdef FEAT_WINDOWS
12386 if (nr < 0)
12387 return NULL;
12388 if (nr == 0)
12389 return curwin;
12390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012391 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12392 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012393 if (--nr <= 0)
12394 break;
12395 return wp;
12396#else
12397 if (nr == 0 || nr == 1)
12398 return curwin;
12399 return NULL;
12400#endif
12401}
12402
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403/*
12404 * "getwinvar()" function
12405 */
12406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *argvars;
12409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012411 getwinvar(argvars, rettv, 0);
12412}
12413
12414/*
12415 * getwinvar() and gettabwinvar()
12416 */
12417 static void
12418getwinvar(argvars, rettv, off)
12419 typval_T *argvars;
12420 typval_T *rettv;
12421 int off; /* 1 for gettabwinvar() */
12422{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 win_T *win, *oldcurwin;
12424 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012425 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012426 tabpage_T *tp = NULL;
12427 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012428 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012430#ifdef FEAT_WINDOWS
12431 if (off == 1)
12432 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12433 else
12434 tp = curtab;
12435#endif
12436 win = find_win_by_nr(&argvars[off], tp);
12437 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012440 rettv->v_type = VAR_STRING;
12441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442
12443 if (win != NULL && varname != NULL)
12444 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012445 /* Set curwin to be our win, temporarily. Also set the tabpage,
12446 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012447 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012448 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012449 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012450 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012451 if (get_option_tv(&varname, rettv, 1) == OK)
12452 done = TRUE;
12453 }
12454 else
12455 {
12456 /* Look up the variable. */
12457 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12458 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12459 varname, FALSE);
12460 if (v != NULL)
12461 {
12462 copy_tv(&v->di_tv, rettv);
12463 done = TRUE;
12464 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012467
12468 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012469 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 }
12471
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012472 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12473 /* use the default return value */
12474 copy_tv(&argvars[off + 2], rettv);
12475
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 --emsg_off;
12477}
12478
12479/*
12480 * "glob()" function
12481 */
12482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *argvars;
12485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012487 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012489 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012491 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012492 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012494 if (argvars[1].v_type != VAR_UNKNOWN)
12495 {
12496 if (get_tv_number_chk(&argvars[1], &error))
12497 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012498 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012499 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012500 if (get_tv_number_chk(&argvars[2], &error))
12501 {
12502 rettv->v_type = VAR_LIST;
12503 rettv->vval.v_list = NULL;
12504 }
12505 if (argvars[3].v_type != VAR_UNKNOWN
12506 && get_tv_number_chk(&argvars[3], &error))
12507 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012508 }
12509 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012510 if (!error)
12511 {
12512 ExpandInit(&xpc);
12513 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012514 if (p_wic)
12515 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012516 if (rettv->v_type == VAR_STRING)
12517 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012518 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012519 else if (rettv_list_alloc(rettv) != FAIL)
12520 {
12521 int i;
12522
12523 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12524 NULL, options, WILD_ALL_KEEP);
12525 for (i = 0; i < xpc.xp_numfiles; i++)
12526 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12527
12528 ExpandCleanup(&xpc);
12529 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012530 }
12531 else
12532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533}
12534
12535/*
12536 * "globpath()" function
12537 */
12538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012540 typval_T *argvars;
12541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012543 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012546 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012547 garray_T ga;
12548 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012550 /* When the optional second argument is non-zero, don't remove matches
12551 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012553 if (argvars[2].v_type != VAR_UNKNOWN)
12554 {
12555 if (get_tv_number_chk(&argvars[2], &error))
12556 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012557 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012558 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012559 if (get_tv_number_chk(&argvars[3], &error))
12560 {
12561 rettv->v_type = VAR_LIST;
12562 rettv->vval.v_list = NULL;
12563 }
12564 if (argvars[4].v_type != VAR_UNKNOWN
12565 && get_tv_number_chk(&argvars[4], &error))
12566 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012567 }
12568 }
12569 if (file != NULL && !error)
12570 {
12571 ga_init2(&ga, (int)sizeof(char_u *), 10);
12572 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12573 if (rettv->v_type == VAR_STRING)
12574 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12575 else if (rettv_list_alloc(rettv) != FAIL)
12576 for (i = 0; i < ga.ga_len; ++i)
12577 list_append_string(rettv->vval.v_list,
12578 ((char_u **)(ga.ga_data))[i], -1);
12579 ga_clear_strings(&ga);
12580 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012581 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583}
12584
12585/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012586 * "glob2regpat()" function
12587 */
12588 static void
12589f_glob2regpat(argvars, rettv)
12590 typval_T *argvars;
12591 typval_T *rettv;
12592{
12593 char_u *pat = get_tv_string_chk(&argvars[0]);
12594
12595 rettv->v_type = VAR_STRING;
12596 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12597}
12598
12599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 * "has()" function
12601 */
12602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012604 typval_T *argvars;
12605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606{
12607 int i;
12608 char_u *name;
12609 int n = FALSE;
12610 static char *(has_list[]) =
12611 {
12612#ifdef AMIGA
12613 "amiga",
12614# ifdef FEAT_ARP
12615 "arp",
12616# endif
12617#endif
12618#ifdef __BEOS__
12619 "beos",
12620#endif
12621#ifdef MSDOS
12622# ifdef DJGPP
12623 "dos32",
12624# else
12625 "dos16",
12626# endif
12627#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012628#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 "mac",
12630#endif
12631#if defined(MACOS_X_UNIX)
12632 "macunix",
12633#endif
12634#ifdef OS2
12635 "os2",
12636#endif
12637#ifdef __QNX__
12638 "qnx",
12639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640#ifdef UNIX
12641 "unix",
12642#endif
12643#ifdef VMS
12644 "vms",
12645#endif
12646#ifdef WIN16
12647 "win16",
12648#endif
12649#ifdef WIN32
12650 "win32",
12651#endif
12652#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12653 "win32unix",
12654#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012655#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 "win64",
12657#endif
12658#ifdef EBCDIC
12659 "ebcdic",
12660#endif
12661#ifndef CASE_INSENSITIVE_FILENAME
12662 "fname_case",
12663#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012664#ifdef HAVE_ACL
12665 "acl",
12666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667#ifdef FEAT_ARABIC
12668 "arabic",
12669#endif
12670#ifdef FEAT_AUTOCMD
12671 "autocmd",
12672#endif
12673#ifdef FEAT_BEVAL
12674 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012675# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12676 "balloon_multiline",
12677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678#endif
12679#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12680 "builtin_terms",
12681# ifdef ALL_BUILTIN_TCAPS
12682 "all_builtin_terms",
12683# endif
12684#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012685#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12686 || defined(FEAT_GUI_W32) \
12687 || defined(FEAT_GUI_MOTIF))
12688 "browsefilter",
12689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690#ifdef FEAT_BYTEOFF
12691 "byte_offset",
12692#endif
12693#ifdef FEAT_CINDENT
12694 "cindent",
12695#endif
12696#ifdef FEAT_CLIENTSERVER
12697 "clientserver",
12698#endif
12699#ifdef FEAT_CLIPBOARD
12700 "clipboard",
12701#endif
12702#ifdef FEAT_CMDL_COMPL
12703 "cmdline_compl",
12704#endif
12705#ifdef FEAT_CMDHIST
12706 "cmdline_hist",
12707#endif
12708#ifdef FEAT_COMMENTS
12709 "comments",
12710#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012711#ifdef FEAT_CONCEAL
12712 "conceal",
12713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714#ifdef FEAT_CRYPT
12715 "cryptv",
12716#endif
12717#ifdef FEAT_CSCOPE
12718 "cscope",
12719#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012720#ifdef FEAT_CURSORBIND
12721 "cursorbind",
12722#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012723#ifdef CURSOR_SHAPE
12724 "cursorshape",
12725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726#ifdef DEBUG
12727 "debug",
12728#endif
12729#ifdef FEAT_CON_DIALOG
12730 "dialog_con",
12731#endif
12732#ifdef FEAT_GUI_DIALOG
12733 "dialog_gui",
12734#endif
12735#ifdef FEAT_DIFF
12736 "diff",
12737#endif
12738#ifdef FEAT_DIGRAPHS
12739 "digraphs",
12740#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012741#ifdef FEAT_DIRECTX
12742 "directx",
12743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#ifdef FEAT_DND
12745 "dnd",
12746#endif
12747#ifdef FEAT_EMACS_TAGS
12748 "emacs_tags",
12749#endif
12750 "eval", /* always present, of course! */
12751#ifdef FEAT_EX_EXTRA
12752 "ex_extra",
12753#endif
12754#ifdef FEAT_SEARCH_EXTRA
12755 "extra_search",
12756#endif
12757#ifdef FEAT_FKMAP
12758 "farsi",
12759#endif
12760#ifdef FEAT_SEARCHPATH
12761 "file_in_path",
12762#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012763#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012764 "filterpipe",
12765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766#ifdef FEAT_FIND_ID
12767 "find_in_path",
12768#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012769#ifdef FEAT_FLOAT
12770 "float",
12771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772#ifdef FEAT_FOLDING
12773 "folding",
12774#endif
12775#ifdef FEAT_FOOTER
12776 "footer",
12777#endif
12778#if !defined(USE_SYSTEM) && defined(UNIX)
12779 "fork",
12780#endif
12781#ifdef FEAT_GETTEXT
12782 "gettext",
12783#endif
12784#ifdef FEAT_GUI
12785 "gui",
12786#endif
12787#ifdef FEAT_GUI_ATHENA
12788# ifdef FEAT_GUI_NEXTAW
12789 "gui_neXtaw",
12790# else
12791 "gui_athena",
12792# endif
12793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794#ifdef FEAT_GUI_GTK
12795 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012798#ifdef FEAT_GUI_GNOME
12799 "gui_gnome",
12800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801#ifdef FEAT_GUI_MAC
12802 "gui_mac",
12803#endif
12804#ifdef FEAT_GUI_MOTIF
12805 "gui_motif",
12806#endif
12807#ifdef FEAT_GUI_PHOTON
12808 "gui_photon",
12809#endif
12810#ifdef FEAT_GUI_W16
12811 "gui_win16",
12812#endif
12813#ifdef FEAT_GUI_W32
12814 "gui_win32",
12815#endif
12816#ifdef FEAT_HANGULIN
12817 "hangul_input",
12818#endif
12819#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12820 "iconv",
12821#endif
12822#ifdef FEAT_INS_EXPAND
12823 "insert_expand",
12824#endif
12825#ifdef FEAT_JUMPLIST
12826 "jumplist",
12827#endif
12828#ifdef FEAT_KEYMAP
12829 "keymap",
12830#endif
12831#ifdef FEAT_LANGMAP
12832 "langmap",
12833#endif
12834#ifdef FEAT_LIBCALL
12835 "libcall",
12836#endif
12837#ifdef FEAT_LINEBREAK
12838 "linebreak",
12839#endif
12840#ifdef FEAT_LISP
12841 "lispindent",
12842#endif
12843#ifdef FEAT_LISTCMDS
12844 "listcmds",
12845#endif
12846#ifdef FEAT_LOCALMAP
12847 "localmap",
12848#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012849#ifdef FEAT_LUA
12850# ifndef DYNAMIC_LUA
12851 "lua",
12852# endif
12853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854#ifdef FEAT_MENU
12855 "menu",
12856#endif
12857#ifdef FEAT_SESSION
12858 "mksession",
12859#endif
12860#ifdef FEAT_MODIFY_FNAME
12861 "modify_fname",
12862#endif
12863#ifdef FEAT_MOUSE
12864 "mouse",
12865#endif
12866#ifdef FEAT_MOUSESHAPE
12867 "mouseshape",
12868#endif
12869#if defined(UNIX) || defined(VMS)
12870# ifdef FEAT_MOUSE_DEC
12871 "mouse_dec",
12872# endif
12873# ifdef FEAT_MOUSE_GPM
12874 "mouse_gpm",
12875# endif
12876# ifdef FEAT_MOUSE_JSB
12877 "mouse_jsbterm",
12878# endif
12879# ifdef FEAT_MOUSE_NET
12880 "mouse_netterm",
12881# endif
12882# ifdef FEAT_MOUSE_PTERM
12883 "mouse_pterm",
12884# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012885# ifdef FEAT_MOUSE_SGR
12886 "mouse_sgr",
12887# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012888# ifdef FEAT_SYSMOUSE
12889 "mouse_sysmouse",
12890# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012891# ifdef FEAT_MOUSE_URXVT
12892 "mouse_urxvt",
12893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894# ifdef FEAT_MOUSE_XTERM
12895 "mouse_xterm",
12896# endif
12897#endif
12898#ifdef FEAT_MBYTE
12899 "multi_byte",
12900#endif
12901#ifdef FEAT_MBYTE_IME
12902 "multi_byte_ime",
12903#endif
12904#ifdef FEAT_MULTI_LANG
12905 "multi_lang",
12906#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012907#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012908#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012909 "mzscheme",
12910#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#ifdef FEAT_OLE
12913 "ole",
12914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915#ifdef FEAT_PATH_EXTRA
12916 "path_extra",
12917#endif
12918#ifdef FEAT_PERL
12919#ifndef DYNAMIC_PERL
12920 "perl",
12921#endif
12922#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012923#ifdef FEAT_PERSISTENT_UNDO
12924 "persistent_undo",
12925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926#ifdef FEAT_PYTHON
12927#ifndef DYNAMIC_PYTHON
12928 "python",
12929#endif
12930#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012931#ifdef FEAT_PYTHON3
12932#ifndef DYNAMIC_PYTHON3
12933 "python3",
12934#endif
12935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936#ifdef FEAT_POSTSCRIPT
12937 "postscript",
12938#endif
12939#ifdef FEAT_PRINTER
12940 "printer",
12941#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012942#ifdef FEAT_PROFILE
12943 "profile",
12944#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012945#ifdef FEAT_RELTIME
12946 "reltime",
12947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948#ifdef FEAT_QUICKFIX
12949 "quickfix",
12950#endif
12951#ifdef FEAT_RIGHTLEFT
12952 "rightleft",
12953#endif
12954#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12955 "ruby",
12956#endif
12957#ifdef FEAT_SCROLLBIND
12958 "scrollbind",
12959#endif
12960#ifdef FEAT_CMDL_INFO
12961 "showcmd",
12962 "cmdline_info",
12963#endif
12964#ifdef FEAT_SIGNS
12965 "signs",
12966#endif
12967#ifdef FEAT_SMARTINDENT
12968 "smartindent",
12969#endif
12970#ifdef FEAT_SNIFF
12971 "sniff",
12972#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012973#ifdef STARTUPTIME
12974 "startuptime",
12975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976#ifdef FEAT_STL_OPT
12977 "statusline",
12978#endif
12979#ifdef FEAT_SUN_WORKSHOP
12980 "sun_workshop",
12981#endif
12982#ifdef FEAT_NETBEANS_INTG
12983 "netbeans_intg",
12984#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012985#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012986 "spell",
12987#endif
12988#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 "syntax",
12990#endif
12991#if defined(USE_SYSTEM) || !defined(UNIX)
12992 "system",
12993#endif
12994#ifdef FEAT_TAG_BINS
12995 "tag_binary",
12996#endif
12997#ifdef FEAT_TAG_OLDSTATIC
12998 "tag_old_static",
12999#endif
13000#ifdef FEAT_TAG_ANYWHITE
13001 "tag_any_white",
13002#endif
13003#ifdef FEAT_TCL
13004# ifndef DYNAMIC_TCL
13005 "tcl",
13006# endif
13007#endif
13008#ifdef TERMINFO
13009 "terminfo",
13010#endif
13011#ifdef FEAT_TERMRESPONSE
13012 "termresponse",
13013#endif
13014#ifdef FEAT_TEXTOBJ
13015 "textobjects",
13016#endif
13017#ifdef HAVE_TGETENT
13018 "tgetent",
13019#endif
13020#ifdef FEAT_TITLE
13021 "title",
13022#endif
13023#ifdef FEAT_TOOLBAR
13024 "toolbar",
13025#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013026#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13027 "unnamedplus",
13028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029#ifdef FEAT_USR_CMDS
13030 "user-commands", /* was accidentally included in 5.4 */
13031 "user_commands",
13032#endif
13033#ifdef FEAT_VIMINFO
13034 "viminfo",
13035#endif
13036#ifdef FEAT_VERTSPLIT
13037 "vertsplit",
13038#endif
13039#ifdef FEAT_VIRTUALEDIT
13040 "virtualedit",
13041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043#ifdef FEAT_VISUALEXTRA
13044 "visualextra",
13045#endif
13046#ifdef FEAT_VREPLACE
13047 "vreplace",
13048#endif
13049#ifdef FEAT_WILDIGN
13050 "wildignore",
13051#endif
13052#ifdef FEAT_WILDMENU
13053 "wildmenu",
13054#endif
13055#ifdef FEAT_WINDOWS
13056 "windows",
13057#endif
13058#ifdef FEAT_WAK
13059 "winaltkeys",
13060#endif
13061#ifdef FEAT_WRITEBACKUP
13062 "writebackup",
13063#endif
13064#ifdef FEAT_XIM
13065 "xim",
13066#endif
13067#ifdef FEAT_XFONTSET
13068 "xfontset",
13069#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013070#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013071 "xpm",
13072 "xpm_w32", /* for backward compatibility */
13073#else
13074# if defined(HAVE_XPM)
13075 "xpm",
13076# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078#ifdef USE_XSMP
13079 "xsmp",
13080#endif
13081#ifdef USE_XSMP_INTERACT
13082 "xsmp_interact",
13083#endif
13084#ifdef FEAT_XCLIPBOARD
13085 "xterm_clipboard",
13086#endif
13087#ifdef FEAT_XTERM_SAVE
13088 "xterm_save",
13089#endif
13090#if defined(UNIX) && defined(FEAT_X11)
13091 "X11",
13092#endif
13093 NULL
13094 };
13095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 for (i = 0; has_list[i] != NULL; ++i)
13098 if (STRICMP(name, has_list[i]) == 0)
13099 {
13100 n = TRUE;
13101 break;
13102 }
13103
13104 if (n == FALSE)
13105 {
13106 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013107 {
13108 if (name[5] == '-'
13109 && STRLEN(name) > 11
13110 && vim_isdigit(name[6])
13111 && vim_isdigit(name[8])
13112 && vim_isdigit(name[10]))
13113 {
13114 int major = atoi((char *)name + 6);
13115 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013116
13117 /* Expect "patch-9.9.01234". */
13118 n = (major < VIM_VERSION_MAJOR
13119 || (major == VIM_VERSION_MAJOR
13120 && (minor < VIM_VERSION_MINOR
13121 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013122 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013123 }
13124 else
13125 n = has_patch(atoi((char *)name + 5));
13126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 else if (STRICMP(name, "vim_starting") == 0)
13128 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013129#ifdef FEAT_MBYTE
13130 else if (STRICMP(name, "multi_byte_encoding") == 0)
13131 n = has_mbyte;
13132#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013133#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13134 else if (STRICMP(name, "balloon_multiline") == 0)
13135 n = multiline_balloon_available();
13136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137#ifdef DYNAMIC_TCL
13138 else if (STRICMP(name, "tcl") == 0)
13139 n = tcl_enabled(FALSE);
13140#endif
13141#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13142 else if (STRICMP(name, "iconv") == 0)
13143 n = iconv_enabled(FALSE);
13144#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013145#ifdef DYNAMIC_LUA
13146 else if (STRICMP(name, "lua") == 0)
13147 n = lua_enabled(FALSE);
13148#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013149#ifdef DYNAMIC_MZSCHEME
13150 else if (STRICMP(name, "mzscheme") == 0)
13151 n = mzscheme_enabled(FALSE);
13152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153#ifdef DYNAMIC_RUBY
13154 else if (STRICMP(name, "ruby") == 0)
13155 n = ruby_enabled(FALSE);
13156#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013157#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158#ifdef DYNAMIC_PYTHON
13159 else if (STRICMP(name, "python") == 0)
13160 n = python_enabled(FALSE);
13161#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013162#endif
13163#ifdef FEAT_PYTHON3
13164#ifdef DYNAMIC_PYTHON3
13165 else if (STRICMP(name, "python3") == 0)
13166 n = python3_enabled(FALSE);
13167#endif
13168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169#ifdef DYNAMIC_PERL
13170 else if (STRICMP(name, "perl") == 0)
13171 n = perl_enabled(FALSE);
13172#endif
13173#ifdef FEAT_GUI
13174 else if (STRICMP(name, "gui_running") == 0)
13175 n = (gui.in_use || gui.starting);
13176# ifdef FEAT_GUI_W32
13177 else if (STRICMP(name, "gui_win32s") == 0)
13178 n = gui_is_win32s();
13179# endif
13180# ifdef FEAT_BROWSE
13181 else if (STRICMP(name, "browse") == 0)
13182 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13183# endif
13184#endif
13185#ifdef FEAT_SYN_HL
13186 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013187 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188#endif
13189#if defined(WIN3264)
13190 else if (STRICMP(name, "win95") == 0)
13191 n = mch_windows95();
13192#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013193#ifdef FEAT_NETBEANS_INTG
13194 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013195 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 }
13198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200}
13201
13202/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013203 * "has_key()" function
13204 */
13205 static void
13206f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *argvars;
13208 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013209{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013210 if (argvars[0].v_type != VAR_DICT)
13211 {
13212 EMSG(_(e_dictreq));
13213 return;
13214 }
13215 if (argvars[0].vval.v_dict == NULL)
13216 return;
13217
13218 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013219 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013220}
13221
13222/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013223 * "haslocaldir()" function
13224 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013225 static void
13226f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013227 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013228 typval_T *rettv;
13229{
13230 rettv->vval.v_number = (curwin->w_localdir != NULL);
13231}
13232
13233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 * "hasmapto()" function
13235 */
13236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013238 typval_T *argvars;
13239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240{
13241 char_u *name;
13242 char_u *mode;
13243 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013244 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013247 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 mode = (char_u *)"nvo";
13249 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013252 if (argvars[2].v_type != VAR_UNKNOWN)
13253 abbr = get_tv_number(&argvars[2]);
13254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255
Bram Moolenaar2c932302006-03-18 21:42:09 +000013256 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260}
13261
13262/*
13263 * "histadd()" function
13264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013267 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269{
13270#ifdef FEAT_CMDHIST
13271 int histype;
13272 char_u *str;
13273 char_u buf[NUMBUFLEN];
13274#endif
13275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 if (check_restricted() || check_secure())
13278 return;
13279#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13281 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 if (histype >= 0)
13283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 if (*str != NUL)
13286 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013287 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013289 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 return;
13291 }
13292 }
13293#endif
13294}
13295
13296/*
13297 * "histdel()" function
13298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013301 typval_T *argvars UNUSED;
13302 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303{
13304#ifdef FEAT_CMDHIST
13305 int n;
13306 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13310 if (str == NULL)
13311 n = 0;
13312 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013315 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 else
13320 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322 get_tv_string_buf(&argvars[1], buf));
13323 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#endif
13325}
13326
13327/*
13328 * "histget()" function
13329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334{
13335#ifdef FEAT_CMDHIST
13336 int type;
13337 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013338 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13341 if (str == NULL)
13342 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013344 {
13345 type = get_histtype(str);
13346 if (argvars[1].v_type == VAR_UNKNOWN)
13347 idx = get_history_idx(type);
13348 else
13349 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13350 /* -1 on type error */
13351 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357}
13358
13359/*
13360 * "histnr()" function
13361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366{
13367 int i;
13368
13369#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 char_u *history = get_tv_string_chk(&argvars[0]);
13371
13372 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 if (i >= HIST_CMD && i < HIST_COUNT)
13374 i = get_history_idx(i);
13375 else
13376#endif
13377 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379}
13380
13381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 * "highlightID(name)" function
13383 */
13384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *argvars;
13387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390}
13391
13392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013393 * "highlight_exists()" function
13394 */
13395 static void
13396f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 typval_T *argvars;
13398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013399{
13400 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13401}
13402
13403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 * "hostname()" function
13405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410{
13411 char_u hostname[256];
13412
13413 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->v_type = VAR_STRING;
13415 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416}
13417
13418/*
13419 * iconv() function
13420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013423 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425{
13426#ifdef FEAT_MBYTE
13427 char_u buf1[NUMBUFLEN];
13428 char_u buf2[NUMBUFLEN];
13429 char_u *from, *to, *str;
13430 vimconv_T vimconv;
13431#endif
13432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433 rettv->v_type = VAR_STRING;
13434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435
13436#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013437 str = get_tv_string(&argvars[0]);
13438 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13439 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 vimconv.vc_type = CONV_NONE;
13441 convert_setup(&vimconv, from, to);
13442
13443 /* If the encodings are equal, no conversion needed. */
13444 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448
13449 convert_setup(&vimconv, NULL, NULL);
13450 vim_free(from);
13451 vim_free(to);
13452#endif
13453}
13454
13455/*
13456 * "indent()" function
13457 */
13458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013460 typval_T *argvars;
13461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462{
13463 linenr_T lnum;
13464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470}
13471
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013472/*
13473 * "index()" function
13474 */
13475 static void
13476f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013477 typval_T *argvars;
13478 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013479{
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 list_T *l;
13481 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013482 long idx = 0;
13483 int ic = FALSE;
13484
13485 rettv->vval.v_number = -1;
13486 if (argvars[0].v_type != VAR_LIST)
13487 {
13488 EMSG(_(e_listreq));
13489 return;
13490 }
13491 l = argvars[0].vval.v_list;
13492 if (l != NULL)
13493 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013494 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 int error = FALSE;
13498
Bram Moolenaar758711c2005-02-02 23:11:38 +000013499 /* Start at specified item. Use the cached index that list_find()
13500 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013502 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013503 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 ic = get_tv_number_chk(&argvars[3], &error);
13505 if (error)
13506 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013507 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013508
Bram Moolenaar758711c2005-02-02 23:11:38 +000013509 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013510 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013511 {
13512 rettv->vval.v_number = idx;
13513 break;
13514 }
13515 }
13516}
13517
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518static int inputsecret_flag = 0;
13519
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013520static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13521
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013523 * This function is used by f_input() and f_inputdialog() functions. The third
13524 * argument to f_input() specifies the type of completion to use at the
13525 * prompt. The third argument to f_inputdialog() specifies the value to return
13526 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 */
13528 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013529get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013532 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 char_u *p = NULL;
13536 int c;
13537 char_u buf[NUMBUFLEN];
13538 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013540 int xp_type = EXPAND_NOTHING;
13541 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545
13546#ifdef NO_CONSOLE_INPUT
13547 /* While starting up, there is no place to enter text. */
13548 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550#endif
13551
13552 cmd_silent = FALSE; /* Want to see the prompt. */
13553 if (prompt != NULL)
13554 {
13555 /* Only the part of the message after the last NL is considered as
13556 * prompt for the command line */
13557 p = vim_strrchr(prompt, '\n');
13558 if (p == NULL)
13559 p = prompt;
13560 else
13561 {
13562 ++p;
13563 c = *p;
13564 *p = NUL;
13565 msg_start();
13566 msg_clr_eos();
13567 msg_puts_attr(prompt, echo_attr);
13568 msg_didout = FALSE;
13569 msg_starthere();
13570 *p = c;
13571 }
13572 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 if (argvars[1].v_type != VAR_UNKNOWN)
13575 {
13576 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13577 if (defstr != NULL)
13578 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013580 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013581 {
13582 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013583 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013584 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013585
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013586 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013587 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013588
Bram Moolenaar4463f292005-09-25 22:20:24 +000013589 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13590 if (xp_name == NULL)
13591 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013592
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013593 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013594
Bram Moolenaar4463f292005-09-25 22:20:24 +000013595 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13596 &xp_arg) == FAIL)
13597 return;
13598 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013599 }
13600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013602 {
13603# ifdef FEAT_EX_EXTRA
13604 int save_ex_normal_busy = ex_normal_busy;
13605 ex_normal_busy = 0;
13606# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013608 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13609 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013610# ifdef FEAT_EX_EXTRA
13611 ex_normal_busy = save_ex_normal_busy;
13612# endif
13613 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013614 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013615 && argvars[1].v_type != VAR_UNKNOWN
13616 && argvars[2].v_type != VAR_UNKNOWN)
13617 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13618 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013619
13620 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 /* since the user typed this, no need to wait for return */
13623 need_wait_return = FALSE;
13624 msg_didout = FALSE;
13625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 cmd_silent = cmd_silent_save;
13627}
13628
13629/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013630 * "input()" function
13631 * Also handles inputsecret() when inputsecret is set.
13632 */
13633 static void
13634f_input(argvars, rettv)
13635 typval_T *argvars;
13636 typval_T *rettv;
13637{
13638 get_user_input(argvars, rettv, FALSE);
13639}
13640
13641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 * "inputdialog()" function
13643 */
13644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *argvars;
13647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648{
13649#if defined(FEAT_GUI_TEXTDIALOG)
13650 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13651 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13652 {
13653 char_u *message;
13654 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 message = get_tv_string_chk(&argvars[0]);
13658 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013659 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013660 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 else
13662 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 if (message != NULL && defstr != NULL
13664 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013665 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 else
13668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 if (message != NULL && defstr != NULL
13670 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013671 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 rettv->vval.v_string = vim_strsave(
13673 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 }
13679 else
13680#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013681 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682}
13683
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013684/*
13685 * "inputlist()" function
13686 */
13687 static void
13688f_inputlist(argvars, rettv)
13689 typval_T *argvars;
13690 typval_T *rettv;
13691{
13692 listitem_T *li;
13693 int selected;
13694 int mouse_used;
13695
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013696#ifdef NO_CONSOLE_INPUT
13697 /* While starting up, there is no place to enter text. */
13698 if (no_console_input())
13699 return;
13700#endif
13701 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13702 {
13703 EMSG2(_(e_listarg), "inputlist()");
13704 return;
13705 }
13706
13707 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013708 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013709 lines_left = Rows; /* avoid more prompt */
13710 msg_scroll = TRUE;
13711 msg_clr_eos();
13712
13713 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13714 {
13715 msg_puts(get_tv_string(&li->li_tv));
13716 msg_putchar('\n');
13717 }
13718
13719 /* Ask for choice. */
13720 selected = prompt_for_number(&mouse_used);
13721 if (mouse_used)
13722 selected -= lines_left;
13723
13724 rettv->vval.v_number = selected;
13725}
13726
13727
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13729
13730/*
13731 * "inputrestore()" function
13732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013735 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737{
13738 if (ga_userinput.ga_len > 0)
13739 {
13740 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13742 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013743 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 }
13745 else if (p_verbose > 1)
13746 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013747 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 }
13750}
13751
13752/*
13753 * "inputsave()" function
13754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013757 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013760 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 if (ga_grow(&ga_userinput, 1) == OK)
13762 {
13763 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13764 + ga_userinput.ga_len);
13765 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013766 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 }
13768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013769 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770}
13771
13772/*
13773 * "inputsecret()" function
13774 */
13775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779{
13780 ++cmdline_star;
13781 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 --cmdline_star;
13784 --inputsecret_flag;
13785}
13786
13787/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013788 * "insert()" function
13789 */
13790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013791f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *argvars;
13793 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013794{
13795 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 listitem_T *item;
13797 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013798 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013799
13800 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013801 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013802 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013803 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013804 {
13805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 before = get_tv_number_chk(&argvars[2], &error);
13807 if (error)
13808 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013809
Bram Moolenaar758711c2005-02-02 23:11:38 +000013810 if (before == l->lv_len)
13811 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013812 else
13813 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013814 item = list_find(l, before);
13815 if (item == NULL)
13816 {
13817 EMSGN(_(e_listidx), before);
13818 l = NULL;
13819 }
13820 }
13821 if (l != NULL)
13822 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013823 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013824 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013825 }
13826 }
13827}
13828
13829/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013830 * "invert(expr)" function
13831 */
13832 static void
13833f_invert(argvars, rettv)
13834 typval_T *argvars;
13835 typval_T *rettv;
13836{
13837 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13838}
13839
13840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 * "isdirectory()" function
13842 */
13843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013845 typval_T *argvars;
13846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013848 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849}
13850
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013851/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013852 * "islocked()" function
13853 */
13854 static void
13855f_islocked(argvars, rettv)
13856 typval_T *argvars;
13857 typval_T *rettv;
13858{
13859 lval_T lv;
13860 char_u *end;
13861 dictitem_T *di;
13862
13863 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013864 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13865 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013866 if (end != NULL && lv.ll_name != NULL)
13867 {
13868 if (*end != NUL)
13869 EMSG(_(e_trailing));
13870 else
13871 {
13872 if (lv.ll_tv == NULL)
13873 {
13874 if (check_changedtick(lv.ll_name))
13875 rettv->vval.v_number = 1; /* always locked */
13876 else
13877 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013878 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013879 if (di != NULL)
13880 {
13881 /* Consider a variable locked when:
13882 * 1. the variable itself is locked
13883 * 2. the value of the variable is locked.
13884 * 3. the List or Dict value is locked.
13885 */
13886 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13887 || tv_islocked(&di->di_tv));
13888 }
13889 }
13890 }
13891 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013892 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013893 else if (lv.ll_newkey != NULL)
13894 EMSG2(_(e_dictkey), lv.ll_newkey);
13895 else if (lv.ll_list != NULL)
13896 /* List item. */
13897 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13898 else
13899 /* Dictionary item. */
13900 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13901 }
13902 }
13903
13904 clear_lval(&lv);
13905}
13906
Bram Moolenaar33570922005-01-25 22:26:29 +000013907static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013908
13909/*
13910 * Turn a dict into a list:
13911 * "what" == 0: list of keys
13912 * "what" == 1: list of values
13913 * "what" == 2: list of items
13914 */
13915 static void
13916dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 typval_T *argvars;
13918 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013919 int what;
13920{
Bram Moolenaar33570922005-01-25 22:26:29 +000013921 list_T *l2;
13922 dictitem_T *di;
13923 hashitem_T *hi;
13924 listitem_T *li;
13925 listitem_T *li2;
13926 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013927 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013928
Bram Moolenaar8c711452005-01-14 21:53:12 +000013929 if (argvars[0].v_type != VAR_DICT)
13930 {
13931 EMSG(_(e_dictreq));
13932 return;
13933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013934 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013935 return;
13936
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013937 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013938 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013939
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013940 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013941 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013943 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013945 --todo;
13946 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013947
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013948 li = listitem_alloc();
13949 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013950 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013951 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013952
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013953 if (what == 0)
13954 {
13955 /* keys() */
13956 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013957 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013958 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13959 }
13960 else if (what == 1)
13961 {
13962 /* values() */
13963 copy_tv(&di->di_tv, &li->li_tv);
13964 }
13965 else
13966 {
13967 /* items() */
13968 l2 = list_alloc();
13969 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013970 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013971 li->li_tv.vval.v_list = l2;
13972 if (l2 == NULL)
13973 break;
13974 ++l2->lv_refcount;
13975
13976 li2 = listitem_alloc();
13977 if (li2 == NULL)
13978 break;
13979 list_append(l2, li2);
13980 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013981 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013982 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13983
13984 li2 = listitem_alloc();
13985 if (li2 == NULL)
13986 break;
13987 list_append(l2, li2);
13988 copy_tv(&di->di_tv, &li2->li_tv);
13989 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013990 }
13991 }
13992}
13993
13994/*
13995 * "items(dict)" function
13996 */
13997 static void
13998f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 typval_T *argvars;
14000 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014001{
14002 dict_list(argvars, rettv, 2);
14003}
14004
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014006 * "join()" function
14007 */
14008 static void
14009f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *argvars;
14011 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014012{
14013 garray_T ga;
14014 char_u *sep;
14015
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016 if (argvars[0].v_type != VAR_LIST)
14017 {
14018 EMSG(_(e_listreq));
14019 return;
14020 }
14021 if (argvars[0].vval.v_list == NULL)
14022 return;
14023 if (argvars[1].v_type == VAR_UNKNOWN)
14024 sep = (char_u *)" ";
14025 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014027
14028 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029
14030 if (sep != NULL)
14031 {
14032 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014033 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014034 ga_append(&ga, NUL);
14035 rettv->vval.v_string = (char_u *)ga.ga_data;
14036 }
14037 else
14038 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014039}
14040
14041/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014042 * "keys()" function
14043 */
14044 static void
14045f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014046 typval_T *argvars;
14047 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014048{
14049 dict_list(argvars, rettv, 0);
14050}
14051
14052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 * "last_buffer_nr()" function.
14054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059{
14060 int n = 0;
14061 buf_T *buf;
14062
14063 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14064 if (n < buf->b_fnum)
14065 n = buf->b_fnum;
14066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014068}
14069
14070/*
14071 * "len()" function
14072 */
14073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014077{
14078 switch (argvars[0].v_type)
14079 {
14080 case VAR_STRING:
14081 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_number = (varnumber_T)STRLEN(
14083 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014084 break;
14085 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014088 case VAR_DICT:
14089 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14090 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014091 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014092 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014093 break;
14094 }
14095}
14096
Bram Moolenaar33570922005-01-25 22:26:29 +000014097static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014098
14099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 typval_T *argvars;
14102 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014103 int type;
14104{
14105#ifdef FEAT_LIBCALL
14106 char_u *string_in;
14107 char_u **string_result;
14108 int nr_result;
14109#endif
14110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014112 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014113 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014114
14115 if (check_restricted() || check_secure())
14116 return;
14117
14118#ifdef FEAT_LIBCALL
14119 /* The first two args must be strings, otherwise its meaningless */
14120 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14121 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014122 string_in = NULL;
14123 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014124 string_in = argvars[2].vval.v_string;
14125 if (type == VAR_NUMBER)
14126 string_result = NULL;
14127 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014128 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014129 if (mch_libcall(argvars[0].vval.v_string,
14130 argvars[1].vval.v_string,
14131 string_in,
14132 argvars[2].vval.v_number,
14133 string_result,
14134 &nr_result) == OK
14135 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014136 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014137 }
14138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139}
14140
14141/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014142 * "libcall()" function
14143 */
14144 static void
14145f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 typval_T *argvars;
14147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014148{
14149 libcall_common(argvars, rettv, VAR_STRING);
14150}
14151
14152/*
14153 * "libcallnr()" function
14154 */
14155 static void
14156f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014157 typval_T *argvars;
14158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159{
14160 libcall_common(argvars, rettv, VAR_NUMBER);
14161}
14162
14163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 * "line(string)" function
14165 */
14166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014168 typval_T *argvars;
14169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170{
14171 linenr_T lnum = 0;
14172 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014173 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014175 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 if (fp != NULL)
14177 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179}
14180
14181/*
14182 * "line2byte(lnum)" function
14183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188{
14189#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191#else
14192 linenr_T lnum;
14193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14199 if (rettv->vval.v_number >= 0)
14200 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201#endif
14202}
14203
14204/*
14205 * "lispindent(lnum)" function
14206 */
14207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014209 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211{
14212#ifdef FEAT_LISP
14213 pos_T pos;
14214 linenr_T lnum;
14215
14216 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14219 {
14220 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 curwin->w_cursor = pos;
14223 }
14224 else
14225#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229/*
14230 * "localtime()" function
14231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014234 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238}
14239
Bram Moolenaar33570922005-01-25 22:26:29 +000014240static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241
14242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014243get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014244 typval_T *argvars;
14245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 int exact;
14247{
14248 char_u *keys;
14249 char_u *which;
14250 char_u buf[NUMBUFLEN];
14251 char_u *keys_buf = NULL;
14252 char_u *rhs;
14253 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014254 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014255 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014256 mapblock_T *mp;
14257 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
14259 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 rettv->v_type = VAR_STRING;
14261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 if (*keys == NUL)
14265 return;
14266
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014267 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014271 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014272 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014273 if (argvars[3].v_type != VAR_UNKNOWN)
14274 get_dict = get_tv_number(&argvars[3]);
14275 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 else
14278 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 if (which == NULL)
14280 return;
14281
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 mode = get_map_mode(&which, 0);
14283
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014284 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014285 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014287
14288 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014290 /* Return a string. */
14291 if (rhs != NULL)
14292 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
Bram Moolenaarbd743252010-10-20 21:23:33 +020014294 }
14295 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14296 {
14297 /* Return a dictionary. */
14298 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14299 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14300 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
Bram Moolenaarbd743252010-10-20 21:23:33 +020014302 dict_add_nr_str(dict, "lhs", 0L, lhs);
14303 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14304 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14305 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14306 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14307 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14308 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014309 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014310 dict_add_nr_str(dict, "mode", 0L, mapmode);
14311
14312 vim_free(lhs);
14313 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 }
14315}
14316
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014317#ifdef FEAT_FLOAT
14318/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014319 * "log()" function
14320 */
14321 static void
14322f_log(argvars, rettv)
14323 typval_T *argvars;
14324 typval_T *rettv;
14325{
14326 float_T f;
14327
14328 rettv->v_type = VAR_FLOAT;
14329 if (get_float_arg(argvars, &f) == OK)
14330 rettv->vval.v_float = log(f);
14331 else
14332 rettv->vval.v_float = 0.0;
14333}
14334
14335/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014336 * "log10()" function
14337 */
14338 static void
14339f_log10(argvars, rettv)
14340 typval_T *argvars;
14341 typval_T *rettv;
14342{
14343 float_T f;
14344
14345 rettv->v_type = VAR_FLOAT;
14346 if (get_float_arg(argvars, &f) == OK)
14347 rettv->vval.v_float = log10(f);
14348 else
14349 rettv->vval.v_float = 0.0;
14350}
14351#endif
14352
Bram Moolenaar1dced572012-04-05 16:54:08 +020014353#ifdef FEAT_LUA
14354/*
14355 * "luaeval()" function
14356 */
14357 static void
14358f_luaeval(argvars, rettv)
14359 typval_T *argvars;
14360 typval_T *rettv;
14361{
14362 char_u *str;
14363 char_u buf[NUMBUFLEN];
14364
14365 str = get_tv_string_buf(&argvars[0], buf);
14366 do_luaeval(str, argvars + 1, rettv);
14367}
14368#endif
14369
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014371 * "map()" function
14372 */
14373 static void
14374f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014375 typval_T *argvars;
14376 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014377{
14378 filter_map(argvars, rettv, TRUE);
14379}
14380
14381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014382 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 */
14384 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 typval_T *argvars;
14387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390}
14391
14392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 */
14395 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014396f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014400 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401}
14402
Bram Moolenaar33570922005-01-25 22:26:29 +000014403static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404
14405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *argvars;
14408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 int type;
14410{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014411 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014412 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014413 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 char_u *pat;
14415 regmatch_T regmatch;
14416 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014417 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 char_u *save_cpo;
14419 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014420 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014421 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014422 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014423 list_T *l = NULL;
14424 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014425 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014426 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427
14428 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14429 save_cpo = p_cpo;
14430 p_cpo = (char_u *)"";
14431
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014432 rettv->vval.v_number = -1;
14433 if (type == 3)
14434 {
14435 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014437 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014438 }
14439 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014441 rettv->v_type = VAR_STRING;
14442 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014445 if (argvars[0].v_type == VAR_LIST)
14446 {
14447 if ((l = argvars[0].vval.v_list) == NULL)
14448 goto theend;
14449 li = l->lv_first;
14450 }
14451 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014452 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014454 len = (long)STRLEN(str);
14455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14458 if (pat == NULL)
14459 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014460
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014461 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014463 int error = FALSE;
14464
14465 start = get_tv_number_chk(&argvars[2], &error);
14466 if (error)
14467 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014468 if (l != NULL)
14469 {
14470 li = list_find(l, start);
14471 if (li == NULL)
14472 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014473 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014474 }
14475 else
14476 {
14477 if (start < 0)
14478 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014479 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014481 /* When "count" argument is there ignore matches before "start",
14482 * otherwise skip part of the string. Differs when pattern is "^"
14483 * or "\<". */
14484 if (argvars[3].v_type != VAR_UNKNOWN)
14485 startcol = start;
14486 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014487 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014488 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014489 len -= start;
14490 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014491 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014492
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014493 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 nth = get_tv_number_chk(&argvars[3], &error);
14495 if (error)
14496 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 }
14498
14499 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14500 if (regmatch.regprog != NULL)
14501 {
14502 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014503
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014504 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014505 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014506 if (l != NULL)
14507 {
14508 if (li == NULL)
14509 {
14510 match = FALSE;
14511 break;
14512 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014513 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014514 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014515 if (str == NULL)
14516 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014517 }
14518
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014519 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014520
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014521 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014522 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014523 if (l == NULL && !match)
14524 break;
14525
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014526 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527 if (l != NULL)
14528 {
14529 li = li->li_next;
14530 ++idx;
14531 }
14532 else
14533 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014534#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014535 startcol = (colnr_T)(regmatch.startp[0]
14536 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014537#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014538 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014539#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014540 if (startcol > (colnr_T)len
14541 || str + startcol <= regmatch.startp[0])
14542 {
14543 match = FALSE;
14544 break;
14545 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014546 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014547 }
14548
14549 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014551 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014553 int i;
14554
14555 /* return list with matched string and submatches */
14556 for (i = 0; i < NSUBEXP; ++i)
14557 {
14558 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014559 {
14560 if (list_append_string(rettv->vval.v_list,
14561 (char_u *)"", 0) == FAIL)
14562 break;
14563 }
14564 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014565 regmatch.startp[i],
14566 (int)(regmatch.endp[i] - regmatch.startp[i]))
14567 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014568 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014569 }
14570 }
14571 else if (type == 2)
14572 {
14573 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014574 if (l != NULL)
14575 copy_tv(&li->li_tv, rettv);
14576 else
14577 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014579 }
14580 else if (l != NULL)
14581 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 else
14583 {
14584 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 (varnumber_T)(regmatch.startp[0] - str);
14587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014590 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 }
14592 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014593 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 }
14595
14596theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014597 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 p_cpo = save_cpo;
14599}
14600
14601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602 * "match()" function
14603 */
14604 static void
14605f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *argvars;
14607 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608{
14609 find_some_match(argvars, rettv, 1);
14610}
14611
14612/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014613 * "matchadd()" function
14614 */
14615 static void
14616f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014617 typval_T *argvars UNUSED;
14618 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014619{
14620#ifdef FEAT_SEARCH_EXTRA
14621 char_u buf[NUMBUFLEN];
14622 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14623 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14624 int prio = 10; /* default priority */
14625 int id = -1;
14626 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014627 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014628
14629 rettv->vval.v_number = -1;
14630
14631 if (grp == NULL || pat == NULL)
14632 return;
14633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014634 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014635 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014636 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014637 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014638 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014639 if (argvars[4].v_type != VAR_UNKNOWN)
14640 {
14641 if (argvars[4].v_type != VAR_DICT)
14642 {
14643 EMSG(_(e_dictreq));
14644 return;
14645 }
14646 if (dict_find(argvars[4].vval.v_dict,
14647 (char_u *)"conceal", -1) != NULL)
14648 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14649 (char_u *)"conceal", FALSE);
14650 }
14651 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014652 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014653 if (error == TRUE)
14654 return;
14655 if (id >= 1 && id <= 3)
14656 {
14657 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14658 return;
14659 }
14660
Bram Moolenaar6561d522015-07-21 15:48:27 +020014661 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14662 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014663#endif
14664}
14665
14666/*
14667 * "matchaddpos()" function
14668 */
14669 static void
14670f_matchaddpos(argvars, rettv)
14671 typval_T *argvars UNUSED;
14672 typval_T *rettv UNUSED;
14673{
14674#ifdef FEAT_SEARCH_EXTRA
14675 char_u buf[NUMBUFLEN];
14676 char_u *group;
14677 int prio = 10;
14678 int id = -1;
14679 int error = FALSE;
14680 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014681 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014682
14683 rettv->vval.v_number = -1;
14684
14685 group = get_tv_string_buf_chk(&argvars[0], buf);
14686 if (group == NULL)
14687 return;
14688
14689 if (argvars[1].v_type != VAR_LIST)
14690 {
14691 EMSG2(_(e_listarg), "matchaddpos()");
14692 return;
14693 }
14694 l = argvars[1].vval.v_list;
14695 if (l == NULL)
14696 return;
14697
14698 if (argvars[2].v_type != VAR_UNKNOWN)
14699 {
14700 prio = get_tv_number_chk(&argvars[2], &error);
14701 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014702 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014703 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014704 if (argvars[4].v_type != VAR_UNKNOWN)
14705 {
14706 if (argvars[4].v_type != VAR_DICT)
14707 {
14708 EMSG(_(e_dictreq));
14709 return;
14710 }
14711 if (dict_find(argvars[4].vval.v_dict,
14712 (char_u *)"conceal", -1) != NULL)
14713 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14714 (char_u *)"conceal", FALSE);
14715 }
14716 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014717 }
14718 if (error == TRUE)
14719 return;
14720
14721 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14722 if (id == 1 || id == 2)
14723 {
14724 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14725 return;
14726 }
14727
Bram Moolenaar6561d522015-07-21 15:48:27 +020014728 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14729 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014730#endif
14731}
14732
14733/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014734 * "matcharg()" function
14735 */
14736 static void
14737f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014738 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014739 typval_T *rettv;
14740{
14741 if (rettv_list_alloc(rettv) == OK)
14742 {
14743#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014744 int id = get_tv_number(&argvars[0]);
14745 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014746
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014747 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014748 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014749 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14750 {
14751 list_append_string(rettv->vval.v_list,
14752 syn_id2name(m->hlg_id), -1);
14753 list_append_string(rettv->vval.v_list, m->pattern, -1);
14754 }
14755 else
14756 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014757 list_append_string(rettv->vval.v_list, NULL, -1);
14758 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014759 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014760 }
14761#endif
14762 }
14763}
14764
14765/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014766 * "matchdelete()" function
14767 */
14768 static void
14769f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014770 typval_T *argvars UNUSED;
14771 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014772{
14773#ifdef FEAT_SEARCH_EXTRA
14774 rettv->vval.v_number = match_delete(curwin,
14775 (int)get_tv_number(&argvars[0]), TRUE);
14776#endif
14777}
14778
14779/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780 * "matchend()" function
14781 */
14782 static void
14783f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 typval_T *argvars;
14785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014786{
14787 find_some_match(argvars, rettv, 0);
14788}
14789
14790/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 * "matchlist()" function
14792 */
14793 static void
14794f_matchlist(argvars, rettv)
14795 typval_T *argvars;
14796 typval_T *rettv;
14797{
14798 find_some_match(argvars, rettv, 3);
14799}
14800
14801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802 * "matchstr()" function
14803 */
14804 static void
14805f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 typval_T *argvars;
14807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014808{
14809 find_some_match(argvars, rettv, 2);
14810}
14811
Bram Moolenaar33570922005-01-25 22:26:29 +000014812static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014813
14814 static void
14815max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014816 typval_T *argvars;
14817 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014818 int domax;
14819{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014820 long n = 0;
14821 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014823
14824 if (argvars[0].v_type == VAR_LIST)
14825 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014826 list_T *l;
14827 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014828
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014829 l = argvars[0].vval.v_list;
14830 if (l != NULL)
14831 {
14832 li = l->lv_first;
14833 if (li != NULL)
14834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014836 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014837 {
14838 li = li->li_next;
14839 if (li == NULL)
14840 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014842 if (domax ? i > n : i < n)
14843 n = i;
14844 }
14845 }
14846 }
14847 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014848 else if (argvars[0].v_type == VAR_DICT)
14849 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014850 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014851 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014852 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014853 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014854
14855 d = argvars[0].vval.v_dict;
14856 if (d != NULL)
14857 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014858 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014859 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014860 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014861 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014862 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014863 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014865 if (first)
14866 {
14867 n = i;
14868 first = FALSE;
14869 }
14870 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014871 n = i;
14872 }
14873 }
14874 }
14875 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014876 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014877 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014878 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014879}
14880
14881/*
14882 * "max()" function
14883 */
14884 static void
14885f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 typval_T *argvars;
14887 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014888{
14889 max_min(argvars, rettv, TRUE);
14890}
14891
14892/*
14893 * "min()" function
14894 */
14895 static void
14896f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014897 typval_T *argvars;
14898 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014899{
14900 max_min(argvars, rettv, FALSE);
14901}
14902
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014903static int mkdir_recurse __ARGS((char_u *dir, int prot));
14904
14905/*
14906 * Create the directory in which "dir" is located, and higher levels when
14907 * needed.
14908 */
14909 static int
14910mkdir_recurse(dir, prot)
14911 char_u *dir;
14912 int prot;
14913{
14914 char_u *p;
14915 char_u *updir;
14916 int r = FAIL;
14917
14918 /* Get end of directory name in "dir".
14919 * We're done when it's "/" or "c:/". */
14920 p = gettail_sep(dir);
14921 if (p <= get_past_head(dir))
14922 return OK;
14923
14924 /* If the directory exists we're done. Otherwise: create it.*/
14925 updir = vim_strnsave(dir, (int)(p - dir));
14926 if (updir == NULL)
14927 return FAIL;
14928 if (mch_isdir(updir))
14929 r = OK;
14930 else if (mkdir_recurse(updir, prot) == OK)
14931 r = vim_mkdir_emsg(updir, prot);
14932 vim_free(updir);
14933 return r;
14934}
14935
14936#ifdef vim_mkdir
14937/*
14938 * "mkdir()" function
14939 */
14940 static void
14941f_mkdir(argvars, rettv)
14942 typval_T *argvars;
14943 typval_T *rettv;
14944{
14945 char_u *dir;
14946 char_u buf[NUMBUFLEN];
14947 int prot = 0755;
14948
14949 rettv->vval.v_number = FAIL;
14950 if (check_restricted() || check_secure())
14951 return;
14952
14953 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014954 if (*dir == NUL)
14955 rettv->vval.v_number = FAIL;
14956 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014957 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014958 if (*gettail(dir) == NUL)
14959 /* remove trailing slashes */
14960 *gettail_sep(dir) = NUL;
14961
14962 if (argvars[1].v_type != VAR_UNKNOWN)
14963 {
14964 if (argvars[2].v_type != VAR_UNKNOWN)
14965 prot = get_tv_number_chk(&argvars[2], NULL);
14966 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14967 mkdir_recurse(dir, prot);
14968 }
14969 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014970 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014971}
14972#endif
14973
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 * "mode()" function
14976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014979 typval_T *argvars;
14980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014982 char_u buf[3];
14983
14984 buf[1] = NUL;
14985 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 if (VIsual_active)
14988 {
14989 if (VIsual_select)
14990 buf[0] = VIsual_mode + 's' - 'v';
14991 else
14992 buf[0] = VIsual_mode;
14993 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014994 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014995 || State == CONFIRM)
14996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014998 if (State == ASKMORE)
14999 buf[1] = 'm';
15000 else if (State == CONFIRM)
15001 buf[1] = '?';
15002 }
15003 else if (State == EXTERNCMD)
15004 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 else if (State & INSERT)
15006 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015007#ifdef FEAT_VREPLACE
15008 if (State & VREPLACE_FLAG)
15009 {
15010 buf[0] = 'R';
15011 buf[1] = 'v';
15012 }
15013 else
15014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 if (State & REPLACE_FLAG)
15016 buf[0] = 'R';
15017 else
15018 buf[0] = 'i';
15019 }
15020 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015023 if (exmode_active)
15024 buf[1] = 'v';
15025 }
15026 else if (exmode_active)
15027 {
15028 buf[0] = 'c';
15029 buf[1] = 'e';
15030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015034 if (finish_op)
15035 buf[1] = 'o';
15036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015038 /* Clear out the minor mode when the argument is not a non-zero number or
15039 * non-empty string. */
15040 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015041 buf[1] = NUL;
15042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 rettv->vval.v_string = vim_strsave(buf);
15044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045}
15046
Bram Moolenaar429fa852013-04-15 12:27:36 +020015047#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015048/*
15049 * "mzeval()" function
15050 */
15051 static void
15052f_mzeval(argvars, rettv)
15053 typval_T *argvars;
15054 typval_T *rettv;
15055{
15056 char_u *str;
15057 char_u buf[NUMBUFLEN];
15058
15059 str = get_tv_string_buf(&argvars[0], buf);
15060 do_mzeval(str, rettv);
15061}
Bram Moolenaar75676462013-01-30 14:55:42 +010015062
15063 void
15064mzscheme_call_vim(name, args, rettv)
15065 char_u *name;
15066 typval_T *args;
15067 typval_T *rettv;
15068{
15069 typval_T argvars[3];
15070
15071 argvars[0].v_type = VAR_STRING;
15072 argvars[0].vval.v_string = name;
15073 copy_tv(args, &argvars[1]);
15074 argvars[2].v_type = VAR_UNKNOWN;
15075 f_call(argvars, rettv);
15076 clear_tv(&argvars[1]);
15077}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015078#endif
15079
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081 * "nextnonblank()" function
15082 */
15083 static void
15084f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 typval_T *argvars;
15086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087{
15088 linenr_T lnum;
15089
15090 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 {
15094 lnum = 0;
15095 break;
15096 }
15097 if (*skipwhite(ml_get(lnum)) != NUL)
15098 break;
15099 }
15100 rettv->vval.v_number = lnum;
15101}
15102
15103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 * "nr2char()" function
15105 */
15106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015107f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 typval_T *argvars;
15109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110{
15111 char_u buf[NUMBUFLEN];
15112
15113#ifdef FEAT_MBYTE
15114 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015115 {
15116 int utf8 = 0;
15117
15118 if (argvars[1].v_type != VAR_UNKNOWN)
15119 utf8 = get_tv_number_chk(&argvars[1], NULL);
15120 if (utf8)
15121 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15122 else
15123 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 else
15126#endif
15127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 buf[1] = NUL;
15130 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131 rettv->v_type = VAR_STRING;
15132 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015133}
15134
15135/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015136 * "or(expr, expr)" function
15137 */
15138 static void
15139f_or(argvars, rettv)
15140 typval_T *argvars;
15141 typval_T *rettv;
15142{
15143 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15144 | get_tv_number_chk(&argvars[1], NULL);
15145}
15146
15147/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015148 * "pathshorten()" function
15149 */
15150 static void
15151f_pathshorten(argvars, rettv)
15152 typval_T *argvars;
15153 typval_T *rettv;
15154{
15155 char_u *p;
15156
15157 rettv->v_type = VAR_STRING;
15158 p = get_tv_string_chk(&argvars[0]);
15159 if (p == NULL)
15160 rettv->vval.v_string = NULL;
15161 else
15162 {
15163 p = vim_strsave(p);
15164 rettv->vval.v_string = p;
15165 if (p != NULL)
15166 shorten_dir(p);
15167 }
15168}
15169
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015170#ifdef FEAT_FLOAT
15171/*
15172 * "pow()" function
15173 */
15174 static void
15175f_pow(argvars, rettv)
15176 typval_T *argvars;
15177 typval_T *rettv;
15178{
15179 float_T fx, fy;
15180
15181 rettv->v_type = VAR_FLOAT;
15182 if (get_float_arg(argvars, &fx) == OK
15183 && get_float_arg(&argvars[1], &fy) == OK)
15184 rettv->vval.v_float = pow(fx, fy);
15185 else
15186 rettv->vval.v_float = 0.0;
15187}
15188#endif
15189
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015190/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191 * "prevnonblank()" function
15192 */
15193 static void
15194f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 typval_T *argvars;
15196 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197{
15198 linenr_T lnum;
15199
15200 lnum = get_tv_lnum(argvars);
15201 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15202 lnum = 0;
15203 else
15204 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15205 --lnum;
15206 rettv->vval.v_number = lnum;
15207}
15208
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015209#ifdef HAVE_STDARG_H
15210/* This dummy va_list is here because:
15211 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15212 * - locally in the function results in a "used before set" warning
15213 * - using va_start() to initialize it gives "function with fixed args" error */
15214static va_list ap;
15215#endif
15216
Bram Moolenaar8c711452005-01-14 21:53:12 +000015217/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015218 * "printf()" function
15219 */
15220 static void
15221f_printf(argvars, rettv)
15222 typval_T *argvars;
15223 typval_T *rettv;
15224{
15225 rettv->v_type = VAR_STRING;
15226 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015227#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015228 {
15229 char_u buf[NUMBUFLEN];
15230 int len;
15231 char_u *s;
15232 int saved_did_emsg = did_emsg;
15233 char *fmt;
15234
15235 /* Get the required length, allocate the buffer and do it for real. */
15236 did_emsg = FALSE;
15237 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015238 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015239 if (!did_emsg)
15240 {
15241 s = alloc(len + 1);
15242 if (s != NULL)
15243 {
15244 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015245 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015246 }
15247 }
15248 did_emsg |= saved_did_emsg;
15249 }
15250#endif
15251}
15252
15253/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254 * "pumvisible()" function
15255 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256 static void
15257f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015258 typval_T *argvars UNUSED;
15259 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015260{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015261#ifdef FEAT_INS_EXPAND
15262 if (pum_visible())
15263 rettv->vval.v_number = 1;
15264#endif
15265}
15266
Bram Moolenaardb913952012-06-29 12:54:53 +020015267#ifdef FEAT_PYTHON3
15268/*
15269 * "py3eval()" function
15270 */
15271 static void
15272f_py3eval(argvars, rettv)
15273 typval_T *argvars;
15274 typval_T *rettv;
15275{
15276 char_u *str;
15277 char_u buf[NUMBUFLEN];
15278
15279 str = get_tv_string_buf(&argvars[0], buf);
15280 do_py3eval(str, rettv);
15281}
15282#endif
15283
15284#ifdef FEAT_PYTHON
15285/*
15286 * "pyeval()" function
15287 */
15288 static void
15289f_pyeval(argvars, rettv)
15290 typval_T *argvars;
15291 typval_T *rettv;
15292{
15293 char_u *str;
15294 char_u buf[NUMBUFLEN];
15295
15296 str = get_tv_string_buf(&argvars[0], buf);
15297 do_pyeval(str, rettv);
15298}
15299#endif
15300
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015301/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015302 * "range()" function
15303 */
15304 static void
15305f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 typval_T *argvars;
15307 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015308{
15309 long start;
15310 long end;
15311 long stride = 1;
15312 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316 if (argvars[1].v_type == VAR_UNKNOWN)
15317 {
15318 end = start - 1;
15319 start = 0;
15320 }
15321 else
15322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015324 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 }
15327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 if (error)
15329 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015331 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015332 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015333 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015334 else
15335 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015336 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015337 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015338 if (list_append_number(rettv->vval.v_list,
15339 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015340 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015341 }
15342}
15343
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015344/*
15345 * "readfile()" function
15346 */
15347 static void
15348f_readfile(argvars, rettv)
15349 typval_T *argvars;
15350 typval_T *rettv;
15351{
15352 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015353 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015354 char_u *fname;
15355 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015356 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15357 int io_size = sizeof(buf);
15358 int readlen; /* size of last fread() */
15359 char_u *prev = NULL; /* previously read bytes, if any */
15360 long prevlen = 0; /* length of data in prev */
15361 long prevsize = 0; /* size of prev buffer */
15362 long maxline = MAXLNUM;
15363 long cnt = 0;
15364 char_u *p; /* position in buf */
15365 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015367 if (argvars[1].v_type != VAR_UNKNOWN)
15368 {
15369 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15370 binary = TRUE;
15371 if (argvars[2].v_type != VAR_UNKNOWN)
15372 maxline = get_tv_number(&argvars[2]);
15373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015374
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015375 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015376 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015377
15378 /* Always open the file in binary mode, library functions have a mind of
15379 * their own about CR-LF conversion. */
15380 fname = get_tv_string(&argvars[0]);
15381 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15382 {
15383 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15384 return;
15385 }
15386
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015387 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015388 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015389 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015390
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015391 /* This for loop processes what was read, but is also entered at end
15392 * of file so that either:
15393 * - an incomplete line gets written
15394 * - a "binary" file gets an empty line at the end if it ends in a
15395 * newline. */
15396 for (p = buf, start = buf;
15397 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15398 ++p)
15399 {
15400 if (*p == '\n' || readlen <= 0)
15401 {
15402 listitem_T *li;
15403 char_u *s = NULL;
15404 long_u len = p - start;
15405
15406 /* Finished a line. Remove CRs before NL. */
15407 if (readlen > 0 && !binary)
15408 {
15409 while (len > 0 && start[len - 1] == '\r')
15410 --len;
15411 /* removal may cross back to the "prev" string */
15412 if (len == 0)
15413 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15414 --prevlen;
15415 }
15416 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015417 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015418 else
15419 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015420 /* Change "prev" buffer to be the right size. This way
15421 * the bytes are only copied once, and very long lines are
15422 * allocated only once. */
15423 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015424 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015425 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015426 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015427 prev = NULL; /* the list will own the string */
15428 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015429 }
15430 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015431 if (s == NULL)
15432 {
15433 do_outofmem_msg((long_u) prevlen + len + 1);
15434 failed = TRUE;
15435 break;
15436 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015437
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015438 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015439 {
15440 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015441 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015442 break;
15443 }
15444 li->li_tv.v_type = VAR_STRING;
15445 li->li_tv.v_lock = 0;
15446 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015447 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015448
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015449 start = p + 1; /* step over newline */
15450 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015451 break;
15452 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015453 else if (*p == NUL)
15454 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015455#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15457 * when finding the BF and check the previous two bytes. */
15458 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015459 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015460 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15461 * + 1, these may be in the "prev" string. */
15462 char_u back1 = p >= buf + 1 ? p[-1]
15463 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15464 char_u back2 = p >= buf + 2 ? p[-2]
15465 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15466 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015467
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015468 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015469 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015470 char_u *dest = p - 2;
15471
15472 /* Usually a BOM is at the beginning of a file, and so at
15473 * the beginning of a line; then we can just step over it.
15474 */
15475 if (start == dest)
15476 start = p + 1;
15477 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015478 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015479 /* have to shuffle buf to close gap */
15480 int adjust_prevlen = 0;
15481
15482 if (dest < buf)
15483 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015484 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015485 dest = buf;
15486 }
15487 if (readlen > p - buf + 1)
15488 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15489 readlen -= 3 - adjust_prevlen;
15490 prevlen -= adjust_prevlen;
15491 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015492 }
15493 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015494 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015495#endif
15496 } /* for */
15497
15498 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15499 break;
15500 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015501 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015502 /* There's part of a line in buf, store it in "prev". */
15503 if (p - start + prevlen >= prevsize)
15504 {
15505 /* need bigger "prev" buffer */
15506 char_u *newprev;
15507
15508 /* A common use case is ordinary text files and "prev" gets a
15509 * fragment of a line, so the first allocation is made
15510 * small, to avoid repeatedly 'allocing' large and
15511 * 'reallocing' small. */
15512 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015513 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 else
15515 {
15516 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015517 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015518 prevsize = grow50pc > growmin ? grow50pc : growmin;
15519 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015520 newprev = prev == NULL ? alloc(prevsize)
15521 : vim_realloc(prev, prevsize);
15522 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015523 {
15524 do_outofmem_msg((long_u)prevsize);
15525 failed = TRUE;
15526 break;
15527 }
15528 prev = newprev;
15529 }
15530 /* Add the line part to end of "prev". */
15531 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015532 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015533 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015534 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015535
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015536 /*
15537 * For a negative line count use only the lines at the end of the file,
15538 * free the rest.
15539 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015540 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015541 while (cnt > -maxline)
15542 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015543 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015544 --cnt;
15545 }
15546
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015547 if (failed)
15548 {
15549 list_free(rettv->vval.v_list, TRUE);
15550 /* readfile doc says an empty list is returned on error */
15551 rettv->vval.v_list = list_alloc();
15552 }
15553
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015554 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015555 fclose(fd);
15556}
15557
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015558#if defined(FEAT_RELTIME)
15559static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15560
15561/*
15562 * Convert a List to proftime_T.
15563 * Return FAIL when there is something wrong.
15564 */
15565 static int
15566list2proftime(arg, tm)
15567 typval_T *arg;
15568 proftime_T *tm;
15569{
15570 long n1, n2;
15571 int error = FALSE;
15572
15573 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15574 || arg->vval.v_list->lv_len != 2)
15575 return FAIL;
15576 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15577 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15578# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015579 tm->HighPart = n1;
15580 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015581# else
15582 tm->tv_sec = n1;
15583 tm->tv_usec = n2;
15584# endif
15585 return error ? FAIL : OK;
15586}
15587#endif /* FEAT_RELTIME */
15588
15589/*
15590 * "reltime()" function
15591 */
15592 static void
15593f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015594 typval_T *argvars UNUSED;
15595 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015596{
15597#ifdef FEAT_RELTIME
15598 proftime_T res;
15599 proftime_T start;
15600
15601 if (argvars[0].v_type == VAR_UNKNOWN)
15602 {
15603 /* No arguments: get current time. */
15604 profile_start(&res);
15605 }
15606 else if (argvars[1].v_type == VAR_UNKNOWN)
15607 {
15608 if (list2proftime(&argvars[0], &res) == FAIL)
15609 return;
15610 profile_end(&res);
15611 }
15612 else
15613 {
15614 /* Two arguments: compute the difference. */
15615 if (list2proftime(&argvars[0], &start) == FAIL
15616 || list2proftime(&argvars[1], &res) == FAIL)
15617 return;
15618 profile_sub(&res, &start);
15619 }
15620
15621 if (rettv_list_alloc(rettv) == OK)
15622 {
15623 long n1, n2;
15624
15625# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015626 n1 = res.HighPart;
15627 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015628# else
15629 n1 = res.tv_sec;
15630 n2 = res.tv_usec;
15631# endif
15632 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15633 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15634 }
15635#endif
15636}
15637
15638/*
15639 * "reltimestr()" function
15640 */
15641 static void
15642f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015643 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015644 typval_T *rettv;
15645{
15646#ifdef FEAT_RELTIME
15647 proftime_T tm;
15648#endif
15649
15650 rettv->v_type = VAR_STRING;
15651 rettv->vval.v_string = NULL;
15652#ifdef FEAT_RELTIME
15653 if (list2proftime(&argvars[0], &tm) == OK)
15654 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15655#endif
15656}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15659static void make_connection __ARGS((void));
15660static int check_connection __ARGS((void));
15661
15662 static void
15663make_connection()
15664{
15665 if (X_DISPLAY == NULL
15666# ifdef FEAT_GUI
15667 && !gui.in_use
15668# endif
15669 )
15670 {
15671 x_force_connect = TRUE;
15672 setup_term_clip();
15673 x_force_connect = FALSE;
15674 }
15675}
15676
15677 static int
15678check_connection()
15679{
15680 make_connection();
15681 if (X_DISPLAY == NULL)
15682 {
15683 EMSG(_("E240: No connection to Vim server"));
15684 return FAIL;
15685 }
15686 return OK;
15687}
15688#endif
15689
15690#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015691static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692
15693 static void
15694remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 typval_T *argvars;
15696 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697 int expr;
15698{
15699 char_u *server_name;
15700 char_u *keys;
15701 char_u *r = NULL;
15702 char_u buf[NUMBUFLEN];
15703# ifdef WIN32
15704 HWND w;
15705# else
15706 Window w;
15707# endif
15708
15709 if (check_restricted() || check_secure())
15710 return;
15711
15712# ifdef FEAT_X11
15713 if (check_connection() == FAIL)
15714 return;
15715# endif
15716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015717 server_name = get_tv_string_chk(&argvars[0]);
15718 if (server_name == NULL)
15719 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720 keys = get_tv_string_buf(&argvars[1], buf);
15721# ifdef WIN32
15722 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15723# else
15724 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15725 < 0)
15726# endif
15727 {
15728 if (r != NULL)
15729 EMSG(r); /* sending worked but evaluation failed */
15730 else
15731 EMSG2(_("E241: Unable to send to %s"), server_name);
15732 return;
15733 }
15734
15735 rettv->vval.v_string = r;
15736
15737 if (argvars[2].v_type != VAR_UNKNOWN)
15738 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015739 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015740 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015741 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015742
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015743 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015744 v.di_tv.v_type = VAR_STRING;
15745 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015746 idvar = get_tv_string_chk(&argvars[2]);
15747 if (idvar != NULL)
15748 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015750 }
15751}
15752#endif
15753
15754/*
15755 * "remote_expr()" function
15756 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757 static void
15758f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015760 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761{
15762 rettv->v_type = VAR_STRING;
15763 rettv->vval.v_string = NULL;
15764#ifdef FEAT_CLIENTSERVER
15765 remote_common(argvars, rettv, TRUE);
15766#endif
15767}
15768
15769/*
15770 * "remote_foreground()" function
15771 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 static void
15773f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015774 typval_T *argvars UNUSED;
15775 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777#ifdef FEAT_CLIENTSERVER
15778# ifdef WIN32
15779 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 {
15781 char_u *server_name = get_tv_string_chk(&argvars[0]);
15782
15783 if (server_name != NULL)
15784 serverForeground(server_name);
15785 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015786# else
15787 /* Send a foreground() expression to the server. */
15788 argvars[1].v_type = VAR_STRING;
15789 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15790 argvars[2].v_type = VAR_UNKNOWN;
15791 remote_common(argvars, rettv, TRUE);
15792 vim_free(argvars[1].vval.v_string);
15793# endif
15794#endif
15795}
15796
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797 static void
15798f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801{
15802#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015803 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 char_u *s = NULL;
15805# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015806 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015808 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809
15810 if (check_restricted() || check_secure())
15811 {
15812 rettv->vval.v_number = -1;
15813 return;
15814 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 serverid = get_tv_string_chk(&argvars[0]);
15816 if (serverid == NULL)
15817 {
15818 rettv->vval.v_number = -1;
15819 return; /* type error; errmsg already given */
15820 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015822 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015823 if (n == 0)
15824 rettv->vval.v_number = -1;
15825 else
15826 {
15827 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15828 rettv->vval.v_number = (s != NULL);
15829 }
15830# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015831 if (check_connection() == FAIL)
15832 return;
15833
15834 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836# endif
15837
15838 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015840 char_u *retvar;
15841
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 v.di_tv.v_type = VAR_STRING;
15843 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015844 retvar = get_tv_string_chk(&argvars[1]);
15845 if (retvar != NULL)
15846 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848 }
15849#else
15850 rettv->vval.v_number = -1;
15851#endif
15852}
15853
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854 static void
15855f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015858{
15859 char_u *r = NULL;
15860
15861#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862 char_u *serverid = get_tv_string_chk(&argvars[0]);
15863
15864 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015865 {
15866# ifdef WIN32
15867 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015868 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015870 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015871 if (n != 0)
15872 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15873 if (r == NULL)
15874# else
15875 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015876 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877# endif
15878 EMSG(_("E277: Unable to read a server reply"));
15879 }
15880#endif
15881 rettv->v_type = VAR_STRING;
15882 rettv->vval.v_string = r;
15883}
15884
15885/*
15886 * "remote_send()" function
15887 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888 static void
15889f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015890 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892{
15893 rettv->v_type = VAR_STRING;
15894 rettv->vval.v_string = NULL;
15895#ifdef FEAT_CLIENTSERVER
15896 remote_common(argvars, rettv, FALSE);
15897#endif
15898}
15899
15900/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015901 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015902 */
15903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015905 typval_T *argvars;
15906 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907{
Bram Moolenaar33570922005-01-25 22:26:29 +000015908 list_T *l;
15909 listitem_T *item, *item2;
15910 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015911 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015912 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015913 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015914 dict_T *d;
15915 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015916 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917
Bram Moolenaar8c711452005-01-14 21:53:12 +000015918 if (argvars[0].v_type == VAR_DICT)
15919 {
15920 if (argvars[2].v_type != VAR_UNKNOWN)
15921 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015922 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015923 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 key = get_tv_string_chk(&argvars[1]);
15926 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015928 di = dict_find(d, key, -1);
15929 if (di == NULL)
15930 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015931 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15932 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015933 {
15934 *rettv = di->di_tv;
15935 init_tv(&di->di_tv);
15936 dictitem_remove(d, di);
15937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015938 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015939 }
15940 }
15941 else if (argvars[0].v_type != VAR_LIST)
15942 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015943 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015944 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015946 int error = FALSE;
15947
15948 idx = get_tv_number_chk(&argvars[1], &error);
15949 if (error)
15950 ; /* type error: do nothing, errmsg already given */
15951 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015952 EMSGN(_(e_listidx), idx);
15953 else
15954 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955 if (argvars[2].v_type == VAR_UNKNOWN)
15956 {
15957 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015958 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015959 *rettv = item->li_tv;
15960 vim_free(item);
15961 }
15962 else
15963 {
15964 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015965 end = get_tv_number_chk(&argvars[2], &error);
15966 if (error)
15967 ; /* type error: do nothing */
15968 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015969 EMSGN(_(e_listidx), end);
15970 else
15971 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015972 int cnt = 0;
15973
15974 for (li = item; li != NULL; li = li->li_next)
15975 {
15976 ++cnt;
15977 if (li == item2)
15978 break;
15979 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015980 if (li == NULL) /* didn't find "item2" after "item" */
15981 EMSG(_(e_invrange));
15982 else
15983 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015984 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015985 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015986 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015987 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015988 l->lv_first = item;
15989 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015990 item->li_prev = NULL;
15991 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015992 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015993 }
15994 }
15995 }
15996 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015997 }
15998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999}
16000
16001/*
16002 * "rename({from}, {to})" function
16003 */
16004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016006 typval_T *argvars;
16007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008{
16009 char_u buf[NUMBUFLEN];
16010
16011 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16015 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016}
16017
16018/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016019 * "repeat()" function
16020 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016021 static void
16022f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 typval_T *argvars;
16024 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016025{
16026 char_u *p;
16027 int n;
16028 int slen;
16029 int len;
16030 char_u *r;
16031 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016032
16033 n = get_tv_number(&argvars[1]);
16034 if (argvars[0].v_type == VAR_LIST)
16035 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016036 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016037 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016038 if (list_extend(rettv->vval.v_list,
16039 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016040 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016041 }
16042 else
16043 {
16044 p = get_tv_string(&argvars[0]);
16045 rettv->v_type = VAR_STRING;
16046 rettv->vval.v_string = NULL;
16047
16048 slen = (int)STRLEN(p);
16049 len = slen * n;
16050 if (len <= 0)
16051 return;
16052
16053 r = alloc(len + 1);
16054 if (r != NULL)
16055 {
16056 for (i = 0; i < n; i++)
16057 mch_memmove(r + i * slen, p, (size_t)slen);
16058 r[len] = NUL;
16059 }
16060
16061 rettv->vval.v_string = r;
16062 }
16063}
16064
16065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 * "resolve()" function
16067 */
16068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016069f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016070 typval_T *argvars;
16071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072{
16073 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016074#ifdef HAVE_READLINK
16075 char_u *buf = NULL;
16076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016078 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079#ifdef FEAT_SHORTCUT
16080 {
16081 char_u *v = NULL;
16082
16083 v = mch_resolve_shortcut(p);
16084 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016087 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 }
16089#else
16090# ifdef HAVE_READLINK
16091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 char_u *cpy;
16093 int len;
16094 char_u *remain = NULL;
16095 char_u *q;
16096 int is_relative_to_current = FALSE;
16097 int has_trailing_pathsep = FALSE;
16098 int limit = 100;
16099
16100 p = vim_strsave(p);
16101
16102 if (p[0] == '.' && (vim_ispathsep(p[1])
16103 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16104 is_relative_to_current = TRUE;
16105
16106 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016107 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016110 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112
16113 q = getnextcomp(p);
16114 if (*q != NUL)
16115 {
16116 /* Separate the first path component in "p", and keep the
16117 * remainder (beginning with the path separator). */
16118 remain = vim_strsave(q - 1);
16119 q[-1] = NUL;
16120 }
16121
Bram Moolenaard9462e32011-04-11 21:35:11 +020016122 buf = alloc(MAXPATHL + 1);
16123 if (buf == NULL)
16124 goto fail;
16125
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 for (;;)
16127 {
16128 for (;;)
16129 {
16130 len = readlink((char *)p, (char *)buf, MAXPATHL);
16131 if (len <= 0)
16132 break;
16133 buf[len] = NUL;
16134
16135 if (limit-- == 0)
16136 {
16137 vim_free(p);
16138 vim_free(remain);
16139 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141 goto fail;
16142 }
16143
16144 /* Ensure that the result will have a trailing path separator
16145 * if the argument has one. */
16146 if (remain == NULL && has_trailing_pathsep)
16147 add_pathsep(buf);
16148
16149 /* Separate the first path component in the link value and
16150 * concatenate the remainders. */
16151 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16152 if (*q != NUL)
16153 {
16154 if (remain == NULL)
16155 remain = vim_strsave(q - 1);
16156 else
16157 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016158 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 if (cpy != NULL)
16160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 vim_free(remain);
16162 remain = cpy;
16163 }
16164 }
16165 q[-1] = NUL;
16166 }
16167
16168 q = gettail(p);
16169 if (q > p && *q == NUL)
16170 {
16171 /* Ignore trailing path separator. */
16172 q[-1] = NUL;
16173 q = gettail(p);
16174 }
16175 if (q > p && !mch_isFullName(buf))
16176 {
16177 /* symlink is relative to directory of argument */
16178 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16179 if (cpy != NULL)
16180 {
16181 STRCPY(cpy, p);
16182 STRCPY(gettail(cpy), buf);
16183 vim_free(p);
16184 p = cpy;
16185 }
16186 }
16187 else
16188 {
16189 vim_free(p);
16190 p = vim_strsave(buf);
16191 }
16192 }
16193
16194 if (remain == NULL)
16195 break;
16196
16197 /* Append the first path component of "remain" to "p". */
16198 q = getnextcomp(remain + 1);
16199 len = q - remain - (*q != NUL);
16200 cpy = vim_strnsave(p, STRLEN(p) + len);
16201 if (cpy != NULL)
16202 {
16203 STRNCAT(cpy, remain, len);
16204 vim_free(p);
16205 p = cpy;
16206 }
16207 /* Shorten "remain". */
16208 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016209 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 else
16211 {
16212 vim_free(remain);
16213 remain = NULL;
16214 }
16215 }
16216
16217 /* If the result is a relative path name, make it explicitly relative to
16218 * the current directory if and only if the argument had this form. */
16219 if (!vim_ispathsep(*p))
16220 {
16221 if (is_relative_to_current
16222 && *p != NUL
16223 && !(p[0] == '.'
16224 && (p[1] == NUL
16225 || vim_ispathsep(p[1])
16226 || (p[1] == '.'
16227 && (p[2] == NUL
16228 || vim_ispathsep(p[2]))))))
16229 {
16230 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016231 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 if (cpy != NULL)
16233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 vim_free(p);
16235 p = cpy;
16236 }
16237 }
16238 else if (!is_relative_to_current)
16239 {
16240 /* Strip leading "./". */
16241 q = p;
16242 while (q[0] == '.' && vim_ispathsep(q[1]))
16243 q += 2;
16244 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016245 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 }
16247 }
16248
16249 /* Ensure that the result will have no trailing path separator
16250 * if the argument had none. But keep "/" or "//". */
16251 if (!has_trailing_pathsep)
16252 {
16253 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016254 if (after_pathsep(p, q))
16255 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 }
16257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016258 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 }
16260# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262# endif
16263#endif
16264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016265 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266
16267#ifdef HAVE_READLINK
16268fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016269 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016271 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272}
16273
16274/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 */
16277 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016278f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 typval_T *argvars;
16280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281{
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 list_T *l;
16283 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 if (argvars[0].v_type != VAR_LIST)
16286 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016287 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016288 && !tv_check_lock(l->lv_lock,
16289 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 {
16291 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016292 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016293 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016294 while (li != NULL)
16295 {
16296 ni = li->li_prev;
16297 list_append(l, li);
16298 li = ni;
16299 }
16300 rettv->vval.v_list = l;
16301 rettv->v_type = VAR_LIST;
16302 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016303 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305}
16306
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016307#define SP_NOMOVE 0x01 /* don't move cursor */
16308#define SP_REPEAT 0x02 /* repeat to find outer pair */
16309#define SP_RETCOUNT 0x04 /* return matchcount */
16310#define SP_SETPCMARK 0x08 /* set previous context mark */
16311#define SP_START 0x10 /* accept match at start position */
16312#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16313#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016314
Bram Moolenaar33570922005-01-25 22:26:29 +000016315static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316
16317/*
16318 * Get flags for a search function.
16319 * Possibly sets "p_ws".
16320 * Returns BACKWARD, FORWARD or zero (for an error).
16321 */
16322 static int
16323get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016324 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 int *flagsp;
16326{
16327 int dir = FORWARD;
16328 char_u *flags;
16329 char_u nbuf[NUMBUFLEN];
16330 int mask;
16331
16332 if (varp->v_type != VAR_UNKNOWN)
16333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 flags = get_tv_string_buf_chk(varp, nbuf);
16335 if (flags == NULL)
16336 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 while (*flags != NUL)
16338 {
16339 switch (*flags)
16340 {
16341 case 'b': dir = BACKWARD; break;
16342 case 'w': p_ws = TRUE; break;
16343 case 'W': p_ws = FALSE; break;
16344 default: mask = 0;
16345 if (flagsp != NULL)
16346 switch (*flags)
16347 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016348 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016349 case 'e': mask = SP_END; break;
16350 case 'm': mask = SP_RETCOUNT; break;
16351 case 'n': mask = SP_NOMOVE; break;
16352 case 'p': mask = SP_SUBPAT; break;
16353 case 'r': mask = SP_REPEAT; break;
16354 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355 }
16356 if (mask == 0)
16357 {
16358 EMSG2(_(e_invarg2), flags);
16359 dir = 0;
16360 }
16361 else
16362 *flagsp |= mask;
16363 }
16364 if (dir == 0)
16365 break;
16366 ++flags;
16367 }
16368 }
16369 return dir;
16370}
16371
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016373 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016375 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016376search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016378 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016379 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016381 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 char_u *pat;
16383 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016384 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 int save_p_ws = p_ws;
16386 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016388 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016389 proftime_T tm;
16390#ifdef FEAT_RELTIME
16391 long time_limit = 0;
16392#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016393 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016394 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016397 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016398 if (dir == 0)
16399 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016400 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016401 if (flags & SP_START)
16402 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016403 if (flags & SP_END)
16404 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016405
Bram Moolenaar76929292008-01-06 19:07:36 +000016406 /* Optional arguments: line number to stop searching and timeout. */
16407 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016408 {
16409 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16410 if (lnum_stop < 0)
16411 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016412#ifdef FEAT_RELTIME
16413 if (argvars[3].v_type != VAR_UNKNOWN)
16414 {
16415 time_limit = get_tv_number_chk(&argvars[3], NULL);
16416 if (time_limit < 0)
16417 goto theend;
16418 }
16419#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016420 }
16421
Bram Moolenaar76929292008-01-06 19:07:36 +000016422#ifdef FEAT_RELTIME
16423 /* Set the time limit, if there is one. */
16424 profile_setlimit(time_limit, &tm);
16425#endif
16426
Bram Moolenaar231334e2005-07-25 20:46:57 +000016427 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016428 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016429 * Check to make sure only those flags are set.
16430 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16431 * flags cannot be set. Check for that condition also.
16432 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016433 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016434 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016436 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016437 goto theend;
16438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016440 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016441 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016442 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016443 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016445 if (flags & SP_SUBPAT)
16446 retval = subpatnum;
16447 else
16448 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016449 if (flags & SP_SETPCMARK)
16450 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016452 if (match_pos != NULL)
16453 {
16454 /* Store the match cursor position */
16455 match_pos->lnum = pos.lnum;
16456 match_pos->col = pos.col + 1;
16457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 /* "/$" will put the cursor after the end of the line, may need to
16459 * correct that here */
16460 check_cursor();
16461 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016462
16463 /* If 'n' flag is used: restore cursor position. */
16464 if (flags & SP_NOMOVE)
16465 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016466 else
16467 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016468theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016470
16471 return retval;
16472}
16473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016474#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016475
16476/*
16477 * round() is not in C90, use ceil() or floor() instead.
16478 */
16479 float_T
16480vim_round(f)
16481 float_T f;
16482{
16483 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16484}
16485
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016486/*
16487 * "round({float})" function
16488 */
16489 static void
16490f_round(argvars, rettv)
16491 typval_T *argvars;
16492 typval_T *rettv;
16493{
16494 float_T f;
16495
16496 rettv->v_type = VAR_FLOAT;
16497 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016498 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016499 else
16500 rettv->vval.v_float = 0.0;
16501}
16502#endif
16503
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016504/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016505 * "screenattr()" function
16506 */
16507 static void
16508f_screenattr(argvars, rettv)
16509 typval_T *argvars UNUSED;
16510 typval_T *rettv;
16511{
16512 int row;
16513 int col;
16514 int c;
16515
16516 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16517 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16518 if (row < 0 || row >= screen_Rows
16519 || col < 0 || col >= screen_Columns)
16520 c = -1;
16521 else
16522 c = ScreenAttrs[LineOffset[row] + col];
16523 rettv->vval.v_number = c;
16524}
16525
16526/*
16527 * "screenchar()" function
16528 */
16529 static void
16530f_screenchar(argvars, rettv)
16531 typval_T *argvars UNUSED;
16532 typval_T *rettv;
16533{
16534 int row;
16535 int col;
16536 int off;
16537 int c;
16538
16539 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16540 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16541 if (row < 0 || row >= screen_Rows
16542 || col < 0 || col >= screen_Columns)
16543 c = -1;
16544 else
16545 {
16546 off = LineOffset[row] + col;
16547#ifdef FEAT_MBYTE
16548 if (enc_utf8 && ScreenLinesUC[off] != 0)
16549 c = ScreenLinesUC[off];
16550 else
16551#endif
16552 c = ScreenLines[off];
16553 }
16554 rettv->vval.v_number = c;
16555}
16556
16557/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016558 * "screencol()" function
16559 *
16560 * First column is 1 to be consistent with virtcol().
16561 */
16562 static void
16563f_screencol(argvars, rettv)
16564 typval_T *argvars UNUSED;
16565 typval_T *rettv;
16566{
16567 rettv->vval.v_number = screen_screencol() + 1;
16568}
16569
16570/*
16571 * "screenrow()" function
16572 */
16573 static void
16574f_screenrow(argvars, rettv)
16575 typval_T *argvars UNUSED;
16576 typval_T *rettv;
16577{
16578 rettv->vval.v_number = screen_screenrow() + 1;
16579}
16580
16581/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016582 * "search()" function
16583 */
16584 static void
16585f_search(argvars, rettv)
16586 typval_T *argvars;
16587 typval_T *rettv;
16588{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016589 int flags = 0;
16590
16591 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592}
16593
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016595 * "searchdecl()" function
16596 */
16597 static void
16598f_searchdecl(argvars, rettv)
16599 typval_T *argvars;
16600 typval_T *rettv;
16601{
16602 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016603 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016604 int error = FALSE;
16605 char_u *name;
16606
16607 rettv->vval.v_number = 1; /* default: FAIL */
16608
16609 name = get_tv_string_chk(&argvars[0]);
16610 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016611 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016612 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016613 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16614 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16615 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016616 if (!error && name != NULL)
16617 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016618 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016619}
16620
16621/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016622 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016624 static int
16625searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016627 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628{
16629 char_u *spat, *mpat, *epat;
16630 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 int dir;
16633 int flags = 0;
16634 char_u nbuf1[NUMBUFLEN];
16635 char_u nbuf2[NUMBUFLEN];
16636 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016637 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016638 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016639 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016642 spat = get_tv_string_chk(&argvars[0]);
16643 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16644 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16645 if (spat == NULL || mpat == NULL || epat == NULL)
16646 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 /* Handle the optional fourth argument: flags */
16649 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016650 if (dir == 0)
16651 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016652
16653 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016654 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16655 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016656 if ((flags & (SP_END | SP_SUBPAT)) != 0
16657 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016658 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016659 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016660 goto theend;
16661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016663 /* Using 'r' implies 'W', otherwise it doesn't work. */
16664 if (flags & SP_REPEAT)
16665 p_ws = FALSE;
16666
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016667 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016668 if (argvars[3].v_type == VAR_UNKNOWN
16669 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 skip = (char_u *)"";
16671 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016673 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016674 if (argvars[5].v_type != VAR_UNKNOWN)
16675 {
16676 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16677 if (lnum_stop < 0)
16678 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016679#ifdef FEAT_RELTIME
16680 if (argvars[6].v_type != VAR_UNKNOWN)
16681 {
16682 time_limit = get_tv_number_chk(&argvars[6], NULL);
16683 if (time_limit < 0)
16684 goto theend;
16685 }
16686#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016687 }
16688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016689 if (skip == NULL)
16690 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016692 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016693 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016694
16695theend:
16696 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016697
16698 return retval;
16699}
16700
16701/*
16702 * "searchpair()" function
16703 */
16704 static void
16705f_searchpair(argvars, rettv)
16706 typval_T *argvars;
16707 typval_T *rettv;
16708{
16709 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16710}
16711
16712/*
16713 * "searchpairpos()" function
16714 */
16715 static void
16716f_searchpairpos(argvars, rettv)
16717 typval_T *argvars;
16718 typval_T *rettv;
16719{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016720 pos_T match_pos;
16721 int lnum = 0;
16722 int col = 0;
16723
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016724 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016725 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016726
16727 if (searchpair_cmn(argvars, &match_pos) > 0)
16728 {
16729 lnum = match_pos.lnum;
16730 col = match_pos.col;
16731 }
16732
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016733 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16734 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016735}
16736
16737/*
16738 * Search for a start/middle/end thing.
16739 * Used by searchpair(), see its documentation for the details.
16740 * Returns 0 or -1 for no match,
16741 */
16742 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016743do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16744 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016745 char_u *spat; /* start pattern */
16746 char_u *mpat; /* middle pattern */
16747 char_u *epat; /* end pattern */
16748 int dir; /* BACKWARD or FORWARD */
16749 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016750 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016751 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016753 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016754{
16755 char_u *save_cpo;
16756 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16757 long retval = 0;
16758 pos_T pos;
16759 pos_T firstpos;
16760 pos_T foundpos;
16761 pos_T save_cursor;
16762 pos_T save_pos;
16763 int n;
16764 int r;
16765 int nest = 1;
16766 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016767 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016768 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016769
16770 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16771 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016772 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016773
Bram Moolenaar76929292008-01-06 19:07:36 +000016774#ifdef FEAT_RELTIME
16775 /* Set the time limit, if there is one. */
16776 profile_setlimit(time_limit, &tm);
16777#endif
16778
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016779 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16780 * start/middle/end (pat3, for the top pair). */
16781 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16782 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16783 if (pat2 == NULL || pat3 == NULL)
16784 goto theend;
16785 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16786 if (*mpat == NUL)
16787 STRCPY(pat3, pat2);
16788 else
16789 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16790 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016791 if (flags & SP_START)
16792 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016793
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 save_cursor = curwin->w_cursor;
16795 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016796 clearpos(&firstpos);
16797 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 pat = pat3;
16799 for (;;)
16800 {
16801 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016802 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16804 /* didn't find it or found the first match again: FAIL */
16805 break;
16806
16807 if (firstpos.lnum == 0)
16808 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016809 if (equalpos(pos, foundpos))
16810 {
16811 /* Found the same position again. Can happen with a pattern that
16812 * has "\zs" at the end and searching backwards. Advance one
16813 * character and try again. */
16814 if (dir == BACKWARD)
16815 decl(&pos);
16816 else
16817 incl(&pos);
16818 }
16819 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016821 /* clear the start flag to avoid getting stuck here */
16822 options &= ~SEARCH_START;
16823
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 /* If the skip pattern matches, ignore this match. */
16825 if (*skip != NUL)
16826 {
16827 save_pos = curwin->w_cursor;
16828 curwin->w_cursor = pos;
16829 r = eval_to_bool(skip, &err, NULL, FALSE);
16830 curwin->w_cursor = save_pos;
16831 if (err)
16832 {
16833 /* Evaluating {skip} caused an error, break here. */
16834 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016835 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 break;
16837 }
16838 if (r)
16839 continue;
16840 }
16841
16842 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16843 {
16844 /* Found end when searching backwards or start when searching
16845 * forward: nested pair. */
16846 ++nest;
16847 pat = pat2; /* nested, don't search for middle */
16848 }
16849 else
16850 {
16851 /* Found end when searching forward or start when searching
16852 * backward: end of (nested) pair; or found middle in outer pair. */
16853 if (--nest == 1)
16854 pat = pat3; /* outer level, search for middle */
16855 }
16856
16857 if (nest == 0)
16858 {
16859 /* Found the match: return matchcount or line number. */
16860 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016861 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016863 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016864 if (flags & SP_SETPCMARK)
16865 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 curwin->w_cursor = pos;
16867 if (!(flags & SP_REPEAT))
16868 break;
16869 nest = 1; /* search for next unmatched */
16870 }
16871 }
16872
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016873 if (match_pos != NULL)
16874 {
16875 /* Store the match cursor position */
16876 match_pos->lnum = curwin->w_cursor.lnum;
16877 match_pos->col = curwin->w_cursor.col + 1;
16878 }
16879
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016881 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 curwin->w_cursor = save_cursor;
16883
16884theend:
16885 vim_free(pat2);
16886 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016887 if (p_cpo == empty_option)
16888 p_cpo = save_cpo;
16889 else
16890 /* Darn, evaluating the {skip} expression changed the value. */
16891 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016892
16893 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894}
16895
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016896/*
16897 * "searchpos()" function
16898 */
16899 static void
16900f_searchpos(argvars, rettv)
16901 typval_T *argvars;
16902 typval_T *rettv;
16903{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016904 pos_T match_pos;
16905 int lnum = 0;
16906 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016907 int n;
16908 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016909
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016910 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016911 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016912
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016913 n = search_cmn(argvars, &match_pos, &flags);
16914 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016915 {
16916 lnum = match_pos.lnum;
16917 col = match_pos.col;
16918 }
16919
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016920 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16921 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016922 if (flags & SP_SUBPAT)
16923 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016924}
16925
16926
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 static void
16928f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016929 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932#ifdef FEAT_CLIENTSERVER
16933 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 char_u *server = get_tv_string_chk(&argvars[0]);
16935 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 if (server == NULL || reply == NULL)
16939 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 if (check_restricted() || check_secure())
16941 return;
16942# ifdef FEAT_X11
16943 if (check_connection() == FAIL)
16944 return;
16945# endif
16946
16947 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 EMSG(_("E258: Unable to send to client"));
16950 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 rettv->vval.v_number = 0;
16953#else
16954 rettv->vval.v_number = -1;
16955#endif
16956}
16957
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 static void
16959f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016960 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962{
16963 char_u *r = NULL;
16964
16965#ifdef FEAT_CLIENTSERVER
16966# ifdef WIN32
16967 r = serverGetVimNames();
16968# else
16969 make_connection();
16970 if (X_DISPLAY != NULL)
16971 r = serverGetVimNames(X_DISPLAY);
16972# endif
16973#endif
16974 rettv->v_type = VAR_STRING;
16975 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976}
16977
16978/*
16979 * "setbufvar()" function
16980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016982f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016984 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985{
16986 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 char_u nbuf[NUMBUFLEN];
16991
16992 if (check_restricted() || check_secure())
16993 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016994 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16995 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016996 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 varp = &argvars[2];
16998
16999 if (buf != NULL && varname != NULL && varp != NULL)
17000 {
17001 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003
17004 if (*varname == '&')
17005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 long numval;
17007 char_u *strval;
17008 int error = FALSE;
17009
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011 numval = get_tv_number_chk(varp, &error);
17012 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 if (!error && strval != NULL)
17014 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 }
17016 else
17017 {
17018 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17019 if (bufvarname != NULL)
17020 {
17021 STRCPY(bufvarname, "b:");
17022 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017023 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 vim_free(bufvarname);
17025 }
17026 }
17027
17028 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031}
17032
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017033 static void
17034f_setcharsearch(argvars, rettv)
17035 typval_T *argvars;
17036 typval_T *rettv UNUSED;
17037{
17038 dict_T *d;
17039 dictitem_T *di;
17040 char_u *csearch;
17041
17042 if (argvars[0].v_type != VAR_DICT)
17043 {
17044 EMSG(_(e_dictreq));
17045 return;
17046 }
17047
17048 if ((d = argvars[0].vval.v_dict) != NULL)
17049 {
17050 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17051 if (csearch != NULL)
17052 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017053#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017054 if (enc_utf8)
17055 {
17056 int pcc[MAX_MCO];
17057 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017058
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017059 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17060 }
17061 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017062#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017063 set_last_csearch(PTR2CHAR(csearch),
17064 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017065 }
17066
17067 di = dict_find(d, (char_u *)"forward", -1);
17068 if (di != NULL)
17069 set_csearch_direction(get_tv_number(&di->di_tv)
17070 ? FORWARD : BACKWARD);
17071
17072 di = dict_find(d, (char_u *)"until", -1);
17073 if (di != NULL)
17074 set_csearch_until(!!get_tv_number(&di->di_tv));
17075 }
17076}
17077
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078/*
17079 * "setcmdpos()" function
17080 */
17081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017082f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 typval_T *argvars;
17084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017086 int pos = (int)get_tv_number(&argvars[0]) - 1;
17087
17088 if (pos >= 0)
17089 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090}
17091
17092/*
17093 * "setline()" function
17094 */
17095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017096f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017097 typval_T *argvars;
17098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099{
17100 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017101 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017102 list_T *l = NULL;
17103 listitem_T *li = NULL;
17104 long added = 0;
17105 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017107 lnum = get_tv_lnum(&argvars[0]);
17108 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017110 l = argvars[1].vval.v_list;
17111 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017113 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017114 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017115
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017116 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017117 for (;;)
17118 {
17119 if (l != NULL)
17120 {
17121 /* list argument, get next string */
17122 if (li == NULL)
17123 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017124 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017125 li = li->li_next;
17126 }
17127
17128 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017129 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017130 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017131
17132 /* When coming here from Insert mode, sync undo, so that this can be
17133 * undone separately from what was previously inserted. */
17134 if (u_sync_once == 2)
17135 {
17136 u_sync_once = 1; /* notify that u_sync() was called */
17137 u_sync(TRUE);
17138 }
17139
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017140 if (lnum <= curbuf->b_ml.ml_line_count)
17141 {
17142 /* existing line, replace it */
17143 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17144 {
17145 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017146 if (lnum == curwin->w_cursor.lnum)
17147 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017148 rettv->vval.v_number = 0; /* OK */
17149 }
17150 }
17151 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17152 {
17153 /* lnum is one past the last line, append the line */
17154 ++added;
17155 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17156 rettv->vval.v_number = 0; /* OK */
17157 }
17158
17159 if (l == NULL) /* only one string argument */
17160 break;
17161 ++lnum;
17162 }
17163
17164 if (added > 0)
17165 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166}
17167
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017168static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17169
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017171 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017172 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017173 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017174set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017175 win_T *wp UNUSED;
17176 typval_T *list_arg UNUSED;
17177 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017178 typval_T *rettv;
17179{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017180#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017181 char_u *act;
17182 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017183#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017184
Bram Moolenaar2641f772005-03-25 21:58:17 +000017185 rettv->vval.v_number = -1;
17186
17187#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017188 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017189 EMSG(_(e_listreq));
17190 else
17191 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017192 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017193
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017194 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017195 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017196 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017197 if (act == NULL)
17198 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017199 if (*act == 'a' || *act == 'r')
17200 action = *act;
17201 }
17202
Bram Moolenaar81484f42012-12-05 15:16:47 +010017203 if (l != NULL && set_errorlist(wp, l, action,
17204 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017205 rettv->vval.v_number = 0;
17206 }
17207#endif
17208}
17209
17210/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017211 * "setloclist()" function
17212 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017213 static void
17214f_setloclist(argvars, rettv)
17215 typval_T *argvars;
17216 typval_T *rettv;
17217{
17218 win_T *win;
17219
17220 rettv->vval.v_number = -1;
17221
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017222 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017223 if (win != NULL)
17224 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17225}
17226
17227/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017228 * "setmatches()" function
17229 */
17230 static void
17231f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017232 typval_T *argvars UNUSED;
17233 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017234{
17235#ifdef FEAT_SEARCH_EXTRA
17236 list_T *l;
17237 listitem_T *li;
17238 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017239 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017240
17241 rettv->vval.v_number = -1;
17242 if (argvars[0].v_type != VAR_LIST)
17243 {
17244 EMSG(_(e_listreq));
17245 return;
17246 }
17247 if ((l = argvars[0].vval.v_list) != NULL)
17248 {
17249
17250 /* To some extent make sure that we are dealing with a list from
17251 * "getmatches()". */
17252 li = l->lv_first;
17253 while (li != NULL)
17254 {
17255 if (li->li_tv.v_type != VAR_DICT
17256 || (d = li->li_tv.vval.v_dict) == NULL)
17257 {
17258 EMSG(_(e_invarg));
17259 return;
17260 }
17261 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017262 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17263 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017264 && dict_find(d, (char_u *)"priority", -1) != NULL
17265 && dict_find(d, (char_u *)"id", -1) != NULL))
17266 {
17267 EMSG(_(e_invarg));
17268 return;
17269 }
17270 li = li->li_next;
17271 }
17272
17273 clear_matches(curwin);
17274 li = l->lv_first;
17275 while (li != NULL)
17276 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017277 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017278 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017279 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017280 char_u *group;
17281 int priority;
17282 int id;
17283 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017284
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017285 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017286 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17287 {
17288 if (s == NULL)
17289 {
17290 s = list_alloc();
17291 if (s == NULL)
17292 return;
17293 }
17294
17295 /* match from matchaddpos() */
17296 for (i = 1; i < 9; i++)
17297 {
17298 sprintf((char *)buf, (char *)"pos%d", i);
17299 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17300 {
17301 if (di->di_tv.v_type != VAR_LIST)
17302 return;
17303
17304 list_append_tv(s, &di->di_tv);
17305 s->lv_refcount++;
17306 }
17307 else
17308 break;
17309 }
17310 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017311
17312 group = get_dict_string(d, (char_u *)"group", FALSE);
17313 priority = (int)get_dict_number(d, (char_u *)"priority");
17314 id = (int)get_dict_number(d, (char_u *)"id");
17315 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17316 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17317 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017318 if (i == 0)
17319 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017320 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017321 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017322 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017323 }
17324 else
17325 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017326 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017327 list_unref(s);
17328 s = NULL;
17329 }
17330
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017331 li = li->li_next;
17332 }
17333 rettv->vval.v_number = 0;
17334 }
17335#endif
17336}
17337
17338/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017339 * "setpos()" function
17340 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017341 static void
17342f_setpos(argvars, rettv)
17343 typval_T *argvars;
17344 typval_T *rettv;
17345{
17346 pos_T pos;
17347 int fnum;
17348 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017349 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017350
Bram Moolenaar08250432008-02-13 11:42:46 +000017351 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017352 name = get_tv_string_chk(argvars);
17353 if (name != NULL)
17354 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017355 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017356 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017357 if (--pos.col < 0)
17358 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017359 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017360 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017361 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017362 if (fnum == curbuf->b_fnum)
17363 {
17364 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017365 if (curswant >= 0)
17366 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017367 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017368 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017369 }
17370 else
17371 EMSG(_(e_invarg));
17372 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017373 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17374 {
17375 /* set mark */
17376 if (setmark_pos(name[1], &pos, fnum) == OK)
17377 rettv->vval.v_number = 0;
17378 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017379 else
17380 EMSG(_(e_invarg));
17381 }
17382 }
17383}
17384
17385/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017386 * "setqflist()" function
17387 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017388 static void
17389f_setqflist(argvars, rettv)
17390 typval_T *argvars;
17391 typval_T *rettv;
17392{
17393 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17394}
17395
17396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 * "setreg()" function
17398 */
17399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017401 typval_T *argvars;
17402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403{
17404 int regname;
17405 char_u *strregname;
17406 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017407 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 int append;
17409 char_u yank_type;
17410 long block_len;
17411
17412 block_len = -1;
17413 yank_type = MAUTO;
17414 append = FALSE;
17415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017416 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017417 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 if (strregname == NULL)
17420 return; /* type error; errmsg already given */
17421 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 if (regname == 0 || regname == '@')
17423 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017427 stropt = get_tv_string_chk(&argvars[2]);
17428 if (stropt == NULL)
17429 return; /* type error */
17430 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 switch (*stropt)
17432 {
17433 case 'a': case 'A': /* append */
17434 append = TRUE;
17435 break;
17436 case 'v': case 'c': /* character-wise selection */
17437 yank_type = MCHAR;
17438 break;
17439 case 'V': case 'l': /* line-wise selection */
17440 yank_type = MLINE;
17441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 case 'b': case Ctrl_V: /* block-wise selection */
17443 yank_type = MBLOCK;
17444 if (VIM_ISDIGIT(stropt[1]))
17445 {
17446 ++stropt;
17447 block_len = getdigits(&stropt) - 1;
17448 --stropt;
17449 }
17450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 }
17452 }
17453
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017454 if (argvars[1].v_type == VAR_LIST)
17455 {
17456 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017457 char_u **allocval;
17458 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017459 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017460 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017461 int len = argvars[1].vval.v_list->lv_len;
17462 listitem_T *li;
17463
Bram Moolenaar7d647822014-04-05 21:28:56 +020017464 /* First half: use for pointers to result lines; second half: use for
17465 * pointers to allocated copies. */
17466 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017467 if (lstval == NULL)
17468 return;
17469 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017470 allocval = lstval + len + 2;
17471 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017472
17473 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17474 li = li->li_next)
17475 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017476 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017477 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017478 goto free_lstval;
17479 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017480 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017481 /* Need to make a copy, next get_tv_string_buf_chk() will
17482 * overwrite the string. */
17483 strval = vim_strsave(buf);
17484 if (strval == NULL)
17485 goto free_lstval;
17486 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017487 }
17488 *curval++ = strval;
17489 }
17490 *curval++ = NULL;
17491
17492 write_reg_contents_lst(regname, lstval, -1,
17493 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017494free_lstval:
17495 while (curallocval > allocval)
17496 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017497 vim_free(lstval);
17498 }
17499 else
17500 {
17501 strval = get_tv_string_chk(&argvars[1]);
17502 if (strval == NULL)
17503 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017504 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508}
17509
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017510/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017511 * "settabvar()" function
17512 */
17513 static void
17514f_settabvar(argvars, rettv)
17515 typval_T *argvars;
17516 typval_T *rettv;
17517{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017518#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017519 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017520 tabpage_T *tp;
17521#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017522 char_u *varname, *tabvarname;
17523 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017524
17525 rettv->vval.v_number = 0;
17526
17527 if (check_restricted() || check_secure())
17528 return;
17529
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017530#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017532#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017533 varname = get_tv_string_chk(&argvars[1]);
17534 varp = &argvars[2];
17535
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017536 if (varname != NULL && varp != NULL
17537#ifdef FEAT_WINDOWS
17538 && tp != NULL
17539#endif
17540 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017541 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017542#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017543 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017544 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017545#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017546
17547 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17548 if (tabvarname != NULL)
17549 {
17550 STRCPY(tabvarname, "t:");
17551 STRCPY(tabvarname + 2, varname);
17552 set_var(tabvarname, varp, TRUE);
17553 vim_free(tabvarname);
17554 }
17555
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017556#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017557 /* Restore current tabpage */
17558 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017559 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017560#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017561 }
17562}
17563
17564/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017565 * "settabwinvar()" function
17566 */
17567 static void
17568f_settabwinvar(argvars, rettv)
17569 typval_T *argvars;
17570 typval_T *rettv;
17571{
17572 setwinvar(argvars, rettv, 1);
17573}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574
17575/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017576 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 typval_T *argvars;
17581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017583 setwinvar(argvars, rettv, 0);
17584}
17585
17586/*
17587 * "setwinvar()" and "settabwinvar()" functions
17588 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017589
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017590 static void
17591setwinvar(argvars, rettv, off)
17592 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017593 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017594 int off;
17595{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 win_T *win;
17597#ifdef FEAT_WINDOWS
17598 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017599 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600#endif
17601 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017602 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017604 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605
17606 if (check_restricted() || check_secure())
17607 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017608
17609#ifdef FEAT_WINDOWS
17610 if (off == 1)
17611 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17612 else
17613 tp = curtab;
17614#endif
17615 win = find_win_by_nr(&argvars[off], tp);
17616 varname = get_tv_string_chk(&argvars[off + 1]);
17617 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
17619 if (win != NULL && varname != NULL && varp != NULL)
17620 {
17621#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017622 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017625 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017627 long numval;
17628 char_u *strval;
17629 int error = FALSE;
17630
17631 ++varname;
17632 numval = get_tv_number_chk(varp, &error);
17633 strval = get_tv_string_buf_chk(varp, nbuf);
17634 if (!error && strval != NULL)
17635 set_option_value(varname, numval, strval, OPT_LOCAL);
17636 }
17637 else
17638 {
17639 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17640 if (winvarname != NULL)
17641 {
17642 STRCPY(winvarname, "w:");
17643 STRCPY(winvarname + 2, varname);
17644 set_var(winvarname, varp, TRUE);
17645 vim_free(winvarname);
17646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 }
17648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017650 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651#endif
17652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653}
17654
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017655#ifdef FEAT_CRYPT
17656/*
17657 * "sha256({string})" function
17658 */
17659 static void
17660f_sha256(argvars, rettv)
17661 typval_T *argvars;
17662 typval_T *rettv;
17663{
17664 char_u *p;
17665
17666 p = get_tv_string(&argvars[0]);
17667 rettv->vval.v_string = vim_strsave(
17668 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17669 rettv->v_type = VAR_STRING;
17670}
17671#endif /* FEAT_CRYPT */
17672
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017674 * "shellescape({string})" function
17675 */
17676 static void
17677f_shellescape(argvars, rettv)
17678 typval_T *argvars;
17679 typval_T *rettv;
17680{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017681 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017682 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017683 rettv->v_type = VAR_STRING;
17684}
17685
17686/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017687 * shiftwidth() function
17688 */
17689 static void
17690f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017691 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017692 typval_T *rettv;
17693{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017694 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017695}
17696
17697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 */
17700 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017701f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017702 typval_T *argvars;
17703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017705 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707 p = get_tv_string(&argvars[0]);
17708 rettv->vval.v_string = vim_strsave(p);
17709 simplify_filename(rettv->vval.v_string); /* simplify in place */
17710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711}
17712
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017713#ifdef FEAT_FLOAT
17714/*
17715 * "sin()" function
17716 */
17717 static void
17718f_sin(argvars, rettv)
17719 typval_T *argvars;
17720 typval_T *rettv;
17721{
17722 float_T f;
17723
17724 rettv->v_type = VAR_FLOAT;
17725 if (get_float_arg(argvars, &f) == OK)
17726 rettv->vval.v_float = sin(f);
17727 else
17728 rettv->vval.v_float = 0.0;
17729}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017730
17731/*
17732 * "sinh()" function
17733 */
17734 static void
17735f_sinh(argvars, rettv)
17736 typval_T *argvars;
17737 typval_T *rettv;
17738{
17739 float_T f;
17740
17741 rettv->v_type = VAR_FLOAT;
17742 if (get_float_arg(argvars, &f) == OK)
17743 rettv->vval.v_float = sinh(f);
17744 else
17745 rettv->vval.v_float = 0.0;
17746}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017747#endif
17748
Bram Moolenaar0d660222005-01-07 21:51:51 +000017749static int
17750#ifdef __BORLANDC__
17751 _RTLENTRYF
17752#endif
17753 item_compare __ARGS((const void *s1, const void *s2));
17754static int
17755#ifdef __BORLANDC__
17756 _RTLENTRYF
17757#endif
17758 item_compare2 __ARGS((const void *s1, const void *s2));
17759
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017760/* struct used in the array that's given to qsort() */
17761typedef struct
17762{
17763 listitem_T *item;
17764 int idx;
17765} sortItem_T;
17766
Bram Moolenaar0d660222005-01-07 21:51:51 +000017767static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017768static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017769static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017770static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017772static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017773static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017774#define ITEM_COMPARE_FAIL 999
17775
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017777 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779 static int
17780#ifdef __BORLANDC__
17781_RTLENTRYF
17782#endif
17783item_compare(s1, s2)
17784 const void *s1;
17785 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017787 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017788 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017789 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017790 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017791 int res;
17792 char_u numbuf1[NUMBUFLEN];
17793 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017795 si1 = (sortItem_T *)s1;
17796 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017797 tv1 = &si1->item->li_tv;
17798 tv2 = &si2->item->li_tv;
17799 /* tv2string() puts quotes around a string and allocates memory. Don't do
17800 * that for string variables. Use a single quote when comparing with a
17801 * non-string to do what the docs promise. */
17802 if (tv1->v_type == VAR_STRING)
17803 {
17804 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17805 p1 = (char_u *)"'";
17806 else
17807 p1 = tv1->vval.v_string;
17808 }
17809 else
17810 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17811 if (tv2->v_type == VAR_STRING)
17812 {
17813 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17814 p2 = (char_u *)"'";
17815 else
17816 p2 = tv2->vval.v_string;
17817 }
17818 else
17819 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017820 if (p1 == NULL)
17821 p1 = (char_u *)"";
17822 if (p2 == NULL)
17823 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017824 if (!item_compare_numeric)
17825 {
17826 if (item_compare_ic)
17827 res = STRICMP(p1, p2);
17828 else
17829 res = STRCMP(p1, p2);
17830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017832 {
17833 double n1, n2;
17834 n1 = strtod((char *)p1, (char **)&p1);
17835 n2 = strtod((char *)p2, (char **)&p2);
17836 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17837 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017838
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017839 /* When the result would be zero, compare the item indexes. Makes the
17840 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017841 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017842 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017843
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 vim_free(tofree1);
17845 vim_free(tofree2);
17846 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847}
17848
17849 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850#ifdef __BORLANDC__
17851_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017853item_compare2(s1, s2)
17854 const void *s1;
17855 const void *s2;
17856{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017857 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017858 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017859 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017860 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017861 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017863 /* shortcut after failure in previous call; compare all items equal */
17864 if (item_compare_func_err)
17865 return 0;
17866
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017867 si1 = (sortItem_T *)s1;
17868 si2 = (sortItem_T *)s2;
17869
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017870 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017871 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017872 copy_tv(&si1->item->li_tv, &argv[0]);
17873 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874
17875 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017876 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017877 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17878 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 clear_tv(&argv[0]);
17880 clear_tv(&argv[1]);
17881
17882 if (res == FAIL)
17883 res = ITEM_COMPARE_FAIL;
17884 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017885 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17886 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017887 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017888 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017889
17890 /* When the result would be zero, compare the pointers themselves. Makes
17891 * the sort stable. */
17892 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017893 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017894
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 return res;
17896}
17897
17898/*
17899 * "sort({list})" function
17900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017902do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 typval_T *argvars;
17904 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017905 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906{
Bram Moolenaar33570922005-01-25 22:26:29 +000017907 list_T *l;
17908 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017909 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 long len;
17911 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017914 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 else
17916 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017918 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017919 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17920 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017921 return;
17922 rettv->vval.v_list = l;
17923 rettv->v_type = VAR_LIST;
17924 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 len = list_len(l);
17927 if (len <= 1)
17928 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017931 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017933 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934 if (argvars[1].v_type != VAR_UNKNOWN)
17935 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017936 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017937 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017938 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017939 else
17940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017941 int error = FALSE;
17942
17943 i = get_tv_number_chk(&argvars[1], &error);
17944 if (error)
17945 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017946 if (i == 1)
17947 item_compare_ic = TRUE;
17948 else
17949 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017950 if (item_compare_func != NULL)
17951 {
17952 if (STRCMP(item_compare_func, "n") == 0)
17953 {
17954 item_compare_func = NULL;
17955 item_compare_numeric = TRUE;
17956 }
17957 else if (STRCMP(item_compare_func, "i") == 0)
17958 {
17959 item_compare_func = NULL;
17960 item_compare_ic = TRUE;
17961 }
17962 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017964
17965 if (argvars[2].v_type != VAR_UNKNOWN)
17966 {
17967 /* optional third argument: {dict} */
17968 if (argvars[2].v_type != VAR_DICT)
17969 {
17970 EMSG(_(e_dictreq));
17971 return;
17972 }
17973 item_compare_selfdict = argvars[2].vval.v_dict;
17974 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976
Bram Moolenaar0d660222005-01-07 21:51:51 +000017977 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017978 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017979 if (ptrs == NULL)
17980 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981
Bram Moolenaar327aa022014-03-25 18:24:23 +010017982 i = 0;
17983 if (sort)
17984 {
17985 /* sort(): ptrs will be the list to sort */
17986 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017987 {
17988 ptrs[i].item = li;
17989 ptrs[i].idx = i;
17990 ++i;
17991 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017992
17993 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017994 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017995 /* test the compare function */
17996 if (item_compare_func != NULL
17997 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017998 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017999 EMSG(_("E702: Sort compare function failed"));
18000 else
18001 {
18002 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018003 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018004 item_compare_func == NULL ? item_compare : item_compare2);
18005
18006 if (!item_compare_func_err)
18007 {
18008 /* Clear the List and append the items in sorted order. */
18009 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18010 l->lv_len = 0;
18011 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018012 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018013 }
18014 }
18015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018018 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18019
18020 /* f_uniq(): ptrs will be a stack of items to remove */
18021 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018022 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018023 item_compare_func_ptr = item_compare_func
18024 ? item_compare2 : item_compare;
18025
18026 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18027 li = li->li_next)
18028 {
18029 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18030 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018031 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018032 if (item_compare_func_err)
18033 {
18034 EMSG(_("E882: Uniq compare function failed"));
18035 break;
18036 }
18037 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018039 if (!item_compare_func_err)
18040 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018041 while (--i >= 0)
18042 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018043 li = ptrs[i].item->li_next;
18044 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018045 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018046 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018047 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018048 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018049 list_fix_watch(l, li);
18050 listitem_free(li);
18051 l->lv_len--;
18052 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 }
18055
18056 vim_free(ptrs);
18057 }
18058}
18059
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018060/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018061 * "sort({list})" function
18062 */
18063 static void
18064f_sort(argvars, rettv)
18065 typval_T *argvars;
18066 typval_T *rettv;
18067{
18068 do_sort_uniq(argvars, rettv, TRUE);
18069}
18070
18071/*
18072 * "uniq({list})" function
18073 */
18074 static void
18075f_uniq(argvars, rettv)
18076 typval_T *argvars;
18077 typval_T *rettv;
18078{
18079 do_sort_uniq(argvars, rettv, FALSE);
18080}
18081
18082/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018083 * "soundfold({word})" function
18084 */
18085 static void
18086f_soundfold(argvars, rettv)
18087 typval_T *argvars;
18088 typval_T *rettv;
18089{
18090 char_u *s;
18091
18092 rettv->v_type = VAR_STRING;
18093 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018094#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018095 rettv->vval.v_string = eval_soundfold(s);
18096#else
18097 rettv->vval.v_string = vim_strsave(s);
18098#endif
18099}
18100
18101/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018102 * "spellbadword()" function
18103 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018104 static void
18105f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018106 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018107 typval_T *rettv;
18108{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018109 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018110 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018111 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018112
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018113 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018114 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018115
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018116#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018117 if (argvars[0].v_type == VAR_UNKNOWN)
18118 {
18119 /* Find the start and length of the badly spelled word. */
18120 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18121 if (len != 0)
18122 word = ml_get_cursor();
18123 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018124 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018125 {
18126 char_u *str = get_tv_string_chk(&argvars[0]);
18127 int capcol = -1;
18128
18129 if (str != NULL)
18130 {
18131 /* Check the argument for spelling. */
18132 while (*str != NUL)
18133 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018134 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018135 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018136 {
18137 word = str;
18138 break;
18139 }
18140 str += len;
18141 }
18142 }
18143 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018144#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018145
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018146 list_append_string(rettv->vval.v_list, word, len);
18147 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018148 attr == HLF_SPB ? "bad" :
18149 attr == HLF_SPR ? "rare" :
18150 attr == HLF_SPL ? "local" :
18151 attr == HLF_SPC ? "caps" :
18152 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018153}
18154
18155/*
18156 * "spellsuggest()" function
18157 */
18158 static void
18159f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018160 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018161 typval_T *rettv;
18162{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018163#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018164 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018165 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018166 int maxcount;
18167 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018168 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018169 listitem_T *li;
18170 int need_capital = FALSE;
18171#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018173 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018174 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018175
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018176#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018177 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018178 {
18179 str = get_tv_string(&argvars[0]);
18180 if (argvars[1].v_type != VAR_UNKNOWN)
18181 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018182 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018183 if (maxcount <= 0)
18184 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018185 if (argvars[2].v_type != VAR_UNKNOWN)
18186 {
18187 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18188 if (typeerr)
18189 return;
18190 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018191 }
18192 else
18193 maxcount = 25;
18194
Bram Moolenaar4770d092006-01-12 23:22:24 +000018195 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018196
18197 for (i = 0; i < ga.ga_len; ++i)
18198 {
18199 str = ((char_u **)ga.ga_data)[i];
18200
18201 li = listitem_alloc();
18202 if (li == NULL)
18203 vim_free(str);
18204 else
18205 {
18206 li->li_tv.v_type = VAR_STRING;
18207 li->li_tv.v_lock = 0;
18208 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018209 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018210 }
18211 }
18212 ga_clear(&ga);
18213 }
18214#endif
18215}
18216
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018218f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018219 typval_T *argvars;
18220 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221{
18222 char_u *str;
18223 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018224 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225 regmatch_T regmatch;
18226 char_u patbuf[NUMBUFLEN];
18227 char_u *save_cpo;
18228 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018230 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018231 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018232
18233 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18234 save_cpo = p_cpo;
18235 p_cpo = (char_u *)"";
18236
18237 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 if (argvars[1].v_type != VAR_UNKNOWN)
18239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018240 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18241 if (pat == NULL)
18242 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018243 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018244 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018245 }
18246 if (pat == NULL || *pat == NUL)
18247 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018249 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018251 if (typeerr)
18252 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18255 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018257 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018258 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018260 if (*str == NUL)
18261 match = FALSE; /* empty item at the end */
18262 else
18263 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 if (match)
18265 end = regmatch.startp[0];
18266 else
18267 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018268 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18269 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018271 if (list_append_string(rettv->vval.v_list, str,
18272 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 }
18275 if (!match)
18276 break;
18277 /* Advance to just after the match. */
18278 if (regmatch.endp[0] > str)
18279 col = 0;
18280 else
18281 {
18282 /* Don't get stuck at the same match. */
18283#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018284 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285#else
18286 col = 1;
18287#endif
18288 }
18289 str = regmatch.endp[0];
18290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291
Bram Moolenaar473de612013-06-08 18:19:48 +020018292 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296}
18297
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018298#ifdef FEAT_FLOAT
18299/*
18300 * "sqrt()" function
18301 */
18302 static void
18303f_sqrt(argvars, rettv)
18304 typval_T *argvars;
18305 typval_T *rettv;
18306{
18307 float_T f;
18308
18309 rettv->v_type = VAR_FLOAT;
18310 if (get_float_arg(argvars, &f) == OK)
18311 rettv->vval.v_float = sqrt(f);
18312 else
18313 rettv->vval.v_float = 0.0;
18314}
18315
18316/*
18317 * "str2float()" function
18318 */
18319 static void
18320f_str2float(argvars, rettv)
18321 typval_T *argvars;
18322 typval_T *rettv;
18323{
18324 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18325
18326 if (*p == '+')
18327 p = skipwhite(p + 1);
18328 (void)string2float(p, &rettv->vval.v_float);
18329 rettv->v_type = VAR_FLOAT;
18330}
18331#endif
18332
Bram Moolenaar2c932302006-03-18 21:42:09 +000018333/*
18334 * "str2nr()" function
18335 */
18336 static void
18337f_str2nr(argvars, rettv)
18338 typval_T *argvars;
18339 typval_T *rettv;
18340{
18341 int base = 10;
18342 char_u *p;
18343 long n;
18344
18345 if (argvars[1].v_type != VAR_UNKNOWN)
18346 {
18347 base = get_tv_number(&argvars[1]);
18348 if (base != 8 && base != 10 && base != 16)
18349 {
18350 EMSG(_(e_invarg));
18351 return;
18352 }
18353 }
18354
18355 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018356 if (*p == '+')
18357 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018358 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018359 rettv->vval.v_number = n;
18360}
18361
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362#ifdef HAVE_STRFTIME
18363/*
18364 * "strftime({format}[, {time}])" function
18365 */
18366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018367f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018368 typval_T *argvars;
18369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370{
18371 char_u result_buf[256];
18372 struct tm *curtime;
18373 time_t seconds;
18374 char_u *p;
18375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018376 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018378 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018379 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 seconds = time(NULL);
18381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018382 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 curtime = localtime(&seconds);
18384 /* MSVC returns NULL for an invalid value of seconds. */
18385 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 else
18388 {
18389# ifdef FEAT_MBYTE
18390 vimconv_T conv;
18391 char_u *enc;
18392
18393 conv.vc_type = CONV_NONE;
18394 enc = enc_locale();
18395 convert_setup(&conv, p_enc, enc);
18396 if (conv.vc_type != CONV_NONE)
18397 p = string_convert(&conv, p, NULL);
18398# endif
18399 if (p != NULL)
18400 (void)strftime((char *)result_buf, sizeof(result_buf),
18401 (char *)p, curtime);
18402 else
18403 result_buf[0] = NUL;
18404
18405# ifdef FEAT_MBYTE
18406 if (conv.vc_type != CONV_NONE)
18407 vim_free(p);
18408 convert_setup(&conv, enc, p_enc);
18409 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018410 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 else
18412# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414
18415# ifdef FEAT_MBYTE
18416 /* Release conversion descriptors */
18417 convert_setup(&conv, NULL, NULL);
18418 vim_free(enc);
18419# endif
18420 }
18421}
18422#endif
18423
18424/*
18425 * "stridx()" function
18426 */
18427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018428f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018429 typval_T *argvars;
18430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431{
18432 char_u buf[NUMBUFLEN];
18433 char_u *needle;
18434 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018435 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018437 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018439 needle = get_tv_string_chk(&argvars[1]);
18440 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018442 if (needle == NULL || haystack == NULL)
18443 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444
Bram Moolenaar33570922005-01-25 22:26:29 +000018445 if (argvars[2].v_type != VAR_UNKNOWN)
18446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018447 int error = FALSE;
18448
18449 start_idx = get_tv_number_chk(&argvars[2], &error);
18450 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018452 if (start_idx >= 0)
18453 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 }
18455
18456 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18457 if (pos != NULL)
18458 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459}
18460
18461/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018462 * "string()" function
18463 */
18464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018465f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018466 typval_T *argvars;
18467 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018468{
18469 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018472 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018473 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018474 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018475 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477}
18478
18479/*
18480 * "strlen()" function
18481 */
18482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018484 typval_T *argvars;
18485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487 rettv->vval.v_number = (varnumber_T)(STRLEN(
18488 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489}
18490
18491/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018492 * "strchars()" function
18493 */
18494 static void
18495f_strchars(argvars, rettv)
18496 typval_T *argvars;
18497 typval_T *rettv;
18498{
18499 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018500 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018501#ifdef FEAT_MBYTE
18502 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018503 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018504#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018505
18506 if (argvars[1].v_type != VAR_UNKNOWN)
18507 skipcc = get_tv_number_chk(&argvars[1], NULL);
18508 if (skipcc < 0 || skipcc > 1)
18509 EMSG(_(e_invarg));
18510 else
18511 {
18512#ifdef FEAT_MBYTE
18513 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18514 while (*s != NUL)
18515 {
18516 func_mb_ptr2char_adv(&s);
18517 ++len;
18518 }
18519 rettv->vval.v_number = len;
18520#else
18521 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18522#endif
18523 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018524}
18525
18526/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018527 * "strdisplaywidth()" function
18528 */
18529 static void
18530f_strdisplaywidth(argvars, rettv)
18531 typval_T *argvars;
18532 typval_T *rettv;
18533{
18534 char_u *s = get_tv_string(&argvars[0]);
18535 int col = 0;
18536
18537 if (argvars[1].v_type != VAR_UNKNOWN)
18538 col = get_tv_number(&argvars[1]);
18539
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018540 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018541}
18542
18543/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018544 * "strwidth()" function
18545 */
18546 static void
18547f_strwidth(argvars, rettv)
18548 typval_T *argvars;
18549 typval_T *rettv;
18550{
18551 char_u *s = get_tv_string(&argvars[0]);
18552
18553 rettv->vval.v_number = (varnumber_T)(
18554#ifdef FEAT_MBYTE
18555 mb_string2cells(s, -1)
18556#else
18557 STRLEN(s)
18558#endif
18559 );
18560}
18561
18562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 * "strpart()" function
18564 */
18565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018567 typval_T *argvars;
18568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569{
18570 char_u *p;
18571 int n;
18572 int len;
18573 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018574 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018576 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 slen = (int)STRLEN(p);
18578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018579 n = get_tv_number_chk(&argvars[1], &error);
18580 if (error)
18581 len = 0;
18582 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 else
18585 len = slen - n; /* default len: all bytes that are available. */
18586
18587 /*
18588 * Only return the overlap between the specified part and the actual
18589 * string.
18590 */
18591 if (n < 0)
18592 {
18593 len += n;
18594 n = 0;
18595 }
18596 else if (n > slen)
18597 n = slen;
18598 if (len < 0)
18599 len = 0;
18600 else if (n + len > slen)
18601 len = slen - n;
18602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018603 rettv->v_type = VAR_STRING;
18604 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605}
18606
18607/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018608 * "strridx()" function
18609 */
18610 static void
18611f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018612 typval_T *argvars;
18613 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614{
18615 char_u buf[NUMBUFLEN];
18616 char_u *needle;
18617 char_u *haystack;
18618 char_u *rest;
18619 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018620 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018622 needle = get_tv_string_chk(&argvars[1]);
18623 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624
18625 rettv->vval.v_number = -1;
18626 if (needle == NULL || haystack == NULL)
18627 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018628
18629 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018630 if (argvars[2].v_type != VAR_UNKNOWN)
18631 {
18632 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018633 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018634 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018635 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018636 }
18637 else
18638 end_idx = haystack_len;
18639
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018641 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018642 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018643 lastmatch = haystack + end_idx;
18644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018646 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 for (rest = haystack; *rest != '\0'; ++rest)
18648 {
18649 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018650 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651 break;
18652 lastmatch = rest;
18653 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018655
18656 if (lastmatch == NULL)
18657 rettv->vval.v_number = -1;
18658 else
18659 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18660}
18661
18662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 * "strtrans()" function
18664 */
18665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018667 typval_T *argvars;
18668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670 rettv->v_type = VAR_STRING;
18671 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672}
18673
18674/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018675 * "submatch()" function
18676 */
18677 static void
18678f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018679 typval_T *argvars;
18680 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018681{
Bram Moolenaar41571762014-04-02 19:00:58 +020018682 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018683 int no;
18684 int retList = 0;
18685
18686 no = (int)get_tv_number_chk(&argvars[0], &error);
18687 if (error)
18688 return;
18689 error = FALSE;
18690 if (argvars[1].v_type != VAR_UNKNOWN)
18691 retList = get_tv_number_chk(&argvars[1], &error);
18692 if (error)
18693 return;
18694
18695 if (retList == 0)
18696 {
18697 rettv->v_type = VAR_STRING;
18698 rettv->vval.v_string = reg_submatch(no);
18699 }
18700 else
18701 {
18702 rettv->v_type = VAR_LIST;
18703 rettv->vval.v_list = reg_submatch_list(no);
18704 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018705}
18706
18707/*
18708 * "substitute()" function
18709 */
18710 static void
18711f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018712 typval_T *argvars;
18713 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714{
18715 char_u patbuf[NUMBUFLEN];
18716 char_u subbuf[NUMBUFLEN];
18717 char_u flagsbuf[NUMBUFLEN];
18718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018719 char_u *str = get_tv_string_chk(&argvars[0]);
18720 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18721 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18722 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18723
Bram Moolenaar0d660222005-01-07 21:51:51 +000018724 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018725 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18726 rettv->vval.v_string = NULL;
18727 else
18728 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018729}
18730
18731/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018732 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738{
18739 int id = 0;
18740#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018741 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 long col;
18743 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018744 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018746 lnum = get_tv_lnum(argvars); /* -1 on type error */
18747 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18748 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018750 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018751 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018752 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753#endif
18754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756}
18757
18758/*
18759 * "synIDattr(id, what [, mode])" function
18760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765{
18766 char_u *p = NULL;
18767#ifdef FEAT_SYN_HL
18768 int id;
18769 char_u *what;
18770 char_u *mode;
18771 char_u modebuf[NUMBUFLEN];
18772 int modec;
18773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774 id = get_tv_number(&argvars[0]);
18775 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018776 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018780 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 modec = 0; /* replace invalid with current */
18782 }
18783 else
18784 {
18785#ifdef FEAT_GUI
18786 if (gui.in_use)
18787 modec = 'g';
18788 else
18789#endif
18790 if (t_colors > 1)
18791 modec = 'c';
18792 else
18793 modec = 't';
18794 }
18795
18796
18797 switch (TOLOWER_ASC(what[0]))
18798 {
18799 case 'b':
18800 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18801 p = highlight_color(id, what, modec);
18802 else /* bold */
18803 p = highlight_has_attr(id, HL_BOLD, modec);
18804 break;
18805
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018806 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 p = highlight_color(id, what, modec);
18808 break;
18809
18810 case 'i':
18811 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18812 p = highlight_has_attr(id, HL_INVERSE, modec);
18813 else /* italic */
18814 p = highlight_has_attr(id, HL_ITALIC, modec);
18815 break;
18816
18817 case 'n': /* name */
18818 p = get_highlight_name(NULL, id - 1);
18819 break;
18820
18821 case 'r': /* reverse */
18822 p = highlight_has_attr(id, HL_INVERSE, modec);
18823 break;
18824
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018825 case 's':
18826 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18827 p = highlight_color(id, what, modec);
18828 else /* standout */
18829 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 break;
18831
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018832 case 'u':
18833 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18834 /* underline */
18835 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18836 else
18837 /* undercurl */
18838 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 break;
18840 }
18841
18842 if (p != NULL)
18843 p = vim_strsave(p);
18844#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018845 rettv->v_type = VAR_STRING;
18846 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847}
18848
18849/*
18850 * "synIDtrans(id)" function
18851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018853f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856{
18857 int id;
18858
18859#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861
18862 if (id > 0)
18863 id = syn_get_final_id(id);
18864 else
18865#endif
18866 id = 0;
18867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018868 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869}
18870
18871/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018872 * "synconcealed(lnum, col)" function
18873 */
18874 static void
18875f_synconcealed(argvars, rettv)
18876 typval_T *argvars UNUSED;
18877 typval_T *rettv;
18878{
18879#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18880 long lnum;
18881 long col;
18882 int syntax_flags = 0;
18883 int cchar;
18884 int matchid = 0;
18885 char_u str[NUMBUFLEN];
18886#endif
18887
18888 rettv->v_type = VAR_LIST;
18889 rettv->vval.v_list = NULL;
18890
18891#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18892 lnum = get_tv_lnum(argvars); /* -1 on type error */
18893 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18894
18895 vim_memset(str, NUL, sizeof(str));
18896
18897 if (rettv_list_alloc(rettv) != FAIL)
18898 {
18899 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18900 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18901 && curwin->w_p_cole > 0)
18902 {
18903 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18904 syntax_flags = get_syntax_info(&matchid);
18905
18906 /* get the conceal character */
18907 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18908 {
18909 cchar = syn_get_sub_char();
18910 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18911 cchar = lcs_conceal;
18912 if (cchar != NUL)
18913 {
18914# ifdef FEAT_MBYTE
18915 if (has_mbyte)
18916 (*mb_char2bytes)(cchar, str);
18917 else
18918# endif
18919 str[0] = cchar;
18920 }
18921 }
18922 }
18923
18924 list_append_number(rettv->vval.v_list,
18925 (syntax_flags & HL_CONCEAL) != 0);
18926 /* -1 to auto-determine strlen */
18927 list_append_string(rettv->vval.v_list, str, -1);
18928 list_append_number(rettv->vval.v_list, matchid);
18929 }
18930#endif
18931}
18932
18933/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018934 * "synstack(lnum, col)" function
18935 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018936 static void
18937f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018938 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018939 typval_T *rettv;
18940{
18941#ifdef FEAT_SYN_HL
18942 long lnum;
18943 long col;
18944 int i;
18945 int id;
18946#endif
18947
18948 rettv->v_type = VAR_LIST;
18949 rettv->vval.v_list = NULL;
18950
18951#ifdef FEAT_SYN_HL
18952 lnum = get_tv_lnum(argvars); /* -1 on type error */
18953 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18954
18955 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018956 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018957 && rettv_list_alloc(rettv) != FAIL)
18958 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018959 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018960 for (i = 0; ; ++i)
18961 {
18962 id = syn_get_stack_item(i);
18963 if (id < 0)
18964 break;
18965 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18966 break;
18967 }
18968 }
18969#endif
18970}
18971
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018973get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018974 typval_T *argvars;
18975 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018976 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018978 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018980 char_u *infile = NULL;
18981 char_u buf[NUMBUFLEN];
18982 int err = FALSE;
18983 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018984 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018985 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018987 rettv->v_type = VAR_STRING;
18988 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018989 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018990 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018991
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018992 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018993 {
18994 /*
18995 * Write the string to a temp file, to be used for input of the shell
18996 * command.
18997 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018998 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018999 {
19000 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019001 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019002 }
19003
19004 fd = mch_fopen((char *)infile, WRITEBIN);
19005 if (fd == NULL)
19006 {
19007 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019008 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019009 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019010 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019011 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019012 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19013 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019014 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019015 else
19016 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019017 size_t len;
19018
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019019 p = get_tv_string_buf_chk(&argvars[1], buf);
19020 if (p == NULL)
19021 {
19022 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019023 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019024 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019025 len = STRLEN(p);
19026 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019027 err = TRUE;
19028 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019029 if (fclose(fd) != 0)
19030 err = TRUE;
19031 if (err)
19032 {
19033 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019034 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019035 }
19036 }
19037
Bram Moolenaar52a72462014-08-29 15:53:52 +020019038 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19039 * echoes typeahead, that messes up the display. */
19040 if (!msg_silent)
19041 flags += SHELL_COOKED;
19042
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019043 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019045 int len;
19046 listitem_T *li;
19047 char_u *s = NULL;
19048 char_u *start;
19049 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019050 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051
Bram Moolenaar52a72462014-08-29 15:53:52 +020019052 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019053 if (res == NULL)
19054 goto errret;
19055
19056 list = list_alloc();
19057 if (list == NULL)
19058 goto errret;
19059
19060 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019062 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019063 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019065 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019066
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019067 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019068 if (s == NULL)
19069 goto errret;
19070
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019071 for (p = s; start < end; ++p, ++start)
19072 *p = *start == NUL ? NL : *start;
19073 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019074
19075 li = listitem_alloc();
19076 if (li == NULL)
19077 {
19078 vim_free(s);
19079 goto errret;
19080 }
19081 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019082 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019083 li->li_tv.vval.v_string = s;
19084 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019086
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019087 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088 rettv->v_type = VAR_LIST;
19089 rettv->vval.v_list = list;
19090 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019092 else
19093 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019094 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019095#ifdef USE_CR
19096 /* translate <CR> into <NL> */
19097 if (res != NULL)
19098 {
19099 char_u *s;
19100
19101 for (s = res; *s; ++s)
19102 {
19103 if (*s == CAR)
19104 *s = NL;
19105 }
19106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107#else
19108# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019109 /* translate <CR><NL> into <NL> */
19110 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019112 char_u *s, *d;
19113
19114 d = res;
19115 for (s = res; *s; ++s)
19116 {
19117 if (s[0] == CAR && s[1] == NL)
19118 ++s;
19119 *d++ = *s;
19120 }
19121 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123# endif
19124#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019125 rettv->vval.v_string = res;
19126 res = NULL;
19127 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019128
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019129errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019130 if (infile != NULL)
19131 {
19132 mch_remove(infile);
19133 vim_free(infile);
19134 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019135 if (res != NULL)
19136 vim_free(res);
19137 if (list != NULL)
19138 list_free(list, TRUE);
19139}
19140
19141/*
19142 * "system()" function
19143 */
19144 static void
19145f_system(argvars, rettv)
19146 typval_T *argvars;
19147 typval_T *rettv;
19148{
19149 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19150}
19151
19152/*
19153 * "systemlist()" function
19154 */
19155 static void
19156f_systemlist(argvars, rettv)
19157 typval_T *argvars;
19158 typval_T *rettv;
19159{
19160 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161}
19162
19163/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019164 * "tabpagebuflist()" function
19165 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019166 static void
19167f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019168 typval_T *argvars UNUSED;
19169 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019170{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019171#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019172 tabpage_T *tp;
19173 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019174
19175 if (argvars[0].v_type == VAR_UNKNOWN)
19176 wp = firstwin;
19177 else
19178 {
19179 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19180 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019181 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019182 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019183 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019184 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019185 for (; wp != NULL; wp = wp->w_next)
19186 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019187 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019188 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019189 }
19190#endif
19191}
19192
19193
19194/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019195 * "tabpagenr()" function
19196 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019197 static void
19198f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019199 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019200 typval_T *rettv;
19201{
19202 int nr = 1;
19203#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019204 char_u *arg;
19205
19206 if (argvars[0].v_type != VAR_UNKNOWN)
19207 {
19208 arg = get_tv_string_chk(&argvars[0]);
19209 nr = 0;
19210 if (arg != NULL)
19211 {
19212 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019213 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019214 else
19215 EMSG2(_(e_invexpr2), arg);
19216 }
19217 }
19218 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019219 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019220#endif
19221 rettv->vval.v_number = nr;
19222}
19223
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019224
19225#ifdef FEAT_WINDOWS
19226static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19227
19228/*
19229 * Common code for tabpagewinnr() and winnr().
19230 */
19231 static int
19232get_winnr(tp, argvar)
19233 tabpage_T *tp;
19234 typval_T *argvar;
19235{
19236 win_T *twin;
19237 int nr = 1;
19238 win_T *wp;
19239 char_u *arg;
19240
19241 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19242 if (argvar->v_type != VAR_UNKNOWN)
19243 {
19244 arg = get_tv_string_chk(argvar);
19245 if (arg == NULL)
19246 nr = 0; /* type error; errmsg already given */
19247 else if (STRCMP(arg, "$") == 0)
19248 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19249 else if (STRCMP(arg, "#") == 0)
19250 {
19251 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19252 if (twin == NULL)
19253 nr = 0;
19254 }
19255 else
19256 {
19257 EMSG2(_(e_invexpr2), arg);
19258 nr = 0;
19259 }
19260 }
19261
19262 if (nr > 0)
19263 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19264 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019265 {
19266 if (wp == NULL)
19267 {
19268 /* didn't find it in this tabpage */
19269 nr = 0;
19270 break;
19271 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019272 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019273 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019274 return nr;
19275}
19276#endif
19277
19278/*
19279 * "tabpagewinnr()" function
19280 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019281 static void
19282f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019283 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019284 typval_T *rettv;
19285{
19286 int nr = 1;
19287#ifdef FEAT_WINDOWS
19288 tabpage_T *tp;
19289
19290 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19291 if (tp == NULL)
19292 nr = 0;
19293 else
19294 nr = get_winnr(tp, &argvars[1]);
19295#endif
19296 rettv->vval.v_number = nr;
19297}
19298
19299
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019300/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019301 * "tagfiles()" function
19302 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019303 static void
19304f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019305 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019306 typval_T *rettv;
19307{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019308 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019309 tagname_T tn;
19310 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019311
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019312 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019313 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019314 fname = alloc(MAXPATHL);
19315 if (fname == NULL)
19316 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019318 for (first = TRUE; ; first = FALSE)
19319 if (get_tagfname(&tn, first, fname) == FAIL
19320 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019321 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019322 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019323 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019324}
19325
19326/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019327 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019328 */
19329 static void
19330f_taglist(argvars, rettv)
19331 typval_T *argvars;
19332 typval_T *rettv;
19333{
19334 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019335
19336 tag_pattern = get_tv_string(&argvars[0]);
19337
19338 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019339 if (*tag_pattern == NUL)
19340 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019341
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019342 if (rettv_list_alloc(rettv) == OK)
19343 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019344}
19345
19346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 * "tempname()" function
19348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353{
19354 static int x = 'A';
19355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019357 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
19359 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19360 * names. Skip 'I' and 'O', they are used for shell redirection. */
19361 do
19362 {
19363 if (x == 'Z')
19364 x = '0';
19365 else if (x == '9')
19366 x = 'A';
19367 else
19368 {
19369#ifdef EBCDIC
19370 if (x == 'I')
19371 x = 'J';
19372 else if (x == 'R')
19373 x = 'S';
19374 else
19375#endif
19376 ++x;
19377 }
19378 } while (x == 'I' || x == 'O');
19379}
19380
19381/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019382 * "test(list)" function: Just checking the walls...
19383 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019384 static void
19385f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019386 typval_T *argvars UNUSED;
19387 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019388{
19389 /* Used for unit testing. Change the code below to your liking. */
19390#if 0
19391 listitem_T *li;
19392 list_T *l;
19393 char_u *bad, *good;
19394
19395 if (argvars[0].v_type != VAR_LIST)
19396 return;
19397 l = argvars[0].vval.v_list;
19398 if (l == NULL)
19399 return;
19400 li = l->lv_first;
19401 if (li == NULL)
19402 return;
19403 bad = get_tv_string(&li->li_tv);
19404 li = li->li_next;
19405 if (li == NULL)
19406 return;
19407 good = get_tv_string(&li->li_tv);
19408 rettv->vval.v_number = test_edit_score(bad, good);
19409#endif
19410}
19411
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019412#ifdef FEAT_FLOAT
19413/*
19414 * "tan()" function
19415 */
19416 static void
19417f_tan(argvars, rettv)
19418 typval_T *argvars;
19419 typval_T *rettv;
19420{
19421 float_T f;
19422
19423 rettv->v_type = VAR_FLOAT;
19424 if (get_float_arg(argvars, &f) == OK)
19425 rettv->vval.v_float = tan(f);
19426 else
19427 rettv->vval.v_float = 0.0;
19428}
19429
19430/*
19431 * "tanh()" function
19432 */
19433 static void
19434f_tanh(argvars, rettv)
19435 typval_T *argvars;
19436 typval_T *rettv;
19437{
19438 float_T f;
19439
19440 rettv->v_type = VAR_FLOAT;
19441 if (get_float_arg(argvars, &f) == OK)
19442 rettv->vval.v_float = tanh(f);
19443 else
19444 rettv->vval.v_float = 0.0;
19445}
19446#endif
19447
Bram Moolenaard52d9742005-08-21 22:20:28 +000019448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 * "tolower(string)" function
19450 */
19451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019452f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019453 typval_T *argvars;
19454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455{
19456 char_u *p;
19457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019458 p = vim_strsave(get_tv_string(&argvars[0]));
19459 rettv->v_type = VAR_STRING;
19460 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461
19462 if (p != NULL)
19463 while (*p != NUL)
19464 {
19465#ifdef FEAT_MBYTE
19466 int l;
19467
19468 if (enc_utf8)
19469 {
19470 int c, lc;
19471
19472 c = utf_ptr2char(p);
19473 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019474 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 /* TODO: reallocate string when byte count changes. */
19476 if (utf_char2len(lc) == l)
19477 utf_char2bytes(lc, p);
19478 p += l;
19479 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019480 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 p += l; /* skip multi-byte character */
19482 else
19483#endif
19484 {
19485 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19486 ++p;
19487 }
19488 }
19489}
19490
19491/*
19492 * "toupper(string)" function
19493 */
19494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019495f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019496 typval_T *argvars;
19497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019499 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019500 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501}
19502
19503/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019504 * "tr(string, fromstr, tostr)" function
19505 */
19506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019508 typval_T *argvars;
19509 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019510{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019511 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019512 char_u *fromstr;
19513 char_u *tostr;
19514 char_u *p;
19515#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019516 int inlen;
19517 int fromlen;
19518 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019519 int idx;
19520 char_u *cpstr;
19521 int cplen;
19522 int first = TRUE;
19523#endif
19524 char_u buf[NUMBUFLEN];
19525 char_u buf2[NUMBUFLEN];
19526 garray_T ga;
19527
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019528 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019529 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19530 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019531
19532 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019533 rettv->v_type = VAR_STRING;
19534 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019535 if (fromstr == NULL || tostr == NULL)
19536 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019537 ga_init2(&ga, (int)sizeof(char), 80);
19538
19539#ifdef FEAT_MBYTE
19540 if (!has_mbyte)
19541#endif
19542 /* not multi-byte: fromstr and tostr must be the same length */
19543 if (STRLEN(fromstr) != STRLEN(tostr))
19544 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019545#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019546error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019547#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019548 EMSG2(_(e_invarg2), fromstr);
19549 ga_clear(&ga);
19550 return;
19551 }
19552
19553 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019554 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019555 {
19556#ifdef FEAT_MBYTE
19557 if (has_mbyte)
19558 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019559 inlen = (*mb_ptr2len)(in_str);
19560 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019561 cplen = inlen;
19562 idx = 0;
19563 for (p = fromstr; *p != NUL; p += fromlen)
19564 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019565 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019566 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019567 {
19568 for (p = tostr; *p != NUL; p += tolen)
19569 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019570 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019571 if (idx-- == 0)
19572 {
19573 cplen = tolen;
19574 cpstr = p;
19575 break;
19576 }
19577 }
19578 if (*p == NUL) /* tostr is shorter than fromstr */
19579 goto error;
19580 break;
19581 }
19582 ++idx;
19583 }
19584
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019585 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019586 {
19587 /* Check that fromstr and tostr have the same number of
19588 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019589 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019590 first = FALSE;
19591 for (p = tostr; *p != NUL; p += tolen)
19592 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019593 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019594 --idx;
19595 }
19596 if (idx != 0)
19597 goto error;
19598 }
19599
Bram Moolenaarcde88542015-08-11 19:14:00 +020019600 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019601 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019602 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019603
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019604 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019605 }
19606 else
19607#endif
19608 {
19609 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019610 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019611 if (p != NULL)
19612 ga_append(&ga, tostr[p - fromstr]);
19613 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019614 ga_append(&ga, *in_str);
19615 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019616 }
19617 }
19618
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019619 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019620 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019621 ga_append(&ga, NUL);
19622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019623 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019624}
19625
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019626#ifdef FEAT_FLOAT
19627/*
19628 * "trunc({float})" function
19629 */
19630 static void
19631f_trunc(argvars, rettv)
19632 typval_T *argvars;
19633 typval_T *rettv;
19634{
19635 float_T f;
19636
19637 rettv->v_type = VAR_FLOAT;
19638 if (get_float_arg(argvars, &f) == OK)
19639 /* trunc() is not in C90, use floor() or ceil() instead. */
19640 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19641 else
19642 rettv->vval.v_float = 0.0;
19643}
19644#endif
19645
Bram Moolenaar8299df92004-07-10 09:47:34 +000019646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 * "type(expr)" function
19648 */
19649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019650f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019651 typval_T *argvars;
19652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019654 int n;
19655
19656 switch (argvars[0].v_type)
19657 {
19658 case VAR_NUMBER: n = 0; break;
19659 case VAR_STRING: n = 1; break;
19660 case VAR_FUNC: n = 2; break;
19661 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019662 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019663#ifdef FEAT_FLOAT
19664 case VAR_FLOAT: n = 5; break;
19665#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019666 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19667 }
19668 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669}
19670
19671/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019672 * "undofile(name)" function
19673 */
19674 static void
19675f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019676 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019677 typval_T *rettv;
19678{
19679 rettv->v_type = VAR_STRING;
19680#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019681 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019682 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019683
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019684 if (*fname == NUL)
19685 {
19686 /* If there is no file name there will be no undo file. */
19687 rettv->vval.v_string = NULL;
19688 }
19689 else
19690 {
19691 char_u *ffname = FullName_save(fname, FALSE);
19692
19693 if (ffname != NULL)
19694 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19695 vim_free(ffname);
19696 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019697 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019698#else
19699 rettv->vval.v_string = NULL;
19700#endif
19701}
19702
19703/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019704 * "undotree()" function
19705 */
19706 static void
19707f_undotree(argvars, rettv)
19708 typval_T *argvars UNUSED;
19709 typval_T *rettv;
19710{
19711 if (rettv_dict_alloc(rettv) == OK)
19712 {
19713 dict_T *dict = rettv->vval.v_dict;
19714 list_T *list;
19715
Bram Moolenaar730cde92010-06-27 05:18:54 +020019716 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019717 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019718 dict_add_nr_str(dict, "save_last",
19719 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019720 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19721 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019722 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019723
19724 list = list_alloc();
19725 if (list != NULL)
19726 {
19727 u_eval_tree(curbuf->b_u_oldhead, list);
19728 dict_add_list(dict, "entries", list);
19729 }
19730 }
19731}
19732
19733/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019734 * "values(dict)" function
19735 */
19736 static void
19737f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 typval_T *argvars;
19739 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019740{
19741 dict_list(argvars, rettv, 1);
19742}
19743
19744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 * "virtcol(string)" function
19746 */
19747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019749 typval_T *argvars;
19750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751{
19752 colnr_T vcol = 0;
19753 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019754 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019756 fp = var2fpos(&argvars[0], FALSE, &fnum);
19757 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19758 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 {
19760 getvvcol(curwin, fp, NULL, NULL, &vcol);
19761 ++vcol;
19762 }
19763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765}
19766
19767/*
19768 * "visualmode()" function
19769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019771f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019772 typval_T *argvars UNUSED;
19773 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 char_u str[2];
19776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 str[0] = curbuf->b_visual_mode_eval;
19779 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781
19782 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019783 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785}
19786
19787/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019788 * "wildmenumode()" function
19789 */
19790 static void
19791f_wildmenumode(argvars, rettv)
19792 typval_T *argvars UNUSED;
19793 typval_T *rettv UNUSED;
19794{
19795#ifdef FEAT_WILDMENU
19796 if (wild_menu_showing)
19797 rettv->vval.v_number = 1;
19798#endif
19799}
19800
19801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 * "winbufnr(nr)" function
19803 */
19804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 typval_T *argvars;
19807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808{
19809 win_T *wp;
19810
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019811 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816}
19817
19818/*
19819 * "wincol()" function
19820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825{
19826 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828}
19829
19830/*
19831 * "winheight(nr)" function
19832 */
19833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 typval_T *argvars;
19836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837{
19838 win_T *wp;
19839
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019840 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845}
19846
19847/*
19848 * "winline()" function
19849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854{
19855 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857}
19858
19859/*
19860 * "winnr()" function
19861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019863f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019864 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866{
19867 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019868
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019870 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019872 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873}
19874
19875/*
19876 * "winrestcmd()" function
19877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019879f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019880 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882{
19883#ifdef FEAT_WINDOWS
19884 win_T *wp;
19885 int winnr = 1;
19886 garray_T ga;
19887 char_u buf[50];
19888
19889 ga_init2(&ga, (int)sizeof(char), 70);
19890 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19891 {
19892 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19893 ga_concat(&ga, buf);
19894# ifdef FEAT_VERTSPLIT
19895 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19896 ga_concat(&ga, buf);
19897# endif
19898 ++winnr;
19899 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019900 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019902 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019904 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019906 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907}
19908
19909/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019910 * "winrestview()" function
19911 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019912 static void
19913f_winrestview(argvars, rettv)
19914 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019915 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019916{
19917 dict_T *dict;
19918
19919 if (argvars[0].v_type != VAR_DICT
19920 || (dict = argvars[0].vval.v_dict) == NULL)
19921 EMSG(_(e_invarg));
19922 else
19923 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019924 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19925 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19926 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19927 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019928#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019929 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19930 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019931#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019932 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19933 {
19934 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19935 curwin->w_set_curswant = FALSE;
19936 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019937
Bram Moolenaar82c25852014-05-28 16:47:16 +020019938 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19939 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019940#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019941 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19942 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019943#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019944 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19945 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19946 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19947 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019948
19949 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019950 win_new_height(curwin, curwin->w_height);
19951# ifdef FEAT_VERTSPLIT
19952 win_new_width(curwin, W_WIDTH(curwin));
19953# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019954 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019955
Bram Moolenaarb851a962014-10-31 15:45:52 +010019956 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019957 curwin->w_topline = 1;
19958 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19959 curwin->w_topline = curbuf->b_ml.ml_line_count;
19960#ifdef FEAT_DIFF
19961 check_topfill(curwin, TRUE);
19962#endif
19963 }
19964}
19965
19966/*
19967 * "winsaveview()" function
19968 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019969 static void
19970f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019971 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019972 typval_T *rettv;
19973{
19974 dict_T *dict;
19975
Bram Moolenaara800b422010-06-27 01:15:55 +020019976 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019977 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019978 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019979
19980 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19981 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19982#ifdef FEAT_VIRTUALEDIT
19983 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19984#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019985 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019986 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19987
19988 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19989#ifdef FEAT_DIFF
19990 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19991#endif
19992 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19993 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19994}
19995
19996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 * "winwidth(nr)" function
19998 */
19999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020000f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020001 typval_T *argvars;
20002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003{
20004 win_T *wp;
20005
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020006 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 else
20010#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020011 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020013 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014#endif
20015}
20016
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020018 * Write list of strings to file
20019 */
20020 static int
20021write_list(fd, list, binary)
20022 FILE *fd;
20023 list_T *list;
20024 int binary;
20025{
20026 listitem_T *li;
20027 int c;
20028 int ret = OK;
20029 char_u *s;
20030
20031 for (li = list->lv_first; li != NULL; li = li->li_next)
20032 {
20033 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20034 {
20035 if (*s == '\n')
20036 c = putc(NUL, fd);
20037 else
20038 c = putc(*s, fd);
20039 if (c == EOF)
20040 {
20041 ret = FAIL;
20042 break;
20043 }
20044 }
20045 if (!binary || li->li_next != NULL)
20046 if (putc('\n', fd) == EOF)
20047 {
20048 ret = FAIL;
20049 break;
20050 }
20051 if (ret == FAIL)
20052 {
20053 EMSG(_(e_write));
20054 break;
20055 }
20056 }
20057 return ret;
20058}
20059
20060/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020061 * "writefile()" function
20062 */
20063 static void
20064f_writefile(argvars, rettv)
20065 typval_T *argvars;
20066 typval_T *rettv;
20067{
20068 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020069 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020070 char_u *fname;
20071 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020072 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020073
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020074 if (check_restricted() || check_secure())
20075 return;
20076
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020077 if (argvars[0].v_type != VAR_LIST)
20078 {
20079 EMSG2(_(e_listarg), "writefile()");
20080 return;
20081 }
20082 if (argvars[0].vval.v_list == NULL)
20083 return;
20084
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020085 if (argvars[2].v_type != VAR_UNKNOWN)
20086 {
20087 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20088 binary = TRUE;
20089 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20090 append = TRUE;
20091 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020092
20093 /* Always open the file in binary mode, library functions have a mind of
20094 * their own about CR-LF conversion. */
20095 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020096 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20097 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020098 {
20099 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20100 ret = -1;
20101 }
20102 else
20103 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020104 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20105 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020106 fclose(fd);
20107 }
20108
20109 rettv->vval.v_number = ret;
20110}
20111
20112/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020113 * "xor(expr, expr)" function
20114 */
20115 static void
20116f_xor(argvars, rettv)
20117 typval_T *argvars;
20118 typval_T *rettv;
20119{
20120 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20121 ^ get_tv_number_chk(&argvars[1], NULL);
20122}
20123
20124
20125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020127 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 */
20129 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020130var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020132 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020133 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020135 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020137 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138
Bram Moolenaara5525202006-03-02 22:52:09 +000020139 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020140 if (varp->v_type == VAR_LIST)
20141 {
20142 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020143 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020144 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020145 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020146
20147 l = varp->vval.v_list;
20148 if (l == NULL)
20149 return NULL;
20150
20151 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020152 pos.lnum = list_find_nr(l, 0L, &error);
20153 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020154 return NULL; /* invalid line number */
20155
20156 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020157 pos.col = list_find_nr(l, 1L, &error);
20158 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020159 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020160 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020161
20162 /* We accept "$" for the column number: last column. */
20163 li = list_find(l, 1L);
20164 if (li != NULL && li->li_tv.v_type == VAR_STRING
20165 && li->li_tv.vval.v_string != NULL
20166 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20167 pos.col = len + 1;
20168
Bram Moolenaara5525202006-03-02 22:52:09 +000020169 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020170 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020171 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020172 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020173
Bram Moolenaara5525202006-03-02 22:52:09 +000020174#ifdef FEAT_VIRTUALEDIT
20175 /* Get the virtual offset. Defaults to zero. */
20176 pos.coladd = list_find_nr(l, 2L, &error);
20177 if (error)
20178 pos.coladd = 0;
20179#endif
20180
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020181 return &pos;
20182 }
20183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020184 name = get_tv_string_chk(varp);
20185 if (name == NULL)
20186 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020187 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020189 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20190 {
20191 if (VIsual_active)
20192 return &VIsual;
20193 return &curwin->w_cursor;
20194 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020195 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020197 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20199 return NULL;
20200 return pp;
20201 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020202
20203#ifdef FEAT_VIRTUALEDIT
20204 pos.coladd = 0;
20205#endif
20206
Bram Moolenaar477933c2007-07-17 14:32:23 +000020207 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020208 {
20209 pos.col = 0;
20210 if (name[1] == '0') /* "w0": first visible line */
20211 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020212 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020213 pos.lnum = curwin->w_topline;
20214 return &pos;
20215 }
20216 else if (name[1] == '$') /* "w$": last visible line */
20217 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020218 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020219 pos.lnum = curwin->w_botline - 1;
20220 return &pos;
20221 }
20222 }
20223 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020225 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 {
20227 pos.lnum = curbuf->b_ml.ml_line_count;
20228 pos.col = 0;
20229 }
20230 else
20231 {
20232 pos.lnum = curwin->w_cursor.lnum;
20233 pos.col = (colnr_T)STRLEN(ml_get_curline());
20234 }
20235 return &pos;
20236 }
20237 return NULL;
20238}
20239
20240/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020241 * Convert list in "arg" into a position and optional file number.
20242 * When "fnump" is NULL there is no file number, only 3 items.
20243 * Note that the column is passed on as-is, the caller may want to decrement
20244 * it to use 1 for the first column.
20245 * Return FAIL when conversion is not possible, doesn't check the position for
20246 * validity.
20247 */
20248 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020249list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020250 typval_T *arg;
20251 pos_T *posp;
20252 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020253 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020254{
20255 list_T *l = arg->vval.v_list;
20256 long i = 0;
20257 long n;
20258
Bram Moolenaar493c1782014-05-28 14:34:46 +020020259 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20260 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020261 if (arg->v_type != VAR_LIST
20262 || l == NULL
20263 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020264 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020265 return FAIL;
20266
20267 if (fnump != NULL)
20268 {
20269 n = list_find_nr(l, i++, NULL); /* fnum */
20270 if (n < 0)
20271 return FAIL;
20272 if (n == 0)
20273 n = curbuf->b_fnum; /* current buffer */
20274 *fnump = n;
20275 }
20276
20277 n = list_find_nr(l, i++, NULL); /* lnum */
20278 if (n < 0)
20279 return FAIL;
20280 posp->lnum = n;
20281
20282 n = list_find_nr(l, i++, NULL); /* col */
20283 if (n < 0)
20284 return FAIL;
20285 posp->col = n;
20286
20287#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020288 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020289 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020290 posp->coladd = 0;
20291 else
20292 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020293#endif
20294
Bram Moolenaar493c1782014-05-28 14:34:46 +020020295 if (curswantp != NULL)
20296 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20297
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020298 return OK;
20299}
20300
20301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 * Get the length of an environment variable name.
20303 * Advance "arg" to the first character after the name.
20304 * Return 0 for error.
20305 */
20306 static int
20307get_env_len(arg)
20308 char_u **arg;
20309{
20310 char_u *p;
20311 int len;
20312
20313 for (p = *arg; vim_isIDc(*p); ++p)
20314 ;
20315 if (p == *arg) /* no name found */
20316 return 0;
20317
20318 len = (int)(p - *arg);
20319 *arg = p;
20320 return len;
20321}
20322
20323/*
20324 * Get the length of the name of a function or internal variable.
20325 * "arg" is advanced to the first non-white character after the name.
20326 * Return 0 if something is wrong.
20327 */
20328 static int
20329get_id_len(arg)
20330 char_u **arg;
20331{
20332 char_u *p;
20333 int len;
20334
20335 /* Find the end of the name. */
20336 for (p = *arg; eval_isnamec(*p); ++p)
20337 ;
20338 if (p == *arg) /* no name found */
20339 return 0;
20340
20341 len = (int)(p - *arg);
20342 *arg = skipwhite(p);
20343
20344 return len;
20345}
20346
20347/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020348 * Get the length of the name of a variable or function.
20349 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020351 * Return -1 if curly braces expansion failed.
20352 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 * If the name contains 'magic' {}'s, expand them and return the
20354 * expanded name in an allocated string via 'alias' - caller must free.
20355 */
20356 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020357get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 char_u **arg;
20359 char_u **alias;
20360 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020361 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362{
20363 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 char_u *p;
20365 char_u *expr_start;
20366 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367
20368 *alias = NULL; /* default to no alias */
20369
20370 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20371 && (*arg)[2] == (int)KE_SNR)
20372 {
20373 /* hard coded <SNR>, already translated */
20374 *arg += 3;
20375 return get_id_len(arg) + 3;
20376 }
20377 len = eval_fname_script(*arg);
20378 if (len > 0)
20379 {
20380 /* literal "<SID>", "s:" or "<SNR>" */
20381 *arg += len;
20382 }
20383
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020385 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020387 p = find_name_end(*arg, &expr_start, &expr_end,
20388 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 if (expr_start != NULL)
20390 {
20391 char_u *temp_string;
20392
20393 if (!evaluate)
20394 {
20395 len += (int)(p - *arg);
20396 *arg = skipwhite(p);
20397 return len;
20398 }
20399
20400 /*
20401 * Include any <SID> etc in the expanded string:
20402 * Thus the -len here.
20403 */
20404 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20405 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020406 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 *alias = temp_string;
20408 *arg = skipwhite(p);
20409 return (int)STRLEN(temp_string);
20410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411
20412 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020413 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 EMSG2(_(e_invexpr2), *arg);
20415
20416 return len;
20417}
20418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419/*
20420 * Find the end of a variable or function name, taking care of magic braces.
20421 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20422 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020423 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 * Return a pointer to just after the name. Equal to "arg" if there is no
20425 * valid name.
20426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020428find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 char_u *arg;
20430 char_u **expr_start;
20431 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020432 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 int mb_nest = 0;
20435 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 char_u *p;
20437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020438 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020440 *expr_start = NULL;
20441 *expr_end = NULL;
20442 }
20443
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020444 /* Quick check for valid starting character. */
20445 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20446 return arg;
20447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020448 for (p = arg; *p != NUL
20449 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020450 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020451 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020452 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020453 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020455 if (*p == '\'')
20456 {
20457 /* skip over 'string' to avoid counting [ and ] inside it. */
20458 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20459 ;
20460 if (*p == NUL)
20461 break;
20462 }
20463 else if (*p == '"')
20464 {
20465 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20466 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20467 if (*p == '\\' && p[1] != NUL)
20468 ++p;
20469 if (*p == NUL)
20470 break;
20471 }
20472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020473 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020475 if (*p == '[')
20476 ++br_nest;
20477 else if (*p == ']')
20478 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020481 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020483 if (*p == '{')
20484 {
20485 mb_nest++;
20486 if (expr_start != NULL && *expr_start == NULL)
20487 *expr_start = p;
20488 }
20489 else if (*p == '}')
20490 {
20491 mb_nest--;
20492 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20493 *expr_end = p;
20494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 }
20497
20498 return p;
20499}
20500
20501/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020502 * Expands out the 'magic' {}'s in a variable/function name.
20503 * Note that this can call itself recursively, to deal with
20504 * constructs like foo{bar}{baz}{bam}
20505 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20506 * "in_start" ^
20507 * "expr_start" ^
20508 * "expr_end" ^
20509 * "in_end" ^
20510 *
20511 * Returns a new allocated string, which the caller must free.
20512 * Returns NULL for failure.
20513 */
20514 static char_u *
20515make_expanded_name(in_start, expr_start, expr_end, in_end)
20516 char_u *in_start;
20517 char_u *expr_start;
20518 char_u *expr_end;
20519 char_u *in_end;
20520{
20521 char_u c1;
20522 char_u *retval = NULL;
20523 char_u *temp_result;
20524 char_u *nextcmd = NULL;
20525
20526 if (expr_end == NULL || in_end == NULL)
20527 return NULL;
20528 *expr_start = NUL;
20529 *expr_end = NUL;
20530 c1 = *in_end;
20531 *in_end = NUL;
20532
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020533 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020534 if (temp_result != NULL && nextcmd == NULL)
20535 {
20536 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20537 + (in_end - expr_end) + 1));
20538 if (retval != NULL)
20539 {
20540 STRCPY(retval, in_start);
20541 STRCAT(retval, temp_result);
20542 STRCAT(retval, expr_end + 1);
20543 }
20544 }
20545 vim_free(temp_result);
20546
20547 *in_end = c1; /* put char back for error messages */
20548 *expr_start = '{';
20549 *expr_end = '}';
20550
20551 if (retval != NULL)
20552 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020553 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020554 if (expr_start != NULL)
20555 {
20556 /* Further expansion! */
20557 temp_result = make_expanded_name(retval, expr_start,
20558 expr_end, temp_result);
20559 vim_free(retval);
20560 retval = temp_result;
20561 }
20562 }
20563
20564 return retval;
20565}
20566
20567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020569 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 */
20571 static int
20572eval_isnamec(c)
20573 int c;
20574{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020575 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20576}
20577
20578/*
20579 * Return TRUE if character "c" can be used as the first character in a
20580 * variable or function name (excluding '{' and '}').
20581 */
20582 static int
20583eval_isnamec1(c)
20584 int c;
20585{
20586 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587}
20588
20589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 * Set number v: variable to "val".
20591 */
20592 void
20593set_vim_var_nr(idx, val)
20594 int idx;
20595 long val;
20596{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020597 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598}
20599
20600/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020601 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 */
20603 long
20604get_vim_var_nr(idx)
20605 int idx;
20606{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020607 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608}
20609
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020610/*
20611 * Get string v: variable value. Uses a static buffer, can only be used once.
20612 */
20613 char_u *
20614get_vim_var_str(idx)
20615 int idx;
20616{
20617 return get_tv_string(&vimvars[idx].vv_tv);
20618}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020619
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020621 * Get List v: variable value. Caller must take care of reference count when
20622 * needed.
20623 */
20624 list_T *
20625get_vim_var_list(idx)
20626 int idx;
20627{
20628 return vimvars[idx].vv_list;
20629}
20630
20631/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020632 * Set v:char to character "c".
20633 */
20634 void
20635set_vim_var_char(c)
20636 int c;
20637{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020638 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020639
20640#ifdef FEAT_MBYTE
20641 if (has_mbyte)
20642 buf[(*mb_char2bytes)(c, buf)] = NUL;
20643 else
20644#endif
20645 {
20646 buf[0] = c;
20647 buf[1] = NUL;
20648 }
20649 set_vim_var_string(VV_CHAR, buf, -1);
20650}
20651
20652/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020653 * Set v:count to "count" and v:count1 to "count1".
20654 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 */
20656 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020657set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 long count;
20659 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020660 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020662 if (set_prevcount)
20663 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020664 vimvars[VV_COUNT].vv_nr = count;
20665 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666}
20667
20668/*
20669 * Set string v: variable to a copy of "val".
20670 */
20671 void
20672set_vim_var_string(idx, val, len)
20673 int idx;
20674 char_u *val;
20675 int len; /* length of "val" to use or -1 (whole string) */
20676{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020677 /* Need to do this (at least) once, since we can't initialize a union.
20678 * Will always be invoked when "v:progname" is set. */
20679 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20680
Bram Moolenaare9a41262005-01-15 22:18:47 +000020681 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020683 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020685 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688}
20689
20690/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020691 * Set List v: variable to "val".
20692 */
20693 void
20694set_vim_var_list(idx, val)
20695 int idx;
20696 list_T *val;
20697{
20698 list_unref(vimvars[idx].vv_list);
20699 vimvars[idx].vv_list = val;
20700 if (val != NULL)
20701 ++val->lv_refcount;
20702}
20703
20704/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020705 * Set Dictionary v: variable to "val".
20706 */
20707 void
20708set_vim_var_dict(idx, val)
20709 int idx;
20710 dict_T *val;
20711{
20712 int todo;
20713 hashitem_T *hi;
20714
20715 dict_unref(vimvars[idx].vv_dict);
20716 vimvars[idx].vv_dict = val;
20717 if (val != NULL)
20718 {
20719 ++val->dv_refcount;
20720
20721 /* Set readonly */
20722 todo = (int)val->dv_hashtab.ht_used;
20723 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20724 {
20725 if (HASHITEM_EMPTY(hi))
20726 continue;
20727 --todo;
20728 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20729 }
20730 }
20731}
20732
20733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 * Set v:register if needed.
20735 */
20736 void
20737set_reg_var(c)
20738 int c;
20739{
20740 char_u regname;
20741
20742 if (c == 0 || c == ' ')
20743 regname = '"';
20744 else
20745 regname = c;
20746 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020747 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 set_vim_var_string(VV_REG, &regname, 1);
20749}
20750
20751/*
20752 * Get or set v:exception. If "oldval" == NULL, return the current value.
20753 * Otherwise, restore the value to "oldval" and return NULL.
20754 * Must always be called in pairs to save and restore v:exception! Does not
20755 * take care of memory allocations.
20756 */
20757 char_u *
20758v_exception(oldval)
20759 char_u *oldval;
20760{
20761 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020762 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763
Bram Moolenaare9a41262005-01-15 22:18:47 +000020764 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 return NULL;
20766}
20767
20768/*
20769 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20770 * Otherwise, restore the value to "oldval" and return NULL.
20771 * Must always be called in pairs to save and restore v:throwpoint! Does not
20772 * take care of memory allocations.
20773 */
20774 char_u *
20775v_throwpoint(oldval)
20776 char_u *oldval;
20777{
20778 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020779 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780
Bram Moolenaare9a41262005-01-15 22:18:47 +000020781 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 return NULL;
20783}
20784
20785#if defined(FEAT_AUTOCMD) || defined(PROTO)
20786/*
20787 * Set v:cmdarg.
20788 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20789 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20790 * Must always be called in pairs!
20791 */
20792 char_u *
20793set_cmdarg(eap, oldarg)
20794 exarg_T *eap;
20795 char_u *oldarg;
20796{
20797 char_u *oldval;
20798 char_u *newval;
20799 unsigned len;
20800
Bram Moolenaare9a41262005-01-15 22:18:47 +000020801 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020802 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020804 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020805 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020806 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 }
20808
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020809 if (eap->force_bin == FORCE_BIN)
20810 len = 6;
20811 else if (eap->force_bin == FORCE_NOBIN)
20812 len = 8;
20813 else
20814 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020815
20816 if (eap->read_edit)
20817 len += 7;
20818
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020819 if (eap->force_ff != 0)
20820 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20821# ifdef FEAT_MBYTE
20822 if (eap->force_enc != 0)
20823 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020824 if (eap->bad_char != 0)
20825 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020826# endif
20827
20828 newval = alloc(len + 1);
20829 if (newval == NULL)
20830 return NULL;
20831
20832 if (eap->force_bin == FORCE_BIN)
20833 sprintf((char *)newval, " ++bin");
20834 else if (eap->force_bin == FORCE_NOBIN)
20835 sprintf((char *)newval, " ++nobin");
20836 else
20837 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020838
20839 if (eap->read_edit)
20840 STRCAT(newval, " ++edit");
20841
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020842 if (eap->force_ff != 0)
20843 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20844 eap->cmd + eap->force_ff);
20845# ifdef FEAT_MBYTE
20846 if (eap->force_enc != 0)
20847 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20848 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020849 if (eap->bad_char == BAD_KEEP)
20850 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20851 else if (eap->bad_char == BAD_DROP)
20852 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20853 else if (eap->bad_char != 0)
20854 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020855# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020856 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020857 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858}
20859#endif
20860
20861/*
20862 * Get the value of internal variable "name".
20863 * Return OK or FAIL.
20864 */
20865 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020866get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 char_u *name;
20868 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020869 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020870 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020871 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020872 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873{
20874 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020875 typval_T *tv = NULL;
20876 typval_T atv;
20877 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879
20880 /* truncate the name, so that we can use strcmp() */
20881 cc = name[len];
20882 name[len] = NUL;
20883
20884 /*
20885 * Check for "b:changedtick".
20886 */
20887 if (STRCMP(name, "b:changedtick") == 0)
20888 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020889 atv.v_type = VAR_NUMBER;
20890 atv.vval.v_number = curbuf->b_changedtick;
20891 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 }
20893
20894 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 * Check for user-defined variables.
20896 */
20897 else
20898 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020899 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020901 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020902 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020903 if (dip != NULL)
20904 *dip = v;
20905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 }
20907
Bram Moolenaare9a41262005-01-15 22:18:47 +000020908 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020910 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020911 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 ret = FAIL;
20913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020914 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020915 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916
20917 name[len] = cc;
20918
20919 return ret;
20920}
20921
20922/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020923 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20924 * Also handle function call with Funcref variable: func(expr)
20925 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20926 */
20927 static int
20928handle_subscript(arg, rettv, evaluate, verbose)
20929 char_u **arg;
20930 typval_T *rettv;
20931 int evaluate; /* do more than finding the end */
20932 int verbose; /* give error messages */
20933{
20934 int ret = OK;
20935 dict_T *selfdict = NULL;
20936 char_u *s;
20937 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020938 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020939
20940 while (ret == OK
20941 && (**arg == '['
20942 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020943 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020944 && !vim_iswhite(*(*arg - 1)))
20945 {
20946 if (**arg == '(')
20947 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020948 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020949 if (evaluate)
20950 {
20951 functv = *rettv;
20952 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020953
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020954 /* Invoke the function. Recursive! */
20955 s = functv.vval.v_string;
20956 }
20957 else
20958 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020959 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020960 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20961 &len, evaluate, selfdict);
20962
20963 /* Clear the funcref afterwards, so that deleting it while
20964 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020965 if (evaluate)
20966 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020967
20968 /* Stop the expression evaluation when immediately aborting on
20969 * error, or when an interrupt occurred or an exception was thrown
20970 * but not caught. */
20971 if (aborting())
20972 {
20973 if (ret == OK)
20974 clear_tv(rettv);
20975 ret = FAIL;
20976 }
20977 dict_unref(selfdict);
20978 selfdict = NULL;
20979 }
20980 else /* **arg == '[' || **arg == '.' */
20981 {
20982 dict_unref(selfdict);
20983 if (rettv->v_type == VAR_DICT)
20984 {
20985 selfdict = rettv->vval.v_dict;
20986 if (selfdict != NULL)
20987 ++selfdict->dv_refcount;
20988 }
20989 else
20990 selfdict = NULL;
20991 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20992 {
20993 clear_tv(rettv);
20994 ret = FAIL;
20995 }
20996 }
20997 }
20998 dict_unref(selfdict);
20999 return ret;
21000}
21001
21002/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021003 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021004 * value).
21005 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021006 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021007alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021008{
Bram Moolenaar33570922005-01-25 22:26:29 +000021009 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021010}
21011
21012/*
21013 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 * The string "s" must have been allocated, it is consumed.
21015 * Return NULL for out of memory, the variable otherwise.
21016 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021017 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021018alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 char_u *s;
21020{
Bram Moolenaar33570922005-01-25 22:26:29 +000021021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023 rettv = alloc_tv();
21024 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021026 rettv->v_type = VAR_STRING;
21027 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 }
21029 else
21030 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021031 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032}
21033
21034/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021035 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021037 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021038free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021039 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040{
21041 if (varp != NULL)
21042 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021043 switch (varp->v_type)
21044 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021045 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021046 func_unref(varp->vval.v_string);
21047 /*FALLTHROUGH*/
21048 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021049 vim_free(varp->vval.v_string);
21050 break;
21051 case VAR_LIST:
21052 list_unref(varp->vval.v_list);
21053 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021054 case VAR_DICT:
21055 dict_unref(varp->vval.v_dict);
21056 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021057 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021058#ifdef FEAT_FLOAT
21059 case VAR_FLOAT:
21060#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021061 case VAR_UNKNOWN:
21062 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021063 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021064 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021065 break;
21066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 vim_free(varp);
21068 }
21069}
21070
21071/*
21072 * Free the memory for a variable value and set the value to NULL or 0.
21073 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021074 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021075clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021076 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077{
21078 if (varp != NULL)
21079 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021080 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021082 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021083 func_unref(varp->vval.v_string);
21084 /*FALLTHROUGH*/
21085 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021086 vim_free(varp->vval.v_string);
21087 varp->vval.v_string = NULL;
21088 break;
21089 case VAR_LIST:
21090 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021091 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021092 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021093 case VAR_DICT:
21094 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021095 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021096 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021097 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021098 varp->vval.v_number = 0;
21099 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021100#ifdef FEAT_FLOAT
21101 case VAR_FLOAT:
21102 varp->vval.v_float = 0.0;
21103 break;
21104#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021105 case VAR_UNKNOWN:
21106 break;
21107 default:
21108 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021110 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 }
21112}
21113
21114/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021115 * Set the value of a variable to NULL without freeing items.
21116 */
21117 static void
21118init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021120{
21121 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021123}
21124
21125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 * Get the number value of a variable.
21127 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021128 * For incompatible types, return 0.
21129 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21130 * caller of incompatible types: it sets *denote to TRUE if "denote"
21131 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 */
21133 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021134get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021137 int error = FALSE;
21138
21139 return get_tv_number_chk(varp, &error); /* return 0L on error */
21140}
21141
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021142 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021143get_tv_number_chk(varp, denote)
21144 typval_T *varp;
21145 int *denote;
21146{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021147 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021149 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021152 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021153#ifdef FEAT_FLOAT
21154 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021155 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021156 break;
21157#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021158 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021159 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021160 break;
21161 case VAR_STRING:
21162 if (varp->vval.v_string != NULL)
21163 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021164 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021165 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021166 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021167 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021168 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021169 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021170 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021171 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021172 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021173 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021174 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021176 if (denote == NULL) /* useful for values that must be unsigned */
21177 n = -1;
21178 else
21179 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021180 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181}
21182
21183/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021184 * Get the lnum from the first argument.
21185 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021186 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 */
21188 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021190 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191{
Bram Moolenaar33570922005-01-25 22:26:29 +000021192 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 linenr_T lnum;
21194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021195 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 if (lnum == 0) /* no valid number, try using line() */
21197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021198 rettv.v_type = VAR_NUMBER;
21199 f_line(argvars, &rettv);
21200 lnum = rettv.vval.v_number;
21201 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 }
21203 return lnum;
21204}
21205
21206/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021207 * Get the lnum from the first argument.
21208 * Also accepts "$", then "buf" is used.
21209 * Returns 0 on error.
21210 */
21211 static linenr_T
21212get_tv_lnum_buf(argvars, buf)
21213 typval_T *argvars;
21214 buf_T *buf;
21215{
21216 if (argvars[0].v_type == VAR_STRING
21217 && argvars[0].vval.v_string != NULL
21218 && argvars[0].vval.v_string[0] == '$'
21219 && buf != NULL)
21220 return buf->b_ml.ml_line_count;
21221 return get_tv_number_chk(&argvars[0], NULL);
21222}
21223
21224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 * Get the string value of a variable.
21226 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021227 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21228 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 * If the String variable has never been set, return an empty string.
21230 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021231 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21232 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 */
21234 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021235get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021236 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237{
21238 static char_u mybuf[NUMBUFLEN];
21239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021240 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241}
21242
21243 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021244get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021245 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021246 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021248 char_u *res = get_tv_string_buf_chk(varp, buf);
21249
21250 return res != NULL ? res : (char_u *)"";
21251}
21252
Bram Moolenaar7d647822014-04-05 21:28:56 +020021253/*
21254 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21255 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021256 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021257get_tv_string_chk(varp)
21258 typval_T *varp;
21259{
21260 static char_u mybuf[NUMBUFLEN];
21261
21262 return get_tv_string_buf_chk(varp, mybuf);
21263}
21264
21265 static char_u *
21266get_tv_string_buf_chk(varp, buf)
21267 typval_T *varp;
21268 char_u *buf;
21269{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021270 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021272 case VAR_NUMBER:
21273 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21274 return buf;
21275 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021276 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021277 break;
21278 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021279 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021280 break;
21281 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021282 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021283 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284#ifdef FEAT_FLOAT
21285 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021286 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021287 break;
21288#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 case VAR_STRING:
21290 if (varp->vval.v_string != NULL)
21291 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021292 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021293 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021294 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021297 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298}
21299
21300/*
21301 * Find variable "name" in the list of variables.
21302 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021303 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021304 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021305 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021308find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021311 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021314 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315
Bram Moolenaara7043832005-01-21 11:56:39 +000021316 ht = find_var_ht(name, &varname);
21317 if (htp != NULL)
21318 *htp = ht;
21319 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021321 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322}
21323
21324/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021325 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021326 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021328 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021329find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021330 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021331 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021332 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021333 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021334{
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 hashitem_T *hi;
21336
21337 if (*varname == NUL)
21338 {
21339 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021340 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021341 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021342 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021343 case 'g': return &globvars_var;
21344 case 'v': return &vimvars_var;
21345 case 'b': return &curbuf->b_bufvar;
21346 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021347#ifdef FEAT_WINDOWS
21348 case 't': return &curtab->tp_winvar;
21349#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021350 case 'l': return current_funccal == NULL
21351 ? NULL : &current_funccal->l_vars_var;
21352 case 'a': return current_funccal == NULL
21353 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021354 }
21355 return NULL;
21356 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021357
21358 hi = hash_find(ht, varname);
21359 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021360 {
21361 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021362 * worked find the variable again. Don't auto-load a script if it was
21363 * loaded already, otherwise it would be loaded every time when
21364 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021365 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021366 {
21367 /* Note: script_autoload() may make "hi" invalid. It must either
21368 * be obtained again or not used. */
21369 if (!script_autoload(varname, FALSE) || aborting())
21370 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021371 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021372 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021373 if (HASHITEM_EMPTY(hi))
21374 return NULL;
21375 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021376 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021377}
21378
21379/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021381 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021382 * Set "varname" to the start of name without ':'.
21383 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021384 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021385find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 char_u *name;
21387 char_u **varname;
21388{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021389 hashitem_T *hi;
21390
Bram Moolenaar73627d02015-08-11 15:46:09 +020021391 if (name[0] == NUL)
21392 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 if (name[1] != ':')
21394 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021395 /* The name must not start with a colon or #. */
21396 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 return NULL;
21398 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021399
21400 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021401 hi = hash_find(&compat_hashtab, name);
21402 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021403 return &compat_hashtab;
21404
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021406 return &globvarht; /* global variable */
21407 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 }
21409 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021410 if (*name == 'g') /* global variable */
21411 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021412 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21413 */
21414 if (vim_strchr(name + 2, ':') != NULL
21415 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021418 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021420 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021421#ifdef FEAT_WINDOWS
21422 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021423 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021424#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021425 if (*name == 'v') /* v: variable */
21426 return &vimvarht;
21427 if (*name == 'a' && current_funccal != NULL) /* function argument */
21428 return &current_funccal->l_avars.dv_hashtab;
21429 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21430 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 if (*name == 's' /* script variable */
21432 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21433 return &SCRIPT_VARS(current_SID);
21434 return NULL;
21435}
21436
21437/*
21438 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021439 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 * Returns NULL when it doesn't exist.
21441 */
21442 char_u *
21443get_var_value(name)
21444 char_u *name;
21445{
Bram Moolenaar33570922005-01-25 22:26:29 +000021446 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021448 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 if (v == NULL)
21450 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021451 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452}
21453
21454/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 * sourcing this script and when executing functions defined in the script.
21457 */
21458 void
21459new_script_vars(id)
21460 scid_T id;
21461{
Bram Moolenaara7043832005-01-21 11:56:39 +000021462 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 hashtab_T *ht;
21464 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021465
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21467 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021468 /* Re-allocating ga_data means that an ht_array pointing to
21469 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021471 for (i = 1; i <= ga_scripts.ga_len; ++i)
21472 {
21473 ht = &SCRIPT_VARS(i);
21474 if (ht->ht_mask == HT_INIT_SIZE - 1)
21475 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021476 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021477 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021478 }
21479
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 while (ga_scripts.ga_len < id)
21481 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021482 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021483 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021484 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 }
21487 }
21488}
21489
21490/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021491 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21492 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 */
21494 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021495init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021496 dict_T *dict;
21497 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021498 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499{
Bram Moolenaar33570922005-01-25 22:26:29 +000021500 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021501 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021502 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021503 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021504 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 dict_var->di_tv.vval.v_dict = dict;
21506 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021507 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021508 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21509 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510}
21511
21512/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021513 * Unreference a dictionary initialized by init_var_dict().
21514 */
21515 void
21516unref_var_dict(dict)
21517 dict_T *dict;
21518{
21519 /* Now the dict needs to be freed if no one else is using it, go back to
21520 * normal reference counting. */
21521 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21522 dict_unref(dict);
21523}
21524
21525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021527 * Frees all allocated variables and the value they contain.
21528 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 */
21530 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021531vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 hashtab_T *ht;
21533{
21534 vars_clear_ext(ht, TRUE);
21535}
21536
21537/*
21538 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21539 */
21540 static void
21541vars_clear_ext(ht, free_val)
21542 hashtab_T *ht;
21543 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544{
Bram Moolenaara7043832005-01-21 11:56:39 +000021545 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 hashitem_T *hi;
21547 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021550 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021551 for (hi = ht->ht_array; todo > 0; ++hi)
21552 {
21553 if (!HASHITEM_EMPTY(hi))
21554 {
21555 --todo;
21556
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021558 * ht_array might change then. hash_clear() takes care of it
21559 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 v = HI2DI(hi);
21561 if (free_val)
21562 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021563 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021565 }
21566 }
21567 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021568 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569}
21570
Bram Moolenaara7043832005-01-21 11:56:39 +000021571/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021572 * Delete a variable from hashtab "ht" at item "hi".
21573 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021576delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 hashtab_T *ht;
21578 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579{
Bram Moolenaar33570922005-01-25 22:26:29 +000021580 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021581
21582 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021583 clear_tv(&di->di_tv);
21584 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585}
21586
21587/*
21588 * List the value of one internal variable.
21589 */
21590 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021591list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021592 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021594 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021596 char_u *tofree;
21597 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021599
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021600 current_copyID += COPYID_INC;
21601 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021602 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021603 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021604 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605}
21606
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021608list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 char_u *prefix;
21610 char_u *name;
21611 int type;
21612 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021613 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614{
Bram Moolenaar31859182007-08-14 20:41:13 +000021615 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21616 msg_start();
21617 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 if (name != NULL) /* "a:" vars don't have a name stored */
21619 msg_puts(name);
21620 msg_putchar(' ');
21621 msg_advance(22);
21622 if (type == VAR_NUMBER)
21623 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 else if (type == VAR_FUNC)
21625 msg_putchar('*');
21626 else if (type == VAR_LIST)
21627 {
21628 msg_putchar('[');
21629 if (*string == '[')
21630 ++string;
21631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021632 else if (type == VAR_DICT)
21633 {
21634 msg_putchar('{');
21635 if (*string == '{')
21636 ++string;
21637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 else
21639 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021642
21643 if (type == VAR_FUNC)
21644 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021645 if (*first)
21646 {
21647 msg_clr_eos();
21648 *first = FALSE;
21649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650}
21651
21652/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021653 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 * If the variable already exists, the value is updated.
21655 * Otherwise the variable is created.
21656 */
21657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021658set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021661 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662{
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021667 ht = find_var_ht(name, &varname);
21668 if (ht == NULL || *varname == NUL)
21669 {
21670 EMSG2(_(e_illvar), name);
21671 return;
21672 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021673 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021674
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021675 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21676 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021677
Bram Moolenaar33570922005-01-25 22:26:29 +000021678 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021681 if (var_check_ro(v->di_flags, name, FALSE)
21682 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 return;
21684 if (v->di_tv.v_type != tv->v_type
21685 && !((v->di_tv.v_type == VAR_STRING
21686 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021687 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021688 || tv->v_type == VAR_NUMBER))
21689#ifdef FEAT_FLOAT
21690 && !((v->di_tv.v_type == VAR_NUMBER
21691 || v->di_tv.v_type == VAR_FLOAT)
21692 && (tv->v_type == VAR_NUMBER
21693 || tv->v_type == VAR_FLOAT))
21694#endif
21695 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021696 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021697 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021698 return;
21699 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021700
21701 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021702 * Handle setting internal v: variables separately where needed to
21703 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021704 */
21705 if (ht == &vimvarht)
21706 {
21707 if (v->di_tv.v_type == VAR_STRING)
21708 {
21709 vim_free(v->di_tv.vval.v_string);
21710 if (copy || tv->v_type != VAR_STRING)
21711 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21712 else
21713 {
21714 /* Take over the string to avoid an extra alloc/free. */
21715 v->di_tv.vval.v_string = tv->vval.v_string;
21716 tv->vval.v_string = NULL;
21717 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021718 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021720 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021721 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021723 if (STRCMP(varname, "searchforward") == 0)
21724 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021725#ifdef FEAT_SEARCH_EXTRA
21726 else if (STRCMP(varname, "hlsearch") == 0)
21727 {
21728 no_hlsearch = !v->di_tv.vval.v_number;
21729 redraw_all_later(SOME_VALID);
21730 }
21731#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021732 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021733 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021734 else if (v->di_tv.v_type != tv->v_type)
21735 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 }
21737
21738 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 }
21740 else /* add a new variable */
21741 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021742 /* Can't add "v:" variable. */
21743 if (ht == &vimvarht)
21744 {
21745 EMSG2(_(e_illvar), name);
21746 return;
21747 }
21748
Bram Moolenaar92124a32005-06-17 22:03:40 +000021749 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021750 if (!valid_varname(varname))
21751 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021752
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021753 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21754 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021755 if (v == NULL)
21756 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021758 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021760 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 return;
21762 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021763 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021765
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021766 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021768 else
21769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021770 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021771 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021772 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774}
21775
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021776/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021777 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 * Also give an error message.
21779 */
21780 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021781var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 int flags;
21783 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021784 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021785{
21786 if (flags & DI_FLAGS_RO)
21787 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021788 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021789 return TRUE;
21790 }
21791 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21792 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021793 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 return TRUE;
21795 }
21796 return FALSE;
21797}
21798
21799/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021800 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21801 * Also give an error message.
21802 */
21803 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021804var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021805 int flags;
21806 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021807 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021808{
21809 if (flags & DI_FLAGS_FIX)
21810 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021811 EMSG2(_("E795: Cannot delete variable %s"),
21812 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021813 return TRUE;
21814 }
21815 return FALSE;
21816}
21817
21818/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021819 * Check if a funcref is assigned to a valid variable name.
21820 * Return TRUE and give an error if not.
21821 */
21822 static int
21823var_check_func_name(name, new_var)
21824 char_u *name; /* points to start of variable name */
21825 int new_var; /* TRUE when creating the variable */
21826{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021827 /* Allow for w: b: s: and t:. */
21828 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021829 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21830 ? name[2] : name[0]))
21831 {
21832 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21833 name);
21834 return TRUE;
21835 }
21836 /* Don't allow hiding a function. When "v" is not NULL we might be
21837 * assigning another function to the same var, the type is checked
21838 * below. */
21839 if (new_var && function_exists(name))
21840 {
21841 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21842 name);
21843 return TRUE;
21844 }
21845 return FALSE;
21846}
21847
21848/*
21849 * Check if a variable name is valid.
21850 * Return FALSE and give an error if not.
21851 */
21852 static int
21853valid_varname(varname)
21854 char_u *varname;
21855{
21856 char_u *p;
21857
21858 for (p = varname; *p != NUL; ++p)
21859 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21860 && *p != AUTOLOAD_CHAR)
21861 {
21862 EMSG2(_(e_illvar), varname);
21863 return FALSE;
21864 }
21865 return TRUE;
21866}
21867
21868/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021869 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021870 * Also give an error message, using "name" or _("name") when use_gettext is
21871 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021872 */
21873 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021874tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021875 int lock;
21876 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021877 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021878{
21879 if (lock & VAR_LOCKED)
21880 {
21881 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021882 name == NULL ? (char_u *)_("Unknown")
21883 : use_gettext ? (char_u *)_(name)
21884 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021885 return TRUE;
21886 }
21887 if (lock & VAR_FIXED)
21888 {
21889 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021890 name == NULL ? (char_u *)_("Unknown")
21891 : use_gettext ? (char_u *)_(name)
21892 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021893 return TRUE;
21894 }
21895 return FALSE;
21896}
21897
21898/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021900 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021901 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021902 * It is OK for "from" and "to" to point to the same item. This is used to
21903 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021904 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021905 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 typval_T *from;
21908 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021910 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021911 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021912 switch (from->v_type)
21913 {
21914 case VAR_NUMBER:
21915 to->vval.v_number = from->vval.v_number;
21916 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021917#ifdef FEAT_FLOAT
21918 case VAR_FLOAT:
21919 to->vval.v_float = from->vval.v_float;
21920 break;
21921#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021922 case VAR_STRING:
21923 case VAR_FUNC:
21924 if (from->vval.v_string == NULL)
21925 to->vval.v_string = NULL;
21926 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021927 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021929 if (from->v_type == VAR_FUNC)
21930 func_ref(to->vval.v_string);
21931 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021932 break;
21933 case VAR_LIST:
21934 if (from->vval.v_list == NULL)
21935 to->vval.v_list = NULL;
21936 else
21937 {
21938 to->vval.v_list = from->vval.v_list;
21939 ++to->vval.v_list->lv_refcount;
21940 }
21941 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021942 case VAR_DICT:
21943 if (from->vval.v_dict == NULL)
21944 to->vval.v_dict = NULL;
21945 else
21946 {
21947 to->vval.v_dict = from->vval.v_dict;
21948 ++to->vval.v_dict->dv_refcount;
21949 }
21950 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021951 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021952 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 break;
21954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955}
21956
21957/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021958 * Make a copy of an item.
21959 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021960 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21961 * reference to an already copied list/dict can be used.
21962 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021963 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021964 static int
21965item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 typval_T *from;
21967 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021968 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021969 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021970{
21971 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021972 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021973
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021975 {
21976 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021977 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021978 }
21979 ++recurse;
21980
21981 switch (from->v_type)
21982 {
21983 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021984#ifdef FEAT_FLOAT
21985 case VAR_FLOAT:
21986#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021987 case VAR_STRING:
21988 case VAR_FUNC:
21989 copy_tv(from, to);
21990 break;
21991 case VAR_LIST:
21992 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021993 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021994 if (from->vval.v_list == NULL)
21995 to->vval.v_list = NULL;
21996 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21997 {
21998 /* use the copy made earlier */
21999 to->vval.v_list = from->vval.v_list->lv_copylist;
22000 ++to->vval.v_list->lv_refcount;
22001 }
22002 else
22003 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22004 if (to->vval.v_list == NULL)
22005 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022006 break;
22007 case VAR_DICT:
22008 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022009 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022010 if (from->vval.v_dict == NULL)
22011 to->vval.v_dict = NULL;
22012 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22013 {
22014 /* use the copy made earlier */
22015 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22016 ++to->vval.v_dict->dv_refcount;
22017 }
22018 else
22019 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22020 if (to->vval.v_dict == NULL)
22021 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022022 break;
22023 default:
22024 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026 }
22027 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022028 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022029}
22030
22031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 * ":echo expr1 ..." print each argument separated with a space, add a
22033 * newline at the end.
22034 * ":echon expr1 ..." print each argument plain.
22035 */
22036 void
22037ex_echo(eap)
22038 exarg_T *eap;
22039{
22040 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022042 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 char_u *p;
22044 int needclr = TRUE;
22045 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022046 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047
22048 if (eap->skip)
22049 ++emsg_skip;
22050 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22051 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022052 /* If eval1() causes an error message the text from the command may
22053 * still need to be cleared. E.g., "echo 22,44". */
22054 need_clr_eos = needclr;
22055
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022057 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 {
22059 /*
22060 * Report the invalid expression unless the expression evaluation
22061 * has been cancelled due to an aborting error, an interrupt, or an
22062 * exception.
22063 */
22064 if (!aborting())
22065 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022066 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 break;
22068 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022069 need_clr_eos = FALSE;
22070
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 if (!eap->skip)
22072 {
22073 if (atstart)
22074 {
22075 atstart = FALSE;
22076 /* Call msg_start() after eval1(), evaluating the expression
22077 * may cause a message to appear. */
22078 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022079 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022080 /* Mark the saved text as finishing the line, so that what
22081 * follows is displayed on a new line when scrolling back
22082 * at the more prompt. */
22083 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 }
22087 else if (eap->cmdidx == CMD_echo)
22088 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022089 current_copyID += COPYID_INC;
22090 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022091 if (p != NULL)
22092 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022094 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022096 if (*p != TAB && needclr)
22097 {
22098 /* remove any text still there from the command */
22099 msg_clr_eos();
22100 needclr = FALSE;
22101 }
22102 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 }
22104 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022105 {
22106#ifdef FEAT_MBYTE
22107 if (has_mbyte)
22108 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022109 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022110
22111 (void)msg_outtrans_len_attr(p, i, echo_attr);
22112 p += i - 1;
22113 }
22114 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022116 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022119 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022121 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 arg = skipwhite(arg);
22123 }
22124 eap->nextcmd = check_nextcmd(arg);
22125
22126 if (eap->skip)
22127 --emsg_skip;
22128 else
22129 {
22130 /* remove text that may still be there from the command */
22131 if (needclr)
22132 msg_clr_eos();
22133 if (eap->cmdidx == CMD_echo)
22134 msg_end();
22135 }
22136}
22137
22138/*
22139 * ":echohl {name}".
22140 */
22141 void
22142ex_echohl(eap)
22143 exarg_T *eap;
22144{
22145 int id;
22146
22147 id = syn_name2id(eap->arg);
22148 if (id == 0)
22149 echo_attr = 0;
22150 else
22151 echo_attr = syn_id2attr(id);
22152}
22153
22154/*
22155 * ":execute expr1 ..." execute the result of an expression.
22156 * ":echomsg expr1 ..." Print a message
22157 * ":echoerr expr1 ..." Print an error
22158 * Each gets spaces around each argument and a newline at the end for
22159 * echo commands
22160 */
22161 void
22162ex_execute(eap)
22163 exarg_T *eap;
22164{
22165 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022166 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 int ret = OK;
22168 char_u *p;
22169 garray_T ga;
22170 int len;
22171 int save_did_emsg;
22172
22173 ga_init2(&ga, 1, 80);
22174
22175 if (eap->skip)
22176 ++emsg_skip;
22177 while (*arg != NUL && *arg != '|' && *arg != '\n')
22178 {
22179 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022180 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 {
22182 /*
22183 * Report the invalid expression unless the expression evaluation
22184 * has been cancelled due to an aborting error, an interrupt, or an
22185 * exception.
22186 */
22187 if (!aborting())
22188 EMSG2(_(e_invexpr2), p);
22189 ret = FAIL;
22190 break;
22191 }
22192
22193 if (!eap->skip)
22194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022195 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 len = (int)STRLEN(p);
22197 if (ga_grow(&ga, len + 2) == FAIL)
22198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022199 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 ret = FAIL;
22201 break;
22202 }
22203 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 ga.ga_len += len;
22207 }
22208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022209 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 arg = skipwhite(arg);
22211 }
22212
22213 if (ret != FAIL && ga.ga_data != NULL)
22214 {
22215 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022218 out_flush();
22219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 else if (eap->cmdidx == CMD_echoerr)
22221 {
22222 /* We don't want to abort following commands, restore did_emsg. */
22223 save_did_emsg = did_emsg;
22224 EMSG((char_u *)ga.ga_data);
22225 if (!force_abort)
22226 did_emsg = save_did_emsg;
22227 }
22228 else if (eap->cmdidx == CMD_execute)
22229 do_cmdline((char_u *)ga.ga_data,
22230 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22231 }
22232
22233 ga_clear(&ga);
22234
22235 if (eap->skip)
22236 --emsg_skip;
22237
22238 eap->nextcmd = check_nextcmd(arg);
22239}
22240
22241/*
22242 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22243 * "arg" points to the "&" or '+' when called, to "option" when returning.
22244 * Returns NULL when no option name found. Otherwise pointer to the char
22245 * after the option name.
22246 */
22247 static char_u *
22248find_option_end(arg, opt_flags)
22249 char_u **arg;
22250 int *opt_flags;
22251{
22252 char_u *p = *arg;
22253
22254 ++p;
22255 if (*p == 'g' && p[1] == ':')
22256 {
22257 *opt_flags = OPT_GLOBAL;
22258 p += 2;
22259 }
22260 else if (*p == 'l' && p[1] == ':')
22261 {
22262 *opt_flags = OPT_LOCAL;
22263 p += 2;
22264 }
22265 else
22266 *opt_flags = 0;
22267
22268 if (!ASCII_ISALPHA(*p))
22269 return NULL;
22270 *arg = p;
22271
22272 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22273 p += 4; /* termcap option */
22274 else
22275 while (ASCII_ISALPHA(*p))
22276 ++p;
22277 return p;
22278}
22279
22280/*
22281 * ":function"
22282 */
22283 void
22284ex_function(eap)
22285 exarg_T *eap;
22286{
22287 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022288 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 int j;
22290 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022292 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 char_u *name = NULL;
22294 char_u *p;
22295 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022296 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 garray_T newargs;
22298 garray_T newlines;
22299 int varargs = FALSE;
22300 int mustend = FALSE;
22301 int flags = 0;
22302 ufunc_T *fp;
22303 int indent;
22304 int nesting;
22305 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 dictitem_T *v;
22307 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022308 static int func_nr = 0; /* number for nameless function */
22309 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022310 hashtab_T *ht;
22311 int todo;
22312 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022313 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314
22315 /*
22316 * ":function" without argument: list functions.
22317 */
22318 if (ends_excmd(*eap->arg))
22319 {
22320 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022321 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022322 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022323 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022324 {
22325 if (!HASHITEM_EMPTY(hi))
22326 {
22327 --todo;
22328 fp = HI2UF(hi);
22329 if (!isdigit(*fp->uf_name))
22330 list_func_head(fp, FALSE);
22331 }
22332 }
22333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 eap->nextcmd = check_nextcmd(eap->arg);
22335 return;
22336 }
22337
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022338 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022339 * ":function /pat": list functions matching pattern.
22340 */
22341 if (*eap->arg == '/')
22342 {
22343 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22344 if (!eap->skip)
22345 {
22346 regmatch_T regmatch;
22347
22348 c = *p;
22349 *p = NUL;
22350 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22351 *p = c;
22352 if (regmatch.regprog != NULL)
22353 {
22354 regmatch.rm_ic = p_ic;
22355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022356 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022357 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22358 {
22359 if (!HASHITEM_EMPTY(hi))
22360 {
22361 --todo;
22362 fp = HI2UF(hi);
22363 if (!isdigit(*fp->uf_name)
22364 && vim_regexec(&regmatch, fp->uf_name, 0))
22365 list_func_head(fp, FALSE);
22366 }
22367 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022368 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022369 }
22370 }
22371 if (*p == '/')
22372 ++p;
22373 eap->nextcmd = check_nextcmd(p);
22374 return;
22375 }
22376
22377 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022378 * Get the function name. There are these situations:
22379 * func normal function name
22380 * "name" == func, "fudi.fd_dict" == NULL
22381 * dict.func new dictionary entry
22382 * "name" == NULL, "fudi.fd_dict" set,
22383 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22384 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022385 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022386 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22387 * dict.func existing dict entry that's not a Funcref
22388 * "name" == NULL, "fudi.fd_dict" set,
22389 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022390 * s:func script-local function name
22391 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022394 name = trans_function_name(&p, eap->skip, 0, &fudi);
22395 paren = (vim_strchr(p, '(') != NULL);
22396 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 {
22398 /*
22399 * Return on an invalid expression in braces, unless the expression
22400 * evaluation has been cancelled due to an aborting error, an
22401 * interrupt, or an exception.
22402 */
22403 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022404 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022405 if (!eap->skip && fudi.fd_newkey != NULL)
22406 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022407 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 else
22411 eap->skip = TRUE;
22412 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022413
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 /* An error in a function call during evaluation of an expression in magic
22415 * braces should not cause the function not to be defined. */
22416 saved_did_emsg = did_emsg;
22417 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418
22419 /*
22420 * ":function func" with only function name: list function.
22421 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022422 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 {
22424 if (!ends_excmd(*skipwhite(p)))
22425 {
22426 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022427 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429 eap->nextcmd = check_nextcmd(p);
22430 if (eap->nextcmd != NULL)
22431 *p = NUL;
22432 if (!eap->skip && !got_int)
22433 {
22434 fp = find_func(name);
22435 if (fp != NULL)
22436 {
22437 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022438 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022440 if (FUNCLINE(fp, j) == NULL)
22441 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 msg_putchar('\n');
22443 msg_outnum((long)(j + 1));
22444 if (j < 9)
22445 msg_putchar(' ');
22446 if (j < 99)
22447 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022448 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 out_flush(); /* show a line at a time */
22450 ui_breakcheck();
22451 }
22452 if (!got_int)
22453 {
22454 msg_putchar('\n');
22455 msg_puts((char_u *)" endfunction");
22456 }
22457 }
22458 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022459 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022461 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 }
22463
22464 /*
22465 * ":function name(arg1, arg2)" Define function.
22466 */
22467 p = skipwhite(p);
22468 if (*p != '(')
22469 {
22470 if (!eap->skip)
22471 {
22472 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022473 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 }
22475 /* attempt to continue by skipping some text */
22476 if (vim_strchr(p, '(') != NULL)
22477 p = vim_strchr(p, '(');
22478 }
22479 p = skipwhite(p + 1);
22480
22481 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22482 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22483
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022484 if (!eap->skip)
22485 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022486 /* Check the name of the function. Unless it's a dictionary function
22487 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022488 if (name != NULL)
22489 arg = name;
22490 else
22491 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022492 if (arg != NULL && (fudi.fd_di == NULL
22493 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022494 {
22495 if (*arg == K_SPECIAL)
22496 j = 3;
22497 else
22498 j = 0;
22499 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22500 : eval_isnamec(arg[j])))
22501 ++j;
22502 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022503 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022504 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022505 /* Disallow using the g: dict. */
22506 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22507 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022508 }
22509
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 /*
22511 * Isolate the arguments: "arg1, arg2, ...)"
22512 */
22513 while (*p != ')')
22514 {
22515 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22516 {
22517 varargs = TRUE;
22518 p += 3;
22519 mustend = TRUE;
22520 }
22521 else
22522 {
22523 arg = p;
22524 while (ASCII_ISALNUM(*p) || *p == '_')
22525 ++p;
22526 if (arg == p || isdigit(*arg)
22527 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22528 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22529 {
22530 if (!eap->skip)
22531 EMSG2(_("E125: Illegal argument: %s"), arg);
22532 break;
22533 }
22534 if (ga_grow(&newargs, 1) == FAIL)
22535 goto erret;
22536 c = *p;
22537 *p = NUL;
22538 arg = vim_strsave(arg);
22539 if (arg == NULL)
22540 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022541
22542 /* Check for duplicate argument name. */
22543 for (i = 0; i < newargs.ga_len; ++i)
22544 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22545 {
22546 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022547 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022548 goto erret;
22549 }
22550
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22552 *p = c;
22553 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 if (*p == ',')
22555 ++p;
22556 else
22557 mustend = TRUE;
22558 }
22559 p = skipwhite(p);
22560 if (mustend && *p != ')')
22561 {
22562 if (!eap->skip)
22563 EMSG2(_(e_invarg2), eap->arg);
22564 break;
22565 }
22566 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022567 if (*p != ')')
22568 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 ++p; /* skip the ')' */
22570
Bram Moolenaare9a41262005-01-15 22:18:47 +000022571 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 for (;;)
22573 {
22574 p = skipwhite(p);
22575 if (STRNCMP(p, "range", 5) == 0)
22576 {
22577 flags |= FC_RANGE;
22578 p += 5;
22579 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022580 else if (STRNCMP(p, "dict", 4) == 0)
22581 {
22582 flags |= FC_DICT;
22583 p += 4;
22584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 else if (STRNCMP(p, "abort", 5) == 0)
22586 {
22587 flags |= FC_ABORT;
22588 p += 5;
22589 }
22590 else
22591 break;
22592 }
22593
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022594 /* When there is a line break use what follows for the function body.
22595 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22596 if (*p == '\n')
22597 line_arg = p + 1;
22598 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 EMSG(_(e_trailing));
22600
22601 /*
22602 * Read the body of the function, until ":endfunction" is found.
22603 */
22604 if (KeyTyped)
22605 {
22606 /* Check if the function already exists, don't let the user type the
22607 * whole function before telling him it doesn't work! For a script we
22608 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022609 if (!eap->skip && !eap->forceit)
22610 {
22611 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22612 EMSG(_(e_funcdict));
22613 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022614 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022617 if (!eap->skip && did_emsg)
22618 goto erret;
22619
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 msg_putchar('\n'); /* don't overwrite the function name */
22621 cmdline_row = msg_row;
22622 }
22623
22624 indent = 2;
22625 nesting = 0;
22626 for (;;)
22627 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022628 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022629 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022630 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022631 saved_wait_return = FALSE;
22632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022634 sourcing_lnum_off = sourcing_lnum;
22635
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022636 if (line_arg != NULL)
22637 {
22638 /* Use eap->arg, split up in parts by line breaks. */
22639 theline = line_arg;
22640 p = vim_strchr(theline, '\n');
22641 if (p == NULL)
22642 line_arg += STRLEN(line_arg);
22643 else
22644 {
22645 *p = NUL;
22646 line_arg = p + 1;
22647 }
22648 }
22649 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 theline = getcmdline(':', 0L, indent);
22651 else
22652 theline = eap->getline(':', eap->cookie, indent);
22653 if (KeyTyped)
22654 lines_left = Rows - 1;
22655 if (theline == NULL)
22656 {
22657 EMSG(_("E126: Missing :endfunction"));
22658 goto erret;
22659 }
22660
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022661 /* Detect line continuation: sourcing_lnum increased more than one. */
22662 if (sourcing_lnum > sourcing_lnum_off + 1)
22663 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22664 else
22665 sourcing_lnum_off = 0;
22666
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 if (skip_until != NULL)
22668 {
22669 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22670 * don't check for ":endfunc". */
22671 if (STRCMP(theline, skip_until) == 0)
22672 {
22673 vim_free(skip_until);
22674 skip_until = NULL;
22675 }
22676 }
22677 else
22678 {
22679 /* skip ':' and blanks*/
22680 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22681 ;
22682
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022683 /* Check for "endfunction". */
22684 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022686 if (line_arg == NULL)
22687 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 break;
22689 }
22690
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022691 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 * at "end". */
22693 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22694 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022695 else if (STRNCMP(p, "if", 2) == 0
22696 || STRNCMP(p, "wh", 2) == 0
22697 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 || STRNCMP(p, "try", 3) == 0)
22699 indent += 2;
22700
22701 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022702 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022704 if (*p == '!')
22705 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022707 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22708 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022710 ++nesting;
22711 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
22713 }
22714
22715 /* Check for ":append" or ":insert". */
22716 p = skip_range(p, NULL);
22717 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22718 || (p[0] == 'i'
22719 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22720 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22721 skip_until = vim_strsave((char_u *)".");
22722
22723 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22724 arg = skipwhite(skiptowhite(p));
22725 if (arg[0] == '<' && arg[1] =='<'
22726 && ((p[0] == 'p' && p[1] == 'y'
22727 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22728 || (p[0] == 'p' && p[1] == 'e'
22729 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22730 || (p[0] == 't' && p[1] == 'c'
22731 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022732 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22733 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22735 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022736 || (p[0] == 'm' && p[1] == 'z'
22737 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 ))
22739 {
22740 /* ":python <<" continues until a dot, like ":append" */
22741 p = skipwhite(arg + 2);
22742 if (*p == NUL)
22743 skip_until = vim_strsave((char_u *)".");
22744 else
22745 skip_until = vim_strsave(p);
22746 }
22747 }
22748
22749 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022750 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022751 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022752 if (line_arg == NULL)
22753 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022755 }
22756
22757 /* Copy the line to newly allocated memory. get_one_sourceline()
22758 * allocates 250 bytes per line, this saves 80% on average. The cost
22759 * is an extra alloc/free. */
22760 p = vim_strsave(theline);
22761 if (p != NULL)
22762 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022763 if (line_arg == NULL)
22764 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022765 theline = p;
22766 }
22767
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022768 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22769
22770 /* Add NULL lines for continuation lines, so that the line count is
22771 * equal to the index in the growarray. */
22772 while (sourcing_lnum_off-- > 0)
22773 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022774
22775 /* Check for end of eap->arg. */
22776 if (line_arg != NULL && *line_arg == NUL)
22777 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 }
22779
22780 /* Don't define the function when skipping commands or when an error was
22781 * detected. */
22782 if (eap->skip || did_emsg)
22783 goto erret;
22784
22785 /*
22786 * If there are no errors, add the function
22787 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022788 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022790 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022791 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022792 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022793 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022794 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022795 goto erret;
22796 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022797
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022798 fp = find_func(name);
22799 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022801 if (!eap->forceit)
22802 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022803 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022804 goto erret;
22805 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022806 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022807 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022808 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022809 name);
22810 goto erret;
22811 }
22812 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 ga_clear_strings(&(fp->uf_args));
22814 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022815 vim_free(name);
22816 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 }
22819 else
22820 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022821 char numbuf[20];
22822
22823 fp = NULL;
22824 if (fudi.fd_newkey == NULL && !eap->forceit)
22825 {
22826 EMSG(_(e_funcdict));
22827 goto erret;
22828 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022829 if (fudi.fd_di == NULL)
22830 {
22831 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022832 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022833 goto erret;
22834 }
22835 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022836 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022837 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022838
22839 /* Give the function a sequential number. Can only be used with a
22840 * Funcref! */
22841 vim_free(name);
22842 sprintf(numbuf, "%d", ++func_nr);
22843 name = vim_strsave((char_u *)numbuf);
22844 if (name == NULL)
22845 goto erret;
22846 }
22847
22848 if (fp == NULL)
22849 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022850 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022851 {
22852 int slen, plen;
22853 char_u *scriptname;
22854
22855 /* Check that the autoload name matches the script name. */
22856 j = FAIL;
22857 if (sourcing_name != NULL)
22858 {
22859 scriptname = autoload_name(name);
22860 if (scriptname != NULL)
22861 {
22862 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022863 plen = (int)STRLEN(p);
22864 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022865 if (slen > plen && fnamecmp(p,
22866 sourcing_name + slen - plen) == 0)
22867 j = OK;
22868 vim_free(scriptname);
22869 }
22870 }
22871 if (j == FAIL)
22872 {
22873 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22874 goto erret;
22875 }
22876 }
22877
22878 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 if (fp == NULL)
22880 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881
22882 if (fudi.fd_dict != NULL)
22883 {
22884 if (fudi.fd_di == NULL)
22885 {
22886 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022887 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022888 if (fudi.fd_di == NULL)
22889 {
22890 vim_free(fp);
22891 goto erret;
22892 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022893 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22894 {
22895 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022896 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022897 goto erret;
22898 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022899 }
22900 else
22901 /* overwrite existing dict entry */
22902 clear_tv(&fudi.fd_di->di_tv);
22903 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022904 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022905 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022907
22908 /* behave like "dict" was used */
22909 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022910 }
22911
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022913 STRCPY(fp->uf_name, name);
22914 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022916 fp->uf_args = newargs;
22917 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022918#ifdef FEAT_PROFILE
22919 fp->uf_tml_count = NULL;
22920 fp->uf_tml_total = NULL;
22921 fp->uf_tml_self = NULL;
22922 fp->uf_profiling = FALSE;
22923 if (prof_def_func())
22924 func_do_profile(fp);
22925#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022926 fp->uf_varargs = varargs;
22927 fp->uf_flags = flags;
22928 fp->uf_calls = 0;
22929 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022930 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931
22932erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 ga_clear_strings(&newargs);
22934 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022935ret_free:
22936 vim_free(skip_until);
22937 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022940 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941}
22942
22943/*
22944 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022945 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022947 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022948 * TFN_INT: internal function name OK
22949 * TFN_QUIET: be quiet
22950 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 * Advances "pp" to just after the function name (if no error).
22952 */
22953 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 char_u **pp;
22956 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022957 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022960 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 char_u *start;
22962 char_u *end;
22963 int lead;
22964 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022967
22968 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022969 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022971
22972 /* Check for hard coded <SNR>: already translated function ID (from a user
22973 * command). */
22974 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22975 && (*pp)[2] == (int)KE_SNR)
22976 {
22977 *pp += 3;
22978 len = get_id_len(pp) + 3;
22979 return vim_strnsave(start, len);
22980 }
22981
22982 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22983 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022985 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022987
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022988 /* Note that TFN_ flags use the same values as GLV_ flags. */
22989 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022990 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022991 if (end == start)
22992 {
22993 if (!skip)
22994 EMSG(_("E129: Function name required"));
22995 goto theend;
22996 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022997 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022998 {
22999 /*
23000 * Report an invalid expression in braces, unless the expression
23001 * evaluation has been cancelled due to an aborting error, an
23002 * interrupt, or an exception.
23003 */
23004 if (!aborting())
23005 {
23006 if (end != NULL)
23007 EMSG2(_(e_invarg2), start);
23008 }
23009 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023010 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023011 goto theend;
23012 }
23013
23014 if (lv.ll_tv != NULL)
23015 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023016 if (fdp != NULL)
23017 {
23018 fdp->fd_dict = lv.ll_dict;
23019 fdp->fd_newkey = lv.ll_newkey;
23020 lv.ll_newkey = NULL;
23021 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023023 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23024 {
23025 name = vim_strsave(lv.ll_tv->vval.v_string);
23026 *pp = end;
23027 }
23028 else
23029 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023030 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23031 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023032 EMSG(_(e_funcref));
23033 else
23034 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023035 name = NULL;
23036 }
23037 goto theend;
23038 }
23039
23040 if (lv.ll_name == NULL)
23041 {
23042 /* Error found, but continue after the function name. */
23043 *pp = end;
23044 goto theend;
23045 }
23046
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023047 /* Check if the name is a Funcref. If so, use the value. */
23048 if (lv.ll_exp_name != NULL)
23049 {
23050 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023051 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023052 if (name == lv.ll_exp_name)
23053 name = NULL;
23054 }
23055 else
23056 {
23057 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023058 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023059 if (name == *pp)
23060 name = NULL;
23061 }
23062 if (name != NULL)
23063 {
23064 name = vim_strsave(name);
23065 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023066 if (STRNCMP(name, "<SNR>", 5) == 0)
23067 {
23068 /* Change "<SNR>" to the byte sequence. */
23069 name[0] = K_SPECIAL;
23070 name[1] = KS_EXTRA;
23071 name[2] = (int)KE_SNR;
23072 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23073 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023074 goto theend;
23075 }
23076
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023077 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023078 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023079 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023080 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23081 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23082 {
23083 /* When there was "s:" already or the name expanded to get a
23084 * leading "s:" then remove it. */
23085 lv.ll_name += 2;
23086 len -= 2;
23087 lead = 2;
23088 }
23089 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023090 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023091 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023092 /* skip over "s:" and "g:" */
23093 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023094 lv.ll_name += 2;
23095 len = (int)(end - lv.ll_name);
23096 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023097
23098 /*
23099 * Copy the function name to allocated memory.
23100 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23101 * Accept <SNR>123_name() outside a script.
23102 */
23103 if (skip)
23104 lead = 0; /* do nothing */
23105 else if (lead > 0)
23106 {
23107 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023108 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23109 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023110 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023111 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023112 if (current_SID <= 0)
23113 {
23114 EMSG(_(e_usingsid));
23115 goto theend;
23116 }
23117 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23118 lead += (int)STRLEN(sid_buf);
23119 }
23120 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023121 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023122 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023123 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023124 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023125 goto theend;
23126 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023127 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023128 {
23129 char_u *cp = vim_strchr(lv.ll_name, ':');
23130
23131 if (cp != NULL && cp < end)
23132 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023133 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023134 goto theend;
23135 }
23136 }
23137
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023138 name = alloc((unsigned)(len + lead + 1));
23139 if (name != NULL)
23140 {
23141 if (lead > 0)
23142 {
23143 name[0] = K_SPECIAL;
23144 name[1] = KS_EXTRA;
23145 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023146 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023147 STRCPY(name + 3, sid_buf);
23148 }
23149 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023150 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023151 }
23152 *pp = end;
23153
23154theend:
23155 clear_lval(&lv);
23156 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157}
23158
23159/*
23160 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23161 * Return 2 if "p" starts with "s:".
23162 * Return 0 otherwise.
23163 */
23164 static int
23165eval_fname_script(p)
23166 char_u *p;
23167{
23168 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23169 || STRNICMP(p + 1, "SNR>", 4) == 0))
23170 return 5;
23171 if (p[0] == 's' && p[1] == ':')
23172 return 2;
23173 return 0;
23174}
23175
23176/*
23177 * Return TRUE if "p" starts with "<SID>" or "s:".
23178 * Only works if eval_fname_script() returned non-zero for "p"!
23179 */
23180 static int
23181eval_fname_sid(p)
23182 char_u *p;
23183{
23184 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23185}
23186
23187/*
23188 * List the head of the function: "name(arg1, arg2)".
23189 */
23190 static void
23191list_func_head(fp, indent)
23192 ufunc_T *fp;
23193 int indent;
23194{
23195 int j;
23196
23197 msg_start();
23198 if (indent)
23199 MSG_PUTS(" ");
23200 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023201 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 {
23203 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023204 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 }
23206 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023207 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023209 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 {
23211 if (j)
23212 MSG_PUTS(", ");
23213 msg_puts(FUNCARG(fp, j));
23214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023215 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 {
23217 if (j)
23218 MSG_PUTS(", ");
23219 MSG_PUTS("...");
23220 }
23221 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023222 if (fp->uf_flags & FC_ABORT)
23223 MSG_PUTS(" abort");
23224 if (fp->uf_flags & FC_RANGE)
23225 MSG_PUTS(" range");
23226 if (fp->uf_flags & FC_DICT)
23227 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023228 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023229 if (p_verbose > 0)
23230 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231}
23232
23233/*
23234 * Find a function by name, return pointer to it in ufuncs.
23235 * Return NULL for unknown function.
23236 */
23237 static ufunc_T *
23238find_func(name)
23239 char_u *name;
23240{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023241 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023243 hi = hash_find(&func_hashtab, name);
23244 if (!HASHITEM_EMPTY(hi))
23245 return HI2UF(hi);
23246 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247}
23248
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023249#if defined(EXITFREE) || defined(PROTO)
23250 void
23251free_all_functions()
23252{
23253 hashitem_T *hi;
23254
23255 /* Need to start all over every time, because func_free() may change the
23256 * hash table. */
23257 while (func_hashtab.ht_used > 0)
23258 for (hi = func_hashtab.ht_array; ; ++hi)
23259 if (!HASHITEM_EMPTY(hi))
23260 {
23261 func_free(HI2UF(hi));
23262 break;
23263 }
23264}
23265#endif
23266
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023267 int
23268translated_function_exists(name)
23269 char_u *name;
23270{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023271 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023272 return find_internal_func(name) >= 0;
23273 return find_func(name) != NULL;
23274}
23275
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023276/*
23277 * Return TRUE if a function "name" exists.
23278 */
23279 static int
23280function_exists(name)
23281 char_u *name;
23282{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023283 char_u *nm = name;
23284 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023285 int n = FALSE;
23286
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023287 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23288 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023289 nm = skipwhite(nm);
23290
23291 /* Only accept "funcname", "funcname ", "funcname (..." and
23292 * "funcname(...", not "funcname!...". */
23293 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023294 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023295 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023296 return n;
23297}
23298
Bram Moolenaara1544c02013-05-30 12:35:52 +020023299 char_u *
23300get_expanded_name(name, check)
23301 char_u *name;
23302 int check;
23303{
23304 char_u *nm = name;
23305 char_u *p;
23306
23307 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23308
23309 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023310 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023311 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023312
Bram Moolenaara1544c02013-05-30 12:35:52 +020023313 vim_free(p);
23314 return NULL;
23315}
23316
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023317/*
23318 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023319 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23320 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023321 */
23322 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023323builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023324 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023325 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023326{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023327 char_u *p;
23328
23329 if (!ASCII_ISLOWER(name[0]))
23330 return FALSE;
23331 p = vim_strchr(name, AUTOLOAD_CHAR);
23332 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023333}
23334
Bram Moolenaar05159a02005-02-26 23:04:13 +000023335#if defined(FEAT_PROFILE) || defined(PROTO)
23336/*
23337 * Start profiling function "fp".
23338 */
23339 static void
23340func_do_profile(fp)
23341 ufunc_T *fp;
23342{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023343 int len = fp->uf_lines.ga_len;
23344
23345 if (len == 0)
23346 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347 fp->uf_tm_count = 0;
23348 profile_zero(&fp->uf_tm_self);
23349 profile_zero(&fp->uf_tm_total);
23350 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023351 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023352 if (fp->uf_tml_total == NULL)
23353 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023354 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023355 if (fp->uf_tml_self == NULL)
23356 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023357 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023358 fp->uf_tml_idx = -1;
23359 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23360 || fp->uf_tml_self == NULL)
23361 return; /* out of memory */
23362
23363 fp->uf_profiling = TRUE;
23364}
23365
23366/*
23367 * Dump the profiling results for all functions in file "fd".
23368 */
23369 void
23370func_dump_profile(fd)
23371 FILE *fd;
23372{
23373 hashitem_T *hi;
23374 int todo;
23375 ufunc_T *fp;
23376 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023377 ufunc_T **sorttab;
23378 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023379
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023380 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023381 if (todo == 0)
23382 return; /* nothing to dump */
23383
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023384 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023385
Bram Moolenaar05159a02005-02-26 23:04:13 +000023386 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23387 {
23388 if (!HASHITEM_EMPTY(hi))
23389 {
23390 --todo;
23391 fp = HI2UF(hi);
23392 if (fp->uf_profiling)
23393 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023394 if (sorttab != NULL)
23395 sorttab[st_len++] = fp;
23396
Bram Moolenaar05159a02005-02-26 23:04:13 +000023397 if (fp->uf_name[0] == K_SPECIAL)
23398 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23399 else
23400 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23401 if (fp->uf_tm_count == 1)
23402 fprintf(fd, "Called 1 time\n");
23403 else
23404 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23405 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23406 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23407 fprintf(fd, "\n");
23408 fprintf(fd, "count total (s) self (s)\n");
23409
23410 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23411 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023412 if (FUNCLINE(fp, i) == NULL)
23413 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023414 prof_func_line(fd, fp->uf_tml_count[i],
23415 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023416 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23417 }
23418 fprintf(fd, "\n");
23419 }
23420 }
23421 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023422
23423 if (sorttab != NULL && st_len > 0)
23424 {
23425 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23426 prof_total_cmp);
23427 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23428 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23429 prof_self_cmp);
23430 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23431 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023432
23433 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023434}
Bram Moolenaar73830342005-02-28 22:48:19 +000023435
23436 static void
23437prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23438 FILE *fd;
23439 ufunc_T **sorttab;
23440 int st_len;
23441 char *title;
23442 int prefer_self; /* when equal print only self time */
23443{
23444 int i;
23445 ufunc_T *fp;
23446
23447 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23448 fprintf(fd, "count total (s) self (s) function\n");
23449 for (i = 0; i < 20 && i < st_len; ++i)
23450 {
23451 fp = sorttab[i];
23452 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23453 prefer_self);
23454 if (fp->uf_name[0] == K_SPECIAL)
23455 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23456 else
23457 fprintf(fd, " %s()\n", fp->uf_name);
23458 }
23459 fprintf(fd, "\n");
23460}
23461
23462/*
23463 * Print the count and times for one function or function line.
23464 */
23465 static void
23466prof_func_line(fd, count, total, self, prefer_self)
23467 FILE *fd;
23468 int count;
23469 proftime_T *total;
23470 proftime_T *self;
23471 int prefer_self; /* when equal print only self time */
23472{
23473 if (count > 0)
23474 {
23475 fprintf(fd, "%5d ", count);
23476 if (prefer_self && profile_equal(total, self))
23477 fprintf(fd, " ");
23478 else
23479 fprintf(fd, "%s ", profile_msg(total));
23480 if (!prefer_self && profile_equal(total, self))
23481 fprintf(fd, " ");
23482 else
23483 fprintf(fd, "%s ", profile_msg(self));
23484 }
23485 else
23486 fprintf(fd, " ");
23487}
23488
23489/*
23490 * Compare function for total time sorting.
23491 */
23492 static int
23493#ifdef __BORLANDC__
23494_RTLENTRYF
23495#endif
23496prof_total_cmp(s1, s2)
23497 const void *s1;
23498 const void *s2;
23499{
23500 ufunc_T *p1, *p2;
23501
23502 p1 = *(ufunc_T **)s1;
23503 p2 = *(ufunc_T **)s2;
23504 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23505}
23506
23507/*
23508 * Compare function for self time sorting.
23509 */
23510 static int
23511#ifdef __BORLANDC__
23512_RTLENTRYF
23513#endif
23514prof_self_cmp(s1, s2)
23515 const void *s1;
23516 const void *s2;
23517{
23518 ufunc_T *p1, *p2;
23519
23520 p1 = *(ufunc_T **)s1;
23521 p2 = *(ufunc_T **)s2;
23522 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23523}
23524
Bram Moolenaar05159a02005-02-26 23:04:13 +000023525#endif
23526
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023527/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023528 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023529 * Return TRUE if a package was loaded.
23530 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023531 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023532script_autoload(name, reload)
23533 char_u *name;
23534 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023535{
23536 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023537 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023538 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023539 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023541 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023542 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023543 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023544 return FALSE;
23545
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023546 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023548 /* Find the name in the list of previously loaded package names. Skip
23549 * "autoload/", it's always the same. */
23550 for (i = 0; i < ga_loaded.ga_len; ++i)
23551 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23552 break;
23553 if (!reload && i < ga_loaded.ga_len)
23554 ret = FALSE; /* was loaded already */
23555 else
23556 {
23557 /* Remember the name if it wasn't loaded already. */
23558 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23559 {
23560 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23561 tofree = NULL;
23562 }
23563
23564 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023565 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023566 ret = TRUE;
23567 }
23568
23569 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023570 return ret;
23571}
23572
23573/*
23574 * Return the autoload script name for a function or variable name.
23575 * Returns NULL when out of memory.
23576 */
23577 static char_u *
23578autoload_name(name)
23579 char_u *name;
23580{
23581 char_u *p;
23582 char_u *scriptname;
23583
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023584 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023585 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23586 if (scriptname == NULL)
23587 return FALSE;
23588 STRCPY(scriptname, "autoload/");
23589 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023590 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023591 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023592 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023593 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023594 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023595}
23596
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23598
23599/*
23600 * Function given to ExpandGeneric() to obtain the list of user defined
23601 * function names.
23602 */
23603 char_u *
23604get_user_func_name(xp, idx)
23605 expand_T *xp;
23606 int idx;
23607{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023608 static long_u done;
23609 static hashitem_T *hi;
23610 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611
23612 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023614 done = 0;
23615 hi = func_hashtab.ht_array;
23616 }
23617 if (done < func_hashtab.ht_used)
23618 {
23619 if (done++ > 0)
23620 ++hi;
23621 while (HASHITEM_EMPTY(hi))
23622 ++hi;
23623 fp = HI2UF(hi);
23624
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023625 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023626 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023627
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023628 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23629 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630
23631 cat_func_name(IObuff, fp);
23632 if (xp->xp_context != EXPAND_USER_FUNC)
23633 {
23634 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023635 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 STRCAT(IObuff, ")");
23637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 return IObuff;
23639 }
23640 return NULL;
23641}
23642
23643#endif /* FEAT_CMDL_COMPL */
23644
23645/*
23646 * Copy the function name of "fp" to buffer "buf".
23647 * "buf" must be able to hold the function name plus three bytes.
23648 * Takes care of script-local function names.
23649 */
23650 static void
23651cat_func_name(buf, fp)
23652 char_u *buf;
23653 ufunc_T *fp;
23654{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023655 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 {
23657 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023658 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 }
23660 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023661 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662}
23663
23664/*
23665 * ":delfunction {name}"
23666 */
23667 void
23668ex_delfunction(eap)
23669 exarg_T *eap;
23670{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023671 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 char_u *p;
23673 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023674 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675
23676 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 name = trans_function_name(&p, eap->skip, 0, &fudi);
23678 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680 {
23681 if (fudi.fd_dict != NULL && !eap->skip)
23682 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 if (!ends_excmd(*skipwhite(p)))
23686 {
23687 vim_free(name);
23688 EMSG(_(e_trailing));
23689 return;
23690 }
23691 eap->nextcmd = check_nextcmd(p);
23692 if (eap->nextcmd != NULL)
23693 *p = NUL;
23694
23695 if (!eap->skip)
23696 fp = find_func(name);
23697 vim_free(name);
23698
23699 if (!eap->skip)
23700 {
23701 if (fp == NULL)
23702 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023703 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 return;
23705 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023706 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 {
23708 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23709 return;
23710 }
23711
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023712 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023714 /* Delete the dict item that refers to the function, it will
23715 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023716 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023718 else
23719 func_free(fp);
23720 }
23721}
23722
23723/*
23724 * Free a function and remove it from the list of functions.
23725 */
23726 static void
23727func_free(fp)
23728 ufunc_T *fp;
23729{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023731
23732 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023733 ga_clear_strings(&(fp->uf_args));
23734 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023735#ifdef FEAT_PROFILE
23736 vim_free(fp->uf_tml_count);
23737 vim_free(fp->uf_tml_total);
23738 vim_free(fp->uf_tml_self);
23739#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023740
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023741 /* remove the function from the function hashtable */
23742 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23743 if (HASHITEM_EMPTY(hi))
23744 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023745 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023746 hash_remove(&func_hashtab, hi);
23747
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023748 vim_free(fp);
23749}
23750
23751/*
23752 * Unreference a Function: decrement the reference count and free it when it
23753 * becomes zero. Only for numbered functions.
23754 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023755 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023756func_unref(name)
23757 char_u *name;
23758{
23759 ufunc_T *fp;
23760
23761 if (name != NULL && isdigit(*name))
23762 {
23763 fp = find_func(name);
23764 if (fp == NULL)
23765 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023766 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023767 {
23768 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023769 * when "uf_calls" becomes zero. */
23770 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023771 func_free(fp);
23772 }
23773 }
23774}
23775
23776/*
23777 * Count a reference to a Function.
23778 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023779 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023780func_ref(name)
23781 char_u *name;
23782{
23783 ufunc_T *fp;
23784
23785 if (name != NULL && isdigit(*name))
23786 {
23787 fp = find_func(name);
23788 if (fp == NULL)
23789 EMSG2(_(e_intern2), "func_ref()");
23790 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023791 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 }
23793}
23794
23795/*
23796 * Call a user function.
23797 */
23798 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023799call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 ufunc_T *fp; /* pointer to function */
23801 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023802 typval_T *argvars; /* arguments */
23803 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 linenr_T firstline; /* first line of range */
23805 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023806 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807{
Bram Moolenaar33570922005-01-25 22:26:29 +000023808 char_u *save_sourcing_name;
23809 linenr_T save_sourcing_lnum;
23810 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023811 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023812 int save_did_emsg;
23813 static int depth = 0;
23814 dictitem_T *v;
23815 int fixvar_idx = 0; /* index in fixvar[] */
23816 int i;
23817 int ai;
23818 char_u numbuf[NUMBUFLEN];
23819 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023820#ifdef FEAT_PROFILE
23821 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023822 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824
23825 /* If depth of calling is getting too high, don't execute the function */
23826 if (depth >= p_mfd)
23827 {
23828 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023829 rettv->v_type = VAR_NUMBER;
23830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831 return;
23832 }
23833 ++depth;
23834
23835 line_breakcheck(); /* check for CTRL-C hit */
23836
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023837 fc = (funccall_T *)alloc(sizeof(funccall_T));
23838 fc->caller = current_funccal;
23839 current_funccal = fc;
23840 fc->func = fp;
23841 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023842 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023843 fc->linenr = 0;
23844 fc->returned = FALSE;
23845 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023847 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23848 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849
Bram Moolenaar33570922005-01-25 22:26:29 +000023850 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023851 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023852 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23853 * each argument variable and saves a lot of time.
23854 */
23855 /*
23856 * Init l: variables.
23857 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023858 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023859 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023860 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023861 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23862 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023863 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023864 name = v->di_key;
23865 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023866 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023867 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023868 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023869 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023870 v->di_tv.vval.v_dict = selfdict;
23871 ++selfdict->dv_refcount;
23872 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023873
Bram Moolenaar33570922005-01-25 22:26:29 +000023874 /*
23875 * Init a: variables.
23876 * Set a:0 to "argcount".
23877 * Set a:000 to a list with room for the "..." arguments.
23878 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023879 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023880 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023881 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023882 /* Use "name" to avoid a warning from some compiler that checks the
23883 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023884 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023885 name = v->di_key;
23886 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023887 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023888 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023889 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023890 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023891 v->di_tv.vval.v_list = &fc->l_varlist;
23892 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23893 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23894 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023895
23896 /*
23897 * Set a:firstline to "firstline" and a:lastline to "lastline".
23898 * Set a:name to named arguments.
23899 * Set a:N to the "..." arguments.
23900 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023901 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023902 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023903 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023904 (varnumber_T)lastline);
23905 for (i = 0; i < argcount; ++i)
23906 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023907 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023908 if (ai < 0)
23909 /* named argument a:name */
23910 name = FUNCARG(fp, i);
23911 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023912 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023913 /* "..." argument a:1, a:2, etc. */
23914 sprintf((char *)numbuf, "%d", ai + 1);
23915 name = numbuf;
23916 }
23917 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23918 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023919 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023920 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23921 }
23922 else
23923 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023924 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23925 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023926 if (v == NULL)
23927 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023928 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023929 }
23930 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023931 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023932
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023933 /* Note: the values are copied directly to avoid alloc/free.
23934 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023935 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023936 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023937
23938 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23939 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023940 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23941 fc->l_listitems[ai].li_tv = argvars[i];
23942 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023943 }
23944 }
23945
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 /* Don't redraw while executing the function. */
23947 ++RedrawingDisabled;
23948 save_sourcing_name = sourcing_name;
23949 save_sourcing_lnum = sourcing_lnum;
23950 sourcing_lnum = 1;
23951 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023952 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 if (sourcing_name != NULL)
23954 {
23955 if (save_sourcing_name != NULL
23956 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23957 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23958 else
23959 STRCPY(sourcing_name, "function ");
23960 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23961
23962 if (p_verbose >= 12)
23963 {
23964 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023965 verbose_enter_scroll();
23966
Bram Moolenaar555b2802005-05-19 21:08:39 +000023967 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 if (p_verbose >= 14)
23969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023971 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023972 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023973 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974
23975 msg_puts((char_u *)"(");
23976 for (i = 0; i < argcount; ++i)
23977 {
23978 if (i > 0)
23979 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023980 if (argvars[i].v_type == VAR_NUMBER)
23981 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 else
23983 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023984 /* Do not want errors such as E724 here. */
23985 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023986 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023987 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023988 if (s != NULL)
23989 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023990 if (vim_strsize(s) > MSG_BUF_CLEN)
23991 {
23992 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23993 s = buf;
23994 }
23995 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023996 vim_free(tofree);
23997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 }
23999 }
24000 msg_puts((char_u *)")");
24001 }
24002 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024003
24004 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 --no_wait_return;
24006 }
24007 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024008#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024009 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024010 {
24011 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24012 func_do_profile(fp);
24013 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024014 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024015 {
24016 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024017 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024018 profile_zero(&fp->uf_tm_children);
24019 }
24020 script_prof_save(&wait_start);
24021 }
24022#endif
24023
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024025 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 save_did_emsg = did_emsg;
24027 did_emsg = FALSE;
24028
24029 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024030 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24032
24033 --RedrawingDisabled;
24034
24035 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024036 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024038 clear_tv(rettv);
24039 rettv->v_type = VAR_NUMBER;
24040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 }
24042
Bram Moolenaar05159a02005-02-26 23:04:13 +000024043#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024044 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024045 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024046 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024047 profile_end(&call_start);
24048 profile_sub_wait(&wait_start, &call_start);
24049 profile_add(&fp->uf_tm_total, &call_start);
24050 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024051 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024052 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024053 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24054 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024055 }
24056 }
24057#endif
24058
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 /* when being verbose, mention the return value */
24060 if (p_verbose >= 12)
24061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024063 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024066 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024067 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024068 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024069 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024072 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024073 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024074 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024075 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024076
Bram Moolenaar555b2802005-05-19 21:08:39 +000024077 /* The value may be very long. Skip the middle part, so that we
24078 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024079 * truncate it at the end. Don't want errors such as E724 here. */
24080 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024081 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024082 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024083 if (s != NULL)
24084 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024085 if (vim_strsize(s) > MSG_BUF_CLEN)
24086 {
24087 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24088 s = buf;
24089 }
24090 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024091 vim_free(tofree);
24092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 }
24094 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024095
24096 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 --no_wait_return;
24098 }
24099
24100 vim_free(sourcing_name);
24101 sourcing_name = save_sourcing_name;
24102 sourcing_lnum = save_sourcing_lnum;
24103 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024104#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024105 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024106 script_prof_restore(&wait_start);
24107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108
24109 if (p_verbose >= 12 && sourcing_name != NULL)
24110 {
24111 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024112 verbose_enter_scroll();
24113
Bram Moolenaar555b2802005-05-19 21:08:39 +000024114 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024116
24117 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 --no_wait_return;
24119 }
24120
24121 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024122 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024124
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024125 /* If the a:000 list and the l: and a: dicts are not referenced we can
24126 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024127 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24128 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24129 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24130 {
24131 free_funccal(fc, FALSE);
24132 }
24133 else
24134 {
24135 hashitem_T *hi;
24136 listitem_T *li;
24137 int todo;
24138
24139 /* "fc" is still in use. This can happen when returning "a:000" or
24140 * assigning "l:" to a global variable.
24141 * Link "fc" in the list for garbage collection later. */
24142 fc->caller = previous_funccal;
24143 previous_funccal = fc;
24144
24145 /* Make a copy of the a: variables, since we didn't do that above. */
24146 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24147 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24148 {
24149 if (!HASHITEM_EMPTY(hi))
24150 {
24151 --todo;
24152 v = HI2DI(hi);
24153 copy_tv(&v->di_tv, &v->di_tv);
24154 }
24155 }
24156
24157 /* Make a copy of the a:000 items, since we didn't do that above. */
24158 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24159 copy_tv(&li->li_tv, &li->li_tv);
24160 }
24161}
24162
24163/*
24164 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024165 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024166 */
24167 static int
24168can_free_funccal(fc, copyID)
24169 funccall_T *fc;
24170 int copyID;
24171{
24172 return (fc->l_varlist.lv_copyID != copyID
24173 && fc->l_vars.dv_copyID != copyID
24174 && fc->l_avars.dv_copyID != copyID);
24175}
24176
24177/*
24178 * Free "fc" and what it contains.
24179 */
24180 static void
24181free_funccal(fc, free_val)
24182 funccall_T *fc;
24183 int free_val; /* a: vars were allocated */
24184{
24185 listitem_T *li;
24186
24187 /* The a: variables typevals may not have been allocated, only free the
24188 * allocated variables. */
24189 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24190
24191 /* free all l: variables */
24192 vars_clear(&fc->l_vars.dv_hashtab);
24193
24194 /* Free the a:000 variables if they were allocated. */
24195 if (free_val)
24196 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24197 clear_tv(&li->li_tv);
24198
24199 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200}
24201
24202/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024203 * Add a number variable "name" to dict "dp" with value "nr".
24204 */
24205 static void
24206add_nr_var(dp, v, name, nr)
24207 dict_T *dp;
24208 dictitem_T *v;
24209 char *name;
24210 varnumber_T nr;
24211{
24212 STRCPY(v->di_key, name);
24213 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24214 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24215 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024216 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024217 v->di_tv.vval.v_number = nr;
24218}
24219
24220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 * ":return [expr]"
24222 */
24223 void
24224ex_return(eap)
24225 exarg_T *eap;
24226{
24227 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024228 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 int returning = FALSE;
24230
24231 if (current_funccal == NULL)
24232 {
24233 EMSG(_("E133: :return not inside a function"));
24234 return;
24235 }
24236
24237 if (eap->skip)
24238 ++emsg_skip;
24239
24240 eap->nextcmd = NULL;
24241 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024242 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 {
24244 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024245 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 }
24249 /* It's safer to return also on error. */
24250 else if (!eap->skip)
24251 {
24252 /*
24253 * Return unless the expression evaluation has been cancelled due to an
24254 * aborting error, an interrupt, or an exception.
24255 */
24256 if (!aborting())
24257 returning = do_return(eap, FALSE, TRUE, NULL);
24258 }
24259
24260 /* When skipping or the return gets pending, advance to the next command
24261 * in this line (!returning). Otherwise, ignore the rest of the line.
24262 * Following lines will be ignored by get_func_line(). */
24263 if (returning)
24264 eap->nextcmd = NULL;
24265 else if (eap->nextcmd == NULL) /* no argument */
24266 eap->nextcmd = check_nextcmd(arg);
24267
24268 if (eap->skip)
24269 --emsg_skip;
24270}
24271
24272/*
24273 * Return from a function. Possibly makes the return pending. Also called
24274 * for a pending return at the ":endtry" or after returning from an extra
24275 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024276 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024277 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 * FALSE when the return gets pending.
24279 */
24280 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024281do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 exarg_T *eap;
24283 int reanimate;
24284 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024285 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286{
24287 int idx;
24288 struct condstack *cstack = eap->cstack;
24289
24290 if (reanimate)
24291 /* Undo the return. */
24292 current_funccal->returned = FALSE;
24293
24294 /*
24295 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24296 * not in its finally clause (which then is to be executed next) is found.
24297 * In this case, make the ":return" pending for execution at the ":endtry".
24298 * Otherwise, return normally.
24299 */
24300 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24301 if (idx >= 0)
24302 {
24303 cstack->cs_pending[idx] = CSTP_RETURN;
24304
24305 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024306 /* A pending return again gets pending. "rettv" points to an
24307 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024309 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 else
24311 {
24312 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024313 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024315 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024317 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 {
24319 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024320 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024321 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 else
24323 EMSG(_(e_outofmem));
24324 }
24325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024326 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327
24328 if (reanimate)
24329 {
24330 /* The pending return value could be overwritten by a ":return"
24331 * without argument in a finally clause; reset the default
24332 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024333 current_funccal->rettv->v_type = VAR_NUMBER;
24334 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 }
24336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024337 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 }
24339 else
24340 {
24341 current_funccal->returned = TRUE;
24342
24343 /* If the return is carried out now, store the return value. For
24344 * a return immediately after reanimation, the value is already
24345 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024346 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024348 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024349 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024351 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 }
24353 }
24354
24355 return idx < 0;
24356}
24357
24358/*
24359 * Free the variable with a pending return value.
24360 */
24361 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024362discard_pending_return(rettv)
24363 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364{
Bram Moolenaar33570922005-01-25 22:26:29 +000024365 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366}
24367
24368/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024369 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 * is an allocated string. Used by report_pending() for verbose messages.
24371 */
24372 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024373get_return_cmd(rettv)
24374 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024376 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024377 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024378 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024380 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024381 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024382 if (s == NULL)
24383 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024384
24385 STRCPY(IObuff, ":return ");
24386 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24387 if (STRLEN(s) + 8 >= IOSIZE)
24388 STRCPY(IObuff + IOSIZE - 4, "...");
24389 vim_free(tofree);
24390 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391}
24392
24393/*
24394 * Get next function line.
24395 * Called by do_cmdline() to get the next line.
24396 * Returns allocated string, or NULL for end of function.
24397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 char_u *
24399get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024400 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024402 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403{
Bram Moolenaar33570922005-01-25 22:26:29 +000024404 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024405 ufunc_T *fp = fcp->func;
24406 char_u *retval;
24407 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408
24409 /* If breakpoints have been added/deleted need to check for it. */
24410 if (fcp->dbg_tick != debug_tick)
24411 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024412 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 sourcing_lnum);
24414 fcp->dbg_tick = debug_tick;
24415 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024416#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024417 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024418 func_line_end(cookie);
24419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420
Bram Moolenaar05159a02005-02-26 23:04:13 +000024421 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024422 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24423 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 retval = NULL;
24425 else
24426 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024427 /* Skip NULL lines (continuation lines). */
24428 while (fcp->linenr < gap->ga_len
24429 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24430 ++fcp->linenr;
24431 if (fcp->linenr >= gap->ga_len)
24432 retval = NULL;
24433 else
24434 {
24435 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24436 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024437#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024438 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024439 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024440#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 }
24443
24444 /* Did we encounter a breakpoint? */
24445 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24446 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024447 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024449 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450 sourcing_lnum);
24451 fcp->dbg_tick = debug_tick;
24452 }
24453
24454 return retval;
24455}
24456
Bram Moolenaar05159a02005-02-26 23:04:13 +000024457#if defined(FEAT_PROFILE) || defined(PROTO)
24458/*
24459 * Called when starting to read a function line.
24460 * "sourcing_lnum" must be correct!
24461 * When skipping lines it may not actually be executed, but we won't find out
24462 * until later and we need to store the time now.
24463 */
24464 void
24465func_line_start(cookie)
24466 void *cookie;
24467{
24468 funccall_T *fcp = (funccall_T *)cookie;
24469 ufunc_T *fp = fcp->func;
24470
24471 if (fp->uf_profiling && sourcing_lnum >= 1
24472 && sourcing_lnum <= fp->uf_lines.ga_len)
24473 {
24474 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024475 /* Skip continuation lines. */
24476 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24477 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024478 fp->uf_tml_execed = FALSE;
24479 profile_start(&fp->uf_tml_start);
24480 profile_zero(&fp->uf_tml_children);
24481 profile_get_wait(&fp->uf_tml_wait);
24482 }
24483}
24484
24485/*
24486 * Called when actually executing a function line.
24487 */
24488 void
24489func_line_exec(cookie)
24490 void *cookie;
24491{
24492 funccall_T *fcp = (funccall_T *)cookie;
24493 ufunc_T *fp = fcp->func;
24494
24495 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24496 fp->uf_tml_execed = TRUE;
24497}
24498
24499/*
24500 * Called when done with a function line.
24501 */
24502 void
24503func_line_end(cookie)
24504 void *cookie;
24505{
24506 funccall_T *fcp = (funccall_T *)cookie;
24507 ufunc_T *fp = fcp->func;
24508
24509 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24510 {
24511 if (fp->uf_tml_execed)
24512 {
24513 ++fp->uf_tml_count[fp->uf_tml_idx];
24514 profile_end(&fp->uf_tml_start);
24515 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024516 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024517 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24518 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024519 }
24520 fp->uf_tml_idx = -1;
24521 }
24522}
24523#endif
24524
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525/*
24526 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024527 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 */
24529 int
24530func_has_ended(cookie)
24531 void *cookie;
24532{
Bram Moolenaar33570922005-01-25 22:26:29 +000024533 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534
24535 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24536 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024537 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 || fcp->returned);
24539}
24540
24541/*
24542 * return TRUE if cookie indicates a function which "abort"s on errors.
24543 */
24544 int
24545func_has_abort(cookie)
24546 void *cookie;
24547{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024548 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549}
24550
24551#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24552typedef enum
24553{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024554 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24555 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24556 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557} var_flavour_T;
24558
24559static var_flavour_T var_flavour __ARGS((char_u *varname));
24560
24561 static var_flavour_T
24562var_flavour(varname)
24563 char_u *varname;
24564{
24565 char_u *p = varname;
24566
24567 if (ASCII_ISUPPER(*p))
24568 {
24569 while (*(++p))
24570 if (ASCII_ISLOWER(*p))
24571 return VAR_FLAVOUR_SESSION;
24572 return VAR_FLAVOUR_VIMINFO;
24573 }
24574 else
24575 return VAR_FLAVOUR_DEFAULT;
24576}
24577#endif
24578
24579#if defined(FEAT_VIMINFO) || defined(PROTO)
24580/*
24581 * Restore global vars that start with a capital from the viminfo file
24582 */
24583 int
24584read_viminfo_varlist(virp, writing)
24585 vir_T *virp;
24586 int writing;
24587{
24588 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024589 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024590 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591
24592 if (!writing && (find_viminfo_parameter('!') != NULL))
24593 {
24594 tab = vim_strchr(virp->vir_line + 1, '\t');
24595 if (tab != NULL)
24596 {
24597 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024598 switch (*tab)
24599 {
24600 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024601#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024602 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024603#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024604 case 'D': type = VAR_DICT; break;
24605 case 'L': type = VAR_LIST; break;
24606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607
24608 tab = vim_strchr(tab, '\t');
24609 if (tab != NULL)
24610 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024611 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024612 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024613 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024615#ifdef FEAT_FLOAT
24616 else if (type == VAR_FLOAT)
24617 (void)string2float(tab + 1, &tv.vval.v_float);
24618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024620 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024621 if (type == VAR_DICT || type == VAR_LIST)
24622 {
24623 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24624
24625 if (etv == NULL)
24626 /* Failed to parse back the dict or list, use it as a
24627 * string. */
24628 tv.v_type = VAR_STRING;
24629 else
24630 {
24631 vim_free(tv.vval.v_string);
24632 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024633 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024634 }
24635 }
24636
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024637 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024638
24639 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024640 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024641 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24642 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 }
24644 }
24645 }
24646
24647 return viminfo_readline(virp);
24648}
24649
24650/*
24651 * Write global vars that start with a capital to the viminfo file
24652 */
24653 void
24654write_viminfo_varlist(fp)
24655 FILE *fp;
24656{
Bram Moolenaar33570922005-01-25 22:26:29 +000024657 hashitem_T *hi;
24658 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024659 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024660 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024661 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024662 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024663 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664
24665 if (find_viminfo_parameter('!') == NULL)
24666 return;
24667
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024668 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024669
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024670 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024671 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024675 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024676 this_var = HI2DI(hi);
24677 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024678 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024679 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024680 {
24681 case VAR_STRING: s = "STR"; break;
24682 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024683#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024684 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024685#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024686 case VAR_DICT: s = "DIC"; break;
24687 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024688 default: continue;
24689 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024690 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024691 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024692 if (p != NULL)
24693 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024694 vim_free(tofree);
24695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 }
24697 }
24698}
24699#endif
24700
24701#if defined(FEAT_SESSION) || defined(PROTO)
24702 int
24703store_session_globals(fd)
24704 FILE *fd;
24705{
Bram Moolenaar33570922005-01-25 22:26:29 +000024706 hashitem_T *hi;
24707 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024708 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 char_u *p, *t;
24710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024711 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024712 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024713 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024714 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024716 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024717 this_var = HI2DI(hi);
24718 if ((this_var->di_tv.v_type == VAR_NUMBER
24719 || this_var->di_tv.v_type == VAR_STRING)
24720 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024721 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024722 /* Escape special characters with a backslash. Turn a LF and
24723 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024724 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024725 (char_u *)"\\\"\n\r");
24726 if (p == NULL) /* out of memory */
24727 break;
24728 for (t = p; *t != NUL; ++t)
24729 if (*t == '\n')
24730 *t = 'n';
24731 else if (*t == '\r')
24732 *t = 'r';
24733 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024734 this_var->di_key,
24735 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24736 : ' ',
24737 p,
24738 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24739 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024740 || put_eol(fd) == FAIL)
24741 {
24742 vim_free(p);
24743 return FAIL;
24744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745 vim_free(p);
24746 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024747#ifdef FEAT_FLOAT
24748 else if (this_var->di_tv.v_type == VAR_FLOAT
24749 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24750 {
24751 float_T f = this_var->di_tv.vval.v_float;
24752 int sign = ' ';
24753
24754 if (f < 0)
24755 {
24756 f = -f;
24757 sign = '-';
24758 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024759 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024760 this_var->di_key, sign, f) < 0)
24761 || put_eol(fd) == FAIL)
24762 return FAIL;
24763 }
24764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 }
24766 }
24767 return OK;
24768}
24769#endif
24770
Bram Moolenaar661b1822005-07-28 22:36:45 +000024771/*
24772 * Display script name where an item was last set.
24773 * Should only be invoked when 'verbose' is non-zero.
24774 */
24775 void
24776last_set_msg(scriptID)
24777 scid_T scriptID;
24778{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024779 char_u *p;
24780
Bram Moolenaar661b1822005-07-28 22:36:45 +000024781 if (scriptID != 0)
24782 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024783 p = home_replace_save(NULL, get_scriptname(scriptID));
24784 if (p != NULL)
24785 {
24786 verbose_enter();
24787 MSG_PUTS(_("\n\tLast set from "));
24788 MSG_PUTS(p);
24789 vim_free(p);
24790 verbose_leave();
24791 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024792 }
24793}
24794
Bram Moolenaard812df62008-11-09 12:46:09 +000024795/*
24796 * List v:oldfiles in a nice way.
24797 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024798 void
24799ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024800 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024801{
24802 list_T *l = vimvars[VV_OLDFILES].vv_list;
24803 listitem_T *li;
24804 int nr = 0;
24805
24806 if (l == NULL)
24807 msg((char_u *)_("No old files"));
24808 else
24809 {
24810 msg_start();
24811 msg_scroll = TRUE;
24812 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24813 {
24814 msg_outnum((long)++nr);
24815 MSG_PUTS(": ");
24816 msg_outtrans(get_tv_string(&li->li_tv));
24817 msg_putchar('\n');
24818 out_flush(); /* output one line at a time */
24819 ui_breakcheck();
24820 }
24821 /* Assume "got_int" was set to truncate the listing. */
24822 got_int = FALSE;
24823
24824#ifdef FEAT_BROWSE_CMD
24825 if (cmdmod.browse)
24826 {
24827 quit_more = FALSE;
24828 nr = prompt_for_number(FALSE);
24829 msg_starthere();
24830 if (nr > 0)
24831 {
24832 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24833 (long)nr);
24834
24835 if (p != NULL)
24836 {
24837 p = expand_env_save(p);
24838 eap->arg = p;
24839 eap->cmdidx = CMD_edit;
24840 cmdmod.browse = FALSE;
24841 do_exedit(eap, NULL);
24842 vim_free(p);
24843 }
24844 }
24845 }
24846#endif
24847 }
24848}
24849
Bram Moolenaar53744302015-07-17 17:38:22 +020024850/* reset v:option_new, v:option_old and v:option_type */
24851 void
24852reset_v_option_vars()
24853{
24854 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24855 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24856 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24857}
24858
24859
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860#endif /* FEAT_EVAL */
24861
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024863#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864
24865#ifdef WIN3264
24866/*
24867 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24868 */
24869static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24870static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24871static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24872
24873/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024874 * Get the short path (8.3) for the filename in "fnamep".
24875 * Only works for a valid file name.
24876 * When the path gets longer "fnamep" is changed and the allocated buffer
24877 * is put in "bufp".
24878 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24879 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 */
24881 static int
24882get_short_pathname(fnamep, bufp, fnamelen)
24883 char_u **fnamep;
24884 char_u **bufp;
24885 int *fnamelen;
24886{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024887 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 char_u *newbuf;
24889
24890 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891 l = GetShortPathName(*fnamep, *fnamep, len);
24892 if (l > len - 1)
24893 {
24894 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024895 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 newbuf = vim_strnsave(*fnamep, l);
24897 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024898 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899
24900 vim_free(*bufp);
24901 *fnamep = *bufp = newbuf;
24902
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024903 /* Really should always succeed, as the buffer is big enough. */
24904 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 }
24906
24907 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024908 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909}
24910
24911/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024912 * Get the short path (8.3) for the filename in "fname". The converted
24913 * path is returned in "bufp".
24914 *
24915 * Some of the directories specified in "fname" may not exist. This function
24916 * will shorten the existing directories at the beginning of the path and then
24917 * append the remaining non-existing path.
24918 *
24919 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024920 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024921 * bufp - Pointer to an allocated buffer for the filename.
24922 * fnamelen - Length of the filename pointed to by fname
24923 *
24924 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 */
24926 static int
24927shortpath_for_invalid_fname(fname, bufp, fnamelen)
24928 char_u **fname;
24929 char_u **bufp;
24930 int *fnamelen;
24931{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024932 char_u *short_fname, *save_fname, *pbuf_unused;
24933 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024935 int old_len, len;
24936 int new_len, sfx_len;
24937 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938
24939 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024940 old_len = *fnamelen;
24941 save_fname = vim_strnsave(*fname, old_len);
24942 pbuf_unused = NULL;
24943 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024945 endp = save_fname + old_len - 1; /* Find the end of the copy */
24946 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024948 /*
24949 * Try shortening the supplied path till it succeeds by removing one
24950 * directory at a time from the tail of the path.
24951 */
24952 len = 0;
24953 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024955 /* go back one path-separator */
24956 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24957 --endp;
24958 if (endp <= save_fname)
24959 break; /* processed the complete path */
24960
24961 /*
24962 * Replace the path separator with a NUL and try to shorten the
24963 * resulting path.
24964 */
24965 ch = *endp;
24966 *endp = 0;
24967 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024968 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024969 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24970 {
24971 retval = FAIL;
24972 goto theend;
24973 }
24974 *endp = ch; /* preserve the string */
24975
24976 if (len > 0)
24977 break; /* successfully shortened the path */
24978
24979 /* failed to shorten the path. Skip the path separator */
24980 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 }
24982
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024983 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024985 /*
24986 * Succeeded in shortening the path. Now concatenate the shortened
24987 * path with the remaining path at the tail.
24988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024990 /* Compute the length of the new path. */
24991 sfx_len = (int)(save_endp - endp) + 1;
24992 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024994 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024995 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024996 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024998 /* There is not enough space in the currently allocated string,
24999 * copy it to a buffer big enough. */
25000 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025002 {
25003 retval = FAIL;
25004 goto theend;
25005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 }
25007 else
25008 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025009 /* Transfer short_fname to the main buffer (it's big enough),
25010 * unless get_short_pathname() did its work in-place. */
25011 *fname = *bufp = save_fname;
25012 if (short_fname != save_fname)
25013 vim_strncpy(save_fname, short_fname, len);
25014 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025016
25017 /* concat the not-shortened part of the path */
25018 vim_strncpy(*fname + len, endp, sfx_len);
25019 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025021
25022theend:
25023 vim_free(pbuf_unused);
25024 vim_free(save_fname);
25025
25026 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027}
25028
25029/*
25030 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025031 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 */
25033 static int
25034shortpath_for_partial(fnamep, bufp, fnamelen)
25035 char_u **fnamep;
25036 char_u **bufp;
25037 int *fnamelen;
25038{
25039 int sepcount, len, tflen;
25040 char_u *p;
25041 char_u *pbuf, *tfname;
25042 int hasTilde;
25043
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025044 /* Count up the path separators from the RHS.. so we know which part
25045 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025047 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048 if (vim_ispathsep(*p))
25049 ++sepcount;
25050
25051 /* Need full path first (use expand_env() to remove a "~/") */
25052 hasTilde = (**fnamep == '~');
25053 if (hasTilde)
25054 pbuf = tfname = expand_env_save(*fnamep);
25055 else
25056 pbuf = tfname = FullName_save(*fnamep, FALSE);
25057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025058 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025060 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062
25063 if (len == 0)
25064 {
25065 /* Don't have a valid filename, so shorten the rest of the
25066 * path if we can. This CAN give us invalid 8.3 filenames, but
25067 * there's not a lot of point in guessing what it might be.
25068 */
25069 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025070 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25071 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072 }
25073
25074 /* Count the paths backward to find the beginning of the desired string. */
25075 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025076 {
25077#ifdef FEAT_MBYTE
25078 if (has_mbyte)
25079 p -= mb_head_off(tfname, p);
25080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081 if (vim_ispathsep(*p))
25082 {
25083 if (sepcount == 0 || (hasTilde && sepcount == 1))
25084 break;
25085 else
25086 sepcount --;
25087 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 if (hasTilde)
25090 {
25091 --p;
25092 if (p >= tfname)
25093 *p = '~';
25094 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 }
25097 else
25098 ++p;
25099
25100 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25101 vim_free(*bufp);
25102 *fnamelen = (int)STRLEN(p);
25103 *bufp = pbuf;
25104 *fnamep = p;
25105
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025106 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107}
25108#endif /* WIN3264 */
25109
25110/*
25111 * Adjust a filename, according to a string of modifiers.
25112 * *fnamep must be NUL terminated when called. When returning, the length is
25113 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025114 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 * When there is an error, *fnamep is set to NULL.
25116 */
25117 int
25118modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25119 char_u *src; /* string with modifiers */
25120 int *usedlen; /* characters after src that are used */
25121 char_u **fnamep; /* file name so far */
25122 char_u **bufp; /* buffer for allocated file name or NULL */
25123 int *fnamelen; /* length of fnamep */
25124{
25125 int valid = 0;
25126 char_u *tail;
25127 char_u *s, *p, *pbuf;
25128 char_u dirname[MAXPATHL];
25129 int c;
25130 int has_fullname = 0;
25131#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025132 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 int has_shortname = 0;
25134#endif
25135
25136repeat:
25137 /* ":p" - full path/file_name */
25138 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25139 {
25140 has_fullname = 1;
25141
25142 valid |= VALID_PATH;
25143 *usedlen += 2;
25144
25145 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25146 if ((*fnamep)[0] == '~'
25147#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25148 && ((*fnamep)[1] == '/'
25149# ifdef BACKSLASH_IN_FILENAME
25150 || (*fnamep)[1] == '\\'
25151# endif
25152 || (*fnamep)[1] == NUL)
25153
25154#endif
25155 )
25156 {
25157 *fnamep = expand_env_save(*fnamep);
25158 vim_free(*bufp); /* free any allocated file name */
25159 *bufp = *fnamep;
25160 if (*fnamep == NULL)
25161 return -1;
25162 }
25163
25164 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025165 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 {
25167 if (vim_ispathsep(*p)
25168 && p[1] == '.'
25169 && (p[2] == NUL
25170 || vim_ispathsep(p[2])
25171 || (p[2] == '.'
25172 && (p[3] == NUL || vim_ispathsep(p[3])))))
25173 break;
25174 }
25175
25176 /* FullName_save() is slow, don't use it when not needed. */
25177 if (*p != NUL || !vim_isAbsName(*fnamep))
25178 {
25179 *fnamep = FullName_save(*fnamep, *p != NUL);
25180 vim_free(*bufp); /* free any allocated file name */
25181 *bufp = *fnamep;
25182 if (*fnamep == NULL)
25183 return -1;
25184 }
25185
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025186#ifdef WIN3264
25187# if _WIN32_WINNT >= 0x0500
25188 if (vim_strchr(*fnamep, '~') != NULL)
25189 {
25190 /* Expand 8.3 filename to full path. Needed to make sure the same
25191 * file does not have two different names.
25192 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25193 p = alloc(_MAX_PATH + 1);
25194 if (p != NULL)
25195 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025196 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025197 {
25198 vim_free(*bufp);
25199 *bufp = *fnamep = p;
25200 }
25201 else
25202 vim_free(p);
25203 }
25204 }
25205# endif
25206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 /* Append a path separator to a directory. */
25208 if (mch_isdir(*fnamep))
25209 {
25210 /* Make room for one or two extra characters. */
25211 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25212 vim_free(*bufp); /* free any allocated file name */
25213 *bufp = *fnamep;
25214 if (*fnamep == NULL)
25215 return -1;
25216 add_pathsep(*fnamep);
25217 }
25218 }
25219
25220 /* ":." - path relative to the current directory */
25221 /* ":~" - path relative to the home directory */
25222 /* ":8" - shortname path - postponed till after */
25223 while (src[*usedlen] == ':'
25224 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25225 {
25226 *usedlen += 2;
25227 if (c == '8')
25228 {
25229#ifdef WIN3264
25230 has_shortname = 1; /* Postpone this. */
25231#endif
25232 continue;
25233 }
25234 pbuf = NULL;
25235 /* Need full path first (use expand_env() to remove a "~/") */
25236 if (!has_fullname)
25237 {
25238 if (c == '.' && **fnamep == '~')
25239 p = pbuf = expand_env_save(*fnamep);
25240 else
25241 p = pbuf = FullName_save(*fnamep, FALSE);
25242 }
25243 else
25244 p = *fnamep;
25245
25246 has_fullname = 0;
25247
25248 if (p != NULL)
25249 {
25250 if (c == '.')
25251 {
25252 mch_dirname(dirname, MAXPATHL);
25253 s = shorten_fname(p, dirname);
25254 if (s != NULL)
25255 {
25256 *fnamep = s;
25257 if (pbuf != NULL)
25258 {
25259 vim_free(*bufp); /* free any allocated file name */
25260 *bufp = pbuf;
25261 pbuf = NULL;
25262 }
25263 }
25264 }
25265 else
25266 {
25267 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25268 /* Only replace it when it starts with '~' */
25269 if (*dirname == '~')
25270 {
25271 s = vim_strsave(dirname);
25272 if (s != NULL)
25273 {
25274 *fnamep = s;
25275 vim_free(*bufp);
25276 *bufp = s;
25277 }
25278 }
25279 }
25280 vim_free(pbuf);
25281 }
25282 }
25283
25284 tail = gettail(*fnamep);
25285 *fnamelen = (int)STRLEN(*fnamep);
25286
25287 /* ":h" - head, remove "/file_name", can be repeated */
25288 /* Don't remove the first "/" or "c:\" */
25289 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25290 {
25291 valid |= VALID_HEAD;
25292 *usedlen += 2;
25293 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025294 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025295 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 *fnamelen = (int)(tail - *fnamep);
25297#ifdef VMS
25298 if (*fnamelen > 0)
25299 *fnamelen += 1; /* the path separator is part of the path */
25300#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025301 if (*fnamelen == 0)
25302 {
25303 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25304 p = vim_strsave((char_u *)".");
25305 if (p == NULL)
25306 return -1;
25307 vim_free(*bufp);
25308 *bufp = *fnamep = tail = p;
25309 *fnamelen = 1;
25310 }
25311 else
25312 {
25313 while (tail > s && !after_pathsep(s, tail))
25314 mb_ptr_back(*fnamep, tail);
25315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 }
25317
25318 /* ":8" - shortname */
25319 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25320 {
25321 *usedlen += 2;
25322#ifdef WIN3264
25323 has_shortname = 1;
25324#endif
25325 }
25326
25327#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025328 /*
25329 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330 */
25331 if (has_shortname)
25332 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025333 /* Copy the string if it is shortened by :h and when it wasn't copied
25334 * yet, because we are going to change it in place. Avoids changing
25335 * the buffer name for "%:8". */
25336 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 {
25338 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025339 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 return -1;
25341 vim_free(*bufp);
25342 *bufp = *fnamep = p;
25343 }
25344
25345 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025346 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025347 if (!has_fullname && !vim_isAbsName(*fnamep))
25348 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025349 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 return -1;
25351 }
25352 else
25353 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025354 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355
Bram Moolenaardc935552011-08-17 15:23:23 +020025356 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025358 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359 return -1;
25360
25361 if (l == 0)
25362 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025363 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025365 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 return -1;
25367 }
25368 *fnamelen = l;
25369 }
25370 }
25371#endif /* WIN3264 */
25372
25373 /* ":t" - tail, just the basename */
25374 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25375 {
25376 *usedlen += 2;
25377 *fnamelen -= (int)(tail - *fnamep);
25378 *fnamep = tail;
25379 }
25380
25381 /* ":e" - extension, can be repeated */
25382 /* ":r" - root, without extension, can be repeated */
25383 while (src[*usedlen] == ':'
25384 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25385 {
25386 /* find a '.' in the tail:
25387 * - for second :e: before the current fname
25388 * - otherwise: The last '.'
25389 */
25390 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25391 s = *fnamep - 2;
25392 else
25393 s = *fnamep + *fnamelen - 1;
25394 for ( ; s > tail; --s)
25395 if (s[0] == '.')
25396 break;
25397 if (src[*usedlen + 1] == 'e') /* :e */
25398 {
25399 if (s > tail)
25400 {
25401 *fnamelen += (int)(*fnamep - (s + 1));
25402 *fnamep = s + 1;
25403#ifdef VMS
25404 /* cut version from the extension */
25405 s = *fnamep + *fnamelen - 1;
25406 for ( ; s > *fnamep; --s)
25407 if (s[0] == ';')
25408 break;
25409 if (s > *fnamep)
25410 *fnamelen = s - *fnamep;
25411#endif
25412 }
25413 else if (*fnamep <= tail)
25414 *fnamelen = 0;
25415 }
25416 else /* :r */
25417 {
25418 if (s > tail) /* remove one extension */
25419 *fnamelen = (int)(s - *fnamep);
25420 }
25421 *usedlen += 2;
25422 }
25423
25424 /* ":s?pat?foo?" - substitute */
25425 /* ":gs?pat?foo?" - global substitute */
25426 if (src[*usedlen] == ':'
25427 && (src[*usedlen + 1] == 's'
25428 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25429 {
25430 char_u *str;
25431 char_u *pat;
25432 char_u *sub;
25433 int sep;
25434 char_u *flags;
25435 int didit = FALSE;
25436
25437 flags = (char_u *)"";
25438 s = src + *usedlen + 2;
25439 if (src[*usedlen + 1] == 'g')
25440 {
25441 flags = (char_u *)"g";
25442 ++s;
25443 }
25444
25445 sep = *s++;
25446 if (sep)
25447 {
25448 /* find end of pattern */
25449 p = vim_strchr(s, sep);
25450 if (p != NULL)
25451 {
25452 pat = vim_strnsave(s, (int)(p - s));
25453 if (pat != NULL)
25454 {
25455 s = p + 1;
25456 /* find end of substitution */
25457 p = vim_strchr(s, sep);
25458 if (p != NULL)
25459 {
25460 sub = vim_strnsave(s, (int)(p - s));
25461 str = vim_strnsave(*fnamep, *fnamelen);
25462 if (sub != NULL && str != NULL)
25463 {
25464 *usedlen = (int)(p + 1 - src);
25465 s = do_string_sub(str, pat, sub, flags);
25466 if (s != NULL)
25467 {
25468 *fnamep = s;
25469 *fnamelen = (int)STRLEN(s);
25470 vim_free(*bufp);
25471 *bufp = s;
25472 didit = TRUE;
25473 }
25474 }
25475 vim_free(sub);
25476 vim_free(str);
25477 }
25478 vim_free(pat);
25479 }
25480 }
25481 /* after using ":s", repeat all the modifiers */
25482 if (didit)
25483 goto repeat;
25484 }
25485 }
25486
Bram Moolenaar26df0922014-02-23 23:39:13 +010025487 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25488 {
25489 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25490 if (p == NULL)
25491 return -1;
25492 vim_free(*bufp);
25493 *bufp = *fnamep = p;
25494 *fnamelen = (int)STRLEN(p);
25495 *usedlen += 2;
25496 }
25497
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 return valid;
25499}
25500
25501/*
25502 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25503 * "flags" can be "g" to do a global substitute.
25504 * Returns an allocated string, NULL for error.
25505 */
25506 char_u *
25507do_string_sub(str, pat, sub, flags)
25508 char_u *str;
25509 char_u *pat;
25510 char_u *sub;
25511 char_u *flags;
25512{
25513 int sublen;
25514 regmatch_T regmatch;
25515 int i;
25516 int do_all;
25517 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025518 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519 garray_T ga;
25520 char_u *ret;
25521 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025522 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
25524 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25525 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025526 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527
25528 ga_init2(&ga, 1, 200);
25529
25530 do_all = (flags[0] == 'g');
25531
25532 regmatch.rm_ic = p_ic;
25533 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25534 if (regmatch.regprog != NULL)
25535 {
25536 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025537 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25539 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025540 /* Skip empty match except for first match. */
25541 if (regmatch.startp[0] == regmatch.endp[0])
25542 {
25543 if (zero_width == regmatch.startp[0])
25544 {
25545 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025546 i = MB_PTR2LEN(tail);
25547 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25548 (size_t)i);
25549 ga.ga_len += i;
25550 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025551 continue;
25552 }
25553 zero_width = regmatch.startp[0];
25554 }
25555
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 /*
25557 * Get some space for a temporary buffer to do the substitution
25558 * into. It will contain:
25559 * - The text up to where the match is.
25560 * - The substituted text.
25561 * - The text after the match.
25562 */
25563 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025564 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25566 {
25567 ga_clear(&ga);
25568 break;
25569 }
25570
25571 /* copy the text up to where the match is */
25572 i = (int)(regmatch.startp[0] - tail);
25573 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25574 /* add the substituted text */
25575 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25576 + ga.ga_len + i, TRUE, TRUE, FALSE);
25577 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025578 tail = regmatch.endp[0];
25579 if (*tail == NUL)
25580 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 if (!do_all)
25582 break;
25583 }
25584
25585 if (ga.ga_data != NULL)
25586 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25587
Bram Moolenaar473de612013-06-08 18:19:48 +020025588 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589 }
25590
25591 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25592 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025593 if (p_cpo == empty_option)
25594 p_cpo = save_cpo;
25595 else
25596 /* Darn, evaluating {sub} expression changed the value. */
25597 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598
25599 return ret;
25600}
25601
25602#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */