blob: a173cc73a7a154116b0531bec3d144404e80ab9a [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 Moolenaar33570922005-01-25 22:26:29 +0000367};
368
369/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370#define vv_type vv_di.di_tv.v_type
371#define vv_nr vv_di.di_tv.vval.v_number
372#define vv_float vv_di.di_tv.vval.v_float
373#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000374#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200377static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000378#define vimvarht vimvardict.dv_hashtab
379
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
381static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
383static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
384static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
386static void list_glob_vars __ARGS((int *first));
387static void list_buf_vars __ARGS((int *first));
388static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_vim_vars __ARGS((int *first));
393static void list_script_vars __ARGS((int *first));
394static void list_func_vars __ARGS((int *first));
395static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
397static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100398static 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 +0000399static void clear_lval __ARGS((lval_T *lp));
400static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
401static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
403static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
404static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
405static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
406static void item_lock __ARGS((typval_T *tv, int deep, int lock));
407static int tv_islocked __ARGS((typval_T *tv));
408
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
410static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000415static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
416static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000418static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000423static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100425static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
426static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
427static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000428static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
432static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100435static 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 +0000436static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000437static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200438static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
440static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000448#ifdef FEAT_FLOAT
449static int string2float __ARGS((char_u *text, float_T *value));
450#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
452static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100453static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static 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 +0200455static 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 +0000456static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000457static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#ifdef FEAT_FLOAT
460static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100464static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200468static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200471static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100484static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100486static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200505static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
510static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200520static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200522#ifdef FEAT_FLOAT
523static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
524#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000538static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000547static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000549static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000555static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200556static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100578static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static 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 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200786static 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 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static 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 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static 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 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200805static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
806static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200809static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static 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 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002260 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002811 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002929 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002930 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002933 if ((di == NULL
2934 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2935 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2936 FALSE)))
2937 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 set_var(lp->ll_name, &tv, FALSE);
2939 clear_tv(&tv);
2940 }
2941 }
2942 else
2943 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002947 else if (tv_check_lock(lp->ll_newkey == NULL
2948 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002949 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 else if (lp->ll_range)
2952 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002953 listitem_T *ll_li = lp->ll_li;
2954 int ll_n1 = lp->ll_n1;
2955
2956 /*
2957 * Check whether any of the list items is locked
2958 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002959 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002961 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002962 return;
2963 ri = ri->li_next;
2964 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2965 break;
2966 ll_li = ll_li->li_next;
2967 ++ll_n1;
2968 }
2969
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 /*
2971 * Assign the List values to the list items.
2972 */
2973 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002974 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 if (op != NULL && *op != '=')
2976 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2977 else
2978 {
2979 clear_tv(&lp->ll_li->li_tv);
2980 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 ri = ri->li_next;
2983 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2984 break;
2985 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002988 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = NULL;
2991 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 lp->ll_li = lp->ll_li->li_next;
2995 ++lp->ll_n1;
2996 }
2997 if (ri != NULL)
2998 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (lp->ll_empty2
3000 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 : lp->ll_n1 != lp->ll_n2)
3002 EMSG(_("E711: List value has not enough items"));
3003 }
3004 else
3005 {
3006 /*
3007 * Assign to a List or Dictionary item.
3008 */
3009 if (lp->ll_newkey != NULL)
3010 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 if (op != NULL && *op != '=')
3012 {
3013 EMSG2(_(e_letwrong), op);
3014 return;
3015 }
3016
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003018 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 if (di == NULL)
3020 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003021 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3022 {
3023 vim_free(di);
3024 return;
3025 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003027 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 else if (op != NULL && *op != '=')
3029 {
3030 tv_op(lp->ll_tv, rettv, op);
3031 return;
3032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003033 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /*
3037 * Assign the value to the variable or list item.
3038 */
3039 if (copy)
3040 copy_tv(rettv, lp->ll_tv);
3041 else
3042 {
3043 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003044 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 }
3047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048}
3049
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3052 * Returns OK or FAIL.
3053 */
3054 static int
3055tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 typval_T *tv1;
3057 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 char_u *op;
3059{
3060 long n;
3061 char_u numbuf[NUMBUFLEN];
3062 char_u *s;
3063
3064 /* Can't do anything with a Funcref or a Dict on the right. */
3065 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3066 {
3067 switch (tv1->v_type)
3068 {
3069 case VAR_DICT:
3070 case VAR_FUNC:
3071 break;
3072
3073 case VAR_LIST:
3074 if (*op != '+' || tv2->v_type != VAR_LIST)
3075 break;
3076 /* List += List */
3077 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3078 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3079 return OK;
3080
3081 case VAR_NUMBER:
3082 case VAR_STRING:
3083 if (tv2->v_type == VAR_LIST)
3084 break;
3085 if (*op == '+' || *op == '-')
3086 {
3087 /* nr += nr or nr -= nr*/
3088 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089#ifdef FEAT_FLOAT
3090 if (tv2->v_type == VAR_FLOAT)
3091 {
3092 float_T f = n;
3093
3094 if (*op == '+')
3095 f += tv2->vval.v_float;
3096 else
3097 f -= tv2->vval.v_float;
3098 clear_tv(tv1);
3099 tv1->v_type = VAR_FLOAT;
3100 tv1->vval.v_float = f;
3101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#endif
3104 {
3105 if (*op == '+')
3106 n += get_tv_number(tv2);
3107 else
3108 n -= get_tv_number(tv2);
3109 clear_tv(tv1);
3110 tv1->v_type = VAR_NUMBER;
3111 tv1->vval.v_number = n;
3112 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 }
3114 else
3115 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 if (tv2->v_type == VAR_FLOAT)
3117 break;
3118
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 /* str .= str */
3120 s = get_tv_string(tv1);
3121 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_STRING;
3124 tv1->vval.v_string = s;
3125 }
3126 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127
3128#ifdef FEAT_FLOAT
3129 case VAR_FLOAT:
3130 {
3131 float_T f;
3132
3133 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3134 && tv2->v_type != VAR_NUMBER
3135 && tv2->v_type != VAR_STRING))
3136 break;
3137 if (tv2->v_type == VAR_FLOAT)
3138 f = tv2->vval.v_float;
3139 else
3140 f = get_tv_number(tv2);
3141 if (*op == '+')
3142 tv1->vval.v_float += f;
3143 else
3144 tv1->vval.v_float -= f;
3145 }
3146 return OK;
3147#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 }
3150
3151 EMSG2(_(e_letwrong), op);
3152 return FAIL;
3153}
3154
3155/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 * Add a watcher to a list.
3157 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003158 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 list_T *l;
3161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162{
3163 lw->lw_next = l->lv_watch;
3164 l->lv_watch = lw;
3165}
3166
3167/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003168 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 * No warning when it isn't found...
3170 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003171 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 list_T *l;
3174 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175{
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177
3178 lwp = &l->lv_watch;
3179 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3180 {
3181 if (lw == lwrem)
3182 {
3183 *lwp = lw->lw_next;
3184 break;
3185 }
3186 lwp = &lw->lw_next;
3187 }
3188}
3189
3190/*
3191 * Just before removing an item from a list: advance watchers to the next
3192 * item.
3193 */
3194 static void
3195list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 list_T *l;
3197 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3202 if (lw->lw_item == item)
3203 lw->lw_item = item->li_next;
3204}
3205
3206/*
3207 * Evaluate the expression used in a ":for var in expr" command.
3208 * "arg" points to "var".
3209 * Set "*errp" to TRUE for an error, FALSE otherwise;
3210 * Return a pointer that holds the info. Null when there is an error.
3211 */
3212 void *
3213eval_for_line(arg, errp, nextcmdp, skip)
3214 char_u *arg;
3215 int *errp;
3216 char_u **nextcmdp;
3217 int skip;
3218{
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 typval_T tv;
3222 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223
3224 *errp = TRUE; /* default: there is an error */
3225
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 if (fi == NULL)
3228 return NULL;
3229
3230 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3231 if (expr == NULL)
3232 return fi;
3233
3234 expr = skipwhite(expr);
3235 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3236 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003237 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 return fi;
3239 }
3240
3241 if (skip)
3242 ++emsg_skip;
3243 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3244 {
3245 *errp = FALSE;
3246 if (!skip)
3247 {
3248 l = tv.vval.v_list;
3249 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003250 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 clear_tv(&tv);
3253 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 else
3255 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003256 /* No need to increment the refcount, it's already set for the
3257 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 fi->fi_list = l;
3259 list_add_watch(l, &fi->fi_lw);
3260 fi->fi_lw.lw_item = l->lv_first;
3261 }
3262 }
3263 }
3264 if (skip)
3265 --emsg_skip;
3266
3267 return fi;
3268}
3269
3270/*
3271 * Use the first item in a ":for" list. Advance to the next.
3272 * Assign the values to the variable (list). "arg" points to the first one.
3273 * Return TRUE when a valid item was found, FALSE when at end of list or
3274 * something wrong.
3275 */
3276 int
3277next_for_item(fi_void, arg)
3278 void *fi_void;
3279 char_u *arg;
3280{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003281 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284
3285 item = fi->fi_lw.lw_item;
3286 if (item == NULL)
3287 result = FALSE;
3288 else
3289 {
3290 fi->fi_lw.lw_item = item->li_next;
3291 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3292 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3293 }
3294 return result;
3295}
3296
3297/*
3298 * Free the structure used to store info used by ":for".
3299 */
3300 void
3301free_for_info(fi_void)
3302 void *fi_void;
3303{
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003306 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003309 list_unref(fi->fi_list);
3310 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 vim_free(fi);
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3315
3316 void
3317set_context_for_expression(xp, arg, cmdidx)
3318 expand_T *xp;
3319 char_u *arg;
3320 cmdidx_T cmdidx;
3321{
3322 int got_eq = FALSE;
3323 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 if (cmdidx == CMD_let)
3327 {
3328 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003329 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 {
3331 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003332 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 {
3334 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 if (vim_iswhite(*p))
3337 break;
3338 }
3339 return;
3340 }
3341 }
3342 else
3343 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3344 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((xp->xp_pattern = vim_strpbrk(arg,
3346 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3347 {
3348 c = *xp->xp_pattern;
3349 if (c == '&')
3350 {
3351 c = xp->xp_pattern[1];
3352 if (c == '&')
3353 {
3354 ++xp->xp_pattern;
3355 xp->xp_context = cmdidx != CMD_let || got_eq
3356 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3357 }
3358 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003361 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3362 xp->xp_pattern += 2;
3363
3364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 else if (c == '$')
3367 {
3368 /* environment variable */
3369 xp->xp_context = EXPAND_ENV_VARS;
3370 }
3371 else if (c == '=')
3372 {
3373 got_eq = TRUE;
3374 xp->xp_context = EXPAND_EXPRESSION;
3375 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003376 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 && xp->xp_context == EXPAND_FUNCTIONS
3378 && vim_strchr(xp->xp_pattern, '(') == NULL)
3379 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003380 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 break;
3382 }
3383 else if (cmdidx != CMD_let || got_eq)
3384 {
3385 if (c == '"') /* string */
3386 {
3387 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3388 if (c == '\\' && xp->xp_pattern[1] != NUL)
3389 ++xp->xp_pattern;
3390 xp->xp_context = EXPAND_NOTHING;
3391 }
3392 else if (c == '\'') /* literal string */
3393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003394 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3396 /* skip */ ;
3397 xp->xp_context = EXPAND_NOTHING;
3398 }
3399 else if (c == '|')
3400 {
3401 if (xp->xp_pattern[1] == '|')
3402 {
3403 ++xp->xp_pattern;
3404 xp->xp_context = EXPAND_EXPRESSION;
3405 }
3406 else
3407 xp->xp_context = EXPAND_COMMANDS;
3408 }
3409 else
3410 xp->xp_context = EXPAND_EXPRESSION;
3411 }
3412 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003413 /* Doesn't look like something valid, expand as an expression
3414 * anyway. */
3415 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 arg = xp->xp_pattern;
3417 if (*arg != NUL)
3418 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3419 /* skip */ ;
3420 }
3421 xp->xp_pattern = arg;
3422}
3423
3424#endif /* FEAT_CMDL_COMPL */
3425
3426/*
3427 * ":1,25call func(arg1, arg2)" function call.
3428 */
3429 void
3430ex_call(eap)
3431 exarg_T *eap;
3432{
3433 char_u *arg = eap->arg;
3434 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003436 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003438 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 linenr_T lnum;
3440 int doesrange;
3441 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003442 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003444 if (eap->skip)
3445 {
3446 /* trans_function_name() doesn't work well when skipping, use eval0()
3447 * instead to skip to any following command, e.g. for:
3448 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003449 ++emsg_skip;
3450 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3451 clear_tv(&rettv);
3452 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003453 return;
3454 }
3455
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003456 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003457 if (fudi.fd_newkey != NULL)
3458 {
3459 /* Still need to give an error message for missing key. */
3460 EMSG2(_(e_dictkey), fudi.fd_newkey);
3461 vim_free(fudi.fd_newkey);
3462 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003463 if (tofree == NULL)
3464 return;
3465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 /* Increase refcount on dictionary, it could get deleted when evaluating
3467 * the arguments. */
3468 if (fudi.fd_dict != NULL)
3469 ++fudi.fd_dict->dv_refcount;
3470
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003473 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar532c7802005-01-27 14:44:31 +00003475 /* Skip white space to allow ":call func ()". Not good, but required for
3476 * backward compatibility. */
3477 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 if (*startarg != '(')
3481 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003482 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 goto end;
3484 }
3485
3486 /*
3487 * When skipping, evaluate the function once, to find the end of the
3488 * arguments.
3489 * When the function takes a range, this is discovered after the first
3490 * call, and the loop is broken.
3491 */
3492 if (eap->skip)
3493 {
3494 ++emsg_skip;
3495 lnum = eap->line2; /* do it once, also with an invalid range */
3496 }
3497 else
3498 lnum = eap->line1;
3499 for ( ; lnum <= eap->line2; ++lnum)
3500 {
3501 if (!eap->skip && eap->addr_count > 0)
3502 {
3503 curwin->w_cursor.lnum = lnum;
3504 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003505#ifdef FEAT_VIRTUALEDIT
3506 curwin->w_cursor.coladd = 0;
3507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003510 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003511 eap->line1, eap->line2, &doesrange,
3512 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 failed = TRUE;
3515 break;
3516 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003517
3518 /* Handle a function returning a Funcref, Dictionary or List. */
3519 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3520 {
3521 failed = TRUE;
3522 break;
3523 }
3524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003525 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (doesrange || eap->skip)
3527 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003530 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003532 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (aborting())
3534 break;
3535 }
3536 if (eap->skip)
3537 --emsg_skip;
3538
3539 if (!failed)
3540 {
3541 /* Check for trailing illegal characters and a following command. */
3542 if (!ends_excmd(*arg))
3543 {
3544 emsg_severe = TRUE;
3545 EMSG(_(e_trailing));
3546 }
3547 else
3548 eap->nextcmd = check_nextcmd(arg);
3549 }
3550
3551end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003552 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003553 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
3555
3556/*
3557 * ":unlet[!] var1 ... " command.
3558 */
3559 void
3560ex_unlet(eap)
3561 exarg_T *eap;
3562{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003563 ex_unletlock(eap, eap->arg, 0);
3564}
3565
3566/*
3567 * ":lockvar" and ":unlockvar" commands
3568 */
3569 void
3570ex_lockvar(eap)
3571 exarg_T *eap;
3572{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 int deep = 2;
3575
3576 if (eap->forceit)
3577 deep = -1;
3578 else if (vim_isdigit(*arg))
3579 {
3580 deep = getdigits(&arg);
3581 arg = skipwhite(arg);
3582 }
3583
3584 ex_unletlock(eap, arg, deep);
3585}
3586
3587/*
3588 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3589 */
3590 static void
3591ex_unletlock(eap, argstart, deep)
3592 exarg_T *eap;
3593 char_u *argstart;
3594 int deep;
3595{
3596 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 do
3602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003604 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003605 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (lv.ll_name == NULL)
3607 error = TRUE; /* error but continue parsing */
3608 if (name_end == NULL || (!vim_iswhite(*name_end)
3609 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 if (name_end != NULL)
3612 {
3613 emsg_severe = TRUE;
3614 EMSG(_(e_trailing));
3615 }
3616 if (!(eap->skip || error))
3617 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
3620
3621 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 {
3623 if (eap->cmdidx == CMD_unlet)
3624 {
3625 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3626 error = TRUE;
3627 }
3628 else
3629 {
3630 if (do_lock_var(&lv, name_end, deep,
3631 eap->cmdidx == CMD_lockvar) == FAIL)
3632 error = TRUE;
3633 }
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (!eap->skip)
3637 clear_lval(&lv);
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 arg = skipwhite(name_end);
3640 } while (!ends_excmd(*arg));
3641
3642 eap->nextcmd = check_nextcmd(arg);
3643}
3644
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 int forceit;
3650{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 int ret = OK;
3652 int cc;
3653
3654 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 cc = *name_end;
3657 *name_end = NUL;
3658
3659 /* Normal name or expanded name. */
3660 if (check_changedtick(lp->ll_name))
3661 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003666 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003667 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003668 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003669 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 else if (lp->ll_range)
3672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003674 listitem_T *ll_li = lp->ll_li;
3675 int ll_n1 = lp->ll_n1;
3676
3677 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3678 {
3679 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003681 return FAIL;
3682 ll_li = li;
3683 ++ll_n1;
3684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685
3686 /* Delete a range of List items. */
3687 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3688 {
3689 li = lp->ll_li->li_next;
3690 listitem_remove(lp->ll_list, lp->ll_li);
3691 lp->ll_li = li;
3692 ++lp->ll_n1;
3693 }
3694 }
3695 else
3696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 /* unlet a List item. */
3699 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003702 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 }
3704
3705 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706}
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708/*
3709 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
3712 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003713do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003715 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hashtab_T *ht;
3718 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003719 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003720 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003721 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 ht = find_var_ht(name, &varname);
3724 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 if (ht == &globvarht)
3727 d = &globvardict;
3728 else if (current_funccal != NULL
3729 && ht == &current_funccal->l_vars.dv_hashtab)
3730 d = &current_funccal->l_vars;
3731 else
3732 {
3733 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3734 d = di->di_tv.vval.v_dict;
3735 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hi = hash_find(ht, varname);
3737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003739 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003740 if (var_check_fixed(di->di_flags, name, FALSE)
3741 || var_check_ro(di->di_flags, name, FALSE)
3742 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003743 return FAIL;
3744 delete_var(ht, hi);
3745 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003748 if (forceit)
3749 return OK;
3750 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 return FAIL;
3752}
3753
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754/*
3755 * Lock or unlock variable indicated by "lp".
3756 * "deep" is the levels to go (-1 for unlimited);
3757 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3758 */
3759 static int
3760do_lock_var(lp, name_end, deep, lock)
3761 lval_T *lp;
3762 char_u *name_end;
3763 int deep;
3764 int lock;
3765{
3766 int ret = OK;
3767 int cc;
3768 dictitem_T *di;
3769
3770 if (deep == 0) /* nothing to do */
3771 return OK;
3772
3773 if (lp->ll_tv == NULL)
3774 {
3775 cc = *name_end;
3776 *name_end = NUL;
3777
3778 /* Normal name or expanded name. */
3779 if (check_changedtick(lp->ll_name))
3780 ret = FAIL;
3781 else
3782 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003783 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003784 if (di == NULL)
3785 ret = FAIL;
3786 else
3787 {
3788 if (lock)
3789 di->di_flags |= DI_FLAGS_LOCK;
3790 else
3791 di->di_flags &= ~DI_FLAGS_LOCK;
3792 item_lock(&di->di_tv, deep, lock);
3793 }
3794 }
3795 *name_end = cc;
3796 }
3797 else if (lp->ll_range)
3798 {
3799 listitem_T *li = lp->ll_li;
3800
3801 /* (un)lock a range of List items. */
3802 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3803 {
3804 item_lock(&li->li_tv, deep, lock);
3805 li = li->li_next;
3806 ++lp->ll_n1;
3807 }
3808 }
3809 else if (lp->ll_list != NULL)
3810 /* (un)lock a List item. */
3811 item_lock(&lp->ll_li->li_tv, deep, lock);
3812 else
3813 /* un(lock) a Dictionary item. */
3814 item_lock(&lp->ll_di->di_tv, deep, lock);
3815
3816 return ret;
3817}
3818
3819/*
3820 * Lock or unlock an item. "deep" is nr of levels to go.
3821 */
3822 static void
3823item_lock(tv, deep, lock)
3824 typval_T *tv;
3825 int deep;
3826 int lock;
3827{
3828 static int recurse = 0;
3829 list_T *l;
3830 listitem_T *li;
3831 dict_T *d;
3832 hashitem_T *hi;
3833 int todo;
3834
3835 if (recurse >= DICT_MAXNEST)
3836 {
3837 EMSG(_("E743: variable nested too deep for (un)lock"));
3838 return;
3839 }
3840 if (deep == 0)
3841 return;
3842 ++recurse;
3843
3844 /* lock/unlock the item itself */
3845 if (lock)
3846 tv->v_lock |= VAR_LOCKED;
3847 else
3848 tv->v_lock &= ~VAR_LOCKED;
3849
3850 switch (tv->v_type)
3851 {
3852 case VAR_LIST:
3853 if ((l = tv->vval.v_list) != NULL)
3854 {
3855 if (lock)
3856 l->lv_lock |= VAR_LOCKED;
3857 else
3858 l->lv_lock &= ~VAR_LOCKED;
3859 if (deep < 0 || deep > 1)
3860 /* recursive: lock/unlock the items the List contains */
3861 for (li = l->lv_first; li != NULL; li = li->li_next)
3862 item_lock(&li->li_tv, deep - 1, lock);
3863 }
3864 break;
3865 case VAR_DICT:
3866 if ((d = tv->vval.v_dict) != NULL)
3867 {
3868 if (lock)
3869 d->dv_lock |= VAR_LOCKED;
3870 else
3871 d->dv_lock &= ~VAR_LOCKED;
3872 if (deep < 0 || deep > 1)
3873 {
3874 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003875 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003876 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3877 {
3878 if (!HASHITEM_EMPTY(hi))
3879 {
3880 --todo;
3881 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3882 }
3883 }
3884 }
3885 }
3886 }
3887 --recurse;
3888}
3889
Bram Moolenaara40058a2005-07-11 22:42:07 +00003890/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003891 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3892 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003893 */
3894 static int
3895tv_islocked(tv)
3896 typval_T *tv;
3897{
3898 return (tv->v_lock & VAR_LOCKED)
3899 || (tv->v_type == VAR_LIST
3900 && tv->vval.v_list != NULL
3901 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3902 || (tv->v_type == VAR_DICT
3903 && tv->vval.v_dict != NULL
3904 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3905}
3906
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3908/*
3909 * Delete all "menutrans_" variables.
3910 */
3911 void
3912del_menutrans_vars()
3913{
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003915 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003920 {
3921 if (!HASHITEM_EMPTY(hi))
3922 {
3923 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3925 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 }
3927 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003928 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929}
3930#endif
3931
3932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3933
3934/*
3935 * Local string buffer for the next two functions to store a variable name
3936 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3937 * get_user_var_name().
3938 */
3939
3940static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3941
3942static char_u *varnamebuf = NULL;
3943static int varnamebuflen = 0;
3944
3945/*
3946 * Function to concatenate a prefix and a variable name.
3947 */
3948 static char_u *
3949cat_prefix_varname(prefix, name)
3950 int prefix;
3951 char_u *name;
3952{
3953 int len;
3954
3955 len = (int)STRLEN(name) + 3;
3956 if (len > varnamebuflen)
3957 {
3958 vim_free(varnamebuf);
3959 len += 10; /* some additional space */
3960 varnamebuf = alloc(len);
3961 if (varnamebuf == NULL)
3962 {
3963 varnamebuflen = 0;
3964 return NULL;
3965 }
3966 varnamebuflen = len;
3967 }
3968 *varnamebuf = prefix;
3969 varnamebuf[1] = ':';
3970 STRCPY(varnamebuf + 2, name);
3971 return varnamebuf;
3972}
3973
3974/*
3975 * Function given to ExpandGeneric() to obtain the list of user defined
3976 * (global/buffer/window/built-in) variable names.
3977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 char_u *
3979get_user_var_name(xp, idx)
3980 expand_T *xp;
3981 int idx;
3982{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003983 static long_u gdone;
3984 static long_u bdone;
3985 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003986#ifdef FEAT_WINDOWS
3987 static long_u tdone;
3988#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 static int vidx;
3990 static hashitem_T *hi;
3991 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003995 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003996#ifdef FEAT_WINDOWS
3997 tdone = 0;
3998#endif
3999 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004000
4001 /* Global variables */
4002 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004004 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 else
4007 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 while (HASHITEM_EMPTY(hi))
4009 ++hi;
4010 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4011 return cat_prefix_varname('g', hi->hi_key);
4012 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004014
4015 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004016 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004017 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004021 else
4022 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004023 while (HASHITEM_EMPTY(hi))
4024 ++hi;
4025 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return (char_u *)"b:changedtick";
4031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004037 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004046#ifdef FEAT_WINDOWS
4047 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004048 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049 if (tdone < ht->ht_used)
4050 {
4051 if (tdone++ == 0)
4052 hi = ht->ht_array;
4053 else
4054 ++hi;
4055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 return cat_prefix_varname('t', hi->hi_key);
4058 }
4059#endif
4060
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 /* v: variables */
4062 if (vidx < VV_LEN)
4063 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 vim_free(varnamebuf);
4066 varnamebuf = NULL;
4067 varnamebuflen = 0;
4068 return NULL;
4069}
4070
4071#endif /* FEAT_CMDL_COMPL */
4072
4073/*
4074 * types for expressions.
4075 */
4076typedef enum
4077{
4078 TYPE_UNKNOWN = 0
4079 , TYPE_EQUAL /* == */
4080 , TYPE_NEQUAL /* != */
4081 , TYPE_GREATER /* > */
4082 , TYPE_GEQUAL /* >= */
4083 , TYPE_SMALLER /* < */
4084 , TYPE_SEQUAL /* <= */
4085 , TYPE_MATCH /* =~ */
4086 , TYPE_NOMATCH /* !~ */
4087} exptype_T;
4088
4089/*
4090 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4093 */
4094
4095/*
4096 * Handle zero level expression.
4097 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004099 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * Return OK or FAIL.
4101 */
4102 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 char_u **nextcmd;
4107 int evaluate;
4108{
4109 int ret;
4110 char_u *p;
4111
4112 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (ret == FAIL || !ends_excmd(*p))
4115 {
4116 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 /*
4119 * Report the invalid expression unless the expression evaluation has
4120 * been cancelled due to an aborting error, an interrupt, or an
4121 * exception.
4122 */
4123 if (!aborting())
4124 EMSG2(_(e_invexpr2), arg);
4125 ret = FAIL;
4126 }
4127 if (nextcmd != NULL)
4128 *nextcmd = check_nextcmd(p);
4129
4130 return ret;
4131}
4132
4133/*
4134 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004135 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004140 * Note: "rettv.v_lock" is not set.
4141 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * Return OK or FAIL.
4143 */
4144 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int evaluate;
4149{
4150 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004151 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 if ((*arg)[0] == '?')
4160 {
4161 result = FALSE;
4162 if (evaluate)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 int error = FALSE;
4165
4166 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (error)
4170 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172
4173 /*
4174 * Get the second variable.
4175 */
4176 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 /*
4181 * Check for the ":".
4182 */
4183 if ((*arg)[0] != ':')
4184 {
4185 EMSG(_("E109: Missing ':' after '?'"));
4186 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
4189 }
4190
4191 /*
4192 * Get the third variable.
4193 */
4194 *arg = skipwhite(*arg + 1);
4195 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4196 {
4197 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
4200 }
4201 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204
4205 return OK;
4206}
4207
4208/*
4209 * Handle first level expression:
4210 * expr2 || expr2 || expr2 logical OR
4211 *
4212 * "arg" must point to the first non-white of the expression.
4213 * "arg" is advanced to the next non-white after the recognized expression.
4214 *
4215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int evaluate;
4222{
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 long result;
4225 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /*
4229 * Get the first variable.
4230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return FAIL;
4233
4234 /*
4235 * Repeat until there is no following "||".
4236 */
4237 first = TRUE;
4238 result = FALSE;
4239 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4240 {
4241 if (evaluate && first)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 first = FALSE;
4249 }
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 2);
4255 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4256 return FAIL;
4257
4258 /*
4259 * Compute the result.
4260 */
4261 if (evaluate && !result)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 if (evaluate)
4270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 rettv->v_type = VAR_NUMBER;
4272 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275
4276 return OK;
4277}
4278
4279/*
4280 * Handle second level expression:
4281 * expr3 && expr3 && expr3 logical AND
4282 *
4283 * "arg" must point to the first non-white of the expression.
4284 * "arg" is advanced to the next non-white after the recognized expression.
4285 *
4286 * Return OK or FAIL.
4287 */
4288 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int evaluate;
4293{
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 long result;
4296 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /*
4300 * Get the first variable.
4301 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return FAIL;
4304
4305 /*
4306 * Repeat until there is no following "&&".
4307 */
4308 first = TRUE;
4309 result = TRUE;
4310 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4311 {
4312 if (evaluate && first)
4313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (error)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 first = FALSE;
4320 }
4321
4322 /*
4323 * Get the second variable.
4324 */
4325 *arg = skipwhite(*arg + 2);
4326 if (eval4(arg, &var2, evaluate && result) == FAIL)
4327 return FAIL;
4328
4329 /*
4330 * Compute the result.
4331 */
4332 if (evaluate && result)
4333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (error)
4338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 if (evaluate)
4341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 rettv->v_type = VAR_NUMBER;
4343 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346
4347 return OK;
4348}
4349
4350/*
4351 * Handle third level expression:
4352 * var1 == var2
4353 * var1 =~ var2
4354 * var1 != var2
4355 * var1 !~ var2
4356 * var1 > var2
4357 * var1 >= var2
4358 * var1 < var2
4359 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004360 * var1 is var2
4361 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 *
4363 * "arg" must point to the first non-white of the expression.
4364 * "arg" is advanced to the next non-white after the recognized expression.
4365 *
4366 * Return OK or FAIL.
4367 */
4368 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 int evaluate;
4373{
Bram Moolenaar33570922005-01-25 22:26:29 +00004374 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 char_u *p;
4376 int i;
4377 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 int len = 2;
4380 long n1, n2;
4381 char_u *s1, *s2;
4382 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4383 regmatch_T regmatch;
4384 int ic;
4385 char_u *save_cpo;
4386
4387 /*
4388 * Get the first variable.
4389 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return FAIL;
4392
4393 p = *arg;
4394 switch (p[0])
4395 {
4396 case '=': if (p[1] == '=')
4397 type = TYPE_EQUAL;
4398 else if (p[1] == '~')
4399 type = TYPE_MATCH;
4400 break;
4401 case '!': if (p[1] == '=')
4402 type = TYPE_NEQUAL;
4403 else if (p[1] == '~')
4404 type = TYPE_NOMATCH;
4405 break;
4406 case '>': if (p[1] != '=')
4407 {
4408 type = TYPE_GREATER;
4409 len = 1;
4410 }
4411 else
4412 type = TYPE_GEQUAL;
4413 break;
4414 case '<': if (p[1] != '=')
4415 {
4416 type = TYPE_SMALLER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_SEQUAL;
4421 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004422 case 'i': if (p[1] == 's')
4423 {
4424 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4425 len = 5;
4426 if (!vim_isIDc(p[len]))
4427 {
4428 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4429 type_is = TRUE;
4430 }
4431 }
4432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434
4435 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004436 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
4438 if (type != TYPE_UNKNOWN)
4439 {
4440 /* extra question mark appended: ignore case */
4441 if (p[len] == '?')
4442 {
4443 ic = TRUE;
4444 ++len;
4445 }
4446 /* extra '#' appended: match case */
4447 else if (p[len] == '#')
4448 {
4449 ic = FALSE;
4450 ++len;
4451 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 else
4454 ic = p_ic;
4455
4456 /*
4457 * Get the second variable.
4458 */
4459 *arg = skipwhite(p + len);
4460 if (eval5(arg, &var2, evaluate) == FAIL)
4461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 return FAIL;
4464 }
4465
4466 if (evaluate)
4467 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 if (type_is && rettv->v_type != var2.v_type)
4469 {
4470 /* For "is" a different type always means FALSE, for "notis"
4471 * it means TRUE. */
4472 n1 = (type == TYPE_NEQUAL);
4473 }
4474 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4475 {
4476 if (type_is)
4477 {
4478 n1 = (rettv->v_type == var2.v_type
4479 && rettv->vval.v_list == var2.vval.v_list);
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 else if (rettv->v_type != var2.v_type
4484 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4485 {
4486 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004489 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004490 clear_tv(rettv);
4491 clear_tv(&var2);
4492 return FAIL;
4493 }
4494 else
4495 {
4496 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004497 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4498 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 if (type == TYPE_NEQUAL)
4500 n1 = !n1;
4501 }
4502 }
4503
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004504 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4505 {
4506 if (type_is)
4507 {
4508 n1 = (rettv->v_type == var2.v_type
4509 && rettv->vval.v_dict == var2.vval.v_dict);
4510 if (type == TYPE_NEQUAL)
4511 n1 = !n1;
4512 }
4513 else if (rettv->v_type != var2.v_type
4514 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4515 {
4516 if (rettv->v_type != var2.v_type)
4517 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4518 else
4519 EMSG(_("E736: Invalid operation for Dictionary"));
4520 clear_tv(rettv);
4521 clear_tv(&var2);
4522 return FAIL;
4523 }
4524 else
4525 {
4526 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004527 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4528 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 if (type == TYPE_NEQUAL)
4530 n1 = !n1;
4531 }
4532 }
4533
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4535 {
4536 if (rettv->v_type != var2.v_type
4537 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4538 {
4539 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004540 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004542 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 clear_tv(rettv);
4544 clear_tv(&var2);
4545 return FAIL;
4546 }
4547 else
4548 {
4549 /* Compare two Funcrefs for being equal or unequal. */
4550 if (rettv->vval.v_string == NULL
4551 || var2.vval.v_string == NULL)
4552 n1 = FALSE;
4553 else
4554 n1 = STRCMP(rettv->vval.v_string,
4555 var2.vval.v_string) == 0;
4556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 }
4560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004561#ifdef FEAT_FLOAT
4562 /*
4563 * If one of the two variables is a float, compare as a float.
4564 * When using "=~" or "!~", always compare as string.
4565 */
4566 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4567 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4568 {
4569 float_T f1, f2;
4570
4571 if (rettv->v_type == VAR_FLOAT)
4572 f1 = rettv->vval.v_float;
4573 else
4574 f1 = get_tv_number(rettv);
4575 if (var2.v_type == VAR_FLOAT)
4576 f2 = var2.vval.v_float;
4577 else
4578 f2 = get_tv_number(&var2);
4579 n1 = FALSE;
4580 switch (type)
4581 {
4582 case TYPE_EQUAL: n1 = (f1 == f2); break;
4583 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4584 case TYPE_GREATER: n1 = (f1 > f2); break;
4585 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4586 case TYPE_SMALLER: n1 = (f1 < f2); break;
4587 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4588 case TYPE_UNKNOWN:
4589 case TYPE_MATCH:
4590 case TYPE_NOMATCH: break; /* avoid gcc warning */
4591 }
4592 }
4593#endif
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /*
4596 * If one of the two variables is a number, compare as a number.
4597 * When using "=~" or "!~", always compare as string.
4598 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 n1 = get_tv_number(rettv);
4603 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 switch (type)
4605 {
4606 case TYPE_EQUAL: n1 = (n1 == n2); break;
4607 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4608 case TYPE_GREATER: n1 = (n1 > n2); break;
4609 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4610 case TYPE_SMALLER: n1 = (n1 < n2); break;
4611 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4612 case TYPE_UNKNOWN:
4613 case TYPE_MATCH:
4614 case TYPE_NOMATCH: break; /* avoid gcc warning */
4615 }
4616 }
4617 else
4618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 s1 = get_tv_string_buf(rettv, buf1);
4620 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4622 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4623 else
4624 i = 0;
4625 n1 = FALSE;
4626 switch (type)
4627 {
4628 case TYPE_EQUAL: n1 = (i == 0); break;
4629 case TYPE_NEQUAL: n1 = (i != 0); break;
4630 case TYPE_GREATER: n1 = (i > 0); break;
4631 case TYPE_GEQUAL: n1 = (i >= 0); break;
4632 case TYPE_SMALLER: n1 = (i < 0); break;
4633 case TYPE_SEQUAL: n1 = (i <= 0); break;
4634
4635 case TYPE_MATCH:
4636 case TYPE_NOMATCH:
4637 /* avoid 'l' flag in 'cpoptions' */
4638 save_cpo = p_cpo;
4639 p_cpo = (char_u *)"";
4640 regmatch.regprog = vim_regcomp(s2,
4641 RE_MAGIC + RE_STRING);
4642 regmatch.rm_ic = ic;
4643 if (regmatch.regprog != NULL)
4644 {
4645 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004646 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (type == TYPE_NOMATCH)
4648 n1 = !n1;
4649 }
4650 p_cpo = save_cpo;
4651 break;
4652
4653 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4654 }
4655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 clear_tv(rettv);
4657 clear_tv(&var2);
4658 rettv->v_type = VAR_NUMBER;
4659 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 }
4662
4663 return OK;
4664}
4665
4666/*
4667 * Handle fourth level expression:
4668 * + number addition
4669 * - number subtraction
4670 * . string concatenation
4671 *
4672 * "arg" must point to the first non-white of the expression.
4673 * "arg" is advanced to the next non-white after the recognized expression.
4674 *
4675 * Return OK or FAIL.
4676 */
4677 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 int evaluate;
4682{
Bram Moolenaar33570922005-01-25 22:26:29 +00004683 typval_T var2;
4684 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 int op;
4686 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004687#ifdef FEAT_FLOAT
4688 float_T f1 = 0, f2 = 0;
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 char_u *s1, *s2;
4691 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4692 char_u *p;
4693
4694 /*
4695 * Get the first variable.
4696 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004697 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 return FAIL;
4699
4700 /*
4701 * Repeat computing, until no '+', '-' or '.' is following.
4702 */
4703 for (;;)
4704 {
4705 op = **arg;
4706 if (op != '+' && op != '-' && op != '.')
4707 break;
4708
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709 if ((op != '+' || rettv->v_type != VAR_LIST)
4710#ifdef FEAT_FLOAT
4711 && (op == '.' || rettv->v_type != VAR_FLOAT)
4712#endif
4713 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
4715 /* For "list + ...", an illegal use of the first operand as
4716 * a number cannot be determined before evaluating the 2nd
4717 * operand: if this is also a list, all is ok.
4718 * For "something . ...", "something - ..." or "non-list + ...",
4719 * we know that the first operand needs to be a string or number
4720 * without evaluating the 2nd operand. So check before to avoid
4721 * side effects after an error. */
4722 if (evaluate && get_tv_string_chk(rettv) == NULL)
4723 {
4724 clear_tv(rettv);
4725 return FAIL;
4726 }
4727 }
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 /*
4730 * Get the second variable.
4731 */
4732 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004733 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 return FAIL;
4737 }
4738
4739 if (evaluate)
4740 {
4741 /*
4742 * Compute the result.
4743 */
4744 if (op == '.')
4745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4747 s2 = get_tv_string_buf_chk(&var2, buf2);
4748 if (s2 == NULL) /* type error ? */
4749 {
4750 clear_tv(rettv);
4751 clear_tv(&var2);
4752 return FAIL;
4753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004754 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(rettv);
4756 rettv->v_type = VAR_STRING;
4757 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004759 else if (op == '+' && rettv->v_type == VAR_LIST
4760 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004761 {
4762 /* concatenate Lists */
4763 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4764 &var3) == FAIL)
4765 {
4766 clear_tv(rettv);
4767 clear_tv(&var2);
4768 return FAIL;
4769 }
4770 clear_tv(rettv);
4771 *rettv = var3;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 else
4774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 int error = FALSE;
4776
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#ifdef FEAT_FLOAT
4778 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780 f1 = rettv->vval.v_float;
4781 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 else
4784#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 n1 = get_tv_number_chk(rettv, &error);
4787 if (error)
4788 {
4789 /* This can only happen for "list + non-list". For
4790 * "non-list + ..." or "something - ...", we returned
4791 * before evaluating the 2nd operand. */
4792 clear_tv(rettv);
4793 return FAIL;
4794 }
4795#ifdef FEAT_FLOAT
4796 if (var2.v_type == VAR_FLOAT)
4797 f1 = n1;
4798#endif
4799 }
4800#ifdef FEAT_FLOAT
4801 if (var2.v_type == VAR_FLOAT)
4802 {
4803 f2 = var2.vval.v_float;
4804 n2 = 0;
4805 }
4806 else
4807#endif
4808 {
4809 n2 = get_tv_number_chk(&var2, &error);
4810 if (error)
4811 {
4812 clear_tv(rettv);
4813 clear_tv(&var2);
4814 return FAIL;
4815 }
4816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
4818 f2 = n2;
4819#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822
4823#ifdef FEAT_FLOAT
4824 /* If there is a float on either side the result is a float. */
4825 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4826 {
4827 if (op == '+')
4828 f1 = f1 + f2;
4829 else
4830 f1 = f1 - f2;
4831 rettv->v_type = VAR_FLOAT;
4832 rettv->vval.v_float = f1;
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835#endif
4836 {
4837 if (op == '+')
4838 n1 = n1 + n2;
4839 else
4840 n1 = n1 - n2;
4841 rettv->v_type = VAR_NUMBER;
4842 rettv->vval.v_number = n1;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848 return OK;
4849}
4850
4851/*
4852 * Handle fifth level expression:
4853 * * number multiplication
4854 * / number division
4855 * % number modulo
4856 *
4857 * "arg" must point to the first non-white of the expression.
4858 * "arg" is advanced to the next non-white after the recognized expression.
4859 *
4860 * Return OK or FAIL.
4861 */
4862 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004863eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004867 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar33570922005-01-25 22:26:29 +00004869 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 int op;
4871 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872#ifdef FEAT_FLOAT
4873 int use_float = FALSE;
4874 float_T f1 = 0, f2;
4875#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878 /*
4879 * Get the first variable.
4880 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 return FAIL;
4883
4884 /*
4885 * Repeat computing, until no '*', '/' or '%' is following.
4886 */
4887 for (;;)
4888 {
4889 op = **arg;
4890 if (op != '*' && op != '/' && op != '%')
4891 break;
4892
4893 if (evaluate)
4894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#ifdef FEAT_FLOAT
4896 if (rettv->v_type == VAR_FLOAT)
4897 {
4898 f1 = rettv->vval.v_float;
4899 use_float = TRUE;
4900 n1 = 0;
4901 }
4902 else
4903#endif
4904 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906 if (error)
4907 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else
4910 n1 = 0;
4911
4912 /*
4913 * Get the second variable.
4914 */
4915 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004916 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
4918
4919 if (evaluate)
4920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921#ifdef FEAT_FLOAT
4922 if (var2.v_type == VAR_FLOAT)
4923 {
4924 if (!use_float)
4925 {
4926 f1 = n1;
4927 use_float = TRUE;
4928 }
4929 f2 = var2.vval.v_float;
4930 n2 = 0;
4931 }
4932 else
4933#endif
4934 {
4935 n2 = get_tv_number_chk(&var2, &error);
4936 clear_tv(&var2);
4937 if (error)
4938 return FAIL;
4939#ifdef FEAT_FLOAT
4940 if (use_float)
4941 f2 = n2;
4942#endif
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 /*
4946 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949#ifdef FEAT_FLOAT
4950 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 if (op == '*')
4953 f1 = f1 * f2;
4954 else if (op == '/')
4955 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004956# ifdef VMS
4957 /* VMS crashes on divide by zero, work around it */
4958 if (f2 == 0.0)
4959 {
4960 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004961 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004963 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 }
4967 else
4968 f1 = f1 / f2;
4969# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 /* We rely on the floating point library to handle divide
4971 * by zero to result in "inf" and not a crash. */
4972 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004977 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 return FAIL;
4979 }
4980 rettv->v_type = VAR_FLOAT;
4981 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 if (op == '*')
4987 n1 = n1 * n2;
4988 else if (op == '/')
4989 {
4990 if (n2 == 0) /* give an error message? */
4991 {
4992 if (n1 == 0)
4993 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4994 else if (n1 < 0)
4995 n1 = -0x7fffffffL;
4996 else
4997 n1 = 0x7fffffffL;
4998 }
4999 else
5000 n1 = n1 / n2;
5001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 {
5004 if (n2 == 0) /* give an error message? */
5005 n1 = 0;
5006 else
5007 n1 = n1 % n2;
5008 }
5009 rettv->v_type = VAR_NUMBER;
5010 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 }
5014
5015 return OK;
5016}
5017
5018/*
5019 * Handle sixth level expression:
5020 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005021 * "string" string constant
5022 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 * &option-name option value
5024 * @r register contents
5025 * identifier variable value
5026 * function() function call
5027 * $VAR environment variable
5028 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005029 * [expr, expr] List
5030 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *
5032 * Also handle:
5033 * ! in front logical NOT
5034 * - in front unary minus
5035 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 * trailing [] subscript in String or List
5037 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 *
5039 * "arg" must point to the first non-white of the expression.
5040 * "arg" is advanced to the next non-white after the recognized expression.
5041 *
5042 * Return OK or FAIL.
5043 */
5044 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005045eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005049 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 long n;
5052 int len;
5053 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *start_leader, *end_leader;
5055 int ret = OK;
5056 char_u *alias;
5057
5058 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * Skip '!' and '-' characters. They are handled later.
5066 */
5067 start_leader = *arg;
5068 while (**arg == '!' || **arg == '-' || **arg == '+')
5069 *arg = skipwhite(*arg + 1);
5070 end_leader = *arg;
5071
5072 switch (**arg)
5073 {
5074 /*
5075 * Number constant.
5076 */
5077 case '0':
5078 case '1':
5079 case '2':
5080 case '3':
5081 case '4':
5082 case '5':
5083 case '6':
5084 case '7':
5085 case '8':
5086 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 {
5088#ifdef FEAT_FLOAT
5089 char_u *p = skipdigits(*arg + 1);
5090 int get_float = FALSE;
5091
5092 /* We accept a float when the format matches
5093 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005094 * strict to avoid backwards compatibility problems.
5095 * Don't look for a float after the "." operator, so that
5096 * ":let vers = 1.2.3" doesn't fail. */
5097 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 get_float = TRUE;
5100 p = skipdigits(p + 2);
5101 if (*p == 'e' || *p == 'E')
5102 {
5103 ++p;
5104 if (*p == '-' || *p == '+')
5105 ++p;
5106 if (!vim_isdigit(*p))
5107 get_float = FALSE;
5108 else
5109 p = skipdigits(p + 1);
5110 }
5111 if (ASCII_ISALPHA(*p) || *p == '.')
5112 get_float = FALSE;
5113 }
5114 if (get_float)
5115 {
5116 float_T f;
5117
5118 *arg += string2float(*arg, &f);
5119 if (evaluate)
5120 {
5121 rettv->v_type = VAR_FLOAT;
5122 rettv->vval.v_float = f;
5123 }
5124 }
5125 else
5126#endif
5127 {
5128 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5129 *arg += len;
5130 if (evaluate)
5131 {
5132 rettv->v_type = VAR_NUMBER;
5133 rettv->vval.v_number = n;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 /*
5140 * String constant: "string".
5141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144
5145 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 break;
5150
5151 /*
5152 * List: [expr, expr]
5153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
5156
5157 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 * Dictionary: {key: val, key: val}
5159 */
5160 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5161 break;
5162
5163 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005164 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168
5169 /*
5170 * Environment variable: $VAR.
5171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Register contents: @r.
5177 */
5178 case '@': ++*arg;
5179 if (evaluate)
5180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005182 rettv->vval.v_string = get_reg_contents(**arg,
5183 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 if (**arg != NUL)
5186 ++*arg;
5187 break;
5188
5189 /*
5190 * nested expression: (expression).
5191 */
5192 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (**arg == ')')
5195 ++*arg;
5196 else if (ret == OK)
5197 {
5198 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 ret = FAIL;
5201 }
5202 break;
5203
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207
5208 if (ret == NOTDONE)
5209 {
5210 /*
5211 * Must be a variable or function name.
5212 * Can also be a curly-braces kind of name: {expr}.
5213 */
5214 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 if (alias != NULL)
5217 s = alias;
5218
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 ret = FAIL;
5221 else
5222 {
5223 if (**arg == '(') /* recursive! */
5224 {
5225 /* If "s" is the name of a variable of type VAR_FUNC
5226 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005227 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228
5229 /* Invoke the function. */
5230 ret = get_func_tv(s, len, rettv, arg,
5231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005232 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005233
5234 /* If evaluate is FALSE rettv->v_type was not set in
5235 * get_func_tv, but it's needed in handle_subscript() to parse
5236 * what follows. So set it here. */
5237 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5238 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005239 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005240 rettv->v_type = VAR_FUNC;
5241 }
5242
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 /* Stop the expression evaluation when immediately
5244 * aborting on error, or when an interrupt occurred or
5245 * an exception was thrown but not caught. */
5246 if (aborting())
5247 {
5248 if (ret == OK)
5249 clear_tv(rettv);
5250 ret = FAIL;
5251 }
5252 }
5253 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005254 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005255 else
5256 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005258 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 *arg = skipwhite(*arg);
5262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5264 * expr(expr). */
5265 if (ret == OK)
5266 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 /*
5269 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5270 */
5271 if (ret == OK && evaluate && end_leader > start_leader)
5272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005274 int val = 0;
5275#ifdef FEAT_FLOAT
5276 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005278 if (rettv->v_type == VAR_FLOAT)
5279 f = rettv->vval.v_float;
5280 else
5281#endif
5282 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 clear_tv(rettv);
5286 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 else
5289 {
5290 while (end_leader > start_leader)
5291 {
5292 --end_leader;
5293 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005294 {
5295#ifdef FEAT_FLOAT
5296 if (rettv->v_type == VAR_FLOAT)
5297 f = !f;
5298 else
5299#endif
5300 val = !val;
5301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 else 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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 {
5315 clear_tv(rettv);
5316 rettv->vval.v_float = f;
5317 }
5318 else
5319#endif
5320 {
5321 clear_tv(rettv);
5322 rettv->v_type = VAR_NUMBER;
5323 rettv->vval.v_number = val;
5324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327
5328 return ret;
5329}
5330
5331/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005332 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5333 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5335 */
5336 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005337eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342{
5343 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 long len = -1;
5347 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005351 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005353 if (verbose)
5354 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return FAIL;
5356 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005357#ifdef FEAT_FLOAT
5358 else if (rettv->v_type == VAR_FLOAT)
5359 {
5360 if (verbose)
5361 EMSG(_(e_float_as_string));
5362 return FAIL;
5363 }
5364#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 /*
5369 * dict.name
5370 */
5371 key = *arg + 1;
5372 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5373 ;
5374 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 /*
5381 * something[idx]
5382 *
5383 * Get the (first) variable from inside the [].
5384 */
5385 *arg = skipwhite(*arg + 1);
5386 if (**arg == ':')
5387 empty1 = TRUE;
5388 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5389 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005390 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5391 {
5392 /* not a number or string */
5393 clear_tv(&var1);
5394 return FAIL;
5395 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396
5397 /*
5398 * Get the second variable from inside the [:].
5399 */
5400 if (**arg == ':')
5401 {
5402 range = TRUE;
5403 *arg = skipwhite(*arg + 1);
5404 if (**arg == ']')
5405 empty2 = TRUE;
5406 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005408 if (!empty1)
5409 clear_tv(&var1);
5410 return FAIL;
5411 }
5412 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5413 {
5414 /* not a number or string */
5415 if (!empty1)
5416 clear_tv(&var1);
5417 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418 return FAIL;
5419 }
5420 }
5421
5422 /* Check for the ']'. */
5423 if (**arg != ']')
5424 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005425 if (verbose)
5426 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 clear_tv(&var1);
5428 if (range)
5429 clear_tv(&var2);
5430 return FAIL;
5431 }
5432 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434
5435 if (evaluate)
5436 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 n1 = 0;
5438 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 n1 = get_tv_number(&var1);
5441 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443 if (range)
5444 {
5445 if (empty2)
5446 n2 = -1;
5447 else
5448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 n2 = get_tv_number(&var2);
5450 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 }
5452 }
5453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 {
5456 case VAR_NUMBER:
5457 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 len = (long)STRLEN(s);
5460 if (range)
5461 {
5462 /* The resulting variable is a substring. If the indexes
5463 * are out of range the result is empty. */
5464 if (n1 < 0)
5465 {
5466 n1 = len + n1;
5467 if (n1 < 0)
5468 n1 = 0;
5469 }
5470 if (n2 < 0)
5471 n2 = len + n2;
5472 else if (n2 >= len)
5473 n2 = len;
5474 if (n1 >= len || n2 < 0 || n1 > n2)
5475 s = NULL;
5476 else
5477 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5478 }
5479 else
5480 {
5481 /* The resulting variable is a string of a single
5482 * character. If the index is too big or negative the
5483 * result is empty. */
5484 if (n1 >= len || n1 < 0)
5485 s = NULL;
5486 else
5487 s = vim_strnsave(s + n1, 1);
5488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 clear_tv(rettv);
5490 rettv->v_type = VAR_STRING;
5491 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 break;
5493
5494 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 if (n1 < 0)
5497 n1 = len + n1;
5498 if (!empty1 && (n1 < 0 || n1 >= len))
5499 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005500 /* For a range we allow invalid values and return an empty
5501 * list. A list index out of range is an error. */
5502 if (!range)
5503 {
5504 if (verbose)
5505 EMSGN(_(e_listidx), n1);
5506 return FAIL;
5507 }
5508 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 }
5510 if (range)
5511 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 list_T *l;
5513 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514
5515 if (n2 < 0)
5516 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005517 else if (n2 >= len)
5518 n2 = len - 1;
5519 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 l = list_alloc();
5522 if (l == NULL)
5523 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 n1 <= n2; ++n1)
5526 {
5527 if (list_append_tv(l, &item->li_tv) == FAIL)
5528 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005529 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 return FAIL;
5531 }
5532 item = item->li_next;
5533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 clear_tv(rettv);
5535 rettv->v_type = VAR_LIST;
5536 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005537 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 else
5540 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005541 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 clear_tv(rettv);
5543 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546
5547 case VAR_DICT:
5548 if (range)
5549 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005550 if (verbose)
5551 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 if (len == -1)
5553 clear_tv(&var1);
5554 return FAIL;
5555 }
5556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005557 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558
5559 if (len == -1)
5560 {
5561 key = get_tv_string(&var1);
5562 if (*key == NUL)
5563 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005564 if (verbose)
5565 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 clear_tv(&var1);
5567 return FAIL;
5568 }
5569 }
5570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005571 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005573 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005574 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 if (len == -1)
5576 clear_tv(&var1);
5577 if (item == NULL)
5578 return FAIL;
5579
5580 copy_tv(&item->di_tv, &var1);
5581 clear_tv(rettv);
5582 *rettv = var1;
5583 }
5584 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 }
5586 }
5587
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 return OK;
5589}
5590
5591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * Get an option value.
5593 * "arg" points to the '&' or '+' before the option name.
5594 * "arg" is advanced to character after the option name.
5595 * Return OK or FAIL.
5596 */
5597 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 int evaluate;
5602{
5603 char_u *option_end;
5604 long numval;
5605 char_u *stringval;
5606 int opt_type;
5607 int c;
5608 int working = (**arg == '+'); /* has("+option") */
5609 int ret = OK;
5610 int opt_flags;
5611
5612 /*
5613 * Isolate the option name and find its value.
5614 */
5615 option_end = find_option_end(arg, &opt_flags);
5616 if (option_end == NULL)
5617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 EMSG2(_("E112: Option name missing: %s"), *arg);
5620 return FAIL;
5621 }
5622
5623 if (!evaluate)
5624 {
5625 *arg = option_end;
5626 return OK;
5627 }
5628
5629 c = *option_end;
5630 *option_end = NUL;
5631 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
5634 if (opt_type == -3) /* invalid name */
5635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 EMSG2(_("E113: Unknown option: %s"), *arg);
5638 ret = FAIL;
5639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (opt_type == -2) /* hidden string option */
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 else if (opt_type == -1) /* hidden number option */
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv->v_type = VAR_NUMBER;
5650 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 else if (opt_type == 1) /* number option */
5653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 rettv->v_type = VAR_NUMBER;
5655 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 else /* string option */
5658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv->v_type = VAR_STRING;
5660 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
5662 }
5663 else if (working && (opt_type == -2 || opt_type == -1))
5664 ret = FAIL;
5665
5666 *option_end = c; /* put back for error messages */
5667 *arg = option_end;
5668
5669 return ret;
5670}
5671
5672/*
5673 * Allocate a variable for a string constant.
5674 * Return OK or FAIL.
5675 */
5676 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int evaluate;
5681{
5682 char_u *p;
5683 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 int extra = 0;
5685
5686 /*
5687 * Find the end of the string, skipping backslashed characters.
5688 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
5691 if (*p == '\\' && p[1] != NUL)
5692 {
5693 ++p;
5694 /* A "\<x>" form occupies at least 4 characters, and produces up
5695 * to 6 characters: reserve space for 2 extra */
5696 if (*p == '<')
5697 extra += 2;
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
5701 if (*p != '"')
5702 {
5703 EMSG2(_("E114: Missing quote: %s"), *arg);
5704 return FAIL;
5705 }
5706
5707 /* If only parsing, set *arg and return here */
5708 if (!evaluate)
5709 {
5710 *arg = p + 1;
5711 return OK;
5712 }
5713
5714 /*
5715 * Copy the string into allocated memory, handling backslashed
5716 * characters.
5717 */
5718 name = alloc((unsigned)(p - *arg + extra));
5719 if (name == NULL)
5720 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 rettv->v_type = VAR_STRING;
5722 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 if (*p == '\\')
5727 {
5728 switch (*++p)
5729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 case 'b': *name++ = BS; ++p; break;
5731 case 'e': *name++ = ESC; ++p; break;
5732 case 'f': *name++ = FF; ++p; break;
5733 case 'n': *name++ = NL; ++p; break;
5734 case 'r': *name++ = CAR; ++p; break;
5735 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 case 'X': /* hex: "\x1", "\x12" */
5738 case 'x':
5739 case 'u': /* Unicode: "\u0023" */
5740 case 'U':
5741 if (vim_isxdigit(p[1]))
5742 {
5743 int n, nr;
5744 int c = toupper(*p);
5745
5746 if (c == 'X')
5747 n = 2;
5748 else
5749 n = 4;
5750 nr = 0;
5751 while (--n >= 0 && vim_isxdigit(p[1]))
5752 {
5753 ++p;
5754 nr = (nr << 4) + hex2nr(*p);
5755 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757#ifdef FEAT_MBYTE
5758 /* For "\u" store the number according to
5759 * 'encoding'. */
5760 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 else
5763#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 break;
5767
5768 /* octal: "\1", "\12", "\123" */
5769 case '0':
5770 case '1':
5771 case '2':
5772 case '3':
5773 case '4':
5774 case '5':
5775 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 case '7': *name = *p++ - '0';
5777 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 *name = (*name << 3) + *p++ - '0';
5780 if (*p >= '0' && *p <= '7')
5781 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 break;
5785
5786 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (extra != 0)
5789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 break;
5792 }
5793 /* FALLTHROUGH */
5794
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797 }
5798 }
5799 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 *arg = p + 1;
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 return OK;
5807}
5808
5809/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 * Return OK or FAIL.
5812 */
5813 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005814get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 int evaluate;
5818{
5819 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005821 int reduce = 0;
5822
5823 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005825 */
5826 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5827 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 break;
5832 ++reduce;
5833 ++p;
5834 }
5835 }
5836
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 return FAIL;
5841 }
5842
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 if (!evaluate)
5845 {
5846 *arg = p + 1;
5847 return OK;
5848 }
5849
5850 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 */
5853 str = alloc((unsigned)((p - *arg) - reduce));
5854 if (str == NULL)
5855 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 rettv->v_type = VAR_STRING;
5857 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 break;
5865 ++p;
5866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 *arg = p + 1;
5871
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 return OK;
5873}
5874
5875/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 * Allocate a variable for a List and fill it from "*arg".
5877 * Return OK or FAIL.
5878 */
5879 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005880get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005882 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 int evaluate;
5884{
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l = NULL;
5886 typval_T tv;
5887 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888
5889 if (evaluate)
5890 {
5891 l = list_alloc();
5892 if (l == NULL)
5893 return FAIL;
5894 }
5895
5896 *arg = skipwhite(*arg + 1);
5897 while (**arg != ']' && **arg != NUL)
5898 {
5899 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5900 goto failret;
5901 if (evaluate)
5902 {
5903 item = listitem_alloc();
5904 if (item != NULL)
5905 {
5906 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005907 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 list_append(l, item);
5909 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005910 else
5911 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 }
5913
5914 if (**arg == ']')
5915 break;
5916 if (**arg != ',')
5917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 goto failret;
5920 }
5921 *arg = skipwhite(*arg + 1);
5922 }
5923
5924 if (**arg != ']')
5925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927failret:
5928 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005929 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 return FAIL;
5931 }
5932
5933 *arg = skipwhite(*arg + 1);
5934 if (evaluate)
5935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005936 rettv->v_type = VAR_LIST;
5937 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 ++l->lv_refcount;
5939 }
5940
5941 return OK;
5942}
5943
5944/*
5945 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005946 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005948 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949list_alloc()
5950{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 list_T *l;
5952
5953 l = (list_T *)alloc_clear(sizeof(list_T));
5954 if (l != NULL)
5955 {
5956 /* Prepend the list to the list of lists for garbage collection. */
5957 if (first_list != NULL)
5958 first_list->lv_used_prev = l;
5959 l->lv_used_prev = NULL;
5960 l->lv_used_next = first_list;
5961 first_list = l;
5962 }
5963 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964}
5965
5966/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005967 * Allocate an empty list for a return value.
5968 * Returns OK or FAIL.
5969 */
5970 static int
5971rettv_list_alloc(rettv)
5972 typval_T *rettv;
5973{
5974 list_T *l = list_alloc();
5975
5976 if (l == NULL)
5977 return FAIL;
5978
5979 rettv->vval.v_list = l;
5980 rettv->v_type = VAR_LIST;
5981 ++l->lv_refcount;
5982 return OK;
5983}
5984
5985/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 * Unreference a list: decrement the reference count and free it when it
5987 * becomes zero.
5988 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005989 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005993 if (l != NULL && --l->lv_refcount <= 0)
5994 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995}
5996
5997/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005998 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 * Ignores the reference count.
6000 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006001 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006002list_free(l, recurse)
6003 list_T *l;
6004 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006008 /* Remove the list from the list of lists for garbage collection. */
6009 if (l->lv_used_prev == NULL)
6010 first_list = l->lv_used_next;
6011 else
6012 l->lv_used_prev->lv_used_next = l->lv_used_next;
6013 if (l->lv_used_next != NULL)
6014 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6015
Bram Moolenaard9fba312005-06-26 22:34:35 +00006016 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006018 /* Remove the item before deleting it. */
6019 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006020 if (recurse || (item->li_tv.v_type != VAR_LIST
6021 && item->li_tv.v_type != VAR_DICT))
6022 clear_tv(&item->li_tv);
6023 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 }
6025 vim_free(l);
6026}
6027
6028/*
6029 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006030 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006032 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033listitem_alloc()
6034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036}
6037
6038/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006039 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006041 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006045 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 vim_free(item);
6047}
6048
6049/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006050 * Remove a list item from a List and free it. Also clears the value.
6051 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006052 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006053listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 list_T *l;
6055 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006056{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006057 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006058 listitem_free(item);
6059}
6060
6061/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 * Get the number of items in a list.
6063 */
6064 static long
6065list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 if (l == NULL)
6069 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006070 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071}
6072
6073/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006074 * Return TRUE when two lists have exactly the same values.
6075 */
6076 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 list_T *l1;
6079 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082{
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006084
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006085 if (l1 == NULL || l2 == NULL)
6086 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 if (l1 == l2)
6088 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006089 if (list_len(l1) != list_len(l2))
6090 return FALSE;
6091
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 for (item1 = l1->lv_first, item2 = l2->lv_first;
6093 item1 != NULL && item2 != NULL;
6094 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 return FALSE;
6097 return item1 == NULL && item2 == NULL;
6098}
6099
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006100#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6101 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006102/*
6103 * Return the dictitem that an entry in a hashtable points to.
6104 */
6105 dictitem_T *
6106dict_lookup(hi)
6107 hashitem_T *hi;
6108{
6109 return HI2DI(hi);
6110}
6111#endif
6112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006114 * Return TRUE when two dictionaries have exactly the same key/values.
6115 */
6116 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 dict_T *d1;
6119 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006120 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006121 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122{
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 hashitem_T *hi;
6124 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006127 if (d1 == NULL || d2 == NULL)
6128 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006129 if (d1 == d2)
6130 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 if (dict_len(d1) != dict_len(d2))
6132 return FALSE;
6133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006134 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 if (!HASHITEM_EMPTY(hi))
6138 {
6139 item2 = dict_find(d2, hi->hi_key, -1);
6140 if (item2 == NULL)
6141 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 return FALSE;
6144 --todo;
6145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 }
6147 return TRUE;
6148}
6149
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150static int tv_equal_recurse_limit;
6151
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006153 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006154 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006155 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156 */
6157 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006158tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 typval_T *tv1;
6160 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161 int ic; /* ignore case */
6162 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163{
6164 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006165 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006167 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006169 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006170 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006172 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173 * recursiveness to a limit. We guess they are equal then.
6174 * A fixed limit has the problem of still taking an awful long time.
6175 * Reduce the limit every time running into it. That should work fine for
6176 * deeply linked structures that are not recursively linked and catch
6177 * recursiveness quickly. */
6178 if (!recursive)
6179 tv_equal_recurse_limit = 1000;
6180 if (recursive_cnt >= tv_equal_recurse_limit)
6181 {
6182 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006185
6186 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006187 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 ++recursive_cnt;
6190 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6191 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006192 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193
6194 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 ++recursive_cnt;
6196 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6197 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006198 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199
6200 case VAR_FUNC:
6201 return (tv1->vval.v_string != NULL
6202 && tv2->vval.v_string != NULL
6203 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6204
6205 case VAR_NUMBER:
6206 return tv1->vval.v_number == tv2->vval.v_number;
6207
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006208#ifdef FEAT_FLOAT
6209 case VAR_FLOAT:
6210 return tv1->vval.v_float == tv2->vval.v_float;
6211#endif
6212
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213 case VAR_STRING:
6214 s1 = get_tv_string_buf(tv1, buf1);
6215 s2 = get_tv_string_buf(tv2, buf2);
6216 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006217 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218
6219 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220 return TRUE;
6221}
6222
6223/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 * Locate item with index "n" in list "l" and return it.
6225 * A negative index is counted from the end; -1 is the last item.
6226 * Returns NULL when "n" is out of range.
6227 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006228 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 long n;
6232{
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 long idx;
6235
6236 if (l == NULL)
6237 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006238
6239 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006240 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006241 n = l->lv_len + n;
6242
6243 /* Check for index out of range. */
6244 if (n < 0 || n >= l->lv_len)
6245 return NULL;
6246
6247 /* When there is a cached index may start search from there. */
6248 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006249 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006250 if (n < l->lv_idx / 2)
6251 {
6252 /* closest to the start of the list */
6253 item = l->lv_first;
6254 idx = 0;
6255 }
6256 else if (n > (l->lv_idx + l->lv_len) / 2)
6257 {
6258 /* closest to the end of the list */
6259 item = l->lv_last;
6260 idx = l->lv_len - 1;
6261 }
6262 else
6263 {
6264 /* closest to the cached index */
6265 item = l->lv_idx_item;
6266 idx = l->lv_idx;
6267 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268 }
6269 else
6270 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271 if (n < l->lv_len / 2)
6272 {
6273 /* closest to the start of the list */
6274 item = l->lv_first;
6275 idx = 0;
6276 }
6277 else
6278 {
6279 /* closest to the end of the list */
6280 item = l->lv_last;
6281 idx = l->lv_len - 1;
6282 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284
6285 while (n > idx)
6286 {
6287 /* search forward */
6288 item = item->li_next;
6289 ++idx;
6290 }
6291 while (n < idx)
6292 {
6293 /* search backward */
6294 item = item->li_prev;
6295 --idx;
6296 }
6297
6298 /* cache the used index */
6299 l->lv_idx = idx;
6300 l->lv_idx_item = item;
6301
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 return item;
6303}
6304
6305/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006306 * Get list item "l[idx]" as a number.
6307 */
6308 static long
6309list_find_nr(l, idx, errorp)
6310 list_T *l;
6311 long idx;
6312 int *errorp; /* set to TRUE when something wrong */
6313{
6314 listitem_T *li;
6315
6316 li = list_find(l, idx);
6317 if (li == NULL)
6318 {
6319 if (errorp != NULL)
6320 *errorp = TRUE;
6321 return -1L;
6322 }
6323 return get_tv_number_chk(&li->li_tv, errorp);
6324}
6325
6326/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006327 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6328 */
6329 char_u *
6330list_find_str(l, idx)
6331 list_T *l;
6332 long idx;
6333{
6334 listitem_T *li;
6335
6336 li = list_find(l, idx - 1);
6337 if (li == NULL)
6338 {
6339 EMSGN(_(e_listidx), idx);
6340 return NULL;
6341 }
6342 return get_tv_string(&li->li_tv);
6343}
6344
6345/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006346 * Locate "item" list "l" and return its index.
6347 * Returns -1 when "item" is not in the list.
6348 */
6349 static long
6350list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 list_T *l;
6352 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006353{
6354 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006356
6357 if (l == NULL)
6358 return -1;
6359 idx = 0;
6360 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6361 ++idx;
6362 if (li == NULL)
6363 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006364 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006365}
6366
6367/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 * Append item "item" to the end of list "l".
6369 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006370 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 list_T *l;
6373 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374{
6375 if (l->lv_last == NULL)
6376 {
6377 /* empty list */
6378 l->lv_first = item;
6379 l->lv_last = item;
6380 item->li_prev = NULL;
6381 }
6382 else
6383 {
6384 l->lv_last->li_next = item;
6385 item->li_prev = l->lv_last;
6386 l->lv_last = item;
6387 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006388 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389 item->li_next = NULL;
6390}
6391
6392/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006396 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *l;
6399 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006401 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402
Bram Moolenaar05159a02005-02-26 23:04:13 +00006403 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006405 copy_tv(tv, &li->li_tv);
6406 list_append(l, li);
6407 return OK;
6408}
6409
6410/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006411 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006412 * Return FAIL when out of memory.
6413 */
6414 int
6415list_append_dict(list, dict)
6416 list_T *list;
6417 dict_T *dict;
6418{
6419 listitem_T *li = listitem_alloc();
6420
6421 if (li == NULL)
6422 return FAIL;
6423 li->li_tv.v_type = VAR_DICT;
6424 li->li_tv.v_lock = 0;
6425 li->li_tv.vval.v_dict = dict;
6426 list_append(list, li);
6427 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428 return OK;
6429}
6430
6431/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006432 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006433 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006434 * Returns FAIL when out of memory.
6435 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006436 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006437list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006438 list_T *l;
6439 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006440 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006441{
6442 listitem_T *li = listitem_alloc();
6443
6444 if (li == NULL)
6445 return FAIL;
6446 list_append(l, li);
6447 li->li_tv.v_type = VAR_STRING;
6448 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006449 if (str == NULL)
6450 li->li_tv.vval.v_string = NULL;
6451 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006452 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006453 return FAIL;
6454 return OK;
6455}
6456
6457/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458 * Append "n" to list "l".
6459 * Returns FAIL when out of memory.
6460 */
6461 static int
6462list_append_number(l, n)
6463 list_T *l;
6464 varnumber_T n;
6465{
6466 listitem_T *li;
6467
6468 li = listitem_alloc();
6469 if (li == NULL)
6470 return FAIL;
6471 li->li_tv.v_type = VAR_NUMBER;
6472 li->li_tv.v_lock = 0;
6473 li->li_tv.vval.v_number = n;
6474 list_append(l, li);
6475 return OK;
6476}
6477
6478/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480 * If "item" is NULL append at the end.
6481 * Return FAIL when out of memory.
6482 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006483 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 list_T *l;
6486 typval_T *tv;
6487 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488{
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490
6491 if (ni == NULL)
6492 return FAIL;
6493 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006494 list_insert(l, ni, item);
6495 return OK;
6496}
6497
6498 void
6499list_insert(l, ni, item)
6500 list_T *l;
6501 listitem_T *ni;
6502 listitem_T *item;
6503{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 if (item == NULL)
6505 /* Append new item at end of list. */
6506 list_append(l, ni);
6507 else
6508 {
6509 /* Insert new item before existing item. */
6510 ni->li_prev = item->li_prev;
6511 ni->li_next = item;
6512 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006515 ++l->lv_idx;
6516 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 l->lv_idx_item = NULL;
6521 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525}
6526
6527/*
6528 * Extend "l1" with "l2".
6529 * If "bef" is NULL append at the end, otherwise insert before this item.
6530 * Returns FAIL when out of memory.
6531 */
6532 static int
6533list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 list_T *l1;
6535 list_T *l2;
6536 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 /* We also quit the loop when we have inserted the original item count of
6542 * the list, avoid a hang when we extend a list with itself. */
6543 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6545 return FAIL;
6546 return OK;
6547}
6548
6549/*
6550 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6551 * Return FAIL when out of memory.
6552 */
6553 static int
6554list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 list_T *l1;
6556 list_T *l2;
6557 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558{
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006561 if (l1 == NULL || l2 == NULL)
6562 return FAIL;
6563
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 if (l == NULL)
6567 return FAIL;
6568 tv->v_type = VAR_LIST;
6569 tv->vval.v_list = l;
6570
6571 /* append all items from the second list */
6572 return list_extend(l, l2, NULL);
6573}
6574
6575/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006576 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * Returns NULL when out of memory.
6580 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006582list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 list_T *copy;
6588 listitem_T *item;
6589 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
6591 if (orig == NULL)
6592 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593
6594 copy = list_alloc();
6595 if (copy != NULL)
6596 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 if (copyID != 0)
6598 {
6599 /* Do this before adding the items, because one of the items may
6600 * refer back to this list. */
6601 orig->lv_copyID = copyID;
6602 orig->lv_copylist = copy;
6603 }
6604 for (item = orig->lv_first; item != NULL && !got_int;
6605 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 {
6607 ni = listitem_alloc();
6608 if (ni == NULL)
6609 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006610 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 {
6612 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6613 {
6614 vim_free(ni);
6615 break;
6616 }
6617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 list_append(copy, ni);
6621 }
6622 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623 if (item != NULL)
6624 {
6625 list_unref(copy);
6626 copy = NULL;
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 }
6629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 return copy;
6631}
6632
6633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006635 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006636 * This used to be called list_remove, but that conflicts with a Sun header
6637 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006639 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006640vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 list_T *l;
6642 listitem_T *item;
6643 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644{
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 /* notify watchers */
6648 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006650 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 list_fix_watch(l, ip);
6652 if (ip == item2)
6653 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006655
6656 if (item2->li_next == NULL)
6657 l->lv_last = item->li_prev;
6658 else
6659 item2->li_next->li_prev = item->li_prev;
6660 if (item->li_prev == NULL)
6661 l->lv_first = item2->li_next;
6662 else
6663 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006664 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665}
6666
6667/*
6668 * Return an allocated string with the string representation of a list.
6669 * May return NULL.
6670 */
6671 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006672list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
6676 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677
6678 if (tv->vval.v_list == NULL)
6679 return NULL;
6680 ga_init2(&ga, (int)sizeof(char), 80);
6681 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 {
6684 vim_free(ga.ga_data);
6685 return NULL;
6686 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 ga_append(&ga, ']');
6688 ga_append(&ga, NUL);
6689 return (char_u *)ga.ga_data;
6690}
6691
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692typedef struct join_S {
6693 char_u *s;
6694 char_u *tofree;
6695} join_T;
6696
6697 static int
6698list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6699 garray_T *gap; /* to store the result in */
6700 list_T *l;
6701 char_u *sep;
6702 int echo_style;
6703 int copyID;
6704 garray_T *join_gap; /* to keep each list item string */
6705{
6706 int i;
6707 join_T *p;
6708 int len;
6709 int sumlen = 0;
6710 int first = TRUE;
6711 char_u *tofree;
6712 char_u numbuf[NUMBUFLEN];
6713 listitem_T *item;
6714 char_u *s;
6715
6716 /* Stringify each item in the list. */
6717 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6718 {
6719 if (echo_style)
6720 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6721 else
6722 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6723 if (s == NULL)
6724 return FAIL;
6725
6726 len = (int)STRLEN(s);
6727 sumlen += len;
6728
6729 ga_grow(join_gap, 1);
6730 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6731 if (tofree != NULL || s != numbuf)
6732 {
6733 p->s = s;
6734 p->tofree = tofree;
6735 }
6736 else
6737 {
6738 p->s = vim_strnsave(s, len);
6739 p->tofree = p->s;
6740 }
6741
6742 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006743 if (did_echo_string_emsg) /* recursion error, bail out */
6744 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006745 }
6746
6747 /* Allocate result buffer with its total size, avoid re-allocation and
6748 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6749 if (join_gap->ga_len >= 2)
6750 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6751 if (ga_grow(gap, sumlen + 2) == FAIL)
6752 return FAIL;
6753
6754 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6755 {
6756 if (first)
6757 first = FALSE;
6758 else
6759 ga_concat(gap, sep);
6760 p = ((join_T *)join_gap->ga_data) + i;
6761
6762 if (p->s != NULL)
6763 ga_concat(gap, p->s);
6764 line_breakcheck();
6765 }
6766
6767 return OK;
6768}
6769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006772 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006773 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006775 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006776list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006778 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006780 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006781 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 garray_T join_ga;
6784 int retval;
6785 join_T *p;
6786 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787
Bram Moolenaard39a7512015-04-16 22:51:22 +02006788 if (l->lv_len < 1)
6789 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6791 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6792
6793 /* Dispose each item in join_ga. */
6794 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796 p = (join_T *)join_ga.ga_data;
6797 for (i = 0; i < join_ga.ga_len; ++i)
6798 {
6799 vim_free(p->tofree);
6800 ++p;
6801 }
6802 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804
6805 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806}
6807
6808/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 * Garbage collection for lists and dictionaries.
6810 *
6811 * We use reference counts to be able to free most items right away when they
6812 * are no longer used. But for composite items it's possible that it becomes
6813 * unused while the reference count is > 0: When there is a recursive
6814 * reference. Example:
6815 * :let l = [1, 2, 3]
6816 * :let d = {9: l}
6817 * :let l[1] = d
6818 *
6819 * Since this is quite unusual we handle this with garbage collection: every
6820 * once in a while find out which lists and dicts are not referenced from any
6821 * variable.
6822 *
6823 * Here is a good reference text about garbage collection (refers to Python
6824 * but it applies to all reference-counting mechanisms):
6825 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827
6828/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 * Do garbage collection for lists and dicts.
6830 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 int
6833garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006836 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 buf_T *buf;
6838 win_T *wp;
6839 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006840 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006841 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006843#ifdef FEAT_WINDOWS
6844 tabpage_T *tp;
6845#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006847 /* Only do this once. */
6848 want_garbage_collect = FALSE;
6849 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006850 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006851
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 /* We advance by two because we add one for items referenced through
6853 * previous_funccal. */
6854 current_copyID += COPYID_INC;
6855 copyID = current_copyID;
6856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 /*
6858 * 1. Go through all accessible variables and mark all lists and dicts
6859 * with copyID.
6860 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861
6862 /* Don't free variables in the previous_funccal list unless they are only
6863 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006864 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6866 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006867 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6868 NULL);
6869 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6870 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 }
6872
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873 /* script-local variables */
6874 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006875 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876
6877 /* buffer-local variables */
6878 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6880 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
6882 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006883 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6885 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006886#ifdef FEAT_AUTOCMD
6887 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6889 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006890#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006892#ifdef FEAT_WINDOWS
6893 /* tabpage-local variables */
6894 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6896 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897#endif
6898
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901
6902 /* function-local variables */
6903 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6904 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6906 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 }
6908
Bram Moolenaard812df62008-11-09 12:46:09 +00006909 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006911
Bram Moolenaar1dced572012-04-05 16:54:08 +02006912#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006914#endif
6915
Bram Moolenaardb913952012-06-29 12:54:53 +02006916#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006918#endif
6919
6920#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006922#endif
6923
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 /*
6927 * 2. Free lists and dictionaries that are not referenced.
6928 */
6929 did_free = free_unref_items(copyID);
6930
6931 /*
6932 * 3. Check if any funccal can be freed now.
6933 */
6934 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006935 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 if (can_free_funccal(*pfc, copyID))
6937 {
6938 fc = *pfc;
6939 *pfc = fc->caller;
6940 free_funccal(fc, TRUE);
6941 did_free = TRUE;
6942 did_free_funccal = TRUE;
6943 }
6944 else
6945 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 if (did_free_funccal)
6948 /* When a funccal was freed some more items might be garbage
6949 * collected, so run again. */
6950 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 else if (p_verbose > 0)
6953 {
6954 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6955 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956
6957 return did_free;
6958}
6959
6960/*
6961 * Free lists and dictionaries that are no longer referenced.
6962 */
6963 static int
6964free_unref_items(copyID)
6965 int copyID;
6966{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006967 dict_T *dd, *dd_next;
6968 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 int did_free = FALSE;
6970
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 */
6974 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 {
6976 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006979 /* Free the Dictionary and ordinary items it contains, but don't
6980 * recurse into Lists and Dictionaries, they will be in the list
6981 * of dicts or list of lists. */
6982 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dd = dd_next;
6986 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987
6988 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 * Go through the list of lists and free items without the copyID.
6990 * But don't free a list that has a watcher (used in a for loop), these
6991 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
6993 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 {
6995 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6997 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006999 /* Free the List and ordinary items it contains, but don't recurse
7000 * into Lists and Dictionaries, they will be in the list of dicts
7001 * or list of lists. */
7002 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 ll = ll_next;
7006 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 return did_free;
7008}
7009
7010/*
7011 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 * "list_stack" is used to add lists to be marked. Can be NULL.
7013 *
7014 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 int
7017set_ref_in_ht(ht, copyID, list_stack)
7018 hashtab_T *ht;
7019 int copyID;
7020 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021{
7022 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 hashtab_T *cur_ht;
7026 ht_stack_T *ht_stack = NULL;
7027 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 cur_ht = ht;
7030 for (;;)
7031 {
7032 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 /* Mark each item in the hashtab. If the item contains a hashtab
7035 * it is added to ht_stack, if it contains a list it is added to
7036 * list_stack. */
7037 todo = (int)cur_ht->ht_used;
7038 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7039 if (!HASHITEM_EMPTY(hi))
7040 {
7041 --todo;
7042 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7043 &ht_stack, list_stack);
7044 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046
7047 if (ht_stack == NULL)
7048 break;
7049
7050 /* take an item from the stack */
7051 cur_ht = ht_stack->ht;
7052 tempitem = ht_stack;
7053 ht_stack = ht_stack->prev;
7054 free(tempitem);
7055 }
7056
7057 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058}
7059
7060/*
7061 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7063 *
7064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 int
7067set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 list_T *l;
7069 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 listitem_T *li;
7073 int abort = FALSE;
7074 list_T *cur_l;
7075 list_stack_T *list_stack = NULL;
7076 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 cur_l = l;
7079 for (;;)
7080 {
7081 if (!abort)
7082 /* Mark each item in the list. If the item contains a hashtab
7083 * it is added to ht_stack, if it contains a list it is added to
7084 * list_stack. */
7085 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7086 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7087 ht_stack, &list_stack);
7088 if (list_stack == NULL)
7089 break;
7090
7091 /* take an item from the stack */
7092 cur_l = list_stack->list;
7093 tempitem = list_stack;
7094 list_stack = list_stack->prev;
7095 free(tempitem);
7096 }
7097
7098 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099}
7100
7101/*
7102 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 * "list_stack" is used to add lists to be marked. Can be NULL.
7104 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7105 *
7106 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 int
7109set_ref_in_item(tv, copyID, ht_stack, list_stack)
7110 typval_T *tv;
7111 int copyID;
7112 ht_stack_T **ht_stack;
7113 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114{
7115 dict_T *dd;
7116 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007118
7119 switch (tv->v_type)
7120 {
7121 case VAR_DICT:
7122 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007123 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007124 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007125 /* Didn't see this dict yet. */
7126 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 if (ht_stack == NULL)
7128 {
7129 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7130 }
7131 else
7132 {
7133 ht_stack_T *newitem = (ht_stack_T*)malloc(
7134 sizeof(ht_stack_T));
7135 if (newitem == NULL)
7136 abort = TRUE;
7137 else
7138 {
7139 newitem->ht = &dd->dv_hashtab;
7140 newitem->prev = *ht_stack;
7141 *ht_stack = newitem;
7142 }
7143 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007145 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146
7147 case VAR_LIST:
7148 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007149 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007151 /* Didn't see this list yet. */
7152 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 if (list_stack == NULL)
7154 {
7155 abort = set_ref_in_list(ll, copyID, ht_stack);
7156 }
7157 else
7158 {
7159 list_stack_T *newitem = (list_stack_T*)malloc(
7160 sizeof(list_stack_T));
7161 if (newitem == NULL)
7162 abort = TRUE;
7163 else
7164 {
7165 newitem->list = ll;
7166 newitem->prev = *list_stack;
7167 *list_stack = newitem;
7168 }
7169 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007171 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174}
7175
7176/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 * Allocate an empty header for a dictionary.
7178 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007179 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180dict_alloc()
7181{
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007186 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007187 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 if (first_dict != NULL)
7189 first_dict->dv_used_prev = d;
7190 d->dv_used_next = first_dict;
7191 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007192 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007196 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201}
7202
7203/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007204 * Allocate an empty dict for a return value.
7205 * Returns OK or FAIL.
7206 */
7207 static int
7208rettv_dict_alloc(rettv)
7209 typval_T *rettv;
7210{
7211 dict_T *d = dict_alloc();
7212
7213 if (d == NULL)
7214 return FAIL;
7215
7216 rettv->vval.v_dict = d;
7217 rettv->v_type = VAR_DICT;
7218 ++d->dv_refcount;
7219 return OK;
7220}
7221
7222
7223/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224 * Unreference a Dictionary: decrement the reference count and free it when it
7225 * becomes zero.
7226 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007227 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007231 if (d != NULL && --d->dv_refcount <= 0)
7232 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233}
7234
7235/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007236 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 * Ignores the reference count.
7238 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007239 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007240dict_free(d, recurse)
7241 dict_T *d;
7242 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007244 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007246 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007248 /* Remove the dict from the list of dicts for garbage collection. */
7249 if (d->dv_used_prev == NULL)
7250 first_dict = d->dv_used_next;
7251 else
7252 d->dv_used_prev->dv_used_next = d->dv_used_next;
7253 if (d->dv_used_next != NULL)
7254 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7255
7256 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007257 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007258 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007261 if (!HASHITEM_EMPTY(hi))
7262 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007263 /* Remove the item before deleting it, just in case there is
7264 * something recursive causing trouble. */
7265 di = HI2DI(hi);
7266 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007267 if (recurse || (di->di_tv.v_type != VAR_LIST
7268 && di->di_tv.v_type != VAR_DICT))
7269 clear_tv(&di->di_tv);
7270 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 --todo;
7272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 vim_free(d);
7276}
7277
7278/*
7279 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 * The "key" is copied to the new item.
7281 * Note that the value of the item "di_tv" still needs to be initialized!
7282 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007284 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285dictitem_alloc(key)
7286 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287{
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007290 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007294 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007295 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297}
7298
7299/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 * Make a copy of a Dictionary item.
7301 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305{
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007308 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7309 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 if (di != NULL)
7311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007313 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 copy_tv(&org->di_tv, &di->di_tv);
7315 }
7316 return di;
7317}
7318
7319/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 * Remove item "item" from Dictionary "dict" and free it.
7321 */
7322 static void
7323dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dict_T *dict;
7325 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326{
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 if (HASHITEM_EMPTY(hi))
7331 EMSG2(_(e_intern2), "dictitem_remove()");
7332 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 dictitem_free(item);
7335}
7336
7337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 * Free a dict item. Also clears the value.
7339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007340 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007345 if (item->di_flags & DI_FLAGS_ALLOC)
7346 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347}
7348
7349/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7351 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007352 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 * Returns NULL when out of memory.
7354 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007359 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360{
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 dict_T *copy;
7362 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365
7366 if (orig == NULL)
7367 return NULL;
7368
7369 copy = dict_alloc();
7370 if (copy != NULL)
7371 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 if (copyID != 0)
7373 {
7374 orig->dv_copyID = copyID;
7375 orig->dv_copydict = copy;
7376 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007377 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 --todo;
7383
7384 di = dictitem_alloc(hi->hi_key);
7385 if (di == NULL)
7386 break;
7387 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 {
7389 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7390 copyID) == FAIL)
7391 {
7392 vim_free(di);
7393 break;
7394 }
7395 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 else
7397 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7398 if (dict_add(copy, di) == FAIL)
7399 {
7400 dictitem_free(di);
7401 break;
7402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 if (todo > 0)
7408 {
7409 dict_unref(copy);
7410 copy = NULL;
7411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 }
7413
7414 return copy;
7415}
7416
7417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007419 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007421 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 dict_T *d;
7424 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425{
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427}
7428
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007430 * Add a number or string entry to dictionary "d".
7431 * When "str" is NULL use number "nr", otherwise use "str".
7432 * Returns FAIL when out of memory and when key already exists.
7433 */
7434 int
7435dict_add_nr_str(d, key, nr, str)
7436 dict_T *d;
7437 char *key;
7438 long nr;
7439 char_u *str;
7440{
7441 dictitem_T *item;
7442
7443 item = dictitem_alloc((char_u *)key);
7444 if (item == NULL)
7445 return FAIL;
7446 item->di_tv.v_lock = 0;
7447 if (str == NULL)
7448 {
7449 item->di_tv.v_type = VAR_NUMBER;
7450 item->di_tv.vval.v_number = nr;
7451 }
7452 else
7453 {
7454 item->di_tv.v_type = VAR_STRING;
7455 item->di_tv.vval.v_string = vim_strsave(str);
7456 }
7457 if (dict_add(d, item) == FAIL)
7458 {
7459 dictitem_free(item);
7460 return FAIL;
7461 }
7462 return OK;
7463}
7464
7465/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007466 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007467 * Returns FAIL when out of memory and when key already exists.
7468 */
7469 int
7470dict_add_list(d, key, list)
7471 dict_T *d;
7472 char *key;
7473 list_T *list;
7474{
7475 dictitem_T *item;
7476
7477 item = dictitem_alloc((char_u *)key);
7478 if (item == NULL)
7479 return FAIL;
7480 item->di_tv.v_lock = 0;
7481 item->di_tv.v_type = VAR_LIST;
7482 item->di_tv.vval.v_list = list;
7483 if (dict_add(d, item) == FAIL)
7484 {
7485 dictitem_free(item);
7486 return FAIL;
7487 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007488 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007489 return OK;
7490}
7491
7492/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 * Get the number of items in a Dictionary.
7494 */
7495 static long
7496dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007498{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 if (d == NULL)
7500 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007501 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502}
7503
7504/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 * Find item "key[len]" in Dictionary "d".
7506 * If "len" is negative use strlen(key).
7507 * Returns NULL when not found.
7508 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007509 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 char_u *key;
7513 int len;
7514{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515#define AKEYLEN 200
7516 char_u buf[AKEYLEN];
7517 char_u *akey;
7518 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (len < 0)
7522 akey = key;
7523 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007524 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 tofree = akey = vim_strnsave(key, len);
7526 if (akey == NULL)
7527 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007528 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 else
7530 {
7531 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007532 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 akey = buf;
7534 }
7535
Bram Moolenaar33570922005-01-25 22:26:29 +00007536 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 vim_free(tofree);
7538 if (HASHITEM_EMPTY(hi))
7539 return NULL;
7540 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541}
7542
7543/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007544 * Get a string item from a dictionary.
7545 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546 * Returns NULL if the entry doesn't exist or out of memory.
7547 */
7548 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007549get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007550 dict_T *d;
7551 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007552 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007553{
7554 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007555 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007556
7557 di = dict_find(d, key, -1);
7558 if (di == NULL)
7559 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007560 s = get_tv_string(&di->di_tv);
7561 if (save && s != NULL)
7562 s = vim_strsave(s);
7563 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564}
7565
7566/*
7567 * Get a number item from a dictionary.
7568 * Returns 0 if the entry doesn't exist or out of memory.
7569 */
7570 long
7571get_dict_number(d, key)
7572 dict_T *d;
7573 char_u *key;
7574{
7575 dictitem_T *di;
7576
7577 di = dict_find(d, key, -1);
7578 if (di == NULL)
7579 return 0;
7580 return get_tv_number(&di->di_tv);
7581}
7582
7583/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 * Return an allocated string with the string representation of a Dictionary.
7585 * May return NULL.
7586 */
7587 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007588dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591{
7592 garray_T ga;
7593 int first = TRUE;
7594 char_u *tofree;
7595 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 return NULL;
7603 ga_init2(&ga, (int)sizeof(char), 80);
7604 ga_append(&ga, '{');
7605
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007606 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007607 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007609 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007611 --todo;
7612
7613 if (first)
7614 first = FALSE;
7615 else
7616 ga_concat(&ga, (char_u *)", ");
7617
7618 tofree = string_quote(hi->hi_key, FALSE);
7619 if (tofree != NULL)
7620 {
7621 ga_concat(&ga, tofree);
7622 vim_free(tofree);
7623 }
7624 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007625 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 if (s != NULL)
7627 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007629 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007630 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007631 line_breakcheck();
7632
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 if (todo > 0)
7636 {
7637 vim_free(ga.ga_data);
7638 return NULL;
7639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640
7641 ga_append(&ga, '}');
7642 ga_append(&ga, NUL);
7643 return (char_u *)ga.ga_data;
7644}
7645
7646/*
7647 * Allocate a variable for a Dictionary and fill it from "*arg".
7648 * Return OK or FAIL. Returns NOTDONE for {expr}.
7649 */
7650 static int
7651get_dict_tv(arg, rettv, evaluate)
7652 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 int evaluate;
7655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dict_T *d = NULL;
7657 typval_T tvkey;
7658 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007659 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 /*
7665 * First check if it's not a curly-braces thing: {expr}.
7666 * Must do this without evaluating, otherwise a function may be called
7667 * twice. Unfortunately this means we need to call eval1() twice for the
7668 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 if (*start != '}')
7672 {
7673 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7674 return FAIL;
7675 if (*start == '}')
7676 return NOTDONE;
7677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678
7679 if (evaluate)
7680 {
7681 d = dict_alloc();
7682 if (d == NULL)
7683 return FAIL;
7684 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007685 tvkey.v_type = VAR_UNKNOWN;
7686 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687
7688 *arg = skipwhite(*arg + 1);
7689 while (**arg != '}' && **arg != NUL)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 goto failret;
7693 if (**arg != ':')
7694 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007695 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 goto failret;
7698 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007699 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007701 key = get_tv_string_buf_chk(&tvkey, buf);
7702 if (key == NULL || *key == NUL)
7703 {
7704 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7705 if (key != NULL)
7706 EMSG(_(e_emptykey));
7707 clear_tv(&tvkey);
7708 goto failret;
7709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711
7712 *arg = skipwhite(*arg + 1);
7713 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7714 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007715 if (evaluate)
7716 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 goto failret;
7718 }
7719 if (evaluate)
7720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 if (item != NULL)
7723 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007724 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 clear_tv(&tv);
7727 goto failret;
7728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 item = dictitem_alloc(key);
7730 clear_tv(&tvkey);
7731 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007734 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007735 if (dict_add(d, item) == FAIL)
7736 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 }
7738 }
7739
7740 if (**arg == '}')
7741 break;
7742 if (**arg != ',')
7743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007744 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 goto failret;
7746 }
7747 *arg = skipwhite(*arg + 1);
7748 }
7749
7750 if (**arg != '}')
7751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007752 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753failret:
7754 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007755 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 return FAIL;
7757 }
7758
7759 *arg = skipwhite(*arg + 1);
7760 if (evaluate)
7761 {
7762 rettv->v_type = VAR_DICT;
7763 rettv->vval.v_dict = d;
7764 ++d->dv_refcount;
7765 }
7766
7767 return OK;
7768}
7769
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007771 * Return a string with the string representation of a variable.
7772 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007773 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007774 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007775 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007776 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777 */
7778 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007779echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007782 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007783 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007784{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 static int recurse = 0;
7786 char_u *r = NULL;
7787
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007790 if (!did_echo_string_emsg)
7791 {
7792 /* Only give this message once for a recursive call to avoid
7793 * flooding the user with errors. And stop iterating over lists
7794 * and dicts. */
7795 did_echo_string_emsg = TRUE;
7796 EMSG(_("E724: variable nested too deep for displaying"));
7797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007799 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 }
7801 ++recurse;
7802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 switch (tv->v_type)
7804 {
7805 case VAR_FUNC:
7806 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 r = tv->vval.v_string;
7808 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007809
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007811 if (tv->vval.v_list == NULL)
7812 {
7813 *tofree = NULL;
7814 r = NULL;
7815 }
7816 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7817 {
7818 *tofree = NULL;
7819 r = (char_u *)"[...]";
7820 }
7821 else
7822 {
7823 tv->vval.v_list->lv_copyID = copyID;
7824 *tofree = list2string(tv, copyID);
7825 r = *tofree;
7826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007828
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007830 if (tv->vval.v_dict == NULL)
7831 {
7832 *tofree = NULL;
7833 r = NULL;
7834 }
7835 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7836 {
7837 *tofree = NULL;
7838 r = (char_u *)"{...}";
7839 }
7840 else
7841 {
7842 tv->vval.v_dict->dv_copyID = copyID;
7843 *tofree = dict2string(tv, copyID);
7844 r = *tofree;
7845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007846 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 case VAR_STRING:
7849 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 *tofree = NULL;
7851 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007853
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854#ifdef FEAT_FLOAT
7855 case VAR_FLOAT:
7856 *tofree = NULL;
7857 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7858 r = numbuf;
7859 break;
7860#endif
7861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007863 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866
Bram Moolenaar8502c702014-06-17 12:51:16 +02007867 if (--recurse == 0)
7868 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007870}
7871
7872/*
7873 * Return a string with the string representation of a variable.
7874 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7875 * "numbuf" is used for a number.
7876 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007877 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 */
7879 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007880tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 char_u **tofree;
7883 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885{
7886 switch (tv->v_type)
7887 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 case VAR_FUNC:
7889 *tofree = string_quote(tv->vval.v_string, TRUE);
7890 return *tofree;
7891 case VAR_STRING:
7892 *tofree = string_quote(tv->vval.v_string, FALSE);
7893 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894#ifdef FEAT_FLOAT
7895 case VAR_FLOAT:
7896 *tofree = NULL;
7897 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7898 return numbuf;
7899#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007902 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007903 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007905 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007907 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908}
7909
7910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 * Return string "str" in ' quotes, doubling ' characters.
7912 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 */
7915 static char_u *
7916string_quote(str, function)
7917 char_u *str;
7918 int function;
7919{
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007921 char_u *p, *r, *s;
7922
Bram Moolenaar33570922005-01-25 22:26:29 +00007923 len = (function ? 13 : 3);
7924 if (str != NULL)
7925 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007926 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007927 for (p = str; *p != NUL; mb_ptr_adv(p))
7928 if (*p == '\'')
7929 ++len;
7930 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 s = r = alloc(len);
7932 if (r != NULL)
7933 {
7934 if (function)
7935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007936 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937 r += 10;
7938 }
7939 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 if (str != NULL)
7942 for (p = str; *p != NUL; )
7943 {
7944 if (*p == '\'')
7945 *r++ = '\'';
7946 MB_COPY_CHAR(p, r);
7947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 if (function)
7950 *r++ = ')';
7951 *r++ = NUL;
7952 }
7953 return s;
7954}
7955
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007956#ifdef FEAT_FLOAT
7957/*
7958 * Convert the string "text" to a floating point number.
7959 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7960 * this always uses a decimal point.
7961 * Returns the length of the text that was consumed.
7962 */
7963 static int
7964string2float(text, value)
7965 char_u *text;
7966 float_T *value; /* result stored here */
7967{
7968 char *s = (char *)text;
7969 float_T f;
7970
7971 f = strtod(s, &s);
7972 *value = f;
7973 return (int)((char_u *)s - text);
7974}
7975#endif
7976
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 * Get the value of an environment variable.
7979 * "arg" is pointing to the '$'. It is advanced to after the name.
7980 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007981 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 */
7983 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 int evaluate;
7988{
7989 char_u *string = NULL;
7990 int len;
7991 int cc;
7992 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007993 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995 ++*arg;
7996 name = *arg;
7997 len = get_env_len(arg);
7998 if (evaluate)
7999 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008000 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008001 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008002
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008003 cc = name[len];
8004 name[len] = NUL;
8005 /* first try vim_getenv(), fast for normal environment vars */
8006 string = vim_getenv(name, &mustfree);
8007 if (string != NULL && *string != NUL)
8008 {
8009 if (!mustfree)
8010 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008012 else
8013 {
8014 if (mustfree)
8015 vim_free(string);
8016
8017 /* next try expanding things like $VIM and ${HOME} */
8018 string = expand_env_save(name - 1);
8019 if (string != NULL && *string == '$')
8020 {
8021 vim_free(string);
8022 string = NULL;
8023 }
8024 }
8025 name[len] = cc;
8026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->v_type = VAR_STRING;
8028 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030
8031 return OK;
8032}
8033
8034/*
8035 * Array with names and number of arguments of all internal functions
8036 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8037 */
8038static struct fst
8039{
8040 char *f_name; /* function name */
8041 char f_min_argc; /* minimal number of arguments */
8042 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008044 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045} functions[] =
8046{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008047#ifdef FEAT_FLOAT
8048 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008049 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008051 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008052 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {"append", 2, 2, f_append},
8054 {"argc", 0, 0, f_argc},
8055 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008056 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008057 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008059 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008061 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008064 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"bufexists", 1, 1, f_bufexists},
8066 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8067 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8068 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8069 {"buflisted", 1, 1, f_buflisted},
8070 {"bufloaded", 1, 1, f_bufloaded},
8071 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008072 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"bufwinnr", 1, 1, f_bufwinnr},
8074 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008075 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008076 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008077 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"ceil", 1, 1, f_ceil},
8080#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008081 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008082 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008084 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008086#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008087 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008088 {"complete_add", 1, 1, f_complete_add},
8089 {"complete_check", 0, 0, f_complete_check},
8090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008093#ifdef FEAT_FLOAT
8094 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008095 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008097 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008099 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008100 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"delete", 1, 1, f_delete},
8102 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008103 {"diff_filler", 1, 1, f_diff_filler},
8104 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008105 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"eventhandler", 0, 0, f_eventhandler},
8109 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008110 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008112#ifdef FEAT_FLOAT
8113 {"exp", 1, 1, f_exp},
8114#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008115 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008116 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008117 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8119 {"filereadable", 1, 1, f_filereadable},
8120 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008121 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008122 {"finddir", 1, 3, f_finddir},
8123 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008124#ifdef FEAT_FLOAT
8125 {"float2nr", 1, 1, f_float2nr},
8126 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008127 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008128#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008129 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"fnamemodify", 2, 2, f_fnamemodify},
8131 {"foldclosed", 1, 1, f_foldclosed},
8132 {"foldclosedend", 1, 1, f_foldclosedend},
8133 {"foldlevel", 1, 1, f_foldlevel},
8134 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008135 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008138 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008139 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008140 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008141 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"getchar", 0, 1, f_getchar},
8143 {"getcharmod", 0, 0, f_getcharmod},
8144 {"getcmdline", 0, 0, f_getcmdline},
8145 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008146 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008147 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008148 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008150 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008151 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"getfsize", 1, 1, f_getfsize},
8153 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008154 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008155 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008156 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008157 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008158 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008159 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008160 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008161 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008163 {"gettabvar", 2, 3, f_gettabvar},
8164 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"getwinposx", 0, 0, f_getwinposx},
8166 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008167 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008168 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008169 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008170 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008172 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008173 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008174 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8176 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8177 {"histadd", 2, 2, f_histadd},
8178 {"histdel", 1, 2, f_histdel},
8179 {"histget", 1, 2, f_histget},
8180 {"histnr", 1, 1, f_histnr},
8181 {"hlID", 1, 1, f_hlID},
8182 {"hlexists", 1, 1, f_hlexists},
8183 {"hostname", 0, 0, f_hostname},
8184 {"iconv", 3, 3, f_iconv},
8185 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008186 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008187 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008189 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"inputrestore", 0, 0, f_inputrestore},
8191 {"inputsave", 0, 0, f_inputsave},
8192 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008193 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008194 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008196 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008197 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008199 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008201 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"libcall", 3, 3, f_libcall},
8203 {"libcallnr", 3, 3, f_libcallnr},
8204 {"line", 1, 1, f_line},
8205 {"line2byte", 1, 1, f_line2byte},
8206 {"lispindent", 1, 1, f_lispindent},
8207 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008208#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008209 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008210 {"log10", 1, 1, f_log10},
8211#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008212#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008213 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008214#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008215 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008216 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008217 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008218 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008219 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008220 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008221 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008222 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008223 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008224 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008225 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008226 {"max", 1, 1, f_max},
8227 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008228#ifdef vim_mkdir
8229 {"mkdir", 1, 3, f_mkdir},
8230#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008231 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008232#ifdef FEAT_MZSCHEME
8233 {"mzeval", 1, 1, f_mzeval},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008236 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008237 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008238 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239#ifdef FEAT_FLOAT
8240 {"pow", 2, 2, f_pow},
8241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008243 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008244 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008245#ifdef FEAT_PYTHON3
8246 {"py3eval", 1, 1, f_py3eval},
8247#endif
8248#ifdef FEAT_PYTHON
8249 {"pyeval", 1, 1, f_pyeval},
8250#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008251 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008252 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008253 {"reltime", 0, 2, f_reltime},
8254 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"remote_expr", 2, 3, f_remote_expr},
8256 {"remote_foreground", 1, 1, f_remote_foreground},
8257 {"remote_peek", 1, 2, f_remote_peek},
8258 {"remote_read", 1, 1, f_remote_read},
8259 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008260 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008262 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008264 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265#ifdef FEAT_FLOAT
8266 {"round", 1, 1, f_round},
8267#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008268 {"screenattr", 2, 2, f_screenattr},
8269 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008270 {"screencol", 0, 0, f_screencol},
8271 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008272 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008273 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008274 {"searchpair", 3, 7, f_searchpair},
8275 {"searchpairpos", 3, 7, f_searchpairpos},
8276 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"server2client", 2, 2, f_server2client},
8278 {"serverlist", 0, 0, f_serverlist},
8279 {"setbufvar", 3, 3, f_setbufvar},
8280 {"setcmdpos", 1, 1, f_setcmdpos},
8281 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008282 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008283 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008284 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008285 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008287 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008288 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008290#ifdef FEAT_CRYPT
8291 {"sha256", 1, 1, f_sha256},
8292#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008293 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008294 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008296#ifdef FEAT_FLOAT
8297 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008298 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008299#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008300 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008301 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008302 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008303 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008304 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008305#ifdef FEAT_FLOAT
8306 {"sqrt", 1, 1, f_sqrt},
8307 {"str2float", 1, 1, f_str2float},
8308#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008309 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008310 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008311 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312#ifdef HAVE_STRFTIME
8313 {"strftime", 1, 2, f_strftime},
8314#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"strlen", 1, 1, f_strlen},
8318 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008319 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008321 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008322 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"substitute", 4, 4, f_substitute},
8324 {"synID", 3, 3, f_synID},
8325 {"synIDattr", 2, 3, f_synIDattr},
8326 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008327 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008328 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008329 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008330 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008331 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008332 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008333 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008334 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008335 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008336#ifdef FEAT_FLOAT
8337 {"tan", 1, 1, f_tan},
8338 {"tanh", 1, 1, f_tanh},
8339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008341 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"tolower", 1, 1, f_tolower},
8343 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008344 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008345#ifdef FEAT_FLOAT
8346 {"trunc", 1, 1, f_trunc},
8347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008349 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008350 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008351 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008352 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"virtcol", 1, 1, f_virtcol},
8354 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008355 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"winbufnr", 1, 1, f_winbufnr},
8357 {"wincol", 0, 0, f_wincol},
8358 {"winheight", 1, 1, f_winheight},
8359 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008360 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008362 {"winrestview", 1, 1, f_winrestview},
8363 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008365 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008366 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367};
8368
8369#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8370
8371/*
8372 * Function given to ExpandGeneric() to obtain the list of internal
8373 * or user defined function names.
8374 */
8375 char_u *
8376get_function_name(xp, idx)
8377 expand_T *xp;
8378 int idx;
8379{
8380 static int intidx = -1;
8381 char_u *name;
8382
8383 if (idx == 0)
8384 intidx = -1;
8385 if (intidx < 0)
8386 {
8387 name = get_user_func_name(xp, idx);
8388 if (name != NULL)
8389 return name;
8390 }
8391 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8392 {
8393 STRCPY(IObuff, functions[intidx].f_name);
8394 STRCAT(IObuff, "(");
8395 if (functions[intidx].f_max_argc == 0)
8396 STRCAT(IObuff, ")");
8397 return IObuff;
8398 }
8399
8400 return NULL;
8401}
8402
8403/*
8404 * Function given to ExpandGeneric() to obtain the list of internal or
8405 * user defined variable or function names.
8406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 char_u *
8408get_expr_name(xp, idx)
8409 expand_T *xp;
8410 int idx;
8411{
8412 static int intidx = -1;
8413 char_u *name;
8414
8415 if (idx == 0)
8416 intidx = -1;
8417 if (intidx < 0)
8418 {
8419 name = get_function_name(xp, idx);
8420 if (name != NULL)
8421 return name;
8422 }
8423 return get_user_var_name(xp, ++intidx);
8424}
8425
8426#endif /* FEAT_CMDL_COMPL */
8427
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008428#if defined(EBCDIC) || defined(PROTO)
8429/*
8430 * Compare struct fst by function name.
8431 */
8432 static int
8433compare_func_name(s1, s2)
8434 const void *s1;
8435 const void *s2;
8436{
8437 struct fst *p1 = (struct fst *)s1;
8438 struct fst *p2 = (struct fst *)s2;
8439
8440 return STRCMP(p1->f_name, p2->f_name);
8441}
8442
8443/*
8444 * Sort the function table by function name.
8445 * The sorting of the table above is ASCII dependant.
8446 * On machines using EBCDIC we have to sort it.
8447 */
8448 static void
8449sortFunctions()
8450{
8451 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8452
8453 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8454}
8455#endif
8456
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*
8459 * Find internal function in table above.
8460 * Return index, or -1 if not found
8461 */
8462 static int
8463find_internal_func(name)
8464 char_u *name; /* name of the function */
8465{
8466 int first = 0;
8467 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8468 int cmp;
8469 int x;
8470
8471 /*
8472 * Find the function name in the table. Binary search.
8473 */
8474 while (first <= last)
8475 {
8476 x = first + ((unsigned)(last - first) >> 1);
8477 cmp = STRCMP(name, functions[x].f_name);
8478 if (cmp < 0)
8479 last = x - 1;
8480 else if (cmp > 0)
8481 first = x + 1;
8482 else
8483 return x;
8484 }
8485 return -1;
8486}
8487
8488/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008489 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8490 * name it contains, otherwise return "name".
8491 */
8492 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008493deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008494 char_u *name;
8495 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008496 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497{
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 int cc;
8500
8501 cc = name[*lenp];
8502 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008503 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 {
8509 *lenp = 0;
8510 return (char_u *)""; /* just in case */
8511 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008512 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 }
8515
8516 return name;
8517}
8518
8519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 * Allocate a variable for the result of a function.
8521 * Return OK or FAIL.
8522 */
8523 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008524get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8525 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 char_u *name; /* name of the function */
8527 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 char_u **arg; /* argument, pointing to the '(' */
8530 linenr_T firstline; /* first line of range */
8531 linenr_T lastline; /* last line of range */
8532 int *doesrange; /* return: function handled range */
8533 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 char_u *argp;
8537 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008538 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 int argcount = 0; /* number of arguments found */
8540
8541 /*
8542 * Get the arguments.
8543 */
8544 argp = *arg;
8545 while (argcount < MAX_FUNC_ARGS)
8546 {
8547 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8548 if (*argp == ')' || *argp == ',' || *argp == NUL)
8549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8551 {
8552 ret = FAIL;
8553 break;
8554 }
8555 ++argcount;
8556 if (*argp != ',')
8557 break;
8558 }
8559 if (*argp == ')')
8560 ++argp;
8561 else
8562 ret = FAIL;
8563
8564 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008566 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 {
8569 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008570 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008572 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577
8578 *arg = skipwhite(argp);
8579 return ret;
8580}
8581
8582
8583/*
8584 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008585 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008586 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 */
8588 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008589call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008591 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008595 typval_T *argvars; /* vars for arguments, must have "argcount"
8596 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 linenr_T firstline; /* first line of range */
8598 linenr_T lastline; /* last line of range */
8599 int *doesrange; /* return: function handled range */
8600 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604#define ERROR_UNKNOWN 0
8605#define ERROR_TOOMANY 1
8606#define ERROR_TOOFEW 2
8607#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608#define ERROR_DICT 4
8609#define ERROR_NONE 5
8610#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 int error = ERROR_NONE;
8612 int i;
8613 int llen;
8614 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615#define FLEN_FIXED 40
8616 char_u fname_buf[FLEN_FIXED + 1];
8617 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008618 char_u *name;
8619
8620 /* Make a copy of the name, if it comes from a funcref variable it could
8621 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008622 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008623 if (name == NULL)
8624 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626 /*
8627 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8628 * Change <SNR>123_name() to K_SNR 123_name().
8629 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 llen = eval_fname_script(name);
8632 if (llen > 0)
8633 {
8634 fname_buf[0] = K_SPECIAL;
8635 fname_buf[1] = KS_EXTRA;
8636 fname_buf[2] = (int)KE_SNR;
8637 i = 3;
8638 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8639 {
8640 if (current_SID <= 0)
8641 error = ERROR_SCRIPT;
8642 else
8643 {
8644 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8645 i = (int)STRLEN(fname_buf);
8646 }
8647 }
8648 if (i + STRLEN(name + llen) < FLEN_FIXED)
8649 {
8650 STRCPY(fname_buf + i, name + llen);
8651 fname = fname_buf;
8652 }
8653 else
8654 {
8655 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8656 if (fname == NULL)
8657 error = ERROR_OTHER;
8658 else
8659 {
8660 mch_memmove(fname, fname_buf, (size_t)i);
8661 STRCPY(fname + i, name + llen);
8662 }
8663 }
8664 }
8665 else
8666 fname = name;
8667
8668 *doesrange = FALSE;
8669
8670
8671 /* execute the function if no errors detected and executing */
8672 if (evaluate && error == ERROR_NONE)
8673 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008674 char_u *rfname = fname;
8675
8676 /* Ignore "g:" before a function name. */
8677 if (fname[0] == 'g' && fname[1] == ':')
8678 rfname = fname + 2;
8679
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008680 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8681 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 error = ERROR_UNKNOWN;
8683
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008684 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
8686 /*
8687 * User defined function.
8688 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008689 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008692 /* Trigger FuncUndefined event, may load the function. */
8693 if (fp == NULL
8694 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008695 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008696 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008698 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 }
8701#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008703 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 {
8705 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008706 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008707 }
8708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 if (fp != NULL)
8710 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008711 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008713 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008715 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008717 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008718 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 else
8720 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008721 int did_save_redo = FALSE;
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 /*
8724 * Call the user function.
8725 * Save and restore search patterns, script variables and
8726 * redo buffer.
8727 */
8728 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008729 if (!ins_compl_active())
8730 {
8731 saveRedobuff();
8732 did_save_redo = TRUE;
8733 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008734 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008736 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008737 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8738 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8739 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008740 /* Function was unreferenced while being used, free it
8741 * now. */
8742 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008743 if (did_save_redo)
8744 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 restore_search_patterns();
8746 error = ERROR_NONE;
8747 }
8748 }
8749 }
8750 else
8751 {
8752 /*
8753 * Find the function name in the table, call its implementation.
8754 */
8755 i = find_internal_func(fname);
8756 if (i >= 0)
8757 {
8758 if (argcount < functions[i].f_min_argc)
8759 error = ERROR_TOOFEW;
8760 else if (argcount > functions[i].f_max_argc)
8761 error = ERROR_TOOMANY;
8762 else
8763 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008764 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 error = ERROR_NONE;
8767 }
8768 }
8769 }
8770 /*
8771 * The function call (or "FuncUndefined" autocommand sequence) might
8772 * have been aborted by an error, an interrupt, or an explicitly thrown
8773 * exception that has not been caught so far. This situation can be
8774 * tested for by calling aborting(). For an error in an internal
8775 * function or for the "E132" error in call_user_func(), however, the
8776 * throw point at which the "force_abort" flag (temporarily reset by
8777 * emsg()) is normally updated has not been reached yet. We need to
8778 * update that flag first to make aborting() reliable.
8779 */
8780 update_force_abort();
8781 }
8782 if (error == ERROR_NONE)
8783 ret = OK;
8784
8785 /*
8786 * Report an error unless the argument evaluation or function call has been
8787 * cancelled due to an aborting error, an interrupt, or an exception.
8788 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008789 if (!aborting())
8790 {
8791 switch (error)
8792 {
8793 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008794 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008795 break;
8796 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008797 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008798 break;
8799 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008800 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008801 name);
8802 break;
8803 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008804 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008805 name);
8806 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008808 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 name);
8810 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008811 }
8812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (fname != name && fname != fname_buf)
8815 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008816 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
8818 return ret;
8819}
8820
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008821/*
8822 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008823 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008824 */
8825 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008826emsg_funcname(ermsg, name)
8827 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008828 char_u *name;
8829{
8830 char_u *p;
8831
8832 if (*name == K_SPECIAL)
8833 p = concat_str((char_u *)"<SNR>", name + 3);
8834 else
8835 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008836 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008837 if (p != name)
8838 vim_free(p);
8839}
8840
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008841/*
8842 * Return TRUE for a non-zero Number and a non-empty String.
8843 */
8844 static int
8845non_zero_arg(argvars)
8846 typval_T *argvars;
8847{
8848 return ((argvars[0].v_type == VAR_NUMBER
8849 && argvars[0].vval.v_number != 0)
8850 || (argvars[0].v_type == VAR_STRING
8851 && argvars[0].vval.v_string != NULL
8852 && *argvars[0].vval.v_string != NUL));
8853}
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855/*********************************************
8856 * Implementation of the built-in functions
8857 */
8858
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008859#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008860static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8861
8862/*
8863 * Get the float value of "argvars[0]" into "f".
8864 * Returns FAIL when the argument is not a Number or Float.
8865 */
8866 static int
8867get_float_arg(argvars, f)
8868 typval_T *argvars;
8869 float_T *f;
8870{
8871 if (argvars[0].v_type == VAR_FLOAT)
8872 {
8873 *f = argvars[0].vval.v_float;
8874 return OK;
8875 }
8876 if (argvars[0].v_type == VAR_NUMBER)
8877 {
8878 *f = (float_T)argvars[0].vval.v_number;
8879 return OK;
8880 }
8881 EMSG(_("E808: Number or Float required"));
8882 return FAIL;
8883}
8884
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885/*
8886 * "abs(expr)" function
8887 */
8888 static void
8889f_abs(argvars, rettv)
8890 typval_T *argvars;
8891 typval_T *rettv;
8892{
8893 if (argvars[0].v_type == VAR_FLOAT)
8894 {
8895 rettv->v_type = VAR_FLOAT;
8896 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8897 }
8898 else
8899 {
8900 varnumber_T n;
8901 int error = FALSE;
8902
8903 n = get_tv_number_chk(&argvars[0], &error);
8904 if (error)
8905 rettv->vval.v_number = -1;
8906 else if (n > 0)
8907 rettv->vval.v_number = n;
8908 else
8909 rettv->vval.v_number = -n;
8910 }
8911}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008912
8913/*
8914 * "acos()" function
8915 */
8916 static void
8917f_acos(argvars, rettv)
8918 typval_T *argvars;
8919 typval_T *rettv;
8920{
8921 float_T f;
8922
8923 rettv->v_type = VAR_FLOAT;
8924 if (get_float_arg(argvars, &f) == OK)
8925 rettv->vval.v_float = acos(f);
8926 else
8927 rettv->vval.v_float = 0.0;
8928}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929#endif
8930
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 */
8934 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008935f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 typval_T *argvars;
8937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008942 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008944 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008945 && !tv_check_lock(l->lv_lock,
8946 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008947 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008949 }
8950 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008951 EMSG(_(e_listreq));
8952}
8953
8954/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008955 * "and(expr, expr)" function
8956 */
8957 static void
8958f_and(argvars, rettv)
8959 typval_T *argvars;
8960 typval_T *rettv;
8961{
8962 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8963 & get_tv_number_chk(&argvars[1], NULL);
8964}
8965
8966/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008967 * "append(lnum, string/list)" function
8968 */
8969 static void
8970f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *argvars;
8972 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008973{
8974 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 list_T *l = NULL;
8977 listitem_T *li = NULL;
8978 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008979 long added = 0;
8980
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008981 /* When coming here from Insert mode, sync undo, so that this can be
8982 * undone separately from what was previously inserted. */
8983 if (u_sync_once == 2)
8984 {
8985 u_sync_once = 1; /* notify that u_sync() was called */
8986 u_sync(TRUE);
8987 }
8988
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 lnum = get_tv_lnum(argvars);
8990 if (lnum >= 0
8991 && lnum <= curbuf->b_ml.ml_line_count
8992 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008996 l = argvars[1].vval.v_list;
8997 if (l == NULL)
8998 return;
8999 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001 for (;;)
9002 {
9003 if (l == NULL)
9004 tv = &argvars[1]; /* append a string */
9005 else if (li == NULL)
9006 break; /* end of list */
9007 else
9008 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009 line = get_tv_string_chk(tv);
9010 if (line == NULL) /* type error */
9011 {
9012 rettv->vval.v_number = 1; /* Failed */
9013 break;
9014 }
9015 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 ++added;
9017 if (l == NULL)
9018 break;
9019 li = li->li_next;
9020 }
9021
9022 appended_lines_mark(lnum, added);
9023 if (curwin->w_cursor.lnum > lnum)
9024 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009026 else
9027 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
9030/*
9031 * "argc()" function
9032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009035 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039}
9040
9041/*
9042 * "argidx()" function
9043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
9052/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009053 * "arglistid()" function
9054 */
9055 static void
9056f_arglistid(argvars, rettv)
9057 typval_T *argvars UNUSED;
9058 typval_T *rettv;
9059{
9060 win_T *wp;
9061 tabpage_T *tp = NULL;
9062 long n;
9063
9064 rettv->vval.v_number = -1;
9065 if (argvars[0].v_type != VAR_UNKNOWN)
9066 {
9067 if (argvars[1].v_type != VAR_UNKNOWN)
9068 {
9069 n = get_tv_number(&argvars[1]);
9070 if (n >= 0)
9071 tp = find_tabpage(n);
9072 }
9073 else
9074 tp = curtab;
9075
9076 if (tp != NULL)
9077 {
9078 wp = find_win_by_nr(&argvars[0], tp);
9079 if (wp != NULL)
9080 rettv->vval.v_number = wp->w_alist->id;
9081 }
9082 }
9083 else
9084 rettv->vval.v_number = curwin->w_alist->id;
9085}
9086
9087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 * "argv(nr)" function
9089 */
9090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 typval_T *argvars;
9093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
9095 int idx;
9096
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009097 if (argvars[0].v_type != VAR_UNKNOWN)
9098 {
9099 idx = get_tv_number_chk(&argvars[0], NULL);
9100 if (idx >= 0 && idx < ARGCOUNT)
9101 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9102 else
9103 rettv->vval.v_string = NULL;
9104 rettv->v_type = VAR_STRING;
9105 }
9106 else if (rettv_list_alloc(rettv) == OK)
9107 for (idx = 0; idx < ARGCOUNT; ++idx)
9108 list_append_string(rettv->vval.v_list,
9109 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110}
9111
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009112#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009113/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009114 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009115 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009116 static void
9117f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009118 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009119 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009120{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009121 float_T f;
9122
9123 rettv->v_type = VAR_FLOAT;
9124 if (get_float_arg(argvars, &f) == OK)
9125 rettv->vval.v_float = asin(f);
9126 else
9127 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009128}
9129
9130/*
9131 * "atan()" function
9132 */
9133 static void
9134f_atan(argvars, rettv)
9135 typval_T *argvars;
9136 typval_T *rettv;
9137{
9138 float_T f;
9139
9140 rettv->v_type = VAR_FLOAT;
9141 if (get_float_arg(argvars, &f) == OK)
9142 rettv->vval.v_float = atan(f);
9143 else
9144 rettv->vval.v_float = 0.0;
9145}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009146
9147/*
9148 * "atan2()" function
9149 */
9150 static void
9151f_atan2(argvars, rettv)
9152 typval_T *argvars;
9153 typval_T *rettv;
9154{
9155 float_T fx, fy;
9156
9157 rettv->v_type = VAR_FLOAT;
9158 if (get_float_arg(argvars, &fx) == OK
9159 && get_float_arg(&argvars[1], &fy) == OK)
9160 rettv->vval.v_float = atan2(fx, fy);
9161 else
9162 rettv->vval.v_float = 0.0;
9163}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009164#endif
9165
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166/*
9167 * "browse(save, title, initdir, default)" function
9168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174#ifdef FEAT_BROWSE
9175 int save;
9176 char_u *title;
9177 char_u *initdir;
9178 char_u *defname;
9179 char_u buf[NUMBUFLEN];
9180 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 save = get_tv_number_chk(&argvars[0], &error);
9184 title = get_tv_string_chk(&argvars[1]);
9185 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9186 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 if (error || title == NULL || initdir == NULL || defname == NULL)
9189 rettv->vval.v_string = NULL;
9190 else
9191 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009192 do_browse(save ? BROWSE_SAVE : 0,
9193 title, defname, NULL, initdir, NULL, curbuf);
9194#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009196#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009198}
9199
9200/*
9201 * "browsedir(title, initdir)" function
9202 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009205 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009206 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009207{
9208#ifdef FEAT_BROWSE
9209 char_u *title;
9210 char_u *initdir;
9211 char_u buf[NUMBUFLEN];
9212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 title = get_tv_string_chk(&argvars[0]);
9214 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009216 if (title == NULL || initdir == NULL)
9217 rettv->vval.v_string = NULL;
9218 else
9219 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009220 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225}
9226
Bram Moolenaar33570922005-01-25 22:26:29 +00009227static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229/*
9230 * Find a buffer by number or exact name.
9231 */
9232 static buf_T *
9233find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009234 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009238 if (avar->v_type == VAR_NUMBER)
9239 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009240 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009242 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009243 if (buf == NULL)
9244 {
9245 /* No full path name match, try a match with a URL or a "nofile"
9246 * buffer, these don't use the full path. */
9247 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9248 if (buf->b_fname != NULL
9249 && (path_with_url(buf->b_fname)
9250#ifdef FEAT_QUICKFIX
9251 || bt_nofile(buf)
9252#endif
9253 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009255 break;
9256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 }
9258 return buf;
9259}
9260
9261/*
9262 * "bufexists(expr)" function
9263 */
9264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009266 typval_T *argvars;
9267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270}
9271
9272/*
9273 * "buflisted(expr)" function
9274 */
9275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T *argvars;
9278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280 buf_T *buf;
9281
9282 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284}
9285
9286/*
9287 * "bufloaded(expr)" function
9288 */
9289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 typval_T *argvars;
9292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
9294 buf_T *buf;
9295
9296 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009297 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298}
9299
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009300static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009301
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302/*
9303 * Get buffer by number or pattern.
9304 */
9305 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009306get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009307 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009308 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 int save_magic;
9312 char_u *save_cpo;
9313 buf_T *buf;
9314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315 if (tv->v_type == VAR_NUMBER)
9316 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009317 if (tv->v_type != VAR_STRING)
9318 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 if (name == NULL || *name == NUL)
9320 return curbuf;
9321 if (name[0] == '$' && name[1] == NUL)
9322 return lastbuf;
9323
9324 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9325 save_magic = p_magic;
9326 p_magic = TRUE;
9327 save_cpo = p_cpo;
9328 p_cpo = (char_u *)"";
9329
9330 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009331 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
9333 p_magic = save_magic;
9334 p_cpo = save_cpo;
9335
9336 /* If not found, try expanding the name, like done for bufexists(). */
9337 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009338 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
9340 return buf;
9341}
9342
9343/*
9344 * "bufname(expr)" function
9345 */
9346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009348 typval_T *argvars;
9349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351 buf_T *buf;
9352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009355 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 --emsg_off;
9362}
9363
9364/*
9365 * "bufnr(expr)" function
9366 */
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009373 int error = FALSE;
9374 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009378 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009379 --emsg_off;
9380
9381 /* If the buffer isn't found and the second argument is not zero create a
9382 * new buffer. */
9383 if (buf == NULL
9384 && argvars[1].v_type != VAR_UNKNOWN
9385 && get_tv_number_chk(&argvars[1], &error) != 0
9386 && !error
9387 && (name = get_tv_string_chk(&argvars[0])) != NULL
9388 && !error)
9389 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9390
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
9397/*
9398 * "bufwinnr(nr)" function
9399 */
9400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 typval_T *argvars;
9403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405#ifdef FEAT_WINDOWS
9406 win_T *wp;
9407 int winnr = 0;
9408#endif
9409 buf_T *buf;
9410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009413 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#ifdef FEAT_WINDOWS
9415 for (wp = firstwin; wp; wp = wp->w_next)
9416 {
9417 ++winnr;
9418 if (wp->w_buffer == buf)
9419 break;
9420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424#endif
9425 --emsg_off;
9426}
9427
9428/*
9429 * "byte2line(byte)" function
9430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009433 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438#else
9439 long boff = 0;
9440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 (linenr_T)0, &boff);
9447#endif
9448}
9449
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009450 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009451byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 typval_T *argvars;
9453 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009454 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009455{
9456#ifdef FEAT_MBYTE
9457 char_u *t;
9458#endif
9459 char_u *str;
9460 long idx;
9461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 str = get_tv_string_chk(&argvars[0]);
9463 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009466 return;
9467
9468#ifdef FEAT_MBYTE
9469 t = str;
9470 for ( ; idx > 0; idx--)
9471 {
9472 if (*t == NUL) /* EOL reached */
9473 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009474 if (enc_utf8 && comp)
9475 t += utf_ptr2len(t);
9476 else
9477 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009478 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009479 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009480#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009483#endif
9484}
9485
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009486/*
9487 * "byteidx()" function
9488 */
9489 static void
9490f_byteidx(argvars, rettv)
9491 typval_T *argvars;
9492 typval_T *rettv;
9493{
9494 byteidx(argvars, rettv, FALSE);
9495}
9496
9497/*
9498 * "byteidxcomp()" function
9499 */
9500 static void
9501f_byteidxcomp(argvars, rettv)
9502 typval_T *argvars;
9503 typval_T *rettv;
9504{
9505 byteidx(argvars, rettv, TRUE);
9506}
9507
Bram Moolenaardb913952012-06-29 12:54:53 +02009508 int
9509func_call(name, args, selfdict, rettv)
9510 char_u *name;
9511 typval_T *args;
9512 dict_T *selfdict;
9513 typval_T *rettv;
9514{
9515 listitem_T *item;
9516 typval_T argv[MAX_FUNC_ARGS + 1];
9517 int argc = 0;
9518 int dummy;
9519 int r = 0;
9520
9521 for (item = args->vval.v_list->lv_first; item != NULL;
9522 item = item->li_next)
9523 {
9524 if (argc == MAX_FUNC_ARGS)
9525 {
9526 EMSG(_("E699: Too many arguments"));
9527 break;
9528 }
9529 /* Make a copy of each argument. This is needed to be able to set
9530 * v_lock to VAR_FIXED in the copy without changing the original list.
9531 */
9532 copy_tv(&item->li_tv, &argv[argc++]);
9533 }
9534
9535 if (item == NULL)
9536 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9537 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9538 &dummy, TRUE, selfdict);
9539
9540 /* Free the arguments. */
9541 while (argc > 0)
9542 clear_tv(&argv[--argc]);
9543
9544 return r;
9545}
9546
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009547/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009548 * "call(func, arglist)" function
9549 */
9550 static void
9551f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009552 typval_T *argvars;
9553 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009554{
9555 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009557
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009558 if (argvars[1].v_type != VAR_LIST)
9559 {
9560 EMSG(_(e_listreq));
9561 return;
9562 }
9563 if (argvars[1].vval.v_list == NULL)
9564 return;
9565
9566 if (argvars[0].v_type == VAR_FUNC)
9567 func = argvars[0].vval.v_string;
9568 else
9569 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 if (*func == NUL)
9571 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009572
Bram Moolenaare9a41262005-01-15 22:18:47 +00009573 if (argvars[2].v_type != VAR_UNKNOWN)
9574 {
9575 if (argvars[2].v_type != VAR_DICT)
9576 {
9577 EMSG(_(e_dictreq));
9578 return;
9579 }
9580 selfdict = argvars[2].vval.v_dict;
9581 }
9582
Bram Moolenaardb913952012-06-29 12:54:53 +02009583 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009584}
9585
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009586#ifdef FEAT_FLOAT
9587/*
9588 * "ceil({float})" function
9589 */
9590 static void
9591f_ceil(argvars, rettv)
9592 typval_T *argvars;
9593 typval_T *rettv;
9594{
9595 float_T f;
9596
9597 rettv->v_type = VAR_FLOAT;
9598 if (get_float_arg(argvars, &f) == OK)
9599 rettv->vval.v_float = ceil(f);
9600 else
9601 rettv->vval.v_float = 0.0;
9602}
9603#endif
9604
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009605/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009606 * "changenr()" function
9607 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009608 static void
9609f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009610 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009611 typval_T *rettv;
9612{
9613 rettv->vval.v_number = curbuf->b_u_seq_cur;
9614}
9615
9616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 * "char2nr(string)" function
9618 */
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624#ifdef FEAT_MBYTE
9625 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009626 {
9627 int utf8 = 0;
9628
9629 if (argvars[1].v_type != VAR_UNKNOWN)
9630 utf8 = get_tv_number_chk(&argvars[1], NULL);
9631
9632 if (utf8)
9633 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9634 else
9635 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 else
9638#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640}
9641
9642/*
9643 * "cindent(lnum)" function
9644 */
9645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650#ifdef FEAT_CINDENT
9651 pos_T pos;
9652 linenr_T lnum;
9653
9654 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9657 {
9658 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 curwin->w_cursor = pos;
9661 }
9662 else
9663#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665}
9666
9667/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009668 * "clearmatches()" function
9669 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009670 static void
9671f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009672 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009673 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009674{
9675#ifdef FEAT_SEARCH_EXTRA
9676 clear_matches(curwin);
9677#endif
9678}
9679
9680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 * "col(string)" function
9682 */
9683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 typval_T *argvars;
9686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 colnr_T col = 0;
9689 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009690 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009692 fp = var2fpos(&argvars[0], FALSE, &fnum);
9693 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 {
9695 if (fp->col == MAXCOL)
9696 {
9697 /* '> can be MAXCOL, get the length of the line then */
9698 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009699 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 else
9701 col = MAXCOL;
9702 }
9703 else
9704 {
9705 col = fp->col + 1;
9706#ifdef FEAT_VIRTUALEDIT
9707 /* col(".") when the cursor is on the NUL at the end of the line
9708 * because of "coladd" can be seen as an extra column. */
9709 if (virtual_active() && fp == &curwin->w_cursor)
9710 {
9711 char_u *p = ml_get_cursor();
9712
9713 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9714 curwin->w_virtcol - curwin->w_cursor.coladd))
9715 {
9716# ifdef FEAT_MBYTE
9717 int l;
9718
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009719 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 col += l;
9721# else
9722 if (*p != NUL && p[1] == NUL)
9723 ++col;
9724# endif
9725 }
9726 }
9727#endif
9728 }
9729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
Bram Moolenaar572cb562005-08-05 21:35:02 +00009733#if defined(FEAT_INS_EXPAND)
9734/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009735 * "complete()" function
9736 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009737 static void
9738f_complete(argvars, rettv)
9739 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009740 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009741{
9742 int startcol;
9743
9744 if ((State & INSERT) == 0)
9745 {
9746 EMSG(_("E785: complete() can only be used in Insert mode"));
9747 return;
9748 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009749
9750 /* Check for undo allowed here, because if something was already inserted
9751 * the line was already saved for undo and this check isn't done. */
9752 if (!undo_allowed())
9753 return;
9754
Bram Moolenaarade00832006-03-10 21:46:58 +00009755 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9756 {
9757 EMSG(_(e_invarg));
9758 return;
9759 }
9760
9761 startcol = get_tv_number_chk(&argvars[0], NULL);
9762 if (startcol <= 0)
9763 return;
9764
9765 set_completion(startcol - 1, argvars[1].vval.v_list);
9766}
9767
9768/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009769 * "complete_add()" function
9770 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009771 static void
9772f_complete_add(argvars, rettv)
9773 typval_T *argvars;
9774 typval_T *rettv;
9775{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009776 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009777}
9778
9779/*
9780 * "complete_check()" function
9781 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009782 static void
9783f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009784 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009785 typval_T *rettv;
9786{
9787 int saved = RedrawingDisabled;
9788
9789 RedrawingDisabled = 0;
9790 ins_compl_check_keys(0);
9791 rettv->vval.v_number = compl_interrupted;
9792 RedrawingDisabled = saved;
9793}
9794#endif
9795
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796/*
9797 * "confirm(message, buttons[, default [, type]])" function
9798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009801 typval_T *argvars UNUSED;
9802 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9805 char_u *message;
9806 char_u *buttons = NULL;
9807 char_u buf[NUMBUFLEN];
9808 char_u buf2[NUMBUFLEN];
9809 int def = 1;
9810 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 char_u *typestr;
9812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814 message = get_tv_string_chk(&argvars[0]);
9815 if (message == NULL)
9816 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009817 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9820 if (buttons == NULL)
9821 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009822 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9828 if (typestr == NULL)
9829 error = TRUE;
9830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 switch (TOUPPER_ASC(*typestr))
9833 {
9834 case 'E': type = VIM_ERROR; break;
9835 case 'Q': type = VIM_QUESTION; break;
9836 case 'I': type = VIM_INFO; break;
9837 case 'W': type = VIM_WARNING; break;
9838 case 'G': type = VIM_GENERIC; break;
9839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 }
9841 }
9842 }
9843 }
9844
9845 if (buttons == NULL || *buttons == NUL)
9846 buttons = (char_u *)_("&Ok");
9847
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009848 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009850 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851#endif
9852}
9853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009854/*
9855 * "copy()" function
9856 */
9857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 typval_T *argvars;
9860 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009861{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009862 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009865#ifdef FEAT_FLOAT
9866/*
9867 * "cos()" function
9868 */
9869 static void
9870f_cos(argvars, rettv)
9871 typval_T *argvars;
9872 typval_T *rettv;
9873{
9874 float_T f;
9875
9876 rettv->v_type = VAR_FLOAT;
9877 if (get_float_arg(argvars, &f) == OK)
9878 rettv->vval.v_float = cos(f);
9879 else
9880 rettv->vval.v_float = 0.0;
9881}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009882
9883/*
9884 * "cosh()" function
9885 */
9886 static void
9887f_cosh(argvars, rettv)
9888 typval_T *argvars;
9889 typval_T *rettv;
9890{
9891 float_T f;
9892
9893 rettv->v_type = VAR_FLOAT;
9894 if (get_float_arg(argvars, &f) == OK)
9895 rettv->vval.v_float = cosh(f);
9896 else
9897 rettv->vval.v_float = 0.0;
9898}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009899#endif
9900
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009902 * "count()" function
9903 */
9904 static void
9905f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009906 typval_T *argvars;
9907 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009908{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009909 long n = 0;
9910 int ic = FALSE;
9911
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 listitem_T *li;
9915 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009917
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 if ((l = argvars[0].vval.v_list) != NULL)
9919 {
9920 li = l->lv_first;
9921 if (argvars[2].v_type != VAR_UNKNOWN)
9922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 int error = FALSE;
9924
9925 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 if (argvars[3].v_type != VAR_UNKNOWN)
9927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 idx = get_tv_number_chk(&argvars[3], &error);
9929 if (!error)
9930 {
9931 li = list_find(l, idx);
9932 if (li == NULL)
9933 EMSGN(_(e_listidx), idx);
9934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009935 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 if (error)
9937 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 }
9939
9940 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009941 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009942 ++n;
9943 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009944 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945 else if (argvars[0].v_type == VAR_DICT)
9946 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009947 int todo;
9948 dict_T *d;
9949 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009951 if ((d = argvars[0].vval.v_dict) != NULL)
9952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009953 int error = FALSE;
9954
Bram Moolenaare9a41262005-01-15 22:18:47 +00009955 if (argvars[2].v_type != VAR_UNKNOWN)
9956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009958 if (argvars[3].v_type != VAR_UNKNOWN)
9959 EMSG(_(e_invarg));
9960 }
9961
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009962 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009964 {
9965 if (!HASHITEM_EMPTY(hi))
9966 {
9967 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009968 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009969 ++n;
9970 }
9971 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009972 }
9973 }
9974 else
9975 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009976 rettv->vval.v_number = n;
9977}
9978
9979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9981 *
9982 * Checks the existence of a cscope connection.
9983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009986 typval_T *argvars UNUSED;
9987 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989#ifdef FEAT_CSCOPE
9990 int num = 0;
9991 char_u *dbpath = NULL;
9992 char_u *prepend = NULL;
9993 char_u buf[NUMBUFLEN];
9994
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009995 if (argvars[0].v_type != VAR_UNKNOWN
9996 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 num = (int)get_tv_number(&argvars[0]);
9999 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010000 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005#endif
10006}
10007
10008/*
10009 * "cursor(lnum, col)" function
10010 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010011 * Moves the cursor to the specified line and column.
10012 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010016 typval_T *argvars;
10017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
10019 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010020#ifdef FEAT_VIRTUALEDIT
10021 long coladd = 0;
10022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010024 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010025 if (argvars[1].v_type == VAR_UNKNOWN)
10026 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010027 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010028 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010029
Bram Moolenaar493c1782014-05-28 14:34:46 +020010030 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010031 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010032 line = pos.lnum;
10033 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010034#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010035 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010036#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010037 if (curswant >= 0)
10038 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010039 }
10040 else
10041 {
10042 line = get_tv_lnum(argvars);
10043 col = get_tv_number_chk(&argvars[1], NULL);
10044#ifdef FEAT_VIRTUALEDIT
10045 if (argvars[2].v_type != VAR_UNKNOWN)
10046 coladd = get_tv_number_chk(&argvars[2], NULL);
10047#endif
10048 }
10049 if (line < 0 || col < 0
10050#ifdef FEAT_VIRTUALEDIT
10051 || coladd < 0
10052#endif
10053 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010054 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (line > 0)
10056 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 if (col > 0)
10058 curwin->w_cursor.col = col - 1;
10059#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010060 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061#endif
10062
10063 /* Make sure the cursor is in a valid position. */
10064 check_cursor();
10065#ifdef FEAT_MBYTE
10066 /* Correct cursor for multi-byte character. */
10067 if (has_mbyte)
10068 mb_adjust_cursor();
10069#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010070
10071 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010072 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073}
10074
10075/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010076 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010083 int noref = 0;
10084
10085 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010087 if (noref < 0 || noref > 1)
10088 EMSG(_(e_invarg));
10089 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010090 {
10091 current_copyID += COPYID_INC;
10092 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094}
10095
10096/*
10097 * "delete()" function
10098 */
10099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 typval_T *argvars;
10102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108}
10109
10110/*
10111 * "did_filetype()" function
10112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010115 typval_T *argvars UNUSED;
10116 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#endif
10121}
10122
10123/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010124 * "diff_filler()" function
10125 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010128 typval_T *argvars UNUSED;
10129 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010130{
10131#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010133#endif
10134}
10135
10136/*
10137 * "diff_hlID()" function
10138 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010141 typval_T *argvars UNUSED;
10142 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010143{
10144#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010146 static linenr_T prev_lnum = 0;
10147 static int changedtick = 0;
10148 static int fnum = 0;
10149 static int change_start = 0;
10150 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010151 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010152 int filler_lines;
10153 int col;
10154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010155 if (lnum < 0) /* ignore type error in {lnum} arg */
10156 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010157 if (lnum != prev_lnum
10158 || changedtick != curbuf->b_changedtick
10159 || fnum != curbuf->b_fnum)
10160 {
10161 /* New line, buffer, change: need to get the values. */
10162 filler_lines = diff_check(curwin, lnum);
10163 if (filler_lines < 0)
10164 {
10165 if (filler_lines == -1)
10166 {
10167 change_start = MAXCOL;
10168 change_end = -1;
10169 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10170 hlID = HLF_ADD; /* added line */
10171 else
10172 hlID = HLF_CHD; /* changed line */
10173 }
10174 else
10175 hlID = HLF_ADD; /* added line */
10176 }
10177 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010178 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010179 prev_lnum = lnum;
10180 changedtick = curbuf->b_changedtick;
10181 fnum = curbuf->b_fnum;
10182 }
10183
10184 if (hlID == HLF_CHD || hlID == HLF_TXD)
10185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010187 if (col >= change_start && col <= change_end)
10188 hlID = HLF_TXD; /* changed text */
10189 else
10190 hlID = HLF_CHD; /* changed line */
10191 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010192 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010193#endif
10194}
10195
10196/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010197 * "empty({expr})" function
10198 */
10199 static void
10200f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 typval_T *argvars;
10202 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010203{
10204 int n;
10205
10206 switch (argvars[0].v_type)
10207 {
10208 case VAR_STRING:
10209 case VAR_FUNC:
10210 n = argvars[0].vval.v_string == NULL
10211 || *argvars[0].vval.v_string == NUL;
10212 break;
10213 case VAR_NUMBER:
10214 n = argvars[0].vval.v_number == 0;
10215 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010216#ifdef FEAT_FLOAT
10217 case VAR_FLOAT:
10218 n = argvars[0].vval.v_float == 0.0;
10219 break;
10220#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010221 case VAR_LIST:
10222 n = argvars[0].vval.v_list == NULL
10223 || argvars[0].vval.v_list->lv_first == NULL;
10224 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 case VAR_DICT:
10226 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010229 default:
10230 EMSG2(_(e_intern2), "f_empty()");
10231 n = 0;
10232 }
10233
10234 rettv->vval.v_number = n;
10235}
10236
10237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 * "escape({string}, {chars})" function
10239 */
10240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 typval_T *argvars;
10243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 char_u buf[NUMBUFLEN];
10246
Bram Moolenaar758711c2005-02-02 23:11:38 +000010247 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10248 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250}
10251
10252/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010253 * "eval()" function
10254 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010255 static void
10256f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010257 typval_T *argvars;
10258 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010260 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 s = get_tv_string_chk(&argvars[0]);
10263 if (s != NULL)
10264 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265
Bram Moolenaar615b9972015-01-14 17:15:05 +010010266 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10268 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010269 if (p != NULL && !aborting())
10270 EMSG2(_(e_invexpr2), p);
10271 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010273 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010275 else if (*s != NUL)
10276 EMSG(_(e_trailing));
10277}
10278
10279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 * "eventhandler()" function
10281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288}
10289
10290/*
10291 * "executable()" function
10292 */
10293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010295 typval_T *argvars;
10296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010298 char_u *name = get_tv_string(&argvars[0]);
10299
10300 /* Check in $PATH and also check directly if there is a directory name. */
10301 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10302 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010303}
10304
10305/*
10306 * "exepath()" function
10307 */
10308 static void
10309f_exepath(argvars, rettv)
10310 typval_T *argvars;
10311 typval_T *rettv;
10312{
10313 char_u *p = NULL;
10314
Bram Moolenaarb5971142015-03-21 17:32:19 +010010315 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010316 rettv->v_type = VAR_STRING;
10317 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318}
10319
10320/*
10321 * "exists()" function
10322 */
10323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010325 typval_T *argvars;
10326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327{
10328 char_u *p;
10329 char_u *name;
10330 int n = FALSE;
10331 int len = 0;
10332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 if (*p == '$') /* environment variable */
10335 {
10336 /* first try "normal" environment variables (fast) */
10337 if (mch_getenv(p + 1) != NULL)
10338 n = TRUE;
10339 else
10340 {
10341 /* try expanding things like $VIM and ${HOME} */
10342 p = expand_env_save(p);
10343 if (p != NULL && *p != '$')
10344 n = TRUE;
10345 vim_free(p);
10346 }
10347 }
10348 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010351 if (*skipwhite(p) != NUL)
10352 n = FALSE; /* trailing garbage */
10353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 else if (*p == '*') /* internal or user defined function */
10355 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010356 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 }
10358 else if (*p == ':')
10359 {
10360 n = cmd_exists(p + 1);
10361 }
10362 else if (*p == '#')
10363 {
10364#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010365 if (p[1] == '#')
10366 n = autocmd_supported(p + 2);
10367 else
10368 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369#endif
10370 }
10371 else /* internal variable */
10372 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010373 char_u *tofree;
10374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010376 /* get_name_len() takes care of expanding curly braces */
10377 name = p;
10378 len = get_name_len(&p, &tofree, TRUE, FALSE);
10379 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010381 if (tofree != NULL)
10382 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010383 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010384 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010386 /* handle d.key, l[idx], f(expr) */
10387 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10388 if (n)
10389 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 }
10391 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010392 if (*p != NUL)
10393 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010395 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 }
10397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010401#ifdef FEAT_FLOAT
10402/*
10403 * "exp()" function
10404 */
10405 static void
10406f_exp(argvars, rettv)
10407 typval_T *argvars;
10408 typval_T *rettv;
10409{
10410 float_T f;
10411
10412 rettv->v_type = VAR_FLOAT;
10413 if (get_float_arg(argvars, &f) == OK)
10414 rettv->vval.v_float = exp(f);
10415 else
10416 rettv->vval.v_float = 0.0;
10417}
10418#endif
10419
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420/*
10421 * "expand()" function
10422 */
10423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 typval_T *argvars;
10426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
10428 char_u *s;
10429 int len;
10430 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010431 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010434 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010437 if (argvars[1].v_type != VAR_UNKNOWN
10438 && argvars[2].v_type != VAR_UNKNOWN
10439 && get_tv_number_chk(&argvars[2], &error)
10440 && !error)
10441 {
10442 rettv->v_type = VAR_LIST;
10443 rettv->vval.v_list = NULL;
10444 }
10445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 if (*s == '%' || *s == '#' || *s == '<')
10448 {
10449 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010450 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010452 if (rettv->v_type == VAR_LIST)
10453 {
10454 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10455 list_append_string(rettv->vval.v_list, result, -1);
10456 else
10457 vim_free(result);
10458 }
10459 else
10460 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 else
10463 {
10464 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010465 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 if (argvars[1].v_type != VAR_UNKNOWN
10467 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010468 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 if (!error)
10470 {
10471 ExpandInit(&xpc);
10472 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010473 if (p_wic)
10474 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010475 if (rettv->v_type == VAR_STRING)
10476 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10477 options, WILD_ALL);
10478 else if (rettv_list_alloc(rettv) != FAIL)
10479 {
10480 int i;
10481
10482 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10483 for (i = 0; i < xpc.xp_numfiles; i++)
10484 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10485 ExpandCleanup(&xpc);
10486 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010487 }
10488 else
10489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 }
10491}
10492
10493/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010494 * Go over all entries in "d2" and add them to "d1".
10495 * When "action" is "error" then a duplicate key is an error.
10496 * When "action" is "force" then a duplicate key is overwritten.
10497 * Otherwise duplicate keys are ignored ("action" is "keep").
10498 */
10499 void
10500dict_extend(d1, d2, action)
10501 dict_T *d1;
10502 dict_T *d2;
10503 char_u *action;
10504{
10505 dictitem_T *di1;
10506 hashitem_T *hi2;
10507 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010508 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010509
10510 todo = (int)d2->dv_hashtab.ht_used;
10511 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10512 {
10513 if (!HASHITEM_EMPTY(hi2))
10514 {
10515 --todo;
10516 di1 = dict_find(d1, hi2->hi_key, -1);
10517 if (d1->dv_scope != 0)
10518 {
10519 /* Disallow replacing a builtin function in l: and g:.
10520 * Check the key to be valid when adding to any
10521 * scope. */
10522 if (d1->dv_scope == VAR_DEF_SCOPE
10523 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10524 && var_check_func_name(hi2->hi_key,
10525 di1 == NULL))
10526 break;
10527 if (!valid_varname(hi2->hi_key))
10528 break;
10529 }
10530 if (di1 == NULL)
10531 {
10532 di1 = dictitem_copy(HI2DI(hi2));
10533 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10534 dictitem_free(di1);
10535 }
10536 else if (*action == 'e')
10537 {
10538 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10539 break;
10540 }
10541 else if (*action == 'f' && HI2DI(hi2) != di1)
10542 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010543 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10544 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010545 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010546 clear_tv(&di1->di_tv);
10547 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10548 }
10549 }
10550 }
10551}
10552
10553/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010554 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010556 */
10557 static void
10558f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010561{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010562 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010563
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010565 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 list_T *l1, *l2;
10567 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010570
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 l1 = argvars[0].vval.v_list;
10572 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010573 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010574 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575 {
10576 if (argvars[2].v_type != VAR_UNKNOWN)
10577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 before = get_tv_number_chk(&argvars[2], &error);
10579 if (error)
10580 return; /* type error; errmsg already given */
10581
Bram Moolenaar758711c2005-02-02 23:11:38 +000010582 if (before == l1->lv_len)
10583 item = NULL;
10584 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010586 item = list_find(l1, before);
10587 if (item == NULL)
10588 {
10589 EMSGN(_(e_listidx), before);
10590 return;
10591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 }
10593 }
10594 else
10595 item = NULL;
10596 list_extend(l1, l2, item);
10597
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 copy_tv(&argvars[0], rettv);
10599 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010600 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10602 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010603 dict_T *d1, *d2;
10604 char_u *action;
10605 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606
10607 d1 = argvars[0].vval.v_dict;
10608 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010609 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010610 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 {
10612 /* Check the third argument. */
10613 if (argvars[2].v_type != VAR_UNKNOWN)
10614 {
10615 static char *(av[]) = {"keep", "force", "error"};
10616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 action = get_tv_string_chk(&argvars[2]);
10618 if (action == NULL)
10619 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010620 for (i = 0; i < 3; ++i)
10621 if (STRCMP(action, av[i]) == 0)
10622 break;
10623 if (i == 3)
10624 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010625 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010626 return;
10627 }
10628 }
10629 else
10630 action = (char_u *)"force";
10631
Bram Moolenaara9922d62013-05-30 13:01:18 +020010632 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 copy_tv(&argvars[0], rettv);
10635 }
10636 }
10637 else
10638 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010639}
10640
10641/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010642 * "feedkeys()" function
10643 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010644 static void
10645f_feedkeys(argvars, rettv)
10646 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010647 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010648{
10649 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010650 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010651 char_u *keys, *flags;
10652 char_u nbuf[NUMBUFLEN];
10653 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010654 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010655
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010656 /* This is not allowed in the sandbox. If the commands would still be
10657 * executed in the sandbox it would be OK, but it probably happens later,
10658 * when "sandbox" is no longer set. */
10659 if (check_secure())
10660 return;
10661
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010662 keys = get_tv_string(&argvars[0]);
10663 if (*keys != NUL)
10664 {
10665 if (argvars[1].v_type != VAR_UNKNOWN)
10666 {
10667 flags = get_tv_string_buf(&argvars[1], nbuf);
10668 for ( ; *flags != NUL; ++flags)
10669 {
10670 switch (*flags)
10671 {
10672 case 'n': remap = FALSE; break;
10673 case 'm': remap = TRUE; break;
10674 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010675 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010676 }
10677 }
10678 }
10679
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010680 /* Need to escape K_SPECIAL and CSI before putting the string in the
10681 * typeahead buffer. */
10682 keys_esc = vim_strsave_escape_csi(keys);
10683 if (keys_esc != NULL)
10684 {
10685 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010686 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010687 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010688 if (vgetc_busy)
10689 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010690 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010691 }
10692}
10693
10694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 * "filereadable()" function
10696 */
10697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010699 typval_T *argvars;
10700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010702 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 char_u *p;
10704 int n;
10705
Bram Moolenaarc236c162008-07-13 17:41:49 +000010706#ifndef O_NONBLOCK
10707# define O_NONBLOCK 0
10708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010710 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10711 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 {
10713 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010714 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 }
10716 else
10717 n = FALSE;
10718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720}
10721
10722/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010723 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 * rights to write into.
10725 */
10726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 typval_T *argvars;
10729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010731 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732}
10733
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010734static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010735
10736 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010737findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010738 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010740 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010741{
10742#ifdef FEAT_SEARCHPATH
10743 char_u *fname;
10744 char_u *fresult = NULL;
10745 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10746 char_u *p;
10747 char_u pathbuf[NUMBUFLEN];
10748 int count = 1;
10749 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010750 int error = FALSE;
10751#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010752
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010753 rettv->vval.v_string = NULL;
10754 rettv->v_type = VAR_STRING;
10755
10756#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010758
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010759 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10762 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010763 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 else
10765 {
10766 if (*p != NUL)
10767 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010770 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010772 }
10773
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010774 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10775 error = TRUE;
10776
10777 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 do
10780 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010781 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010782 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 fresult = find_file_in_path_option(first ? fname : NULL,
10784 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010785 0, first, path,
10786 find_what,
10787 curbuf->b_ffname,
10788 find_what == FINDFILE_DIR
10789 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010791
10792 if (fresult != NULL && rettv->v_type == VAR_LIST)
10793 list_append_string(rettv->vval.v_list, fresult, -1);
10794
10795 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010797
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010798 if (rettv->v_type == VAR_STRING)
10799 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010800#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010801}
10802
Bram Moolenaar33570922005-01-25 22:26:29 +000010803static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10804static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010805
10806/*
10807 * Implementation of map() and filter().
10808 */
10809 static void
10810filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813 int map;
10814{
10815 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 listitem_T *li, *nli;
10818 list_T *l = NULL;
10819 dictitem_T *di;
10820 hashtab_T *ht;
10821 hashitem_T *hi;
10822 dict_T *d = NULL;
10823 typval_T save_val;
10824 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010825 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010826 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010827 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010828 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010829 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010830 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010831 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010832
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010834 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010835 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010836 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 return;
10838 }
10839 else if (argvars[0].v_type == VAR_DICT)
10840 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010841 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010842 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 return;
10844 }
10845 else
10846 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010847 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 return;
10849 }
10850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 expr = get_tv_string_buf_chk(&argvars[1], buf);
10852 /* On type errors, the preceding call has already displayed an error
10853 * message. Avoid a misleading error message for an empty string that
10854 * was not passed as argument. */
10855 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010857 prepare_vimvar(VV_VAL, &save_val);
10858 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010859
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010860 /* We reset "did_emsg" to be able to detect whether an error
10861 * occurred during evaluation of the expression. */
10862 save_did_emsg = did_emsg;
10863 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010864
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010865 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 vimvars[VV_KEY].vv_type = VAR_STRING;
10869
10870 ht = &d->dv_hashtab;
10871 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010872 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 if (!HASHITEM_EMPTY(hi))
10876 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010877 int r;
10878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 --todo;
10880 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010881 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010882 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10883 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 break;
10885 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010886 r = filter_map_one(&di->di_tv, expr, map, &rem);
10887 clear_tv(&vimvars[VV_KEY].vv_tv);
10888 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889 break;
10890 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010891 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010892 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10893 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010894 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 }
10898 }
10899 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 }
10901 else
10902 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010903 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 for (li = l->lv_first; li != NULL; li = nli)
10906 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010907 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010908 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010910 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010911 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010912 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010913 break;
10914 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010916 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010917 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010918 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010919
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010920 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010922
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010923 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010924 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010925
10926 copy_tv(&argvars[0], rettv);
10927}
10928
10929 static int
10930filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010931 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 char_u *expr;
10933 int map;
10934 int *remp;
10935{
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010938 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939
Bram Moolenaar33570922005-01-25 22:26:29 +000010940 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 s = expr;
10942 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010943 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 if (*s != NUL) /* check for trailing chars after expr */
10945 {
10946 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010947 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010948 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 }
10950 if (map)
10951 {
10952 /* map(): replace the list item value */
10953 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010954 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010955 *tv = rettv;
10956 }
10957 else
10958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 int error = FALSE;
10960
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 /* On type error, nothing has been removed; return FAIL to stop the
10965 * loop. The error message was given by get_tv_number_chk(). */
10966 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010967 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010969 retval = OK;
10970theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010971 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010972 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010973}
10974
10975/*
10976 * "filter()" function
10977 */
10978 static void
10979f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010980 typval_T *argvars;
10981 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010982{
10983 filter_map(argvars, rettv, FALSE);
10984}
10985
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987 * "finddir({fname}[, {path}[, {count}]])" function
10988 */
10989 static void
10990f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010991 typval_T *argvars;
10992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010993{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010994 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010995}
10996
10997/*
10998 * "findfile({fname}[, {path}[, {count}]])" function
10999 */
11000 static void
11001f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011002 typval_T *argvars;
11003 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011004{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011005 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006}
11007
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011008#ifdef FEAT_FLOAT
11009/*
11010 * "float2nr({float})" function
11011 */
11012 static void
11013f_float2nr(argvars, rettv)
11014 typval_T *argvars;
11015 typval_T *rettv;
11016{
11017 float_T f;
11018
11019 if (get_float_arg(argvars, &f) == OK)
11020 {
11021 if (f < -0x7fffffff)
11022 rettv->vval.v_number = -0x7fffffff;
11023 else if (f > 0x7fffffff)
11024 rettv->vval.v_number = 0x7fffffff;
11025 else
11026 rettv->vval.v_number = (varnumber_T)f;
11027 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011028}
11029
11030/*
11031 * "floor({float})" function
11032 */
11033 static void
11034f_floor(argvars, rettv)
11035 typval_T *argvars;
11036 typval_T *rettv;
11037{
11038 float_T f;
11039
11040 rettv->v_type = VAR_FLOAT;
11041 if (get_float_arg(argvars, &f) == OK)
11042 rettv->vval.v_float = floor(f);
11043 else
11044 rettv->vval.v_float = 0.0;
11045}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011046
11047/*
11048 * "fmod()" function
11049 */
11050 static void
11051f_fmod(argvars, rettv)
11052 typval_T *argvars;
11053 typval_T *rettv;
11054{
11055 float_T fx, fy;
11056
11057 rettv->v_type = VAR_FLOAT;
11058 if (get_float_arg(argvars, &fx) == OK
11059 && get_float_arg(&argvars[1], &fy) == OK)
11060 rettv->vval.v_float = fmod(fx, fy);
11061 else
11062 rettv->vval.v_float = 0.0;
11063}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011064#endif
11065
Bram Moolenaar0d660222005-01-07 21:51:51 +000011066/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011067 * "fnameescape({string})" function
11068 */
11069 static void
11070f_fnameescape(argvars, rettv)
11071 typval_T *argvars;
11072 typval_T *rettv;
11073{
11074 rettv->vval.v_string = vim_strsave_fnameescape(
11075 get_tv_string(&argvars[0]), FALSE);
11076 rettv->v_type = VAR_STRING;
11077}
11078
11079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 * "fnamemodify({fname}, {mods})" function
11081 */
11082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011084 typval_T *argvars;
11085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086{
11087 char_u *fname;
11088 char_u *mods;
11089 int usedlen = 0;
11090 int len;
11091 char_u *fbuf = NULL;
11092 char_u buf[NUMBUFLEN];
11093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 fname = get_tv_string_chk(&argvars[0]);
11095 mods = get_tv_string_buf_chk(&argvars[1], buf);
11096 if (fname == NULL || mods == NULL)
11097 fname = NULL;
11098 else
11099 {
11100 len = (int)STRLEN(fname);
11101 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 vim_free(fbuf);
11110}
11111
Bram Moolenaar33570922005-01-25 22:26:29 +000011112static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
11114/*
11115 * "foldclosed()" function
11116 */
11117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011119 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011121 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122{
11123#ifdef FEAT_FOLDING
11124 linenr_T lnum;
11125 linenr_T first, last;
11126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11129 {
11130 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11131 {
11132 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 return;
11137 }
11138 }
11139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141}
11142
11143/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144 * "foldclosed()" function
11145 */
11146 static void
11147f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 typval_T *argvars;
11149 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150{
11151 foldclosed_both(argvars, rettv, FALSE);
11152}
11153
11154/*
11155 * "foldclosedend()" function
11156 */
11157 static void
11158f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011159 typval_T *argvars;
11160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161{
11162 foldclosed_both(argvars, rettv, TRUE);
11163}
11164
11165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 * "foldlevel()" function
11167 */
11168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011170 typval_T *argvars UNUSED;
11171 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172{
11173#ifdef FEAT_FOLDING
11174 linenr_T lnum;
11175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180}
11181
11182/*
11183 * "foldtext()" function
11184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011187 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189{
11190#ifdef FEAT_FOLDING
11191 linenr_T lnum;
11192 char_u *s;
11193 char_u *r;
11194 int len;
11195 char *txt;
11196#endif
11197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198 rettv->v_type = VAR_STRING;
11199 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011201 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11202 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11203 <= curbuf->b_ml.ml_line_count
11204 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 {
11206 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011207 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11208 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 {
11210 if (!linewhite(lnum))
11211 break;
11212 ++lnum;
11213 }
11214
11215 /* Find interesting text in this line. */
11216 s = skipwhite(ml_get(lnum));
11217 /* skip C comment-start */
11218 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011221 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011223 {
11224 s = skipwhite(ml_get(lnum + 1));
11225 if (*s == '*')
11226 s = skipwhite(s + 1);
11227 }
11228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 txt = _("+-%s%3ld lines: ");
11230 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 + 20 /* for %3ld */
11233 + STRLEN(s))); /* concatenated */
11234 if (r != NULL)
11235 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11237 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11238 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 len = (int)STRLEN(r);
11240 STRCAT(r, s);
11241 /* remove 'foldmarker' and 'commentstring' */
11242 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 }
11245 }
11246#endif
11247}
11248
11249/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011250 * "foldtextresult(lnum)" function
11251 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011254 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011255 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011256{
11257#ifdef FEAT_FOLDING
11258 linenr_T lnum;
11259 char_u *text;
11260 char_u buf[51];
11261 foldinfo_T foldinfo;
11262 int fold_count;
11263#endif
11264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->v_type = VAR_STRING;
11266 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011267#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 /* treat illegal types and illegal string values for {lnum} the same */
11270 if (lnum < 0)
11271 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011272 fold_count = foldedCount(curwin, lnum, &foldinfo);
11273 if (fold_count > 0)
11274 {
11275 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11276 &foldinfo, buf);
11277 if (text == buf)
11278 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011280 }
11281#endif
11282}
11283
11284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 * "foreground()" function
11286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011289 typval_T *argvars UNUSED;
11290 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292#ifdef FEAT_GUI
11293 if (gui.in_use)
11294 gui_mch_set_foreground();
11295#else
11296# ifdef WIN32
11297 win32_set_foreground();
11298# endif
11299#endif
11300}
11301
11302/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011303 * "function()" function
11304 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 typval_T *argvars;
11308 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011309{
11310 char_u *s;
11311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011313 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011314 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011315 /* Don't check an autoload name for existence here. */
11316 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011317 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011318 else
11319 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011320 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011321 {
11322 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011323 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011324
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011325 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11326 * also be called from another script. Using trans_function_name()
11327 * would also work, but some plugins depend on the name being
11328 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011329 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011330 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011331 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011332 if (rettv->vval.v_string != NULL)
11333 {
11334 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011335 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011336 }
11337 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011338 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011339 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011341 }
11342}
11343
11344/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011345 * "garbagecollect()" function
11346 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011347 static void
11348f_garbagecollect(argvars, rettv)
11349 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011350 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011351{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011352 /* This is postponed until we are back at the toplevel, because we may be
11353 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11354 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011355
11356 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11357 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011358}
11359
11360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011361 * "get()" function
11362 */
11363 static void
11364f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *argvars;
11366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011367{
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 listitem_T *li;
11369 list_T *l;
11370 dictitem_T *di;
11371 dict_T *d;
11372 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011373
Bram Moolenaare9a41262005-01-15 22:18:47 +000011374 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011376 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 int error = FALSE;
11379
11380 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11381 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011382 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011385 else if (argvars[0].v_type == VAR_DICT)
11386 {
11387 if ((d = argvars[0].vval.v_dict) != NULL)
11388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011389 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011390 if (di != NULL)
11391 tv = &di->di_tv;
11392 }
11393 }
11394 else
11395 EMSG2(_(e_listdictarg), "get()");
11396
11397 if (tv == NULL)
11398 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011399 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011400 copy_tv(&argvars[2], rettv);
11401 }
11402 else
11403 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011404}
11405
Bram Moolenaar342337a2005-07-21 21:11:17 +000011406static 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 +000011407
11408/*
11409 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011410 * Return a range (from start to end) of lines in rettv from the specified
11411 * buffer.
11412 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011413 */
11414 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011415get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011416 buf_T *buf;
11417 linenr_T start;
11418 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011419 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011420 typval_T *rettv;
11421{
11422 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011423
Bram Moolenaar959a1432013-12-14 12:17:38 +010011424 rettv->v_type = VAR_STRING;
11425 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011426 if (retlist && rettv_list_alloc(rettv) == FAIL)
11427 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011428
11429 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11430 return;
11431
11432 if (!retlist)
11433 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011434 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11435 p = ml_get_buf(buf, start, FALSE);
11436 else
11437 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011438 rettv->vval.v_string = vim_strsave(p);
11439 }
11440 else
11441 {
11442 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011443 return;
11444
11445 if (start < 1)
11446 start = 1;
11447 if (end > buf->b_ml.ml_line_count)
11448 end = buf->b_ml.ml_line_count;
11449 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011450 if (list_append_string(rettv->vval.v_list,
11451 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011452 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011453 }
11454}
11455
11456/*
11457 * "getbufline()" function
11458 */
11459 static void
11460f_getbufline(argvars, rettv)
11461 typval_T *argvars;
11462 typval_T *rettv;
11463{
11464 linenr_T lnum;
11465 linenr_T end;
11466 buf_T *buf;
11467
11468 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11469 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011470 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011471 --emsg_off;
11472
Bram Moolenaar661b1822005-07-28 22:36:45 +000011473 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011474 if (argvars[2].v_type == VAR_UNKNOWN)
11475 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011476 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011477 end = get_tv_lnum_buf(&argvars[2], buf);
11478
Bram Moolenaar342337a2005-07-21 21:11:17 +000011479 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011480}
11481
Bram Moolenaar0d660222005-01-07 21:51:51 +000011482/*
11483 * "getbufvar()" function
11484 */
11485 static void
11486f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011487 typval_T *argvars;
11488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489{
11490 buf_T *buf;
11491 buf_T *save_curbuf;
11492 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011494 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11497 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011499 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011501 rettv->v_type = VAR_STRING;
11502 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503
11504 if (buf != NULL && varname != NULL)
11505 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011506 /* set curbuf to be our buf, temporarily */
11507 save_curbuf = curbuf;
11508 curbuf = buf;
11509
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011511 {
11512 if (get_option_tv(&varname, rettv, TRUE) == OK)
11513 done = TRUE;
11514 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011515 else if (STRCMP(varname, "changedtick") == 0)
11516 {
11517 rettv->v_type = VAR_NUMBER;
11518 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011519 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011520 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011521 else
11522 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011523 /* Look up the variable. */
11524 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11525 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11526 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011528 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011529 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011530 done = TRUE;
11531 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011533
11534 /* restore previous notion of curbuf */
11535 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011536 }
11537
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011538 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11539 /* use the default value */
11540 copy_tv(&argvars[2], rettv);
11541
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542 --emsg_off;
11543}
11544
11545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 * "getchar()" function
11547 */
11548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552{
11553 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011556 /* Position the cursor. Needed after a message that ends in a space. */
11557 windgoto(msg_row, msg_col);
11558
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 ++no_mapping;
11560 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011561 for (;;)
11562 {
11563 if (argvars[0].v_type == VAR_UNKNOWN)
11564 /* getchar(): blocking wait. */
11565 n = safe_vgetc();
11566 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11567 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011568 n = vpeekc_any();
11569 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011570 /* illegal argument or getchar(0) and no char avail: return zero */
11571 n = 0;
11572 else
11573 /* getchar(0) and char avail: return char */
11574 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011575
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011576 if (n == K_IGNORE)
11577 continue;
11578 break;
11579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 --no_mapping;
11581 --allow_keys;
11582
Bram Moolenaar219b8702006-11-01 14:32:36 +000011583 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11584 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11585 vimvars[VV_MOUSE_COL].vv_nr = 0;
11586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 if (IS_SPECIAL(n) || mod_mask != 0)
11589 {
11590 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11591 int i = 0;
11592
11593 /* Turn a special key into three bytes, plus modifier. */
11594 if (mod_mask != 0)
11595 {
11596 temp[i++] = K_SPECIAL;
11597 temp[i++] = KS_MODIFIER;
11598 temp[i++] = mod_mask;
11599 }
11600 if (IS_SPECIAL(n))
11601 {
11602 temp[i++] = K_SPECIAL;
11603 temp[i++] = K_SECOND(n);
11604 temp[i++] = K_THIRD(n);
11605 }
11606#ifdef FEAT_MBYTE
11607 else if (has_mbyte)
11608 i += (*mb_char2bytes)(n, temp + i);
11609#endif
11610 else
11611 temp[i++] = n;
11612 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613 rettv->v_type = VAR_STRING;
11614 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011615
11616#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011617 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011618 {
11619 int row = mouse_row;
11620 int col = mouse_col;
11621 win_T *win;
11622 linenr_T lnum;
11623# ifdef FEAT_WINDOWS
11624 win_T *wp;
11625# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011626 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011627
11628 if (row >= 0 && col >= 0)
11629 {
11630 /* Find the window at the mouse coordinates and compute the
11631 * text position. */
11632 win = mouse_find_win(&row, &col);
11633 (void)mouse_comp_pos(win, &row, &col, &lnum);
11634# ifdef FEAT_WINDOWS
11635 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011636 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011637# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011638 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011639 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11640 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11641 }
11642 }
11643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 }
11645}
11646
11647/*
11648 * "getcharmod()" function
11649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011652 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656}
11657
11658/*
11659 * "getcmdline()" function
11660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011663 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->v_type = VAR_STRING;
11667 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668}
11669
11670/*
11671 * "getcmdpos()" function
11672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011675 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679}
11680
11681/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011682 * "getcmdtype()" function
11683 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011684 static void
11685f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011686 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011687 typval_T *rettv;
11688{
11689 rettv->v_type = VAR_STRING;
11690 rettv->vval.v_string = alloc(2);
11691 if (rettv->vval.v_string != NULL)
11692 {
11693 rettv->vval.v_string[0] = get_cmdline_type();
11694 rettv->vval.v_string[1] = NUL;
11695 }
11696}
11697
11698/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011699 * "getcmdwintype()" function
11700 */
11701 static void
11702f_getcmdwintype(argvars, rettv)
11703 typval_T *argvars UNUSED;
11704 typval_T *rettv;
11705{
11706 rettv->v_type = VAR_STRING;
11707 rettv->vval.v_string = NULL;
11708#ifdef FEAT_CMDWIN
11709 rettv->vval.v_string = alloc(2);
11710 if (rettv->vval.v_string != NULL)
11711 {
11712 rettv->vval.v_string[0] = cmdwin_type;
11713 rettv->vval.v_string[1] = NUL;
11714 }
11715#endif
11716}
11717
11718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 * "getcwd()" function
11720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011723 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011726 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011728 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011729 rettv->vval.v_string = NULL;
11730 cwd = alloc(MAXPATHL);
11731 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011733 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11734 {
11735 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011737 if (rettv->vval.v_string != NULL)
11738 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011740 }
11741 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 }
11743}
11744
11745/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011746 * "getfontname()" function
11747 */
11748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011751 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011755#ifdef FEAT_GUI
11756 if (gui.in_use)
11757 {
11758 GuiFont font;
11759 char_u *name = NULL;
11760
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011761 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011762 {
11763 /* Get the "Normal" font. Either the name saved by
11764 * hl_set_font_name() or from the font ID. */
11765 font = gui.norm_font;
11766 name = hl_get_font_name();
11767 }
11768 else
11769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011771 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11772 return;
11773 font = gui_mch_get_font(name, FALSE);
11774 if (font == NOFONT)
11775 return; /* Invalid font name, return empty string. */
11776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011778 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011779 gui_mch_free_font(font);
11780 }
11781#endif
11782}
11783
11784/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011785 * "getfperm({fname})" function
11786 */
11787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011791{
11792 char_u *fname;
11793 struct stat st;
11794 char_u *perm = NULL;
11795 char_u flags[] = "rwx";
11796 int i;
11797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011801 if (mch_stat((char *)fname, &st) >= 0)
11802 {
11803 perm = vim_strsave((char_u *)"---------");
11804 if (perm != NULL)
11805 {
11806 for (i = 0; i < 9; i++)
11807 {
11808 if (st.st_mode & (1 << (8 - i)))
11809 perm[i] = flags[i % 3];
11810 }
11811 }
11812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011814}
11815
11816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 * "getfsize({fname})" function
11818 */
11819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011821 typval_T *argvars;
11822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823{
11824 char_u *fname;
11825 struct stat st;
11826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830
11831 if (mch_stat((char *)fname, &st) >= 0)
11832 {
11833 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011838
11839 /* non-perfect check for overflow */
11840 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11841 rettv->vval.v_number = -2;
11842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 }
11844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846}
11847
11848/*
11849 * "getftime({fname})" function
11850 */
11851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011853 typval_T *argvars;
11854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855{
11856 char_u *fname;
11857 struct stat st;
11858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860
11861 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865}
11866
11867/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011868 * "getftype({fname})" function
11869 */
11870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 typval_T *argvars;
11873 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011874{
11875 char_u *fname;
11876 struct stat st;
11877 char_u *type = NULL;
11878 char *t;
11879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011883 if (mch_lstat((char *)fname, &st) >= 0)
11884 {
11885#ifdef S_ISREG
11886 if (S_ISREG(st.st_mode))
11887 t = "file";
11888 else if (S_ISDIR(st.st_mode))
11889 t = "dir";
11890# ifdef S_ISLNK
11891 else if (S_ISLNK(st.st_mode))
11892 t = "link";
11893# endif
11894# ifdef S_ISBLK
11895 else if (S_ISBLK(st.st_mode))
11896 t = "bdev";
11897# endif
11898# ifdef S_ISCHR
11899 else if (S_ISCHR(st.st_mode))
11900 t = "cdev";
11901# endif
11902# ifdef S_ISFIFO
11903 else if (S_ISFIFO(st.st_mode))
11904 t = "fifo";
11905# endif
11906# ifdef S_ISSOCK
11907 else if (S_ISSOCK(st.st_mode))
11908 t = "fifo";
11909# endif
11910 else
11911 t = "other";
11912#else
11913# ifdef S_IFMT
11914 switch (st.st_mode & S_IFMT)
11915 {
11916 case S_IFREG: t = "file"; break;
11917 case S_IFDIR: t = "dir"; break;
11918# ifdef S_IFLNK
11919 case S_IFLNK: t = "link"; break;
11920# endif
11921# ifdef S_IFBLK
11922 case S_IFBLK: t = "bdev"; break;
11923# endif
11924# ifdef S_IFCHR
11925 case S_IFCHR: t = "cdev"; break;
11926# endif
11927# ifdef S_IFIFO
11928 case S_IFIFO: t = "fifo"; break;
11929# endif
11930# ifdef S_IFSOCK
11931 case S_IFSOCK: t = "socket"; break;
11932# endif
11933 default: t = "other";
11934 }
11935# else
11936 if (mch_isdir(fname))
11937 t = "dir";
11938 else
11939 t = "file";
11940# endif
11941#endif
11942 type = vim_strsave((char_u *)t);
11943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011944 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011945}
11946
11947/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011948 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011949 */
11950 static void
11951f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *argvars;
11953 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011954{
11955 linenr_T lnum;
11956 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011957 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011958
11959 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011960 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011961 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011962 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011963 retlist = FALSE;
11964 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011965 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011966 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011968 retlist = TRUE;
11969 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011970
Bram Moolenaar342337a2005-07-21 21:11:17 +000011971 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011972}
11973
11974/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011975 * "getmatches()" function
11976 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011977 static void
11978f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011979 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011980 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011981{
11982#ifdef FEAT_SEARCH_EXTRA
11983 dict_T *dict;
11984 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011985 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011986
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011987 if (rettv_list_alloc(rettv) == OK)
11988 {
11989 while (cur != NULL)
11990 {
11991 dict = dict_alloc();
11992 if (dict == NULL)
11993 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011994 if (cur->match.regprog == NULL)
11995 {
11996 /* match added with matchaddpos() */
11997 for (i = 0; i < MAXPOSMATCH; ++i)
11998 {
11999 llpos_T *llpos;
12000 char buf[6];
12001 list_T *l;
12002
12003 llpos = &cur->pos.pos[i];
12004 if (llpos->lnum == 0)
12005 break;
12006 l = list_alloc();
12007 if (l == NULL)
12008 break;
12009 list_append_number(l, (varnumber_T)llpos->lnum);
12010 if (llpos->col > 0)
12011 {
12012 list_append_number(l, (varnumber_T)llpos->col);
12013 list_append_number(l, (varnumber_T)llpos->len);
12014 }
12015 sprintf(buf, "pos%d", i + 1);
12016 dict_add_list(dict, buf, l);
12017 }
12018 }
12019 else
12020 {
12021 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12022 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012023 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012024 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12025 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12026 list_append_dict(rettv->vval.v_list, dict);
12027 cur = cur->next;
12028 }
12029 }
12030#endif
12031}
12032
12033/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012034 * "getpid()" function
12035 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012036 static void
12037f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012038 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012039 typval_T *rettv;
12040{
12041 rettv->vval.v_number = mch_get_pid();
12042}
12043
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012044static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12045
12046/*
12047 * "getcurpos()" function
12048 */
12049 static void
12050f_getcurpos(argvars, rettv)
12051 typval_T *argvars;
12052 typval_T *rettv;
12053{
12054 getpos_both(argvars, rettv, TRUE);
12055}
12056
Bram Moolenaar18081e32008-02-20 19:11:07 +000012057/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012058 * "getpos(string)" function
12059 */
12060 static void
12061f_getpos(argvars, rettv)
12062 typval_T *argvars;
12063 typval_T *rettv;
12064{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012065 getpos_both(argvars, rettv, FALSE);
12066}
12067
12068 static void
12069getpos_both(argvars, rettv, getcurpos)
12070 typval_T *argvars;
12071 typval_T *rettv;
12072 int getcurpos;
12073{
Bram Moolenaara5525202006-03-02 22:52:09 +000012074 pos_T *fp;
12075 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012076 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012077
12078 if (rettv_list_alloc(rettv) == OK)
12079 {
12080 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012081 if (getcurpos)
12082 fp = &curwin->w_cursor;
12083 else
12084 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012085 if (fnum != -1)
12086 list_append_number(l, (varnumber_T)fnum);
12087 else
12088 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012089 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12090 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012091 list_append_number(l, (fp != NULL)
12092 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012093 : (varnumber_T)0);
12094 list_append_number(l,
12095#ifdef FEAT_VIRTUALEDIT
12096 (fp != NULL) ? (varnumber_T)fp->coladd :
12097#endif
12098 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012099 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012100 list_append_number(l, curwin->w_curswant == MAXCOL ?
12101 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012102 }
12103 else
12104 rettv->vval.v_number = FALSE;
12105}
12106
12107/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012108 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012109 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012110 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012111f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012112 typval_T *argvars UNUSED;
12113 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012114{
12115#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012116 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012117#endif
12118
Bram Moolenaar2641f772005-03-25 21:58:17 +000012119#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012120 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012121 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012122 wp = NULL;
12123 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12124 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012125 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012126 if (wp == NULL)
12127 return;
12128 }
12129
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012130 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012131 }
12132#endif
12133}
12134
12135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 * "getreg()" function
12137 */
12138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 typval_T *argvars;
12141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142{
12143 char_u *strregname;
12144 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012145 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012146 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012149 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012151 strregname = get_tv_string_chk(&argvars[0]);
12152 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012155 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012156 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12157 return_list = get_tv_number_chk(&argvars[2], &error);
12158 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012161 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012162
12163 if (error)
12164 return;
12165
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 regname = (strregname == NULL ? '"' : *strregname);
12167 if (regname == 0)
12168 regname = '"';
12169
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012170 if (return_list)
12171 {
12172 rettv->v_type = VAR_LIST;
12173 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12174 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012175 if (rettv->vval.v_list != NULL)
12176 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012177 }
12178 else
12179 {
12180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = get_reg_contents(regname,
12182 arg2 ? GREG_EXPR_SRC : 0);
12183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185
12186/*
12187 * "getregtype()" function
12188 */
12189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 typval_T *argvars;
12192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193{
12194 char_u *strregname;
12195 int regname;
12196 char_u buf[NUMBUFLEN + 2];
12197 long reglen = 0;
12198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012199 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012200 {
12201 strregname = get_tv_string_chk(&argvars[0]);
12202 if (strregname == NULL) /* type error; errmsg already given */
12203 {
12204 rettv->v_type = VAR_STRING;
12205 rettv->vval.v_string = NULL;
12206 return;
12207 }
12208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 else
12210 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012211 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212
12213 regname = (strregname == NULL ? '"' : *strregname);
12214 if (regname == 0)
12215 regname = '"';
12216
12217 buf[0] = NUL;
12218 buf[1] = NUL;
12219 switch (get_reg_type(regname, &reglen))
12220 {
12221 case MLINE: buf[0] = 'V'; break;
12222 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 case MBLOCK:
12224 buf[0] = Ctrl_V;
12225 sprintf((char *)buf + 1, "%ld", reglen + 1);
12226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 rettv->v_type = VAR_STRING;
12229 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230}
12231
12232/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012233 * "gettabvar()" function
12234 */
12235 static void
12236f_gettabvar(argvars, rettv)
12237 typval_T *argvars;
12238 typval_T *rettv;
12239{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012240 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012241 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012242 dictitem_T *v;
12243 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012244 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012245
12246 rettv->v_type = VAR_STRING;
12247 rettv->vval.v_string = NULL;
12248
12249 varname = get_tv_string_chk(&argvars[1]);
12250 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12251 if (tp != NULL && varname != NULL)
12252 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012253 /* Set tp to be our tabpage, temporarily. Also set the window to the
12254 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012255 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12256 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012257 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012258 /* look up the variable */
12259 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12260 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12261 if (v != NULL)
12262 {
12263 copy_tv(&v->di_tv, rettv);
12264 done = TRUE;
12265 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012266 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012267
12268 /* restore previous notion of curwin */
12269 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012270 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012271
12272 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012273 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012274}
12275
12276/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012277 * "gettabwinvar()" function
12278 */
12279 static void
12280f_gettabwinvar(argvars, rettv)
12281 typval_T *argvars;
12282 typval_T *rettv;
12283{
12284 getwinvar(argvars, rettv, 1);
12285}
12286
12287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 * "getwinposx()" function
12289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296#ifdef FEAT_GUI
12297 if (gui.in_use)
12298 {
12299 int x, y;
12300
12301 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 }
12304#endif
12305}
12306
12307/*
12308 * "getwinposy()" function
12309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316#ifdef FEAT_GUI
12317 if (gui.in_use)
12318 {
12319 int x, y;
12320
12321 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 }
12324#endif
12325}
12326
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012327/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012328 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012329 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012330 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012331find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012332 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012333 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012334{
12335#ifdef FEAT_WINDOWS
12336 win_T *wp;
12337#endif
12338 int nr;
12339
12340 nr = get_tv_number_chk(vp, NULL);
12341
12342#ifdef FEAT_WINDOWS
12343 if (nr < 0)
12344 return NULL;
12345 if (nr == 0)
12346 return curwin;
12347
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012348 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12349 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012350 if (--nr <= 0)
12351 break;
12352 return wp;
12353#else
12354 if (nr == 0 || nr == 1)
12355 return curwin;
12356 return NULL;
12357#endif
12358}
12359
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360/*
12361 * "getwinvar()" function
12362 */
12363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 typval_T *argvars;
12366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012368 getwinvar(argvars, rettv, 0);
12369}
12370
12371/*
12372 * getwinvar() and gettabwinvar()
12373 */
12374 static void
12375getwinvar(argvars, rettv, off)
12376 typval_T *argvars;
12377 typval_T *rettv;
12378 int off; /* 1 for gettabwinvar() */
12379{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 win_T *win, *oldcurwin;
12381 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012382 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012383 tabpage_T *tp = NULL;
12384 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012385 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012387#ifdef FEAT_WINDOWS
12388 if (off == 1)
12389 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12390 else
12391 tp = curtab;
12392#endif
12393 win = find_win_by_nr(&argvars[off], tp);
12394 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012395 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012397 rettv->v_type = VAR_STRING;
12398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399
12400 if (win != NULL && varname != NULL)
12401 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012402 /* Set curwin to be our win, temporarily. Also set the tabpage,
12403 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012404 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012405 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012406 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012407 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012408 if (get_option_tv(&varname, rettv, 1) == OK)
12409 done = TRUE;
12410 }
12411 else
12412 {
12413 /* Look up the variable. */
12414 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12415 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12416 varname, FALSE);
12417 if (v != NULL)
12418 {
12419 copy_tv(&v->di_tv, rettv);
12420 done = TRUE;
12421 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012424
12425 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012426 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 }
12428
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012429 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12430 /* use the default return value */
12431 copy_tv(&argvars[off + 2], rettv);
12432
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 --emsg_off;
12434}
12435
12436/*
12437 * "glob()" function
12438 */
12439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012441 typval_T *argvars;
12442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012444 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012446 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012448 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012449 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012451 if (argvars[1].v_type != VAR_UNKNOWN)
12452 {
12453 if (get_tv_number_chk(&argvars[1], &error))
12454 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012455 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012456 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012457 if (get_tv_number_chk(&argvars[2], &error))
12458 {
12459 rettv->v_type = VAR_LIST;
12460 rettv->vval.v_list = NULL;
12461 }
12462 if (argvars[3].v_type != VAR_UNKNOWN
12463 && get_tv_number_chk(&argvars[3], &error))
12464 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012465 }
12466 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012467 if (!error)
12468 {
12469 ExpandInit(&xpc);
12470 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012471 if (p_wic)
12472 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012473 if (rettv->v_type == VAR_STRING)
12474 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012475 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012476 else if (rettv_list_alloc(rettv) != FAIL)
12477 {
12478 int i;
12479
12480 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12481 NULL, options, WILD_ALL_KEEP);
12482 for (i = 0; i < xpc.xp_numfiles; i++)
12483 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12484
12485 ExpandCleanup(&xpc);
12486 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012487 }
12488 else
12489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490}
12491
12492/*
12493 * "globpath()" function
12494 */
12495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012497 typval_T *argvars;
12498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012500 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012502 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012503 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012504 garray_T ga;
12505 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012507 /* When the optional second argument is non-zero, don't remove matches
12508 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012510 if (argvars[2].v_type != VAR_UNKNOWN)
12511 {
12512 if (get_tv_number_chk(&argvars[2], &error))
12513 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012514 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012515 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012516 if (get_tv_number_chk(&argvars[3], &error))
12517 {
12518 rettv->v_type = VAR_LIST;
12519 rettv->vval.v_list = NULL;
12520 }
12521 if (argvars[4].v_type != VAR_UNKNOWN
12522 && get_tv_number_chk(&argvars[4], &error))
12523 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012524 }
12525 }
12526 if (file != NULL && !error)
12527 {
12528 ga_init2(&ga, (int)sizeof(char_u *), 10);
12529 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12530 if (rettv->v_type == VAR_STRING)
12531 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12532 else if (rettv_list_alloc(rettv) != FAIL)
12533 for (i = 0; i < ga.ga_len; ++i)
12534 list_append_string(rettv->vval.v_list,
12535 ((char_u **)(ga.ga_data))[i], -1);
12536 ga_clear_strings(&ga);
12537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540}
12541
12542/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012543 * "glob2regpat()" function
12544 */
12545 static void
12546f_glob2regpat(argvars, rettv)
12547 typval_T *argvars;
12548 typval_T *rettv;
12549{
12550 char_u *pat = get_tv_string_chk(&argvars[0]);
12551
12552 rettv->v_type = VAR_STRING;
12553 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12554}
12555
12556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557 * "has()" function
12558 */
12559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012561 typval_T *argvars;
12562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
12564 int i;
12565 char_u *name;
12566 int n = FALSE;
12567 static char *(has_list[]) =
12568 {
12569#ifdef AMIGA
12570 "amiga",
12571# ifdef FEAT_ARP
12572 "arp",
12573# endif
12574#endif
12575#ifdef __BEOS__
12576 "beos",
12577#endif
12578#ifdef MSDOS
12579# ifdef DJGPP
12580 "dos32",
12581# else
12582 "dos16",
12583# endif
12584#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012585#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 "mac",
12587#endif
12588#if defined(MACOS_X_UNIX)
12589 "macunix",
12590#endif
12591#ifdef OS2
12592 "os2",
12593#endif
12594#ifdef __QNX__
12595 "qnx",
12596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597#ifdef UNIX
12598 "unix",
12599#endif
12600#ifdef VMS
12601 "vms",
12602#endif
12603#ifdef WIN16
12604 "win16",
12605#endif
12606#ifdef WIN32
12607 "win32",
12608#endif
12609#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12610 "win32unix",
12611#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012612#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 "win64",
12614#endif
12615#ifdef EBCDIC
12616 "ebcdic",
12617#endif
12618#ifndef CASE_INSENSITIVE_FILENAME
12619 "fname_case",
12620#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012621#ifdef HAVE_ACL
12622 "acl",
12623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624#ifdef FEAT_ARABIC
12625 "arabic",
12626#endif
12627#ifdef FEAT_AUTOCMD
12628 "autocmd",
12629#endif
12630#ifdef FEAT_BEVAL
12631 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012632# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12633 "balloon_multiline",
12634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635#endif
12636#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12637 "builtin_terms",
12638# ifdef ALL_BUILTIN_TCAPS
12639 "all_builtin_terms",
12640# endif
12641#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012642#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12643 || defined(FEAT_GUI_W32) \
12644 || defined(FEAT_GUI_MOTIF))
12645 "browsefilter",
12646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647#ifdef FEAT_BYTEOFF
12648 "byte_offset",
12649#endif
12650#ifdef FEAT_CINDENT
12651 "cindent",
12652#endif
12653#ifdef FEAT_CLIENTSERVER
12654 "clientserver",
12655#endif
12656#ifdef FEAT_CLIPBOARD
12657 "clipboard",
12658#endif
12659#ifdef FEAT_CMDL_COMPL
12660 "cmdline_compl",
12661#endif
12662#ifdef FEAT_CMDHIST
12663 "cmdline_hist",
12664#endif
12665#ifdef FEAT_COMMENTS
12666 "comments",
12667#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012668#ifdef FEAT_CONCEAL
12669 "conceal",
12670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671#ifdef FEAT_CRYPT
12672 "cryptv",
12673#endif
12674#ifdef FEAT_CSCOPE
12675 "cscope",
12676#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012677#ifdef FEAT_CURSORBIND
12678 "cursorbind",
12679#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012680#ifdef CURSOR_SHAPE
12681 "cursorshape",
12682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683#ifdef DEBUG
12684 "debug",
12685#endif
12686#ifdef FEAT_CON_DIALOG
12687 "dialog_con",
12688#endif
12689#ifdef FEAT_GUI_DIALOG
12690 "dialog_gui",
12691#endif
12692#ifdef FEAT_DIFF
12693 "diff",
12694#endif
12695#ifdef FEAT_DIGRAPHS
12696 "digraphs",
12697#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012698#ifdef FEAT_DIRECTX
12699 "directx",
12700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701#ifdef FEAT_DND
12702 "dnd",
12703#endif
12704#ifdef FEAT_EMACS_TAGS
12705 "emacs_tags",
12706#endif
12707 "eval", /* always present, of course! */
12708#ifdef FEAT_EX_EXTRA
12709 "ex_extra",
12710#endif
12711#ifdef FEAT_SEARCH_EXTRA
12712 "extra_search",
12713#endif
12714#ifdef FEAT_FKMAP
12715 "farsi",
12716#endif
12717#ifdef FEAT_SEARCHPATH
12718 "file_in_path",
12719#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012720#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012721 "filterpipe",
12722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723#ifdef FEAT_FIND_ID
12724 "find_in_path",
12725#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012726#ifdef FEAT_FLOAT
12727 "float",
12728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729#ifdef FEAT_FOLDING
12730 "folding",
12731#endif
12732#ifdef FEAT_FOOTER
12733 "footer",
12734#endif
12735#if !defined(USE_SYSTEM) && defined(UNIX)
12736 "fork",
12737#endif
12738#ifdef FEAT_GETTEXT
12739 "gettext",
12740#endif
12741#ifdef FEAT_GUI
12742 "gui",
12743#endif
12744#ifdef FEAT_GUI_ATHENA
12745# ifdef FEAT_GUI_NEXTAW
12746 "gui_neXtaw",
12747# else
12748 "gui_athena",
12749# endif
12750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751#ifdef FEAT_GUI_GTK
12752 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012755#ifdef FEAT_GUI_GNOME
12756 "gui_gnome",
12757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758#ifdef FEAT_GUI_MAC
12759 "gui_mac",
12760#endif
12761#ifdef FEAT_GUI_MOTIF
12762 "gui_motif",
12763#endif
12764#ifdef FEAT_GUI_PHOTON
12765 "gui_photon",
12766#endif
12767#ifdef FEAT_GUI_W16
12768 "gui_win16",
12769#endif
12770#ifdef FEAT_GUI_W32
12771 "gui_win32",
12772#endif
12773#ifdef FEAT_HANGULIN
12774 "hangul_input",
12775#endif
12776#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12777 "iconv",
12778#endif
12779#ifdef FEAT_INS_EXPAND
12780 "insert_expand",
12781#endif
12782#ifdef FEAT_JUMPLIST
12783 "jumplist",
12784#endif
12785#ifdef FEAT_KEYMAP
12786 "keymap",
12787#endif
12788#ifdef FEAT_LANGMAP
12789 "langmap",
12790#endif
12791#ifdef FEAT_LIBCALL
12792 "libcall",
12793#endif
12794#ifdef FEAT_LINEBREAK
12795 "linebreak",
12796#endif
12797#ifdef FEAT_LISP
12798 "lispindent",
12799#endif
12800#ifdef FEAT_LISTCMDS
12801 "listcmds",
12802#endif
12803#ifdef FEAT_LOCALMAP
12804 "localmap",
12805#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012806#ifdef FEAT_LUA
12807# ifndef DYNAMIC_LUA
12808 "lua",
12809# endif
12810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811#ifdef FEAT_MENU
12812 "menu",
12813#endif
12814#ifdef FEAT_SESSION
12815 "mksession",
12816#endif
12817#ifdef FEAT_MODIFY_FNAME
12818 "modify_fname",
12819#endif
12820#ifdef FEAT_MOUSE
12821 "mouse",
12822#endif
12823#ifdef FEAT_MOUSESHAPE
12824 "mouseshape",
12825#endif
12826#if defined(UNIX) || defined(VMS)
12827# ifdef FEAT_MOUSE_DEC
12828 "mouse_dec",
12829# endif
12830# ifdef FEAT_MOUSE_GPM
12831 "mouse_gpm",
12832# endif
12833# ifdef FEAT_MOUSE_JSB
12834 "mouse_jsbterm",
12835# endif
12836# ifdef FEAT_MOUSE_NET
12837 "mouse_netterm",
12838# endif
12839# ifdef FEAT_MOUSE_PTERM
12840 "mouse_pterm",
12841# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012842# ifdef FEAT_MOUSE_SGR
12843 "mouse_sgr",
12844# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012845# ifdef FEAT_SYSMOUSE
12846 "mouse_sysmouse",
12847# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012848# ifdef FEAT_MOUSE_URXVT
12849 "mouse_urxvt",
12850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851# ifdef FEAT_MOUSE_XTERM
12852 "mouse_xterm",
12853# endif
12854#endif
12855#ifdef FEAT_MBYTE
12856 "multi_byte",
12857#endif
12858#ifdef FEAT_MBYTE_IME
12859 "multi_byte_ime",
12860#endif
12861#ifdef FEAT_MULTI_LANG
12862 "multi_lang",
12863#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012864#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012865#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012866 "mzscheme",
12867#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869#ifdef FEAT_OLE
12870 "ole",
12871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#ifdef FEAT_PATH_EXTRA
12873 "path_extra",
12874#endif
12875#ifdef FEAT_PERL
12876#ifndef DYNAMIC_PERL
12877 "perl",
12878#endif
12879#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012880#ifdef FEAT_PERSISTENT_UNDO
12881 "persistent_undo",
12882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883#ifdef FEAT_PYTHON
12884#ifndef DYNAMIC_PYTHON
12885 "python",
12886#endif
12887#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012888#ifdef FEAT_PYTHON3
12889#ifndef DYNAMIC_PYTHON3
12890 "python3",
12891#endif
12892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893#ifdef FEAT_POSTSCRIPT
12894 "postscript",
12895#endif
12896#ifdef FEAT_PRINTER
12897 "printer",
12898#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012899#ifdef FEAT_PROFILE
12900 "profile",
12901#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012902#ifdef FEAT_RELTIME
12903 "reltime",
12904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905#ifdef FEAT_QUICKFIX
12906 "quickfix",
12907#endif
12908#ifdef FEAT_RIGHTLEFT
12909 "rightleft",
12910#endif
12911#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12912 "ruby",
12913#endif
12914#ifdef FEAT_SCROLLBIND
12915 "scrollbind",
12916#endif
12917#ifdef FEAT_CMDL_INFO
12918 "showcmd",
12919 "cmdline_info",
12920#endif
12921#ifdef FEAT_SIGNS
12922 "signs",
12923#endif
12924#ifdef FEAT_SMARTINDENT
12925 "smartindent",
12926#endif
12927#ifdef FEAT_SNIFF
12928 "sniff",
12929#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012930#ifdef STARTUPTIME
12931 "startuptime",
12932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933#ifdef FEAT_STL_OPT
12934 "statusline",
12935#endif
12936#ifdef FEAT_SUN_WORKSHOP
12937 "sun_workshop",
12938#endif
12939#ifdef FEAT_NETBEANS_INTG
12940 "netbeans_intg",
12941#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012942#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012943 "spell",
12944#endif
12945#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 "syntax",
12947#endif
12948#if defined(USE_SYSTEM) || !defined(UNIX)
12949 "system",
12950#endif
12951#ifdef FEAT_TAG_BINS
12952 "tag_binary",
12953#endif
12954#ifdef FEAT_TAG_OLDSTATIC
12955 "tag_old_static",
12956#endif
12957#ifdef FEAT_TAG_ANYWHITE
12958 "tag_any_white",
12959#endif
12960#ifdef FEAT_TCL
12961# ifndef DYNAMIC_TCL
12962 "tcl",
12963# endif
12964#endif
12965#ifdef TERMINFO
12966 "terminfo",
12967#endif
12968#ifdef FEAT_TERMRESPONSE
12969 "termresponse",
12970#endif
12971#ifdef FEAT_TEXTOBJ
12972 "textobjects",
12973#endif
12974#ifdef HAVE_TGETENT
12975 "tgetent",
12976#endif
12977#ifdef FEAT_TITLE
12978 "title",
12979#endif
12980#ifdef FEAT_TOOLBAR
12981 "toolbar",
12982#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012983#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12984 "unnamedplus",
12985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986#ifdef FEAT_USR_CMDS
12987 "user-commands", /* was accidentally included in 5.4 */
12988 "user_commands",
12989#endif
12990#ifdef FEAT_VIMINFO
12991 "viminfo",
12992#endif
12993#ifdef FEAT_VERTSPLIT
12994 "vertsplit",
12995#endif
12996#ifdef FEAT_VIRTUALEDIT
12997 "virtualedit",
12998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000#ifdef FEAT_VISUALEXTRA
13001 "visualextra",
13002#endif
13003#ifdef FEAT_VREPLACE
13004 "vreplace",
13005#endif
13006#ifdef FEAT_WILDIGN
13007 "wildignore",
13008#endif
13009#ifdef FEAT_WILDMENU
13010 "wildmenu",
13011#endif
13012#ifdef FEAT_WINDOWS
13013 "windows",
13014#endif
13015#ifdef FEAT_WAK
13016 "winaltkeys",
13017#endif
13018#ifdef FEAT_WRITEBACKUP
13019 "writebackup",
13020#endif
13021#ifdef FEAT_XIM
13022 "xim",
13023#endif
13024#ifdef FEAT_XFONTSET
13025 "xfontset",
13026#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013027#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013028 "xpm",
13029 "xpm_w32", /* for backward compatibility */
13030#else
13031# if defined(HAVE_XPM)
13032 "xpm",
13033# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035#ifdef USE_XSMP
13036 "xsmp",
13037#endif
13038#ifdef USE_XSMP_INTERACT
13039 "xsmp_interact",
13040#endif
13041#ifdef FEAT_XCLIPBOARD
13042 "xterm_clipboard",
13043#endif
13044#ifdef FEAT_XTERM_SAVE
13045 "xterm_save",
13046#endif
13047#if defined(UNIX) && defined(FEAT_X11)
13048 "X11",
13049#endif
13050 NULL
13051 };
13052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 for (i = 0; has_list[i] != NULL; ++i)
13055 if (STRICMP(name, has_list[i]) == 0)
13056 {
13057 n = TRUE;
13058 break;
13059 }
13060
13061 if (n == FALSE)
13062 {
13063 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013064 {
13065 if (name[5] == '-'
13066 && STRLEN(name) > 11
13067 && vim_isdigit(name[6])
13068 && vim_isdigit(name[8])
13069 && vim_isdigit(name[10]))
13070 {
13071 int major = atoi((char *)name + 6);
13072 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013073
13074 /* Expect "patch-9.9.01234". */
13075 n = (major < VIM_VERSION_MAJOR
13076 || (major == VIM_VERSION_MAJOR
13077 && (minor < VIM_VERSION_MINOR
13078 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013079 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013080 }
13081 else
13082 n = has_patch(atoi((char *)name + 5));
13083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 else if (STRICMP(name, "vim_starting") == 0)
13085 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013086#ifdef FEAT_MBYTE
13087 else if (STRICMP(name, "multi_byte_encoding") == 0)
13088 n = has_mbyte;
13089#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013090#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13091 else if (STRICMP(name, "balloon_multiline") == 0)
13092 n = multiline_balloon_available();
13093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094#ifdef DYNAMIC_TCL
13095 else if (STRICMP(name, "tcl") == 0)
13096 n = tcl_enabled(FALSE);
13097#endif
13098#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13099 else if (STRICMP(name, "iconv") == 0)
13100 n = iconv_enabled(FALSE);
13101#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013102#ifdef DYNAMIC_LUA
13103 else if (STRICMP(name, "lua") == 0)
13104 n = lua_enabled(FALSE);
13105#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013106#ifdef DYNAMIC_MZSCHEME
13107 else if (STRICMP(name, "mzscheme") == 0)
13108 n = mzscheme_enabled(FALSE);
13109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110#ifdef DYNAMIC_RUBY
13111 else if (STRICMP(name, "ruby") == 0)
13112 n = ruby_enabled(FALSE);
13113#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013114#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115#ifdef DYNAMIC_PYTHON
13116 else if (STRICMP(name, "python") == 0)
13117 n = python_enabled(FALSE);
13118#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013119#endif
13120#ifdef FEAT_PYTHON3
13121#ifdef DYNAMIC_PYTHON3
13122 else if (STRICMP(name, "python3") == 0)
13123 n = python3_enabled(FALSE);
13124#endif
13125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126#ifdef DYNAMIC_PERL
13127 else if (STRICMP(name, "perl") == 0)
13128 n = perl_enabled(FALSE);
13129#endif
13130#ifdef FEAT_GUI
13131 else if (STRICMP(name, "gui_running") == 0)
13132 n = (gui.in_use || gui.starting);
13133# ifdef FEAT_GUI_W32
13134 else if (STRICMP(name, "gui_win32s") == 0)
13135 n = gui_is_win32s();
13136# endif
13137# ifdef FEAT_BROWSE
13138 else if (STRICMP(name, "browse") == 0)
13139 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13140# endif
13141#endif
13142#ifdef FEAT_SYN_HL
13143 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013144 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145#endif
13146#if defined(WIN3264)
13147 else if (STRICMP(name, "win95") == 0)
13148 n = mch_windows95();
13149#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013150#ifdef FEAT_NETBEANS_INTG
13151 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013152 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 }
13155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157}
13158
13159/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013160 * "has_key()" function
13161 */
13162 static void
13163f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013164 typval_T *argvars;
13165 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013166{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013167 if (argvars[0].v_type != VAR_DICT)
13168 {
13169 EMSG(_(e_dictreq));
13170 return;
13171 }
13172 if (argvars[0].vval.v_dict == NULL)
13173 return;
13174
13175 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013176 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013177}
13178
13179/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013180 * "haslocaldir()" function
13181 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013182 static void
13183f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013184 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013185 typval_T *rettv;
13186{
13187 rettv->vval.v_number = (curwin->w_localdir != NULL);
13188}
13189
13190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 * "hasmapto()" function
13192 */
13193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 typval_T *argvars;
13196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197{
13198 char_u *name;
13199 char_u *mode;
13200 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013201 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 mode = (char_u *)"nvo";
13206 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013209 if (argvars[2].v_type != VAR_UNKNOWN)
13210 abbr = get_tv_number(&argvars[2]);
13211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
Bram Moolenaar2c932302006-03-18 21:42:09 +000013213 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217}
13218
13219/*
13220 * "histadd()" function
13221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013224 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226{
13227#ifdef FEAT_CMDHIST
13228 int histype;
13229 char_u *str;
13230 char_u buf[NUMBUFLEN];
13231#endif
13232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 if (check_restricted() || check_secure())
13235 return;
13236#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013237 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13238 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 if (histype >= 0)
13240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 if (*str != NUL)
13243 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013244 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 return;
13248 }
13249 }
13250#endif
13251}
13252
13253/*
13254 * "histdel()" function
13255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013258 typval_T *argvars UNUSED;
13259 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260{
13261#ifdef FEAT_CMDHIST
13262 int n;
13263 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013264 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13267 if (str == NULL)
13268 n = 0;
13269 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013272 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013274 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013275 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 else
13277 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013278 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 get_tv_string_buf(&argvars[1], buf));
13280 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281#endif
13282}
13283
13284/*
13285 * "histget()" function
13286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291{
13292#ifdef FEAT_CMDHIST
13293 int type;
13294 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13298 if (str == NULL)
13299 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013301 {
13302 type = get_histtype(str);
13303 if (argvars[1].v_type == VAR_UNKNOWN)
13304 idx = get_history_idx(type);
13305 else
13306 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13307 /* -1 on type error */
13308 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314}
13315
13316/*
13317 * "histnr()" function
13318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323{
13324 int i;
13325
13326#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 char_u *history = get_tv_string_chk(&argvars[0]);
13328
13329 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 if (i >= HIST_CMD && i < HIST_COUNT)
13331 i = get_history_idx(i);
13332 else
13333#endif
13334 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336}
13337
13338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 * "highlightID(name)" function
13340 */
13341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013343 typval_T *argvars;
13344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347}
13348
13349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013350 * "highlight_exists()" function
13351 */
13352 static void
13353f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *argvars;
13355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013356{
13357 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13358}
13359
13360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 * "hostname()" function
13362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013365 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367{
13368 char_u hostname[256];
13369
13370 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->v_type = VAR_STRING;
13372 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373}
13374
13375/*
13376 * iconv() function
13377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013380 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382{
13383#ifdef FEAT_MBYTE
13384 char_u buf1[NUMBUFLEN];
13385 char_u buf2[NUMBUFLEN];
13386 char_u *from, *to, *str;
13387 vimconv_T vimconv;
13388#endif
13389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->v_type = VAR_STRING;
13391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392
13393#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013394 str = get_tv_string(&argvars[0]);
13395 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13396 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 vimconv.vc_type = CONV_NONE;
13398 convert_setup(&vimconv, from, to);
13399
13400 /* If the encodings are equal, no conversion needed. */
13401 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013402 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405
13406 convert_setup(&vimconv, NULL, NULL);
13407 vim_free(from);
13408 vim_free(to);
13409#endif
13410}
13411
13412/*
13413 * "indent()" function
13414 */
13415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419{
13420 linenr_T lnum;
13421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427}
13428
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013429/*
13430 * "index()" function
13431 */
13432 static void
13433f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 typval_T *argvars;
13435 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013436{
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 list_T *l;
13438 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013439 long idx = 0;
13440 int ic = FALSE;
13441
13442 rettv->vval.v_number = -1;
13443 if (argvars[0].v_type != VAR_LIST)
13444 {
13445 EMSG(_(e_listreq));
13446 return;
13447 }
13448 l = argvars[0].vval.v_list;
13449 if (l != NULL)
13450 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013451 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013454 int error = FALSE;
13455
Bram Moolenaar758711c2005-02-02 23:11:38 +000013456 /* Start at specified item. Use the cached index that list_find()
13457 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013459 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013460 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 ic = get_tv_number_chk(&argvars[3], &error);
13462 if (error)
13463 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013465
Bram Moolenaar758711c2005-02-02 23:11:38 +000013466 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013467 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013468 {
13469 rettv->vval.v_number = idx;
13470 break;
13471 }
13472 }
13473}
13474
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475static int inputsecret_flag = 0;
13476
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013477static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13478
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013480 * This function is used by f_input() and f_inputdialog() functions. The third
13481 * argument to f_input() specifies the type of completion to use at the
13482 * prompt. The third argument to f_inputdialog() specifies the value to return
13483 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 */
13485 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013486get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 typval_T *argvars;
13488 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013489 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 char_u *p = NULL;
13493 int c;
13494 char_u buf[NUMBUFLEN];
13495 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013497 int xp_type = EXPAND_NOTHING;
13498 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013500 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502
13503#ifdef NO_CONSOLE_INPUT
13504 /* While starting up, there is no place to enter text. */
13505 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507#endif
13508
13509 cmd_silent = FALSE; /* Want to see the prompt. */
13510 if (prompt != NULL)
13511 {
13512 /* Only the part of the message after the last NL is considered as
13513 * prompt for the command line */
13514 p = vim_strrchr(prompt, '\n');
13515 if (p == NULL)
13516 p = prompt;
13517 else
13518 {
13519 ++p;
13520 c = *p;
13521 *p = NUL;
13522 msg_start();
13523 msg_clr_eos();
13524 msg_puts_attr(prompt, echo_attr);
13525 msg_didout = FALSE;
13526 msg_starthere();
13527 *p = c;
13528 }
13529 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 if (argvars[1].v_type != VAR_UNKNOWN)
13532 {
13533 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13534 if (defstr != NULL)
13535 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013537 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013538 {
13539 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013540 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013541 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013542
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013543 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013544 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013545
Bram Moolenaar4463f292005-09-25 22:20:24 +000013546 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13547 if (xp_name == NULL)
13548 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013550 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013551
Bram Moolenaar4463f292005-09-25 22:20:24 +000013552 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13553 &xp_arg) == FAIL)
13554 return;
13555 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013556 }
13557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013558 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013559 {
13560# ifdef FEAT_EX_EXTRA
13561 int save_ex_normal_busy = ex_normal_busy;
13562 ex_normal_busy = 0;
13563# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013565 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13566 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013567# ifdef FEAT_EX_EXTRA
13568 ex_normal_busy = save_ex_normal_busy;
13569# endif
13570 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013571 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013572 && argvars[1].v_type != VAR_UNKNOWN
13573 && argvars[2].v_type != VAR_UNKNOWN)
13574 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13575 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013576
13577 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 /* since the user typed this, no need to wait for return */
13580 need_wait_return = FALSE;
13581 msg_didout = FALSE;
13582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 cmd_silent = cmd_silent_save;
13584}
13585
13586/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013587 * "input()" function
13588 * Also handles inputsecret() when inputsecret is set.
13589 */
13590 static void
13591f_input(argvars, rettv)
13592 typval_T *argvars;
13593 typval_T *rettv;
13594{
13595 get_user_input(argvars, rettv, FALSE);
13596}
13597
13598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 * "inputdialog()" function
13600 */
13601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *argvars;
13604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605{
13606#if defined(FEAT_GUI_TEXTDIALOG)
13607 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13608 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13609 {
13610 char_u *message;
13611 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 message = get_tv_string_chk(&argvars[0]);
13615 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013616 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013617 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 else
13619 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013620 if (message != NULL && defstr != NULL
13621 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013622 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 else
13625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 if (message != NULL && defstr != NULL
13627 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013628 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->vval.v_string = vim_strsave(
13630 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 }
13636 else
13637#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013638 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639}
13640
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013641/*
13642 * "inputlist()" function
13643 */
13644 static void
13645f_inputlist(argvars, rettv)
13646 typval_T *argvars;
13647 typval_T *rettv;
13648{
13649 listitem_T *li;
13650 int selected;
13651 int mouse_used;
13652
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013653#ifdef NO_CONSOLE_INPUT
13654 /* While starting up, there is no place to enter text. */
13655 if (no_console_input())
13656 return;
13657#endif
13658 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13659 {
13660 EMSG2(_(e_listarg), "inputlist()");
13661 return;
13662 }
13663
13664 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013665 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013666 lines_left = Rows; /* avoid more prompt */
13667 msg_scroll = TRUE;
13668 msg_clr_eos();
13669
13670 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13671 {
13672 msg_puts(get_tv_string(&li->li_tv));
13673 msg_putchar('\n');
13674 }
13675
13676 /* Ask for choice. */
13677 selected = prompt_for_number(&mouse_used);
13678 if (mouse_used)
13679 selected -= lines_left;
13680
13681 rettv->vval.v_number = selected;
13682}
13683
13684
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13686
13687/*
13688 * "inputrestore()" function
13689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694{
13695 if (ga_userinput.ga_len > 0)
13696 {
13697 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13699 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013700 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 }
13702 else if (p_verbose > 1)
13703 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013704 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 }
13707}
13708
13709/*
13710 * "inputsave()" function
13711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013714 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013717 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 if (ga_grow(&ga_userinput, 1) == OK)
13719 {
13720 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13721 + ga_userinput.ga_len);
13722 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013723 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 }
13725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727}
13728
13729/*
13730 * "inputsecret()" function
13731 */
13732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736{
13737 ++cmdline_star;
13738 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 --cmdline_star;
13741 --inputsecret_flag;
13742}
13743
13744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013745 * "insert()" function
13746 */
13747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013749 typval_T *argvars;
13750 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013751{
13752 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 listitem_T *item;
13754 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013756
13757 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013759 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013760 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013761 {
13762 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 before = get_tv_number_chk(&argvars[2], &error);
13764 if (error)
13765 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013766
Bram Moolenaar758711c2005-02-02 23:11:38 +000013767 if (before == l->lv_len)
13768 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013769 else
13770 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013771 item = list_find(l, before);
13772 if (item == NULL)
13773 {
13774 EMSGN(_(e_listidx), before);
13775 l = NULL;
13776 }
13777 }
13778 if (l != NULL)
13779 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013780 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013781 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013782 }
13783 }
13784}
13785
13786/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013787 * "invert(expr)" function
13788 */
13789 static void
13790f_invert(argvars, rettv)
13791 typval_T *argvars;
13792 typval_T *rettv;
13793{
13794 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13795}
13796
13797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 * "isdirectory()" function
13799 */
13800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013802 typval_T *argvars;
13803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806}
13807
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013808/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013809 * "islocked()" function
13810 */
13811 static void
13812f_islocked(argvars, rettv)
13813 typval_T *argvars;
13814 typval_T *rettv;
13815{
13816 lval_T lv;
13817 char_u *end;
13818 dictitem_T *di;
13819
13820 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013821 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13822 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013823 if (end != NULL && lv.ll_name != NULL)
13824 {
13825 if (*end != NUL)
13826 EMSG(_(e_trailing));
13827 else
13828 {
13829 if (lv.ll_tv == NULL)
13830 {
13831 if (check_changedtick(lv.ll_name))
13832 rettv->vval.v_number = 1; /* always locked */
13833 else
13834 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013835 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013836 if (di != NULL)
13837 {
13838 /* Consider a variable locked when:
13839 * 1. the variable itself is locked
13840 * 2. the value of the variable is locked.
13841 * 3. the List or Dict value is locked.
13842 */
13843 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13844 || tv_islocked(&di->di_tv));
13845 }
13846 }
13847 }
13848 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013849 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013850 else if (lv.ll_newkey != NULL)
13851 EMSG2(_(e_dictkey), lv.ll_newkey);
13852 else if (lv.ll_list != NULL)
13853 /* List item. */
13854 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13855 else
13856 /* Dictionary item. */
13857 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13858 }
13859 }
13860
13861 clear_lval(&lv);
13862}
13863
Bram Moolenaar33570922005-01-25 22:26:29 +000013864static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013865
13866/*
13867 * Turn a dict into a list:
13868 * "what" == 0: list of keys
13869 * "what" == 1: list of values
13870 * "what" == 2: list of items
13871 */
13872 static void
13873dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013874 typval_T *argvars;
13875 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013876 int what;
13877{
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 list_T *l2;
13879 dictitem_T *di;
13880 hashitem_T *hi;
13881 listitem_T *li;
13882 listitem_T *li2;
13883 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013884 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013885
Bram Moolenaar8c711452005-01-14 21:53:12 +000013886 if (argvars[0].v_type != VAR_DICT)
13887 {
13888 EMSG(_(e_dictreq));
13889 return;
13890 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013891 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013892 return;
13893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013894 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013895 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013896
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013897 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013898 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013899 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013900 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013901 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013902 --todo;
13903 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013905 li = listitem_alloc();
13906 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013907 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013908 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013909
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013910 if (what == 0)
13911 {
13912 /* keys() */
13913 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013914 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013915 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13916 }
13917 else if (what == 1)
13918 {
13919 /* values() */
13920 copy_tv(&di->di_tv, &li->li_tv);
13921 }
13922 else
13923 {
13924 /* items() */
13925 l2 = list_alloc();
13926 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013927 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013928 li->li_tv.vval.v_list = l2;
13929 if (l2 == NULL)
13930 break;
13931 ++l2->lv_refcount;
13932
13933 li2 = listitem_alloc();
13934 if (li2 == NULL)
13935 break;
13936 list_append(l2, li2);
13937 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013938 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013939 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13940
13941 li2 = listitem_alloc();
13942 if (li2 == NULL)
13943 break;
13944 list_append(l2, li2);
13945 copy_tv(&di->di_tv, &li2->li_tv);
13946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013947 }
13948 }
13949}
13950
13951/*
13952 * "items(dict)" function
13953 */
13954 static void
13955f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013956 typval_T *argvars;
13957 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013958{
13959 dict_list(argvars, rettv, 2);
13960}
13961
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013963 * "join()" function
13964 */
13965 static void
13966f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *argvars;
13968 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013969{
13970 garray_T ga;
13971 char_u *sep;
13972
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013973 if (argvars[0].v_type != VAR_LIST)
13974 {
13975 EMSG(_(e_listreq));
13976 return;
13977 }
13978 if (argvars[0].vval.v_list == NULL)
13979 return;
13980 if (argvars[1].v_type == VAR_UNKNOWN)
13981 sep = (char_u *)" ";
13982 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013983 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013984
13985 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986
13987 if (sep != NULL)
13988 {
13989 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013990 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 ga_append(&ga, NUL);
13992 rettv->vval.v_string = (char_u *)ga.ga_data;
13993 }
13994 else
13995 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013996}
13997
13998/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 * "keys()" function
14000 */
14001 static void
14002f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014003 typval_T *argvars;
14004 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014005{
14006 dict_list(argvars, rettv, 0);
14007}
14008
14009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 * "last_buffer_nr()" function.
14011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014014 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016{
14017 int n = 0;
14018 buf_T *buf;
14019
14020 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14021 if (n < buf->b_fnum)
14022 n = buf->b_fnum;
14023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014024 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014025}
14026
14027/*
14028 * "len()" function
14029 */
14030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014031f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014032 typval_T *argvars;
14033 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014034{
14035 switch (argvars[0].v_type)
14036 {
14037 case VAR_STRING:
14038 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039 rettv->vval.v_number = (varnumber_T)STRLEN(
14040 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014041 break;
14042 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014044 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014045 case VAR_DICT:
14046 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14047 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014048 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014049 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014050 break;
14051 }
14052}
14053
Bram Moolenaar33570922005-01-25 22:26:29 +000014054static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014055
14056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *argvars;
14059 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014060 int type;
14061{
14062#ifdef FEAT_LIBCALL
14063 char_u *string_in;
14064 char_u **string_result;
14065 int nr_result;
14066#endif
14067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014069 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014071
14072 if (check_restricted() || check_secure())
14073 return;
14074
14075#ifdef FEAT_LIBCALL
14076 /* The first two args must be strings, otherwise its meaningless */
14077 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14078 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014079 string_in = NULL;
14080 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014081 string_in = argvars[2].vval.v_string;
14082 if (type == VAR_NUMBER)
14083 string_result = NULL;
14084 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014086 if (mch_libcall(argvars[0].vval.v_string,
14087 argvars[1].vval.v_string,
14088 string_in,
14089 argvars[2].vval.v_number,
14090 string_result,
14091 &nr_result) == OK
14092 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014093 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014094 }
14095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096}
14097
14098/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014099 * "libcall()" function
14100 */
14101 static void
14102f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 typval_T *argvars;
14104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105{
14106 libcall_common(argvars, rettv, VAR_STRING);
14107}
14108
14109/*
14110 * "libcallnr()" function
14111 */
14112 static void
14113f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014114 typval_T *argvars;
14115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116{
14117 libcall_common(argvars, rettv, VAR_NUMBER);
14118}
14119
14120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 * "line(string)" function
14122 */
14123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 typval_T *argvars;
14126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127{
14128 linenr_T lnum = 0;
14129 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014130 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014132 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 if (fp != NULL)
14134 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136}
14137
14138/*
14139 * "line2byte(lnum)" function
14140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145{
14146#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014147 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148#else
14149 linenr_T lnum;
14150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14156 if (rettv->vval.v_number >= 0)
14157 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158#endif
14159}
14160
14161/*
14162 * "lispindent(lnum)" function
14163 */
14164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014165f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168{
14169#ifdef FEAT_LISP
14170 pos_T pos;
14171 linenr_T lnum;
14172
14173 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14176 {
14177 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 curwin->w_cursor = pos;
14180 }
14181 else
14182#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184}
14185
14186/*
14187 * "localtime()" function
14188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195}
14196
Bram Moolenaar33570922005-01-25 22:26:29 +000014197static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198
14199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 typval_T *argvars;
14202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 int exact;
14204{
14205 char_u *keys;
14206 char_u *which;
14207 char_u buf[NUMBUFLEN];
14208 char_u *keys_buf = NULL;
14209 char_u *rhs;
14210 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014211 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014212 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014213 mapblock_T *mp;
14214 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215
14216 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->v_type = VAR_STRING;
14218 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014220 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221 if (*keys == NUL)
14222 return;
14223
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014224 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014227 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014228 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014229 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014230 if (argvars[3].v_type != VAR_UNKNOWN)
14231 get_dict = get_tv_number(&argvars[3]);
14232 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 else
14235 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 if (which == NULL)
14237 return;
14238
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 mode = get_map_mode(&which, 0);
14240
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014241 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014242 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014244
14245 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014247 /* Return a string. */
14248 if (rhs != NULL)
14249 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250
Bram Moolenaarbd743252010-10-20 21:23:33 +020014251 }
14252 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14253 {
14254 /* Return a dictionary. */
14255 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14256 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14257 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
Bram Moolenaarbd743252010-10-20 21:23:33 +020014259 dict_add_nr_str(dict, "lhs", 0L, lhs);
14260 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14261 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14262 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14263 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14264 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14265 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014266 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014267 dict_add_nr_str(dict, "mode", 0L, mapmode);
14268
14269 vim_free(lhs);
14270 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 }
14272}
14273
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014274#ifdef FEAT_FLOAT
14275/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014276 * "log()" function
14277 */
14278 static void
14279f_log(argvars, rettv)
14280 typval_T *argvars;
14281 typval_T *rettv;
14282{
14283 float_T f;
14284
14285 rettv->v_type = VAR_FLOAT;
14286 if (get_float_arg(argvars, &f) == OK)
14287 rettv->vval.v_float = log(f);
14288 else
14289 rettv->vval.v_float = 0.0;
14290}
14291
14292/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014293 * "log10()" function
14294 */
14295 static void
14296f_log10(argvars, rettv)
14297 typval_T *argvars;
14298 typval_T *rettv;
14299{
14300 float_T f;
14301
14302 rettv->v_type = VAR_FLOAT;
14303 if (get_float_arg(argvars, &f) == OK)
14304 rettv->vval.v_float = log10(f);
14305 else
14306 rettv->vval.v_float = 0.0;
14307}
14308#endif
14309
Bram Moolenaar1dced572012-04-05 16:54:08 +020014310#ifdef FEAT_LUA
14311/*
14312 * "luaeval()" function
14313 */
14314 static void
14315f_luaeval(argvars, rettv)
14316 typval_T *argvars;
14317 typval_T *rettv;
14318{
14319 char_u *str;
14320 char_u buf[NUMBUFLEN];
14321
14322 str = get_tv_string_buf(&argvars[0], buf);
14323 do_luaeval(str, argvars + 1, rettv);
14324}
14325#endif
14326
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014328 * "map()" function
14329 */
14330 static void
14331f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014332 typval_T *argvars;
14333 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014334{
14335 filter_map(argvars, rettv, TRUE);
14336}
14337
14338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014339 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 */
14341 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014342f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014343 typval_T *argvars;
14344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347}
14348
14349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 */
14352 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014353f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *argvars;
14355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358}
14359
Bram Moolenaar33570922005-01-25 22:26:29 +000014360static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361
14362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 typval_T *argvars;
14365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 int type;
14367{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014368 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014369 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014370 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 char_u *pat;
14372 regmatch_T regmatch;
14373 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014374 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 char_u *save_cpo;
14376 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014377 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014378 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014379 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014380 list_T *l = NULL;
14381 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014382 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014383 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384
14385 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14386 save_cpo = p_cpo;
14387 p_cpo = (char_u *)"";
14388
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014389 rettv->vval.v_number = -1;
14390 if (type == 3)
14391 {
14392 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014393 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014394 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014395 }
14396 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 rettv->v_type = VAR_STRING;
14399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014402 if (argvars[0].v_type == VAR_LIST)
14403 {
14404 if ((l = argvars[0].vval.v_list) == NULL)
14405 goto theend;
14406 li = l->lv_first;
14407 }
14408 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014409 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014411 len = (long)STRLEN(str);
14412 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14415 if (pat == NULL)
14416 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014417
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014418 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 int error = FALSE;
14421
14422 start = get_tv_number_chk(&argvars[2], &error);
14423 if (error)
14424 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014425 if (l != NULL)
14426 {
14427 li = list_find(l, start);
14428 if (li == NULL)
14429 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014430 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014431 }
14432 else
14433 {
14434 if (start < 0)
14435 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014436 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014437 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014438 /* When "count" argument is there ignore matches before "start",
14439 * otherwise skip part of the string. Differs when pattern is "^"
14440 * or "\<". */
14441 if (argvars[3].v_type != VAR_UNKNOWN)
14442 startcol = start;
14443 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014444 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014445 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014446 len -= start;
14447 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014448 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014449
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014450 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 nth = get_tv_number_chk(&argvars[3], &error);
14452 if (error)
14453 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 }
14455
14456 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14457 if (regmatch.regprog != NULL)
14458 {
14459 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014460
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014461 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014462 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014463 if (l != NULL)
14464 {
14465 if (li == NULL)
14466 {
14467 match = FALSE;
14468 break;
14469 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014470 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014471 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014472 if (str == NULL)
14473 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014474 }
14475
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014476 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014477
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014478 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014479 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 if (l == NULL && !match)
14481 break;
14482
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014483 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014484 if (l != NULL)
14485 {
14486 li = li->li_next;
14487 ++idx;
14488 }
14489 else
14490 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014491#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014492 startcol = (colnr_T)(regmatch.startp[0]
14493 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014494#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014495 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014496#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014497 if (startcol > (colnr_T)len
14498 || str + startcol <= regmatch.startp[0])
14499 {
14500 match = FALSE;
14501 break;
14502 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014503 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014504 }
14505
14506 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014508 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014509 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014510 int i;
14511
14512 /* return list with matched string and submatches */
14513 for (i = 0; i < NSUBEXP; ++i)
14514 {
14515 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014516 {
14517 if (list_append_string(rettv->vval.v_list,
14518 (char_u *)"", 0) == FAIL)
14519 break;
14520 }
14521 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014522 regmatch.startp[i],
14523 (int)(regmatch.endp[i] - regmatch.startp[i]))
14524 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014525 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014526 }
14527 }
14528 else if (type == 2)
14529 {
14530 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014531 if (l != NULL)
14532 copy_tv(&li->li_tv, rettv);
14533 else
14534 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014536 }
14537 else if (l != NULL)
14538 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 else
14540 {
14541 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 (varnumber_T)(regmatch.startp[0] - str);
14544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014547 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 }
14549 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014550 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 }
14552
14553theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014554 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 p_cpo = save_cpo;
14556}
14557
14558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559 * "match()" function
14560 */
14561 static void
14562f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014563 typval_T *argvars;
14564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565{
14566 find_some_match(argvars, rettv, 1);
14567}
14568
14569/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014570 * "matchadd()" function
14571 */
14572 static void
14573f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014574 typval_T *argvars UNUSED;
14575 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014576{
14577#ifdef FEAT_SEARCH_EXTRA
14578 char_u buf[NUMBUFLEN];
14579 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14580 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14581 int prio = 10; /* default priority */
14582 int id = -1;
14583 int error = FALSE;
14584
14585 rettv->vval.v_number = -1;
14586
14587 if (grp == NULL || pat == NULL)
14588 return;
14589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014590 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014591 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014592 if (argvars[3].v_type != VAR_UNKNOWN)
14593 id = get_tv_number_chk(&argvars[3], &error);
14594 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014595 if (error == TRUE)
14596 return;
14597 if (id >= 1 && id <= 3)
14598 {
14599 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14600 return;
14601 }
14602
Bram Moolenaarb3414592014-06-17 17:48:32 +020014603 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14604#endif
14605}
14606
14607/*
14608 * "matchaddpos()" function
14609 */
14610 static void
14611f_matchaddpos(argvars, rettv)
14612 typval_T *argvars UNUSED;
14613 typval_T *rettv UNUSED;
14614{
14615#ifdef FEAT_SEARCH_EXTRA
14616 char_u buf[NUMBUFLEN];
14617 char_u *group;
14618 int prio = 10;
14619 int id = -1;
14620 int error = FALSE;
14621 list_T *l;
14622
14623 rettv->vval.v_number = -1;
14624
14625 group = get_tv_string_buf_chk(&argvars[0], buf);
14626 if (group == NULL)
14627 return;
14628
14629 if (argvars[1].v_type != VAR_LIST)
14630 {
14631 EMSG2(_(e_listarg), "matchaddpos()");
14632 return;
14633 }
14634 l = argvars[1].vval.v_list;
14635 if (l == NULL)
14636 return;
14637
14638 if (argvars[2].v_type != VAR_UNKNOWN)
14639 {
14640 prio = get_tv_number_chk(&argvars[2], &error);
14641 if (argvars[3].v_type != VAR_UNKNOWN)
14642 id = get_tv_number_chk(&argvars[3], &error);
14643 }
14644 if (error == TRUE)
14645 return;
14646
14647 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14648 if (id == 1 || id == 2)
14649 {
14650 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14651 return;
14652 }
14653
14654 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014655#endif
14656}
14657
14658/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014659 * "matcharg()" function
14660 */
14661 static void
14662f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014663 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014664 typval_T *rettv;
14665{
14666 if (rettv_list_alloc(rettv) == OK)
14667 {
14668#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014669 int id = get_tv_number(&argvars[0]);
14670 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014671
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014672 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014673 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014674 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14675 {
14676 list_append_string(rettv->vval.v_list,
14677 syn_id2name(m->hlg_id), -1);
14678 list_append_string(rettv->vval.v_list, m->pattern, -1);
14679 }
14680 else
14681 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014682 list_append_string(rettv->vval.v_list, NULL, -1);
14683 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014684 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014685 }
14686#endif
14687 }
14688}
14689
14690/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014691 * "matchdelete()" function
14692 */
14693 static void
14694f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014695 typval_T *argvars UNUSED;
14696 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014697{
14698#ifdef FEAT_SEARCH_EXTRA
14699 rettv->vval.v_number = match_delete(curwin,
14700 (int)get_tv_number(&argvars[0]), TRUE);
14701#endif
14702}
14703
14704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705 * "matchend()" function
14706 */
14707 static void
14708f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 typval_T *argvars;
14710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711{
14712 find_some_match(argvars, rettv, 0);
14713}
14714
14715/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716 * "matchlist()" function
14717 */
14718 static void
14719f_matchlist(argvars, rettv)
14720 typval_T *argvars;
14721 typval_T *rettv;
14722{
14723 find_some_match(argvars, rettv, 3);
14724}
14725
14726/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014727 * "matchstr()" function
14728 */
14729 static void
14730f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014731 typval_T *argvars;
14732 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733{
14734 find_some_match(argvars, rettv, 2);
14735}
14736
Bram Moolenaar33570922005-01-25 22:26:29 +000014737static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014738
14739 static void
14740max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
14742 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014743 int domax;
14744{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014745 long n = 0;
14746 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014747 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014748
14749 if (argvars[0].v_type == VAR_LIST)
14750 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014751 list_T *l;
14752 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014753
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014754 l = argvars[0].vval.v_list;
14755 if (l != NULL)
14756 {
14757 li = l->lv_first;
14758 if (li != NULL)
14759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014761 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014762 {
14763 li = li->li_next;
14764 if (li == NULL)
14765 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014766 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014767 if (domax ? i > n : i < n)
14768 n = i;
14769 }
14770 }
14771 }
14772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014773 else if (argvars[0].v_type == VAR_DICT)
14774 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014775 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014776 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014778 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014779
14780 d = argvars[0].vval.v_dict;
14781 if (d != NULL)
14782 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014783 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014785 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014786 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014787 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014788 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014790 if (first)
14791 {
14792 n = i;
14793 first = FALSE;
14794 }
14795 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014796 n = i;
14797 }
14798 }
14799 }
14800 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014801 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014802 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014804}
14805
14806/*
14807 * "max()" function
14808 */
14809 static void
14810f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014811 typval_T *argvars;
14812 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014813{
14814 max_min(argvars, rettv, TRUE);
14815}
14816
14817/*
14818 * "min()" function
14819 */
14820 static void
14821f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014822 typval_T *argvars;
14823 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014824{
14825 max_min(argvars, rettv, FALSE);
14826}
14827
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014828static int mkdir_recurse __ARGS((char_u *dir, int prot));
14829
14830/*
14831 * Create the directory in which "dir" is located, and higher levels when
14832 * needed.
14833 */
14834 static int
14835mkdir_recurse(dir, prot)
14836 char_u *dir;
14837 int prot;
14838{
14839 char_u *p;
14840 char_u *updir;
14841 int r = FAIL;
14842
14843 /* Get end of directory name in "dir".
14844 * We're done when it's "/" or "c:/". */
14845 p = gettail_sep(dir);
14846 if (p <= get_past_head(dir))
14847 return OK;
14848
14849 /* If the directory exists we're done. Otherwise: create it.*/
14850 updir = vim_strnsave(dir, (int)(p - dir));
14851 if (updir == NULL)
14852 return FAIL;
14853 if (mch_isdir(updir))
14854 r = OK;
14855 else if (mkdir_recurse(updir, prot) == OK)
14856 r = vim_mkdir_emsg(updir, prot);
14857 vim_free(updir);
14858 return r;
14859}
14860
14861#ifdef vim_mkdir
14862/*
14863 * "mkdir()" function
14864 */
14865 static void
14866f_mkdir(argvars, rettv)
14867 typval_T *argvars;
14868 typval_T *rettv;
14869{
14870 char_u *dir;
14871 char_u buf[NUMBUFLEN];
14872 int prot = 0755;
14873
14874 rettv->vval.v_number = FAIL;
14875 if (check_restricted() || check_secure())
14876 return;
14877
14878 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014879 if (*dir == NUL)
14880 rettv->vval.v_number = FAIL;
14881 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014882 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014883 if (*gettail(dir) == NUL)
14884 /* remove trailing slashes */
14885 *gettail_sep(dir) = NUL;
14886
14887 if (argvars[1].v_type != VAR_UNKNOWN)
14888 {
14889 if (argvars[2].v_type != VAR_UNKNOWN)
14890 prot = get_tv_number_chk(&argvars[2], NULL);
14891 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14892 mkdir_recurse(dir, prot);
14893 }
14894 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014895 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014896}
14897#endif
14898
Bram Moolenaar0d660222005-01-07 21:51:51 +000014899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 * "mode()" function
14901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014904 typval_T *argvars;
14905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014907 char_u buf[3];
14908
14909 buf[1] = NUL;
14910 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 if (VIsual_active)
14913 {
14914 if (VIsual_select)
14915 buf[0] = VIsual_mode + 's' - 'v';
14916 else
14917 buf[0] = VIsual_mode;
14918 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014919 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014920 || State == CONFIRM)
14921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014923 if (State == ASKMORE)
14924 buf[1] = 'm';
14925 else if (State == CONFIRM)
14926 buf[1] = '?';
14927 }
14928 else if (State == EXTERNCMD)
14929 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 else if (State & INSERT)
14931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014932#ifdef FEAT_VREPLACE
14933 if (State & VREPLACE_FLAG)
14934 {
14935 buf[0] = 'R';
14936 buf[1] = 'v';
14937 }
14938 else
14939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 if (State & REPLACE_FLAG)
14941 buf[0] = 'R';
14942 else
14943 buf[0] = 'i';
14944 }
14945 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014948 if (exmode_active)
14949 buf[1] = 'v';
14950 }
14951 else if (exmode_active)
14952 {
14953 buf[0] = 'c';
14954 buf[1] = 'e';
14955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014959 if (finish_op)
14960 buf[1] = 'o';
14961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014963 /* Clear out the minor mode when the argument is not a non-zero number or
14964 * non-empty string. */
14965 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014966 buf[1] = NUL;
14967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014968 rettv->vval.v_string = vim_strsave(buf);
14969 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970}
14971
Bram Moolenaar429fa852013-04-15 12:27:36 +020014972#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014973/*
14974 * "mzeval()" function
14975 */
14976 static void
14977f_mzeval(argvars, rettv)
14978 typval_T *argvars;
14979 typval_T *rettv;
14980{
14981 char_u *str;
14982 char_u buf[NUMBUFLEN];
14983
14984 str = get_tv_string_buf(&argvars[0], buf);
14985 do_mzeval(str, rettv);
14986}
Bram Moolenaar75676462013-01-30 14:55:42 +010014987
14988 void
14989mzscheme_call_vim(name, args, rettv)
14990 char_u *name;
14991 typval_T *args;
14992 typval_T *rettv;
14993{
14994 typval_T argvars[3];
14995
14996 argvars[0].v_type = VAR_STRING;
14997 argvars[0].vval.v_string = name;
14998 copy_tv(args, &argvars[1]);
14999 argvars[2].v_type = VAR_UNKNOWN;
15000 f_call(argvars, rettv);
15001 clear_tv(&argvars[1]);
15002}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015003#endif
15004
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006 * "nextnonblank()" function
15007 */
15008 static void
15009f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 typval_T *argvars;
15011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015012{
15013 linenr_T lnum;
15014
15015 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015017 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 {
15019 lnum = 0;
15020 break;
15021 }
15022 if (*skipwhite(ml_get(lnum)) != NUL)
15023 break;
15024 }
15025 rettv->vval.v_number = lnum;
15026}
15027
15028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 * "nr2char()" function
15030 */
15031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015033 typval_T *argvars;
15034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035{
15036 char_u buf[NUMBUFLEN];
15037
15038#ifdef FEAT_MBYTE
15039 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015040 {
15041 int utf8 = 0;
15042
15043 if (argvars[1].v_type != VAR_UNKNOWN)
15044 utf8 = get_tv_number_chk(&argvars[1], NULL);
15045 if (utf8)
15046 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15047 else
15048 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 else
15051#endif
15052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 buf[1] = NUL;
15055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 rettv->v_type = VAR_STRING;
15057 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015058}
15059
15060/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015061 * "or(expr, expr)" function
15062 */
15063 static void
15064f_or(argvars, rettv)
15065 typval_T *argvars;
15066 typval_T *rettv;
15067{
15068 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15069 | get_tv_number_chk(&argvars[1], NULL);
15070}
15071
15072/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015073 * "pathshorten()" function
15074 */
15075 static void
15076f_pathshorten(argvars, rettv)
15077 typval_T *argvars;
15078 typval_T *rettv;
15079{
15080 char_u *p;
15081
15082 rettv->v_type = VAR_STRING;
15083 p = get_tv_string_chk(&argvars[0]);
15084 if (p == NULL)
15085 rettv->vval.v_string = NULL;
15086 else
15087 {
15088 p = vim_strsave(p);
15089 rettv->vval.v_string = p;
15090 if (p != NULL)
15091 shorten_dir(p);
15092 }
15093}
15094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015095#ifdef FEAT_FLOAT
15096/*
15097 * "pow()" function
15098 */
15099 static void
15100f_pow(argvars, rettv)
15101 typval_T *argvars;
15102 typval_T *rettv;
15103{
15104 float_T fx, fy;
15105
15106 rettv->v_type = VAR_FLOAT;
15107 if (get_float_arg(argvars, &fx) == OK
15108 && get_float_arg(&argvars[1], &fy) == OK)
15109 rettv->vval.v_float = pow(fx, fy);
15110 else
15111 rettv->vval.v_float = 0.0;
15112}
15113#endif
15114
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116 * "prevnonblank()" function
15117 */
15118 static void
15119f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015120 typval_T *argvars;
15121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122{
15123 linenr_T lnum;
15124
15125 lnum = get_tv_lnum(argvars);
15126 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15127 lnum = 0;
15128 else
15129 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15130 --lnum;
15131 rettv->vval.v_number = lnum;
15132}
15133
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015134#ifdef HAVE_STDARG_H
15135/* This dummy va_list is here because:
15136 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15137 * - locally in the function results in a "used before set" warning
15138 * - using va_start() to initialize it gives "function with fixed args" error */
15139static va_list ap;
15140#endif
15141
Bram Moolenaar8c711452005-01-14 21:53:12 +000015142/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015143 * "printf()" function
15144 */
15145 static void
15146f_printf(argvars, rettv)
15147 typval_T *argvars;
15148 typval_T *rettv;
15149{
15150 rettv->v_type = VAR_STRING;
15151 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015152#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015153 {
15154 char_u buf[NUMBUFLEN];
15155 int len;
15156 char_u *s;
15157 int saved_did_emsg = did_emsg;
15158 char *fmt;
15159
15160 /* Get the required length, allocate the buffer and do it for real. */
15161 did_emsg = FALSE;
15162 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015163 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015164 if (!did_emsg)
15165 {
15166 s = alloc(len + 1);
15167 if (s != NULL)
15168 {
15169 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015170 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015171 }
15172 }
15173 did_emsg |= saved_did_emsg;
15174 }
15175#endif
15176}
15177
15178/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015179 * "pumvisible()" function
15180 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181 static void
15182f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015183 typval_T *argvars UNUSED;
15184 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015185{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015186#ifdef FEAT_INS_EXPAND
15187 if (pum_visible())
15188 rettv->vval.v_number = 1;
15189#endif
15190}
15191
Bram Moolenaardb913952012-06-29 12:54:53 +020015192#ifdef FEAT_PYTHON3
15193/*
15194 * "py3eval()" function
15195 */
15196 static void
15197f_py3eval(argvars, rettv)
15198 typval_T *argvars;
15199 typval_T *rettv;
15200{
15201 char_u *str;
15202 char_u buf[NUMBUFLEN];
15203
15204 str = get_tv_string_buf(&argvars[0], buf);
15205 do_py3eval(str, rettv);
15206}
15207#endif
15208
15209#ifdef FEAT_PYTHON
15210/*
15211 * "pyeval()" function
15212 */
15213 static void
15214f_pyeval(argvars, rettv)
15215 typval_T *argvars;
15216 typval_T *rettv;
15217{
15218 char_u *str;
15219 char_u buf[NUMBUFLEN];
15220
15221 str = get_tv_string_buf(&argvars[0], buf);
15222 do_pyeval(str, rettv);
15223}
15224#endif
15225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015226/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015227 * "range()" function
15228 */
15229 static void
15230f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015231 typval_T *argvars;
15232 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015233{
15234 long start;
15235 long end;
15236 long stride = 1;
15237 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015240 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015241 if (argvars[1].v_type == VAR_UNKNOWN)
15242 {
15243 end = start - 1;
15244 start = 0;
15245 }
15246 else
15247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015249 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015251 }
15252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 if (error)
15254 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015255 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015256 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015257 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015258 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015259 else
15260 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015261 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015262 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015263 if (list_append_number(rettv->vval.v_list,
15264 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015265 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015266 }
15267}
15268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015269/*
15270 * "readfile()" function
15271 */
15272 static void
15273f_readfile(argvars, rettv)
15274 typval_T *argvars;
15275 typval_T *rettv;
15276{
15277 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015278 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015279 char_u *fname;
15280 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015281 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15282 int io_size = sizeof(buf);
15283 int readlen; /* size of last fread() */
15284 char_u *prev = NULL; /* previously read bytes, if any */
15285 long prevlen = 0; /* length of data in prev */
15286 long prevsize = 0; /* size of prev buffer */
15287 long maxline = MAXLNUM;
15288 long cnt = 0;
15289 char_u *p; /* position in buf */
15290 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015291
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015292 if (argvars[1].v_type != VAR_UNKNOWN)
15293 {
15294 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15295 binary = TRUE;
15296 if (argvars[2].v_type != VAR_UNKNOWN)
15297 maxline = get_tv_number(&argvars[2]);
15298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015301 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015302
15303 /* Always open the file in binary mode, library functions have a mind of
15304 * their own about CR-LF conversion. */
15305 fname = get_tv_string(&argvars[0]);
15306 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15307 {
15308 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15309 return;
15310 }
15311
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015312 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015313 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015314 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015315
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015316 /* This for loop processes what was read, but is also entered at end
15317 * of file so that either:
15318 * - an incomplete line gets written
15319 * - a "binary" file gets an empty line at the end if it ends in a
15320 * newline. */
15321 for (p = buf, start = buf;
15322 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15323 ++p)
15324 {
15325 if (*p == '\n' || readlen <= 0)
15326 {
15327 listitem_T *li;
15328 char_u *s = NULL;
15329 long_u len = p - start;
15330
15331 /* Finished a line. Remove CRs before NL. */
15332 if (readlen > 0 && !binary)
15333 {
15334 while (len > 0 && start[len - 1] == '\r')
15335 --len;
15336 /* removal may cross back to the "prev" string */
15337 if (len == 0)
15338 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15339 --prevlen;
15340 }
15341 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015342 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015343 else
15344 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015345 /* Change "prev" buffer to be the right size. This way
15346 * the bytes are only copied once, and very long lines are
15347 * allocated only once. */
15348 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015349 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015350 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015352 prev = NULL; /* the list will own the string */
15353 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015354 }
15355 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015356 if (s == NULL)
15357 {
15358 do_outofmem_msg((long_u) prevlen + len + 1);
15359 failed = TRUE;
15360 break;
15361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015363 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 {
15365 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015366 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367 break;
15368 }
15369 li->li_tv.v_type = VAR_STRING;
15370 li->li_tv.v_lock = 0;
15371 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015372 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015374 start = p + 1; /* step over newline */
15375 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015376 break;
15377 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015378 else if (*p == NUL)
15379 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015380#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015381 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15382 * when finding the BF and check the previous two bytes. */
15383 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015384 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015385 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15386 * + 1, these may be in the "prev" string. */
15387 char_u back1 = p >= buf + 1 ? p[-1]
15388 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15389 char_u back2 = p >= buf + 2 ? p[-2]
15390 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15391 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015392
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015393 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015394 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015395 char_u *dest = p - 2;
15396
15397 /* Usually a BOM is at the beginning of a file, and so at
15398 * the beginning of a line; then we can just step over it.
15399 */
15400 if (start == dest)
15401 start = p + 1;
15402 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015403 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015404 /* have to shuffle buf to close gap */
15405 int adjust_prevlen = 0;
15406
15407 if (dest < buf)
15408 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015409 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015410 dest = buf;
15411 }
15412 if (readlen > p - buf + 1)
15413 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15414 readlen -= 3 - adjust_prevlen;
15415 prevlen -= adjust_prevlen;
15416 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015417 }
15418 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015419 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015420#endif
15421 } /* for */
15422
15423 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15424 break;
15425 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015426 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015427 /* There's part of a line in buf, store it in "prev". */
15428 if (p - start + prevlen >= prevsize)
15429 {
15430 /* need bigger "prev" buffer */
15431 char_u *newprev;
15432
15433 /* A common use case is ordinary text files and "prev" gets a
15434 * fragment of a line, so the first allocation is made
15435 * small, to avoid repeatedly 'allocing' large and
15436 * 'reallocing' small. */
15437 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015438 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015439 else
15440 {
15441 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015442 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015443 prevsize = grow50pc > growmin ? grow50pc : growmin;
15444 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015445 newprev = prev == NULL ? alloc(prevsize)
15446 : vim_realloc(prev, prevsize);
15447 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015448 {
15449 do_outofmem_msg((long_u)prevsize);
15450 failed = TRUE;
15451 break;
15452 }
15453 prev = newprev;
15454 }
15455 /* Add the line part to end of "prev". */
15456 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015457 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015458 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015459 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015460
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015461 /*
15462 * For a negative line count use only the lines at the end of the file,
15463 * free the rest.
15464 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015465 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015466 while (cnt > -maxline)
15467 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015468 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015469 --cnt;
15470 }
15471
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015472 if (failed)
15473 {
15474 list_free(rettv->vval.v_list, TRUE);
15475 /* readfile doc says an empty list is returned on error */
15476 rettv->vval.v_list = list_alloc();
15477 }
15478
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015479 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015480 fclose(fd);
15481}
15482
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015483#if defined(FEAT_RELTIME)
15484static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15485
15486/*
15487 * Convert a List to proftime_T.
15488 * Return FAIL when there is something wrong.
15489 */
15490 static int
15491list2proftime(arg, tm)
15492 typval_T *arg;
15493 proftime_T *tm;
15494{
15495 long n1, n2;
15496 int error = FALSE;
15497
15498 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15499 || arg->vval.v_list->lv_len != 2)
15500 return FAIL;
15501 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15502 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15503# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015504 tm->HighPart = n1;
15505 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015506# else
15507 tm->tv_sec = n1;
15508 tm->tv_usec = n2;
15509# endif
15510 return error ? FAIL : OK;
15511}
15512#endif /* FEAT_RELTIME */
15513
15514/*
15515 * "reltime()" function
15516 */
15517 static void
15518f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015519 typval_T *argvars UNUSED;
15520 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015521{
15522#ifdef FEAT_RELTIME
15523 proftime_T res;
15524 proftime_T start;
15525
15526 if (argvars[0].v_type == VAR_UNKNOWN)
15527 {
15528 /* No arguments: get current time. */
15529 profile_start(&res);
15530 }
15531 else if (argvars[1].v_type == VAR_UNKNOWN)
15532 {
15533 if (list2proftime(&argvars[0], &res) == FAIL)
15534 return;
15535 profile_end(&res);
15536 }
15537 else
15538 {
15539 /* Two arguments: compute the difference. */
15540 if (list2proftime(&argvars[0], &start) == FAIL
15541 || list2proftime(&argvars[1], &res) == FAIL)
15542 return;
15543 profile_sub(&res, &start);
15544 }
15545
15546 if (rettv_list_alloc(rettv) == OK)
15547 {
15548 long n1, n2;
15549
15550# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015551 n1 = res.HighPart;
15552 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015553# else
15554 n1 = res.tv_sec;
15555 n2 = res.tv_usec;
15556# endif
15557 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15558 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15559 }
15560#endif
15561}
15562
15563/*
15564 * "reltimestr()" function
15565 */
15566 static void
15567f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015568 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015569 typval_T *rettv;
15570{
15571#ifdef FEAT_RELTIME
15572 proftime_T tm;
15573#endif
15574
15575 rettv->v_type = VAR_STRING;
15576 rettv->vval.v_string = NULL;
15577#ifdef FEAT_RELTIME
15578 if (list2proftime(&argvars[0], &tm) == OK)
15579 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15580#endif
15581}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015582
Bram Moolenaar0d660222005-01-07 21:51:51 +000015583#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15584static void make_connection __ARGS((void));
15585static int check_connection __ARGS((void));
15586
15587 static void
15588make_connection()
15589{
15590 if (X_DISPLAY == NULL
15591# ifdef FEAT_GUI
15592 && !gui.in_use
15593# endif
15594 )
15595 {
15596 x_force_connect = TRUE;
15597 setup_term_clip();
15598 x_force_connect = FALSE;
15599 }
15600}
15601
15602 static int
15603check_connection()
15604{
15605 make_connection();
15606 if (X_DISPLAY == NULL)
15607 {
15608 EMSG(_("E240: No connection to Vim server"));
15609 return FAIL;
15610 }
15611 return OK;
15612}
15613#endif
15614
15615#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015616static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015617
15618 static void
15619remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015620 typval_T *argvars;
15621 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015622 int expr;
15623{
15624 char_u *server_name;
15625 char_u *keys;
15626 char_u *r = NULL;
15627 char_u buf[NUMBUFLEN];
15628# ifdef WIN32
15629 HWND w;
15630# else
15631 Window w;
15632# endif
15633
15634 if (check_restricted() || check_secure())
15635 return;
15636
15637# ifdef FEAT_X11
15638 if (check_connection() == FAIL)
15639 return;
15640# endif
15641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015642 server_name = get_tv_string_chk(&argvars[0]);
15643 if (server_name == NULL)
15644 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645 keys = get_tv_string_buf(&argvars[1], buf);
15646# ifdef WIN32
15647 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15648# else
15649 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15650 < 0)
15651# endif
15652 {
15653 if (r != NULL)
15654 EMSG(r); /* sending worked but evaluation failed */
15655 else
15656 EMSG2(_("E241: Unable to send to %s"), server_name);
15657 return;
15658 }
15659
15660 rettv->vval.v_string = r;
15661
15662 if (argvars[2].v_type != VAR_UNKNOWN)
15663 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015665 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015666 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015668 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 v.di_tv.v_type = VAR_STRING;
15670 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 idvar = get_tv_string_chk(&argvars[2]);
15672 if (idvar != NULL)
15673 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015675 }
15676}
15677#endif
15678
15679/*
15680 * "remote_expr()" function
15681 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015682 static void
15683f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015685 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015686{
15687 rettv->v_type = VAR_STRING;
15688 rettv->vval.v_string = NULL;
15689#ifdef FEAT_CLIENTSERVER
15690 remote_common(argvars, rettv, TRUE);
15691#endif
15692}
15693
15694/*
15695 * "remote_foreground()" function
15696 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697 static void
15698f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015699 typval_T *argvars UNUSED;
15700 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702#ifdef FEAT_CLIENTSERVER
15703# ifdef WIN32
15704 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015705 {
15706 char_u *server_name = get_tv_string_chk(&argvars[0]);
15707
15708 if (server_name != NULL)
15709 serverForeground(server_name);
15710 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711# else
15712 /* Send a foreground() expression to the server. */
15713 argvars[1].v_type = VAR_STRING;
15714 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15715 argvars[2].v_type = VAR_UNKNOWN;
15716 remote_common(argvars, rettv, TRUE);
15717 vim_free(argvars[1].vval.v_string);
15718# endif
15719#endif
15720}
15721
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 static void
15723f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726{
15727#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015728 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729 char_u *s = NULL;
15730# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015731 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015733 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734
15735 if (check_restricted() || check_secure())
15736 {
15737 rettv->vval.v_number = -1;
15738 return;
15739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015740 serverid = get_tv_string_chk(&argvars[0]);
15741 if (serverid == NULL)
15742 {
15743 rettv->vval.v_number = -1;
15744 return; /* type error; errmsg already given */
15745 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015747 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748 if (n == 0)
15749 rettv->vval.v_number = -1;
15750 else
15751 {
15752 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15753 rettv->vval.v_number = (s != NULL);
15754 }
15755# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756 if (check_connection() == FAIL)
15757 return;
15758
15759 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015760 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761# endif
15762
15763 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015765 char_u *retvar;
15766
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 v.di_tv.v_type = VAR_STRING;
15768 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015769 retvar = get_tv_string_chk(&argvars[1]);
15770 if (retvar != NULL)
15771 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773 }
15774#else
15775 rettv->vval.v_number = -1;
15776#endif
15777}
15778
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779 static void
15780f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783{
15784 char_u *r = NULL;
15785
15786#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015787 char_u *serverid = get_tv_string_chk(&argvars[0]);
15788
15789 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015790 {
15791# ifdef WIN32
15792 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015793 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015795 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796 if (n != 0)
15797 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15798 if (r == NULL)
15799# else
15800 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015802# endif
15803 EMSG(_("E277: Unable to read a server reply"));
15804 }
15805#endif
15806 rettv->v_type = VAR_STRING;
15807 rettv->vval.v_string = r;
15808}
15809
15810/*
15811 * "remote_send()" function
15812 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813 static void
15814f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817{
15818 rettv->v_type = VAR_STRING;
15819 rettv->vval.v_string = NULL;
15820#ifdef FEAT_CLIENTSERVER
15821 remote_common(argvars, rettv, FALSE);
15822#endif
15823}
15824
15825/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015826 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015827 */
15828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015829f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T *argvars;
15831 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015832{
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 list_T *l;
15834 listitem_T *item, *item2;
15835 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015836 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015837 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015838 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015839 dict_T *d;
15840 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015841 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015842
Bram Moolenaar8c711452005-01-14 21:53:12 +000015843 if (argvars[0].v_type == VAR_DICT)
15844 {
15845 if (argvars[2].v_type != VAR_UNKNOWN)
15846 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015847 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015848 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 key = get_tv_string_chk(&argvars[1]);
15851 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015853 di = dict_find(d, key, -1);
15854 if (di == NULL)
15855 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015856 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15857 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 {
15859 *rettv = di->di_tv;
15860 init_tv(&di->di_tv);
15861 dictitem_remove(d, di);
15862 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015864 }
15865 }
15866 else if (argvars[0].v_type != VAR_LIST)
15867 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015868 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015869 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 int error = FALSE;
15872
15873 idx = get_tv_number_chk(&argvars[1], &error);
15874 if (error)
15875 ; /* type error: do nothing, errmsg already given */
15876 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877 EMSGN(_(e_listidx), idx);
15878 else
15879 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015880 if (argvars[2].v_type == VAR_UNKNOWN)
15881 {
15882 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015883 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015884 *rettv = item->li_tv;
15885 vim_free(item);
15886 }
15887 else
15888 {
15889 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015890 end = get_tv_number_chk(&argvars[2], &error);
15891 if (error)
15892 ; /* type error: do nothing */
15893 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015894 EMSGN(_(e_listidx), end);
15895 else
15896 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015897 int cnt = 0;
15898
15899 for (li = item; li != NULL; li = li->li_next)
15900 {
15901 ++cnt;
15902 if (li == item2)
15903 break;
15904 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015905 if (li == NULL) /* didn't find "item2" after "item" */
15906 EMSG(_(e_invrange));
15907 else
15908 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015909 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015910 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015911 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015912 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015913 l->lv_first = item;
15914 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015915 item->li_prev = NULL;
15916 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015917 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015918 }
15919 }
15920 }
15921 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015922 }
15923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924}
15925
15926/*
15927 * "rename({from}, {to})" function
15928 */
15929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015930f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015931 typval_T *argvars;
15932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933{
15934 char_u buf[NUMBUFLEN];
15935
15936 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15940 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941}
15942
15943/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015944 * "repeat()" function
15945 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015946 static void
15947f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015948 typval_T *argvars;
15949 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015950{
15951 char_u *p;
15952 int n;
15953 int slen;
15954 int len;
15955 char_u *r;
15956 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015957
15958 n = get_tv_number(&argvars[1]);
15959 if (argvars[0].v_type == VAR_LIST)
15960 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015962 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 if (list_extend(rettv->vval.v_list,
15964 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015965 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015966 }
15967 else
15968 {
15969 p = get_tv_string(&argvars[0]);
15970 rettv->v_type = VAR_STRING;
15971 rettv->vval.v_string = NULL;
15972
15973 slen = (int)STRLEN(p);
15974 len = slen * n;
15975 if (len <= 0)
15976 return;
15977
15978 r = alloc(len + 1);
15979 if (r != NULL)
15980 {
15981 for (i = 0; i < n; i++)
15982 mch_memmove(r + i * slen, p, (size_t)slen);
15983 r[len] = NUL;
15984 }
15985
15986 rettv->vval.v_string = r;
15987 }
15988}
15989
15990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 * "resolve()" function
15992 */
15993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *argvars;
15996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997{
15998 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015999#ifdef HAVE_READLINK
16000 char_u *buf = NULL;
16001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004#ifdef FEAT_SHORTCUT
16005 {
16006 char_u *v = NULL;
16007
16008 v = mch_resolve_shortcut(p);
16009 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 }
16014#else
16015# ifdef HAVE_READLINK
16016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 char_u *cpy;
16018 int len;
16019 char_u *remain = NULL;
16020 char_u *q;
16021 int is_relative_to_current = FALSE;
16022 int has_trailing_pathsep = FALSE;
16023 int limit = 100;
16024
16025 p = vim_strsave(p);
16026
16027 if (p[0] == '.' && (vim_ispathsep(p[1])
16028 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16029 is_relative_to_current = TRUE;
16030
16031 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016032 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016035 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037
16038 q = getnextcomp(p);
16039 if (*q != NUL)
16040 {
16041 /* Separate the first path component in "p", and keep the
16042 * remainder (beginning with the path separator). */
16043 remain = vim_strsave(q - 1);
16044 q[-1] = NUL;
16045 }
16046
Bram Moolenaard9462e32011-04-11 21:35:11 +020016047 buf = alloc(MAXPATHL + 1);
16048 if (buf == NULL)
16049 goto fail;
16050
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 for (;;)
16052 {
16053 for (;;)
16054 {
16055 len = readlink((char *)p, (char *)buf, MAXPATHL);
16056 if (len <= 0)
16057 break;
16058 buf[len] = NUL;
16059
16060 if (limit-- == 0)
16061 {
16062 vim_free(p);
16063 vim_free(remain);
16064 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 goto fail;
16067 }
16068
16069 /* Ensure that the result will have a trailing path separator
16070 * if the argument has one. */
16071 if (remain == NULL && has_trailing_pathsep)
16072 add_pathsep(buf);
16073
16074 /* Separate the first path component in the link value and
16075 * concatenate the remainders. */
16076 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16077 if (*q != NUL)
16078 {
16079 if (remain == NULL)
16080 remain = vim_strsave(q - 1);
16081 else
16082 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016083 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 if (cpy != NULL)
16085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 vim_free(remain);
16087 remain = cpy;
16088 }
16089 }
16090 q[-1] = NUL;
16091 }
16092
16093 q = gettail(p);
16094 if (q > p && *q == NUL)
16095 {
16096 /* Ignore trailing path separator. */
16097 q[-1] = NUL;
16098 q = gettail(p);
16099 }
16100 if (q > p && !mch_isFullName(buf))
16101 {
16102 /* symlink is relative to directory of argument */
16103 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16104 if (cpy != NULL)
16105 {
16106 STRCPY(cpy, p);
16107 STRCPY(gettail(cpy), buf);
16108 vim_free(p);
16109 p = cpy;
16110 }
16111 }
16112 else
16113 {
16114 vim_free(p);
16115 p = vim_strsave(buf);
16116 }
16117 }
16118
16119 if (remain == NULL)
16120 break;
16121
16122 /* Append the first path component of "remain" to "p". */
16123 q = getnextcomp(remain + 1);
16124 len = q - remain - (*q != NUL);
16125 cpy = vim_strnsave(p, STRLEN(p) + len);
16126 if (cpy != NULL)
16127 {
16128 STRNCAT(cpy, remain, len);
16129 vim_free(p);
16130 p = cpy;
16131 }
16132 /* Shorten "remain". */
16133 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016134 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 else
16136 {
16137 vim_free(remain);
16138 remain = NULL;
16139 }
16140 }
16141
16142 /* If the result is a relative path name, make it explicitly relative to
16143 * the current directory if and only if the argument had this form. */
16144 if (!vim_ispathsep(*p))
16145 {
16146 if (is_relative_to_current
16147 && *p != NUL
16148 && !(p[0] == '.'
16149 && (p[1] == NUL
16150 || vim_ispathsep(p[1])
16151 || (p[1] == '.'
16152 && (p[2] == NUL
16153 || vim_ispathsep(p[2]))))))
16154 {
16155 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016156 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 if (cpy != NULL)
16158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 vim_free(p);
16160 p = cpy;
16161 }
16162 }
16163 else if (!is_relative_to_current)
16164 {
16165 /* Strip leading "./". */
16166 q = p;
16167 while (q[0] == '.' && vim_ispathsep(q[1]))
16168 q += 2;
16169 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016170 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 }
16172 }
16173
16174 /* Ensure that the result will have no trailing path separator
16175 * if the argument had none. But keep "/" or "//". */
16176 if (!has_trailing_pathsep)
16177 {
16178 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016179 if (after_pathsep(p, q))
16180 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 }
16182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 }
16185# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187# endif
16188#endif
16189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191
16192#ifdef HAVE_READLINK
16193fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016194 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016196 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197}
16198
16199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 */
16202 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *argvars;
16205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206{
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 list_T *l;
16208 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 if (argvars[0].v_type != VAR_LIST)
16211 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016212 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016213 && !tv_check_lock(l->lv_lock,
16214 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215 {
16216 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016217 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016218 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 while (li != NULL)
16220 {
16221 ni = li->li_prev;
16222 list_append(l, li);
16223 li = ni;
16224 }
16225 rettv->vval.v_list = l;
16226 rettv->v_type = VAR_LIST;
16227 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016228 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230}
16231
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016232#define SP_NOMOVE 0x01 /* don't move cursor */
16233#define SP_REPEAT 0x02 /* repeat to find outer pair */
16234#define SP_RETCOUNT 0x04 /* return matchcount */
16235#define SP_SETPCMARK 0x08 /* set previous context mark */
16236#define SP_START 0x10 /* accept match at start position */
16237#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16238#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016239
Bram Moolenaar33570922005-01-25 22:26:29 +000016240static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241
16242/*
16243 * Get flags for a search function.
16244 * Possibly sets "p_ws".
16245 * Returns BACKWARD, FORWARD or zero (for an error).
16246 */
16247 static int
16248get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016249 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250 int *flagsp;
16251{
16252 int dir = FORWARD;
16253 char_u *flags;
16254 char_u nbuf[NUMBUFLEN];
16255 int mask;
16256
16257 if (varp->v_type != VAR_UNKNOWN)
16258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 flags = get_tv_string_buf_chk(varp, nbuf);
16260 if (flags == NULL)
16261 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262 while (*flags != NUL)
16263 {
16264 switch (*flags)
16265 {
16266 case 'b': dir = BACKWARD; break;
16267 case 'w': p_ws = TRUE; break;
16268 case 'W': p_ws = FALSE; break;
16269 default: mask = 0;
16270 if (flagsp != NULL)
16271 switch (*flags)
16272 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016273 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016274 case 'e': mask = SP_END; break;
16275 case 'm': mask = SP_RETCOUNT; break;
16276 case 'n': mask = SP_NOMOVE; break;
16277 case 'p': mask = SP_SUBPAT; break;
16278 case 'r': mask = SP_REPEAT; break;
16279 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016280 }
16281 if (mask == 0)
16282 {
16283 EMSG2(_(e_invarg2), flags);
16284 dir = 0;
16285 }
16286 else
16287 *flagsp |= mask;
16288 }
16289 if (dir == 0)
16290 break;
16291 ++flags;
16292 }
16293 }
16294 return dir;
16295}
16296
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016298 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016300 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016301search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016303 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016304 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016306 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 char_u *pat;
16308 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016309 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 int save_p_ws = p_ws;
16311 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016312 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016313 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016314 proftime_T tm;
16315#ifdef FEAT_RELTIME
16316 long time_limit = 0;
16317#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016318 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016319 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016321 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016322 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016323 if (dir == 0)
16324 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016325 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016326 if (flags & SP_START)
16327 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016328 if (flags & SP_END)
16329 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016330
Bram Moolenaar76929292008-01-06 19:07:36 +000016331 /* Optional arguments: line number to stop searching and timeout. */
16332 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016333 {
16334 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16335 if (lnum_stop < 0)
16336 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016337#ifdef FEAT_RELTIME
16338 if (argvars[3].v_type != VAR_UNKNOWN)
16339 {
16340 time_limit = get_tv_number_chk(&argvars[3], NULL);
16341 if (time_limit < 0)
16342 goto theend;
16343 }
16344#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016345 }
16346
Bram Moolenaar76929292008-01-06 19:07:36 +000016347#ifdef FEAT_RELTIME
16348 /* Set the time limit, if there is one. */
16349 profile_setlimit(time_limit, &tm);
16350#endif
16351
Bram Moolenaar231334e2005-07-25 20:46:57 +000016352 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016353 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016354 * Check to make sure only those flags are set.
16355 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16356 * flags cannot be set. Check for that condition also.
16357 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016358 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016359 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016362 goto theend;
16363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016365 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016366 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016367 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016368 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016370 if (flags & SP_SUBPAT)
16371 retval = subpatnum;
16372 else
16373 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016374 if (flags & SP_SETPCMARK)
16375 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016377 if (match_pos != NULL)
16378 {
16379 /* Store the match cursor position */
16380 match_pos->lnum = pos.lnum;
16381 match_pos->col = pos.col + 1;
16382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 /* "/$" will put the cursor after the end of the line, may need to
16384 * correct that here */
16385 check_cursor();
16386 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016387
16388 /* If 'n' flag is used: restore cursor position. */
16389 if (flags & SP_NOMOVE)
16390 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016391 else
16392 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016393theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016395
16396 return retval;
16397}
16398
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016399#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016400
16401/*
16402 * round() is not in C90, use ceil() or floor() instead.
16403 */
16404 float_T
16405vim_round(f)
16406 float_T f;
16407{
16408 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16409}
16410
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016411/*
16412 * "round({float})" function
16413 */
16414 static void
16415f_round(argvars, rettv)
16416 typval_T *argvars;
16417 typval_T *rettv;
16418{
16419 float_T f;
16420
16421 rettv->v_type = VAR_FLOAT;
16422 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016423 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016424 else
16425 rettv->vval.v_float = 0.0;
16426}
16427#endif
16428
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016429/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016430 * "screenattr()" function
16431 */
16432 static void
16433f_screenattr(argvars, rettv)
16434 typval_T *argvars UNUSED;
16435 typval_T *rettv;
16436{
16437 int row;
16438 int col;
16439 int c;
16440
16441 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16442 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16443 if (row < 0 || row >= screen_Rows
16444 || col < 0 || col >= screen_Columns)
16445 c = -1;
16446 else
16447 c = ScreenAttrs[LineOffset[row] + col];
16448 rettv->vval.v_number = c;
16449}
16450
16451/*
16452 * "screenchar()" function
16453 */
16454 static void
16455f_screenchar(argvars, rettv)
16456 typval_T *argvars UNUSED;
16457 typval_T *rettv;
16458{
16459 int row;
16460 int col;
16461 int off;
16462 int c;
16463
16464 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16465 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16466 if (row < 0 || row >= screen_Rows
16467 || col < 0 || col >= screen_Columns)
16468 c = -1;
16469 else
16470 {
16471 off = LineOffset[row] + col;
16472#ifdef FEAT_MBYTE
16473 if (enc_utf8 && ScreenLinesUC[off] != 0)
16474 c = ScreenLinesUC[off];
16475 else
16476#endif
16477 c = ScreenLines[off];
16478 }
16479 rettv->vval.v_number = c;
16480}
16481
16482/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016483 * "screencol()" function
16484 *
16485 * First column is 1 to be consistent with virtcol().
16486 */
16487 static void
16488f_screencol(argvars, rettv)
16489 typval_T *argvars UNUSED;
16490 typval_T *rettv;
16491{
16492 rettv->vval.v_number = screen_screencol() + 1;
16493}
16494
16495/*
16496 * "screenrow()" function
16497 */
16498 static void
16499f_screenrow(argvars, rettv)
16500 typval_T *argvars UNUSED;
16501 typval_T *rettv;
16502{
16503 rettv->vval.v_number = screen_screenrow() + 1;
16504}
16505
16506/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016507 * "search()" function
16508 */
16509 static void
16510f_search(argvars, rettv)
16511 typval_T *argvars;
16512 typval_T *rettv;
16513{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016514 int flags = 0;
16515
16516 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517}
16518
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016520 * "searchdecl()" function
16521 */
16522 static void
16523f_searchdecl(argvars, rettv)
16524 typval_T *argvars;
16525 typval_T *rettv;
16526{
16527 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016528 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016529 int error = FALSE;
16530 char_u *name;
16531
16532 rettv->vval.v_number = 1; /* default: FAIL */
16533
16534 name = get_tv_string_chk(&argvars[0]);
16535 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016536 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016537 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016538 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16539 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16540 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016541 if (!error && name != NULL)
16542 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016543 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016544}
16545
16546/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016547 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016549 static int
16550searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016552 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553{
16554 char_u *spat, *mpat, *epat;
16555 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 int dir;
16558 int flags = 0;
16559 char_u nbuf1[NUMBUFLEN];
16560 char_u nbuf2[NUMBUFLEN];
16561 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016562 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016563 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016564 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016567 spat = get_tv_string_chk(&argvars[0]);
16568 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16569 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16570 if (spat == NULL || mpat == NULL || epat == NULL)
16571 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 /* Handle the optional fourth argument: flags */
16574 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016575 if (dir == 0)
16576 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016577
16578 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016579 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16580 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016581 if ((flags & (SP_END | SP_SUBPAT)) != 0
16582 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016583 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016584 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016585 goto theend;
16586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016588 /* Using 'r' implies 'W', otherwise it doesn't work. */
16589 if (flags & SP_REPEAT)
16590 p_ws = FALSE;
16591
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016592 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016593 if (argvars[3].v_type == VAR_UNKNOWN
16594 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 skip = (char_u *)"";
16596 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016599 if (argvars[5].v_type != VAR_UNKNOWN)
16600 {
16601 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16602 if (lnum_stop < 0)
16603 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016604#ifdef FEAT_RELTIME
16605 if (argvars[6].v_type != VAR_UNKNOWN)
16606 {
16607 time_limit = get_tv_number_chk(&argvars[6], NULL);
16608 if (time_limit < 0)
16609 goto theend;
16610 }
16611#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016612 }
16613 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 if (skip == NULL)
16615 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016617 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016618 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016619
16620theend:
16621 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016622
16623 return retval;
16624}
16625
16626/*
16627 * "searchpair()" function
16628 */
16629 static void
16630f_searchpair(argvars, rettv)
16631 typval_T *argvars;
16632 typval_T *rettv;
16633{
16634 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16635}
16636
16637/*
16638 * "searchpairpos()" function
16639 */
16640 static void
16641f_searchpairpos(argvars, rettv)
16642 typval_T *argvars;
16643 typval_T *rettv;
16644{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016645 pos_T match_pos;
16646 int lnum = 0;
16647 int col = 0;
16648
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016649 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016650 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016651
16652 if (searchpair_cmn(argvars, &match_pos) > 0)
16653 {
16654 lnum = match_pos.lnum;
16655 col = match_pos.col;
16656 }
16657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016658 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16659 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016660}
16661
16662/*
16663 * Search for a start/middle/end thing.
16664 * Used by searchpair(), see its documentation for the details.
16665 * Returns 0 or -1 for no match,
16666 */
16667 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016668do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16669 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016670 char_u *spat; /* start pattern */
16671 char_u *mpat; /* middle pattern */
16672 char_u *epat; /* end pattern */
16673 int dir; /* BACKWARD or FORWARD */
16674 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016675 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016676 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016677 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016678 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016679{
16680 char_u *save_cpo;
16681 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16682 long retval = 0;
16683 pos_T pos;
16684 pos_T firstpos;
16685 pos_T foundpos;
16686 pos_T save_cursor;
16687 pos_T save_pos;
16688 int n;
16689 int r;
16690 int nest = 1;
16691 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016692 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016693 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016694
16695 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16696 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016697 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016698
Bram Moolenaar76929292008-01-06 19:07:36 +000016699#ifdef FEAT_RELTIME
16700 /* Set the time limit, if there is one. */
16701 profile_setlimit(time_limit, &tm);
16702#endif
16703
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016704 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16705 * start/middle/end (pat3, for the top pair). */
16706 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16707 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16708 if (pat2 == NULL || pat3 == NULL)
16709 goto theend;
16710 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16711 if (*mpat == NUL)
16712 STRCPY(pat3, pat2);
16713 else
16714 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16715 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016716 if (flags & SP_START)
16717 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016718
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 save_cursor = curwin->w_cursor;
16720 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016721 clearpos(&firstpos);
16722 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 pat = pat3;
16724 for (;;)
16725 {
16726 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016727 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16729 /* didn't find it or found the first match again: FAIL */
16730 break;
16731
16732 if (firstpos.lnum == 0)
16733 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016734 if (equalpos(pos, foundpos))
16735 {
16736 /* Found the same position again. Can happen with a pattern that
16737 * has "\zs" at the end and searching backwards. Advance one
16738 * character and try again. */
16739 if (dir == BACKWARD)
16740 decl(&pos);
16741 else
16742 incl(&pos);
16743 }
16744 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016746 /* clear the start flag to avoid getting stuck here */
16747 options &= ~SEARCH_START;
16748
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 /* If the skip pattern matches, ignore this match. */
16750 if (*skip != NUL)
16751 {
16752 save_pos = curwin->w_cursor;
16753 curwin->w_cursor = pos;
16754 r = eval_to_bool(skip, &err, NULL, FALSE);
16755 curwin->w_cursor = save_pos;
16756 if (err)
16757 {
16758 /* Evaluating {skip} caused an error, break here. */
16759 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016760 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 break;
16762 }
16763 if (r)
16764 continue;
16765 }
16766
16767 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16768 {
16769 /* Found end when searching backwards or start when searching
16770 * forward: nested pair. */
16771 ++nest;
16772 pat = pat2; /* nested, don't search for middle */
16773 }
16774 else
16775 {
16776 /* Found end when searching forward or start when searching
16777 * backward: end of (nested) pair; or found middle in outer pair. */
16778 if (--nest == 1)
16779 pat = pat3; /* outer level, search for middle */
16780 }
16781
16782 if (nest == 0)
16783 {
16784 /* Found the match: return matchcount or line number. */
16785 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016786 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016788 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016789 if (flags & SP_SETPCMARK)
16790 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 curwin->w_cursor = pos;
16792 if (!(flags & SP_REPEAT))
16793 break;
16794 nest = 1; /* search for next unmatched */
16795 }
16796 }
16797
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016798 if (match_pos != NULL)
16799 {
16800 /* Store the match cursor position */
16801 match_pos->lnum = curwin->w_cursor.lnum;
16802 match_pos->col = curwin->w_cursor.col + 1;
16803 }
16804
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016806 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 curwin->w_cursor = save_cursor;
16808
16809theend:
16810 vim_free(pat2);
16811 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016812 if (p_cpo == empty_option)
16813 p_cpo = save_cpo;
16814 else
16815 /* Darn, evaluating the {skip} expression changed the value. */
16816 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016817
16818 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819}
16820
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016821/*
16822 * "searchpos()" function
16823 */
16824 static void
16825f_searchpos(argvars, rettv)
16826 typval_T *argvars;
16827 typval_T *rettv;
16828{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016829 pos_T match_pos;
16830 int lnum = 0;
16831 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016832 int n;
16833 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016834
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016835 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016836 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016837
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016838 n = search_cmn(argvars, &match_pos, &flags);
16839 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016840 {
16841 lnum = match_pos.lnum;
16842 col = match_pos.col;
16843 }
16844
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016845 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16846 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016847 if (flags & SP_SUBPAT)
16848 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016849}
16850
16851
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852 static void
16853f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857#ifdef FEAT_CLIENTSERVER
16858 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016859 char_u *server = get_tv_string_chk(&argvars[0]);
16860 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 if (server == NULL || reply == NULL)
16864 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 if (check_restricted() || check_secure())
16866 return;
16867# ifdef FEAT_X11
16868 if (check_connection() == FAIL)
16869 return;
16870# endif
16871
16872 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 EMSG(_("E258: Unable to send to client"));
16875 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877 rettv->vval.v_number = 0;
16878#else
16879 rettv->vval.v_number = -1;
16880#endif
16881}
16882
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883 static void
16884f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887{
16888 char_u *r = NULL;
16889
16890#ifdef FEAT_CLIENTSERVER
16891# ifdef WIN32
16892 r = serverGetVimNames();
16893# else
16894 make_connection();
16895 if (X_DISPLAY != NULL)
16896 r = serverGetVimNames(X_DISPLAY);
16897# endif
16898#endif
16899 rettv->v_type = VAR_STRING;
16900 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901}
16902
16903/*
16904 * "setbufvar()" function
16905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016907f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016908 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016909 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910{
16911 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 char_u nbuf[NUMBUFLEN];
16916
16917 if (check_restricted() || check_secure())
16918 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016919 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16920 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016921 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 varp = &argvars[2];
16923
16924 if (buf != NULL && varname != NULL && varp != NULL)
16925 {
16926 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928
16929 if (*varname == '&')
16930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 long numval;
16932 char_u *strval;
16933 int error = FALSE;
16934
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 numval = get_tv_number_chk(varp, &error);
16937 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 if (!error && strval != NULL)
16939 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 }
16941 else
16942 {
16943 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16944 if (bufvarname != NULL)
16945 {
16946 STRCPY(bufvarname, "b:");
16947 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016948 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 vim_free(bufvarname);
16950 }
16951 }
16952
16953 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956}
16957
16958/*
16959 * "setcmdpos()" function
16960 */
16961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016962f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016963 typval_T *argvars;
16964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016966 int pos = (int)get_tv_number(&argvars[0]) - 1;
16967
16968 if (pos >= 0)
16969 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
16972/*
16973 * "setline()" function
16974 */
16975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016976f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 typval_T *argvars;
16978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979{
16980 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016981 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016982 list_T *l = NULL;
16983 listitem_T *li = NULL;
16984 long added = 0;
16985 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016987 lnum = get_tv_lnum(&argvars[0]);
16988 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016990 l = argvars[1].vval.v_list;
16991 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016993 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016994 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016995
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016996 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016997 for (;;)
16998 {
16999 if (l != NULL)
17000 {
17001 /* list argument, get next string */
17002 if (li == NULL)
17003 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017005 li = li->li_next;
17006 }
17007
17008 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017010 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017011
17012 /* When coming here from Insert mode, sync undo, so that this can be
17013 * undone separately from what was previously inserted. */
17014 if (u_sync_once == 2)
17015 {
17016 u_sync_once = 1; /* notify that u_sync() was called */
17017 u_sync(TRUE);
17018 }
17019
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017020 if (lnum <= curbuf->b_ml.ml_line_count)
17021 {
17022 /* existing line, replace it */
17023 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17024 {
17025 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017026 if (lnum == curwin->w_cursor.lnum)
17027 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017028 rettv->vval.v_number = 0; /* OK */
17029 }
17030 }
17031 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17032 {
17033 /* lnum is one past the last line, append the line */
17034 ++added;
17035 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17036 rettv->vval.v_number = 0; /* OK */
17037 }
17038
17039 if (l == NULL) /* only one string argument */
17040 break;
17041 ++lnum;
17042 }
17043
17044 if (added > 0)
17045 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046}
17047
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017048static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17049
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017051 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017052 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017053 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017054set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017055 win_T *wp UNUSED;
17056 typval_T *list_arg UNUSED;
17057 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017058 typval_T *rettv;
17059{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017060#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017061 char_u *act;
17062 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017063#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017064
Bram Moolenaar2641f772005-03-25 21:58:17 +000017065 rettv->vval.v_number = -1;
17066
17067#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017068 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017069 EMSG(_(e_listreq));
17070 else
17071 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017072 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017073
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017074 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017075 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017076 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 if (act == NULL)
17078 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017079 if (*act == 'a' || *act == 'r')
17080 action = *act;
17081 }
17082
Bram Moolenaar81484f42012-12-05 15:16:47 +010017083 if (l != NULL && set_errorlist(wp, l, action,
17084 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017085 rettv->vval.v_number = 0;
17086 }
17087#endif
17088}
17089
17090/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017091 * "setloclist()" function
17092 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017093 static void
17094f_setloclist(argvars, rettv)
17095 typval_T *argvars;
17096 typval_T *rettv;
17097{
17098 win_T *win;
17099
17100 rettv->vval.v_number = -1;
17101
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017102 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017103 if (win != NULL)
17104 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17105}
17106
17107/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017108 * "setmatches()" function
17109 */
17110 static void
17111f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017112 typval_T *argvars UNUSED;
17113 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017114{
17115#ifdef FEAT_SEARCH_EXTRA
17116 list_T *l;
17117 listitem_T *li;
17118 dict_T *d;
17119
17120 rettv->vval.v_number = -1;
17121 if (argvars[0].v_type != VAR_LIST)
17122 {
17123 EMSG(_(e_listreq));
17124 return;
17125 }
17126 if ((l = argvars[0].vval.v_list) != NULL)
17127 {
17128
17129 /* To some extent make sure that we are dealing with a list from
17130 * "getmatches()". */
17131 li = l->lv_first;
17132 while (li != NULL)
17133 {
17134 if (li->li_tv.v_type != VAR_DICT
17135 || (d = li->li_tv.vval.v_dict) == NULL)
17136 {
17137 EMSG(_(e_invarg));
17138 return;
17139 }
17140 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17141 && dict_find(d, (char_u *)"pattern", -1) != NULL
17142 && dict_find(d, (char_u *)"priority", -1) != NULL
17143 && dict_find(d, (char_u *)"id", -1) != NULL))
17144 {
17145 EMSG(_(e_invarg));
17146 return;
17147 }
17148 li = li->li_next;
17149 }
17150
17151 clear_matches(curwin);
17152 li = l->lv_first;
17153 while (li != NULL)
17154 {
17155 d = li->li_tv.vval.v_dict;
17156 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17157 get_dict_string(d, (char_u *)"pattern", FALSE),
17158 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017159 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017160 li = li->li_next;
17161 }
17162 rettv->vval.v_number = 0;
17163 }
17164#endif
17165}
17166
17167/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017168 * "setpos()" function
17169 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017170 static void
17171f_setpos(argvars, rettv)
17172 typval_T *argvars;
17173 typval_T *rettv;
17174{
17175 pos_T pos;
17176 int fnum;
17177 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017178 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017179
Bram Moolenaar08250432008-02-13 11:42:46 +000017180 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017181 name = get_tv_string_chk(argvars);
17182 if (name != NULL)
17183 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017184 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017185 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017186 if (--pos.col < 0)
17187 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017188 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017189 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017190 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017191 if (fnum == curbuf->b_fnum)
17192 {
17193 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017194 if (curswant >= 0)
17195 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017196 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017197 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017198 }
17199 else
17200 EMSG(_(e_invarg));
17201 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017202 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17203 {
17204 /* set mark */
17205 if (setmark_pos(name[1], &pos, fnum) == OK)
17206 rettv->vval.v_number = 0;
17207 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017208 else
17209 EMSG(_(e_invarg));
17210 }
17211 }
17212}
17213
17214/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017215 * "setqflist()" function
17216 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017217 static void
17218f_setqflist(argvars, rettv)
17219 typval_T *argvars;
17220 typval_T *rettv;
17221{
17222 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17223}
17224
17225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 * "setreg()" function
17227 */
17228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017230 typval_T *argvars;
17231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232{
17233 int regname;
17234 char_u *strregname;
17235 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017236 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 int append;
17238 char_u yank_type;
17239 long block_len;
17240
17241 block_len = -1;
17242 yank_type = MAUTO;
17243 append = FALSE;
17244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017246 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017248 if (strregname == NULL)
17249 return; /* type error; errmsg already given */
17250 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 if (regname == 0 || regname == '@')
17252 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017254 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 stropt = get_tv_string_chk(&argvars[2]);
17257 if (stropt == NULL)
17258 return; /* type error */
17259 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 switch (*stropt)
17261 {
17262 case 'a': case 'A': /* append */
17263 append = TRUE;
17264 break;
17265 case 'v': case 'c': /* character-wise selection */
17266 yank_type = MCHAR;
17267 break;
17268 case 'V': case 'l': /* line-wise selection */
17269 yank_type = MLINE;
17270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 case 'b': case Ctrl_V: /* block-wise selection */
17272 yank_type = MBLOCK;
17273 if (VIM_ISDIGIT(stropt[1]))
17274 {
17275 ++stropt;
17276 block_len = getdigits(&stropt) - 1;
17277 --stropt;
17278 }
17279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 }
17281 }
17282
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017283 if (argvars[1].v_type == VAR_LIST)
17284 {
17285 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017286 char_u **allocval;
17287 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017288 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017289 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017290 int len = argvars[1].vval.v_list->lv_len;
17291 listitem_T *li;
17292
Bram Moolenaar7d647822014-04-05 21:28:56 +020017293 /* First half: use for pointers to result lines; second half: use for
17294 * pointers to allocated copies. */
17295 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017296 if (lstval == NULL)
17297 return;
17298 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017299 allocval = lstval + len + 2;
17300 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017301
17302 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17303 li = li->li_next)
17304 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017305 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017306 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017307 goto free_lstval;
17308 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017309 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017310 /* Need to make a copy, next get_tv_string_buf_chk() will
17311 * overwrite the string. */
17312 strval = vim_strsave(buf);
17313 if (strval == NULL)
17314 goto free_lstval;
17315 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017316 }
17317 *curval++ = strval;
17318 }
17319 *curval++ = NULL;
17320
17321 write_reg_contents_lst(regname, lstval, -1,
17322 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017323free_lstval:
17324 while (curallocval > allocval)
17325 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017326 vim_free(lstval);
17327 }
17328 else
17329 {
17330 strval = get_tv_string_chk(&argvars[1]);
17331 if (strval == NULL)
17332 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017333 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337}
17338
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017339/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017340 * "settabvar()" function
17341 */
17342 static void
17343f_settabvar(argvars, rettv)
17344 typval_T *argvars;
17345 typval_T *rettv;
17346{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017347#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017348 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017349 tabpage_T *tp;
17350#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017351 char_u *varname, *tabvarname;
17352 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017353
17354 rettv->vval.v_number = 0;
17355
17356 if (check_restricted() || check_secure())
17357 return;
17358
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017359#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017360 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017361#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017362 varname = get_tv_string_chk(&argvars[1]);
17363 varp = &argvars[2];
17364
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017365 if (varname != NULL && varp != NULL
17366#ifdef FEAT_WINDOWS
17367 && tp != NULL
17368#endif
17369 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017370 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017371#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017372 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017373 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017374#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017375
17376 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17377 if (tabvarname != NULL)
17378 {
17379 STRCPY(tabvarname, "t:");
17380 STRCPY(tabvarname + 2, varname);
17381 set_var(tabvarname, varp, TRUE);
17382 vim_free(tabvarname);
17383 }
17384
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017385#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017386 /* Restore current tabpage */
17387 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017388 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017389#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017390 }
17391}
17392
17393/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017394 * "settabwinvar()" function
17395 */
17396 static void
17397f_settabwinvar(argvars, rettv)
17398 typval_T *argvars;
17399 typval_T *rettv;
17400{
17401 setwinvar(argvars, rettv, 1);
17402}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403
17404/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017405 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 typval_T *argvars;
17410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017412 setwinvar(argvars, rettv, 0);
17413}
17414
17415/*
17416 * "setwinvar()" and "settabwinvar()" functions
17417 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017418
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017419 static void
17420setwinvar(argvars, rettv, off)
17421 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017422 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017423 int off;
17424{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 win_T *win;
17426#ifdef FEAT_WINDOWS
17427 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017428 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429#endif
17430 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017431 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017433 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434
17435 if (check_restricted() || check_secure())
17436 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017437
17438#ifdef FEAT_WINDOWS
17439 if (off == 1)
17440 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17441 else
17442 tp = curtab;
17443#endif
17444 win = find_win_by_nr(&argvars[off], tp);
17445 varname = get_tv_string_chk(&argvars[off + 1]);
17446 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447
17448 if (win != NULL && varname != NULL && varp != NULL)
17449 {
17450#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017451 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017454 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017456 long numval;
17457 char_u *strval;
17458 int error = FALSE;
17459
17460 ++varname;
17461 numval = get_tv_number_chk(varp, &error);
17462 strval = get_tv_string_buf_chk(varp, nbuf);
17463 if (!error && strval != NULL)
17464 set_option_value(varname, numval, strval, OPT_LOCAL);
17465 }
17466 else
17467 {
17468 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17469 if (winvarname != NULL)
17470 {
17471 STRCPY(winvarname, "w:");
17472 STRCPY(winvarname + 2, varname);
17473 set_var(winvarname, varp, TRUE);
17474 vim_free(winvarname);
17475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 }
17477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017479 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480#endif
17481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482}
17483
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017484#ifdef FEAT_CRYPT
17485/*
17486 * "sha256({string})" function
17487 */
17488 static void
17489f_sha256(argvars, rettv)
17490 typval_T *argvars;
17491 typval_T *rettv;
17492{
17493 char_u *p;
17494
17495 p = get_tv_string(&argvars[0]);
17496 rettv->vval.v_string = vim_strsave(
17497 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17498 rettv->v_type = VAR_STRING;
17499}
17500#endif /* FEAT_CRYPT */
17501
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017503 * "shellescape({string})" function
17504 */
17505 static void
17506f_shellescape(argvars, rettv)
17507 typval_T *argvars;
17508 typval_T *rettv;
17509{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017510 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017511 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017512 rettv->v_type = VAR_STRING;
17513}
17514
17515/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017516 * shiftwidth() function
17517 */
17518 static void
17519f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017520 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017521 typval_T *rettv;
17522{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017523 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017524}
17525
17526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017527 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 */
17529 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 typval_T *argvars;
17532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535
Bram Moolenaar0d660222005-01-07 21:51:51 +000017536 p = get_tv_string(&argvars[0]);
17537 rettv->vval.v_string = vim_strsave(p);
17538 simplify_filename(rettv->vval.v_string); /* simplify in place */
17539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540}
17541
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017542#ifdef FEAT_FLOAT
17543/*
17544 * "sin()" function
17545 */
17546 static void
17547f_sin(argvars, rettv)
17548 typval_T *argvars;
17549 typval_T *rettv;
17550{
17551 float_T f;
17552
17553 rettv->v_type = VAR_FLOAT;
17554 if (get_float_arg(argvars, &f) == OK)
17555 rettv->vval.v_float = sin(f);
17556 else
17557 rettv->vval.v_float = 0.0;
17558}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017559
17560/*
17561 * "sinh()" function
17562 */
17563 static void
17564f_sinh(argvars, rettv)
17565 typval_T *argvars;
17566 typval_T *rettv;
17567{
17568 float_T f;
17569
17570 rettv->v_type = VAR_FLOAT;
17571 if (get_float_arg(argvars, &f) == OK)
17572 rettv->vval.v_float = sinh(f);
17573 else
17574 rettv->vval.v_float = 0.0;
17575}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017576#endif
17577
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578static int
17579#ifdef __BORLANDC__
17580 _RTLENTRYF
17581#endif
17582 item_compare __ARGS((const void *s1, const void *s2));
17583static int
17584#ifdef __BORLANDC__
17585 _RTLENTRYF
17586#endif
17587 item_compare2 __ARGS((const void *s1, const void *s2));
17588
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017589/* struct used in the array that's given to qsort() */
17590typedef struct
17591{
17592 listitem_T *item;
17593 int idx;
17594} sortItem_T;
17595
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017597static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017599static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017600static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017601static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017602static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603#define ITEM_COMPARE_FAIL 999
17604
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017606 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017608 static int
17609#ifdef __BORLANDC__
17610_RTLENTRYF
17611#endif
17612item_compare(s1, s2)
17613 const void *s1;
17614 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017616 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017617 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017618 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017619 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017620 int res;
17621 char_u numbuf1[NUMBUFLEN];
17622 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017624 si1 = (sortItem_T *)s1;
17625 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017626 tv1 = &si1->item->li_tv;
17627 tv2 = &si2->item->li_tv;
17628 /* tv2string() puts quotes around a string and allocates memory. Don't do
17629 * that for string variables. Use a single quote when comparing with a
17630 * non-string to do what the docs promise. */
17631 if (tv1->v_type == VAR_STRING)
17632 {
17633 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17634 p1 = (char_u *)"'";
17635 else
17636 p1 = tv1->vval.v_string;
17637 }
17638 else
17639 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17640 if (tv2->v_type == VAR_STRING)
17641 {
17642 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17643 p2 = (char_u *)"'";
17644 else
17645 p2 = tv2->vval.v_string;
17646 }
17647 else
17648 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017649 if (p1 == NULL)
17650 p1 = (char_u *)"";
17651 if (p2 == NULL)
17652 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017653 if (!item_compare_numeric)
17654 {
17655 if (item_compare_ic)
17656 res = STRICMP(p1, p2);
17657 else
17658 res = STRCMP(p1, p2);
17659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017661 {
17662 double n1, n2;
17663 n1 = strtod((char *)p1, (char **)&p1);
17664 n2 = strtod((char *)p2, (char **)&p2);
17665 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17666 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017667
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017668 /* When the result would be zero, compare the item indexes. Makes the
17669 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017670 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017671 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017672
Bram Moolenaar0d660222005-01-07 21:51:51 +000017673 vim_free(tofree1);
17674 vim_free(tofree2);
17675 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676}
17677
17678 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017679#ifdef __BORLANDC__
17680_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017682item_compare2(s1, s2)
17683 const void *s1;
17684 const void *s2;
17685{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017686 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017688 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017689 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017690 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017692 /* shortcut after failure in previous call; compare all items equal */
17693 if (item_compare_func_err)
17694 return 0;
17695
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017696 si1 = (sortItem_T *)s1;
17697 si2 = (sortItem_T *)s2;
17698
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017699 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017700 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017701 copy_tv(&si1->item->li_tv, &argv[0]);
17702 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703
17704 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017705 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017706 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17707 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708 clear_tv(&argv[0]);
17709 clear_tv(&argv[1]);
17710
17711 if (res == FAIL)
17712 res = ITEM_COMPARE_FAIL;
17713 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017714 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17715 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017716 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017717 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017718
17719 /* When the result would be zero, compare the pointers themselves. Makes
17720 * the sort stable. */
17721 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017722 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017723
Bram Moolenaar0d660222005-01-07 21:51:51 +000017724 return res;
17725}
17726
17727/*
17728 * "sort({list})" function
17729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017731do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017732 typval_T *argvars;
17733 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017734 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735{
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 list_T *l;
17737 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017738 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 long len;
17740 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017743 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 else
17745 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017746 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017747 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017748 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17749 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017750 return;
17751 rettv->vval.v_list = l;
17752 rettv->v_type = VAR_LIST;
17753 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754
Bram Moolenaar0d660222005-01-07 21:51:51 +000017755 len = list_len(l);
17756 if (len <= 1)
17757 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758
Bram Moolenaar0d660222005-01-07 21:51:51 +000017759 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017760 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017762 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763 if (argvars[1].v_type != VAR_UNKNOWN)
17764 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017765 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017768 else
17769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017770 int error = FALSE;
17771
17772 i = get_tv_number_chk(&argvars[1], &error);
17773 if (error)
17774 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017775 if (i == 1)
17776 item_compare_ic = TRUE;
17777 else
17778 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017779 if (item_compare_func != NULL)
17780 {
17781 if (STRCMP(item_compare_func, "n") == 0)
17782 {
17783 item_compare_func = NULL;
17784 item_compare_numeric = TRUE;
17785 }
17786 else if (STRCMP(item_compare_func, "i") == 0)
17787 {
17788 item_compare_func = NULL;
17789 item_compare_ic = TRUE;
17790 }
17791 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017793
17794 if (argvars[2].v_type != VAR_UNKNOWN)
17795 {
17796 /* optional third argument: {dict} */
17797 if (argvars[2].v_type != VAR_DICT)
17798 {
17799 EMSG(_(e_dictreq));
17800 return;
17801 }
17802 item_compare_selfdict = argvars[2].vval.v_dict;
17803 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaar0d660222005-01-07 21:51:51 +000017806 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017807 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017808 if (ptrs == NULL)
17809 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
Bram Moolenaar327aa022014-03-25 18:24:23 +010017811 i = 0;
17812 if (sort)
17813 {
17814 /* sort(): ptrs will be the list to sort */
17815 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017816 {
17817 ptrs[i].item = li;
17818 ptrs[i].idx = i;
17819 ++i;
17820 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017821
17822 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017823 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017824 /* test the compare function */
17825 if (item_compare_func != NULL
17826 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017827 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017828 EMSG(_("E702: Sort compare function failed"));
17829 else
17830 {
17831 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017832 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017833 item_compare_func == NULL ? item_compare : item_compare2);
17834
17835 if (!item_compare_func_err)
17836 {
17837 /* Clear the List and append the items in sorted order. */
17838 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17839 l->lv_len = 0;
17840 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017841 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017842 }
17843 }
17844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017846 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017847 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17848
17849 /* f_uniq(): ptrs will be a stack of items to remove */
17850 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017851 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017852 item_compare_func_ptr = item_compare_func
17853 ? item_compare2 : item_compare;
17854
17855 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17856 li = li->li_next)
17857 {
17858 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17859 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017860 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017861 if (item_compare_func_err)
17862 {
17863 EMSG(_("E882: Uniq compare function failed"));
17864 break;
17865 }
17866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017868 if (!item_compare_func_err)
17869 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017870 while (--i >= 0)
17871 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017872 li = ptrs[i].item->li_next;
17873 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017874 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017875 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017876 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017877 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017878 list_fix_watch(l, li);
17879 listitem_free(li);
17880 l->lv_len--;
17881 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017882 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017883 }
17884
17885 vim_free(ptrs);
17886 }
17887}
17888
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017889/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017890 * "sort({list})" function
17891 */
17892 static void
17893f_sort(argvars, rettv)
17894 typval_T *argvars;
17895 typval_T *rettv;
17896{
17897 do_sort_uniq(argvars, rettv, TRUE);
17898}
17899
17900/*
17901 * "uniq({list})" function
17902 */
17903 static void
17904f_uniq(argvars, rettv)
17905 typval_T *argvars;
17906 typval_T *rettv;
17907{
17908 do_sort_uniq(argvars, rettv, FALSE);
17909}
17910
17911/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017912 * "soundfold({word})" function
17913 */
17914 static void
17915f_soundfold(argvars, rettv)
17916 typval_T *argvars;
17917 typval_T *rettv;
17918{
17919 char_u *s;
17920
17921 rettv->v_type = VAR_STRING;
17922 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017923#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017924 rettv->vval.v_string = eval_soundfold(s);
17925#else
17926 rettv->vval.v_string = vim_strsave(s);
17927#endif
17928}
17929
17930/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017931 * "spellbadword()" function
17932 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017933 static void
17934f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017935 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017936 typval_T *rettv;
17937{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017938 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017939 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017940 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017942 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017943 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017944
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017945#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017946 if (argvars[0].v_type == VAR_UNKNOWN)
17947 {
17948 /* Find the start and length of the badly spelled word. */
17949 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17950 if (len != 0)
17951 word = ml_get_cursor();
17952 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017953 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017954 {
17955 char_u *str = get_tv_string_chk(&argvars[0]);
17956 int capcol = -1;
17957
17958 if (str != NULL)
17959 {
17960 /* Check the argument for spelling. */
17961 while (*str != NUL)
17962 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017963 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017964 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017965 {
17966 word = str;
17967 break;
17968 }
17969 str += len;
17970 }
17971 }
17972 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017973#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017974
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017975 list_append_string(rettv->vval.v_list, word, len);
17976 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017977 attr == HLF_SPB ? "bad" :
17978 attr == HLF_SPR ? "rare" :
17979 attr == HLF_SPL ? "local" :
17980 attr == HLF_SPC ? "caps" :
17981 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017982}
17983
17984/*
17985 * "spellsuggest()" function
17986 */
17987 static void
17988f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017989 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017990 typval_T *rettv;
17991{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017992#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017993 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017994 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017995 int maxcount;
17996 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017997 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017998 listitem_T *li;
17999 int need_capital = FALSE;
18000#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018001
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018002 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018003 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018004
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018005#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018006 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018007 {
18008 str = get_tv_string(&argvars[0]);
18009 if (argvars[1].v_type != VAR_UNKNOWN)
18010 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018011 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018012 if (maxcount <= 0)
18013 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018014 if (argvars[2].v_type != VAR_UNKNOWN)
18015 {
18016 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18017 if (typeerr)
18018 return;
18019 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018020 }
18021 else
18022 maxcount = 25;
18023
Bram Moolenaar4770d092006-01-12 23:22:24 +000018024 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018025
18026 for (i = 0; i < ga.ga_len; ++i)
18027 {
18028 str = ((char_u **)ga.ga_data)[i];
18029
18030 li = listitem_alloc();
18031 if (li == NULL)
18032 vim_free(str);
18033 else
18034 {
18035 li->li_tv.v_type = VAR_STRING;
18036 li->li_tv.v_lock = 0;
18037 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018038 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018039 }
18040 }
18041 ga_clear(&ga);
18042 }
18043#endif
18044}
18045
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018047f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 typval_T *argvars;
18049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018050{
18051 char_u *str;
18052 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018053 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 regmatch_T regmatch;
18055 char_u patbuf[NUMBUFLEN];
18056 char_u *save_cpo;
18057 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018058 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018059 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018060 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018061
18062 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18063 save_cpo = p_cpo;
18064 p_cpo = (char_u *)"";
18065
18066 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018067 if (argvars[1].v_type != VAR_UNKNOWN)
18068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018069 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18070 if (pat == NULL)
18071 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018073 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018074 }
18075 if (pat == NULL || *pat == NUL)
18076 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018078 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018080 if (typeerr)
18081 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18084 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018086 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018087 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018089 if (*str == NUL)
18090 match = FALSE; /* empty item at the end */
18091 else
18092 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018093 if (match)
18094 end = regmatch.startp[0];
18095 else
18096 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018097 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18098 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018100 if (list_append_string(rettv->vval.v_list, str,
18101 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018102 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103 }
18104 if (!match)
18105 break;
18106 /* Advance to just after the match. */
18107 if (regmatch.endp[0] > str)
18108 col = 0;
18109 else
18110 {
18111 /* Don't get stuck at the same match. */
18112#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018113 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018114#else
18115 col = 1;
18116#endif
18117 }
18118 str = regmatch.endp[0];
18119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120
Bram Moolenaar473de612013-06-08 18:19:48 +020018121 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123
Bram Moolenaar0d660222005-01-07 21:51:51 +000018124 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125}
18126
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018127#ifdef FEAT_FLOAT
18128/*
18129 * "sqrt()" function
18130 */
18131 static void
18132f_sqrt(argvars, rettv)
18133 typval_T *argvars;
18134 typval_T *rettv;
18135{
18136 float_T f;
18137
18138 rettv->v_type = VAR_FLOAT;
18139 if (get_float_arg(argvars, &f) == OK)
18140 rettv->vval.v_float = sqrt(f);
18141 else
18142 rettv->vval.v_float = 0.0;
18143}
18144
18145/*
18146 * "str2float()" function
18147 */
18148 static void
18149f_str2float(argvars, rettv)
18150 typval_T *argvars;
18151 typval_T *rettv;
18152{
18153 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18154
18155 if (*p == '+')
18156 p = skipwhite(p + 1);
18157 (void)string2float(p, &rettv->vval.v_float);
18158 rettv->v_type = VAR_FLOAT;
18159}
18160#endif
18161
Bram Moolenaar2c932302006-03-18 21:42:09 +000018162/*
18163 * "str2nr()" function
18164 */
18165 static void
18166f_str2nr(argvars, rettv)
18167 typval_T *argvars;
18168 typval_T *rettv;
18169{
18170 int base = 10;
18171 char_u *p;
18172 long n;
18173
18174 if (argvars[1].v_type != VAR_UNKNOWN)
18175 {
18176 base = get_tv_number(&argvars[1]);
18177 if (base != 8 && base != 10 && base != 16)
18178 {
18179 EMSG(_(e_invarg));
18180 return;
18181 }
18182 }
18183
18184 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018185 if (*p == '+')
18186 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018187 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18188 rettv->vval.v_number = n;
18189}
18190
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191#ifdef HAVE_STRFTIME
18192/*
18193 * "strftime({format}[, {time}])" function
18194 */
18195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018196f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018197 typval_T *argvars;
18198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199{
18200 char_u result_buf[256];
18201 struct tm *curtime;
18202 time_t seconds;
18203 char_u *p;
18204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018207 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018208 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 seconds = time(NULL);
18210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 curtime = localtime(&seconds);
18213 /* MSVC returns NULL for an invalid value of seconds. */
18214 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018215 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 else
18217 {
18218# ifdef FEAT_MBYTE
18219 vimconv_T conv;
18220 char_u *enc;
18221
18222 conv.vc_type = CONV_NONE;
18223 enc = enc_locale();
18224 convert_setup(&conv, p_enc, enc);
18225 if (conv.vc_type != CONV_NONE)
18226 p = string_convert(&conv, p, NULL);
18227# endif
18228 if (p != NULL)
18229 (void)strftime((char *)result_buf, sizeof(result_buf),
18230 (char *)p, curtime);
18231 else
18232 result_buf[0] = NUL;
18233
18234# ifdef FEAT_MBYTE
18235 if (conv.vc_type != CONV_NONE)
18236 vim_free(p);
18237 convert_setup(&conv, enc, p_enc);
18238 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018239 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 else
18241# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243
18244# ifdef FEAT_MBYTE
18245 /* Release conversion descriptors */
18246 convert_setup(&conv, NULL, NULL);
18247 vim_free(enc);
18248# endif
18249 }
18250}
18251#endif
18252
18253/*
18254 * "stridx()" function
18255 */
18256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018258 typval_T *argvars;
18259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260{
18261 char_u buf[NUMBUFLEN];
18262 char_u *needle;
18263 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018264 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018268 needle = get_tv_string_chk(&argvars[1]);
18269 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018270 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018271 if (needle == NULL || haystack == NULL)
18272 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
Bram Moolenaar33570922005-01-25 22:26:29 +000018274 if (argvars[2].v_type != VAR_UNKNOWN)
18275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018276 int error = FALSE;
18277
18278 start_idx = get_tv_number_chk(&argvars[2], &error);
18279 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018280 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018281 if (start_idx >= 0)
18282 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018283 }
18284
18285 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18286 if (pos != NULL)
18287 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288}
18289
18290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018291 * "string()" function
18292 */
18293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018295 typval_T *argvars;
18296 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018297{
18298 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018299 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018302 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018303 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018304 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306}
18307
18308/*
18309 * "strlen()" function
18310 */
18311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018312f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 typval_T *argvars;
18314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018316 rettv->vval.v_number = (varnumber_T)(STRLEN(
18317 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318}
18319
18320/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018321 * "strchars()" function
18322 */
18323 static void
18324f_strchars(argvars, rettv)
18325 typval_T *argvars;
18326 typval_T *rettv;
18327{
18328 char_u *s = get_tv_string(&argvars[0]);
18329#ifdef FEAT_MBYTE
18330 varnumber_T len = 0;
18331
18332 while (*s != NUL)
18333 {
18334 mb_cptr2char_adv(&s);
18335 ++len;
18336 }
18337 rettv->vval.v_number = len;
18338#else
18339 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18340#endif
18341}
18342
18343/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018344 * "strdisplaywidth()" function
18345 */
18346 static void
18347f_strdisplaywidth(argvars, rettv)
18348 typval_T *argvars;
18349 typval_T *rettv;
18350{
18351 char_u *s = get_tv_string(&argvars[0]);
18352 int col = 0;
18353
18354 if (argvars[1].v_type != VAR_UNKNOWN)
18355 col = get_tv_number(&argvars[1]);
18356
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018357 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018358}
18359
18360/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018361 * "strwidth()" function
18362 */
18363 static void
18364f_strwidth(argvars, rettv)
18365 typval_T *argvars;
18366 typval_T *rettv;
18367{
18368 char_u *s = get_tv_string(&argvars[0]);
18369
18370 rettv->vval.v_number = (varnumber_T)(
18371#ifdef FEAT_MBYTE
18372 mb_string2cells(s, -1)
18373#else
18374 STRLEN(s)
18375#endif
18376 );
18377}
18378
18379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 * "strpart()" function
18381 */
18382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018384 typval_T *argvars;
18385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
18387 char_u *p;
18388 int n;
18389 int len;
18390 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018391 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018393 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 slen = (int)STRLEN(p);
18395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018396 n = get_tv_number_chk(&argvars[1], &error);
18397 if (error)
18398 len = 0;
18399 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018400 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 else
18402 len = slen - n; /* default len: all bytes that are available. */
18403
18404 /*
18405 * Only return the overlap between the specified part and the actual
18406 * string.
18407 */
18408 if (n < 0)
18409 {
18410 len += n;
18411 n = 0;
18412 }
18413 else if (n > slen)
18414 n = slen;
18415 if (len < 0)
18416 len = 0;
18417 else if (n + len > slen)
18418 len = slen - n;
18419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420 rettv->v_type = VAR_STRING;
18421 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422}
18423
18424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425 * "strridx()" function
18426 */
18427 static void
18428f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018429 typval_T *argvars;
18430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431{
18432 char_u buf[NUMBUFLEN];
18433 char_u *needle;
18434 char_u *haystack;
18435 char_u *rest;
18436 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018437 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018439 needle = get_tv_string_chk(&argvars[1]);
18440 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018441
18442 rettv->vval.v_number = -1;
18443 if (needle == NULL || haystack == NULL)
18444 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018445
18446 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018447 if (argvars[2].v_type != VAR_UNKNOWN)
18448 {
18449 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018451 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018452 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018453 }
18454 else
18455 end_idx = haystack_len;
18456
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018458 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018459 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018460 lastmatch = haystack + end_idx;
18461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018462 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018463 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 for (rest = haystack; *rest != '\0'; ++rest)
18465 {
18466 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018467 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468 break;
18469 lastmatch = rest;
18470 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018471 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472
18473 if (lastmatch == NULL)
18474 rettv->vval.v_number = -1;
18475 else
18476 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18477}
18478
18479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 * "strtrans()" function
18481 */
18482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483f_strtrans(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->v_type = VAR_STRING;
18488 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489}
18490
18491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018492 * "submatch()" function
18493 */
18494 static void
18495f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018496 typval_T *argvars;
18497 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018498{
Bram Moolenaar41571762014-04-02 19:00:58 +020018499 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018500 int no;
18501 int retList = 0;
18502
18503 no = (int)get_tv_number_chk(&argvars[0], &error);
18504 if (error)
18505 return;
18506 error = FALSE;
18507 if (argvars[1].v_type != VAR_UNKNOWN)
18508 retList = get_tv_number_chk(&argvars[1], &error);
18509 if (error)
18510 return;
18511
18512 if (retList == 0)
18513 {
18514 rettv->v_type = VAR_STRING;
18515 rettv->vval.v_string = reg_submatch(no);
18516 }
18517 else
18518 {
18519 rettv->v_type = VAR_LIST;
18520 rettv->vval.v_list = reg_submatch_list(no);
18521 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018522}
18523
18524/*
18525 * "substitute()" function
18526 */
18527 static void
18528f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 typval_T *argvars;
18530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018531{
18532 char_u patbuf[NUMBUFLEN];
18533 char_u subbuf[NUMBUFLEN];
18534 char_u flagsbuf[NUMBUFLEN];
18535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018536 char_u *str = get_tv_string_chk(&argvars[0]);
18537 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18538 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18539 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18540
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018542 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18543 rettv->vval.v_string = NULL;
18544 else
18545 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018546}
18547
18548/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018549 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018552f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555{
18556 int id = 0;
18557#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018558 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 long col;
18560 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018561 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018563 lnum = get_tv_lnum(argvars); /* -1 on type error */
18564 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18565 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018567 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018568 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018569 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570#endif
18571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573}
18574
18575/*
18576 * "synIDattr(id, what [, mode])" function
18577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018580 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582{
18583 char_u *p = NULL;
18584#ifdef FEAT_SYN_HL
18585 int id;
18586 char_u *what;
18587 char_u *mode;
18588 char_u modebuf[NUMBUFLEN];
18589 int modec;
18590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 id = get_tv_number(&argvars[0]);
18592 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018597 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 modec = 0; /* replace invalid with current */
18599 }
18600 else
18601 {
18602#ifdef FEAT_GUI
18603 if (gui.in_use)
18604 modec = 'g';
18605 else
18606#endif
18607 if (t_colors > 1)
18608 modec = 'c';
18609 else
18610 modec = 't';
18611 }
18612
18613
18614 switch (TOLOWER_ASC(what[0]))
18615 {
18616 case 'b':
18617 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18618 p = highlight_color(id, what, modec);
18619 else /* bold */
18620 p = highlight_has_attr(id, HL_BOLD, modec);
18621 break;
18622
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018623 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 p = highlight_color(id, what, modec);
18625 break;
18626
18627 case 'i':
18628 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18629 p = highlight_has_attr(id, HL_INVERSE, modec);
18630 else /* italic */
18631 p = highlight_has_attr(id, HL_ITALIC, modec);
18632 break;
18633
18634 case 'n': /* name */
18635 p = get_highlight_name(NULL, id - 1);
18636 break;
18637
18638 case 'r': /* reverse */
18639 p = highlight_has_attr(id, HL_INVERSE, modec);
18640 break;
18641
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018642 case 's':
18643 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18644 p = highlight_color(id, what, modec);
18645 else /* standout */
18646 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 break;
18648
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018649 case 'u':
18650 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18651 /* underline */
18652 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18653 else
18654 /* undercurl */
18655 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 break;
18657 }
18658
18659 if (p != NULL)
18660 p = vim_strsave(p);
18661#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018662 rettv->v_type = VAR_STRING;
18663 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664}
18665
18666/*
18667 * "synIDtrans(id)" function
18668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673{
18674 int id;
18675
18676#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018677 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678
18679 if (id > 0)
18680 id = syn_get_final_id(id);
18681 else
18682#endif
18683 id = 0;
18684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686}
18687
18688/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018689 * "synconcealed(lnum, col)" function
18690 */
18691 static void
18692f_synconcealed(argvars, rettv)
18693 typval_T *argvars UNUSED;
18694 typval_T *rettv;
18695{
18696#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18697 long lnum;
18698 long col;
18699 int syntax_flags = 0;
18700 int cchar;
18701 int matchid = 0;
18702 char_u str[NUMBUFLEN];
18703#endif
18704
18705 rettv->v_type = VAR_LIST;
18706 rettv->vval.v_list = NULL;
18707
18708#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18709 lnum = get_tv_lnum(argvars); /* -1 on type error */
18710 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18711
18712 vim_memset(str, NUL, sizeof(str));
18713
18714 if (rettv_list_alloc(rettv) != FAIL)
18715 {
18716 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18717 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18718 && curwin->w_p_cole > 0)
18719 {
18720 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18721 syntax_flags = get_syntax_info(&matchid);
18722
18723 /* get the conceal character */
18724 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18725 {
18726 cchar = syn_get_sub_char();
18727 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18728 cchar = lcs_conceal;
18729 if (cchar != NUL)
18730 {
18731# ifdef FEAT_MBYTE
18732 if (has_mbyte)
18733 (*mb_char2bytes)(cchar, str);
18734 else
18735# endif
18736 str[0] = cchar;
18737 }
18738 }
18739 }
18740
18741 list_append_number(rettv->vval.v_list,
18742 (syntax_flags & HL_CONCEAL) != 0);
18743 /* -1 to auto-determine strlen */
18744 list_append_string(rettv->vval.v_list, str, -1);
18745 list_append_number(rettv->vval.v_list, matchid);
18746 }
18747#endif
18748}
18749
18750/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018751 * "synstack(lnum, col)" function
18752 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018753 static void
18754f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018755 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018756 typval_T *rettv;
18757{
18758#ifdef FEAT_SYN_HL
18759 long lnum;
18760 long col;
18761 int i;
18762 int id;
18763#endif
18764
18765 rettv->v_type = VAR_LIST;
18766 rettv->vval.v_list = NULL;
18767
18768#ifdef FEAT_SYN_HL
18769 lnum = get_tv_lnum(argvars); /* -1 on type error */
18770 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18771
18772 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018773 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018774 && rettv_list_alloc(rettv) != FAIL)
18775 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018776 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018777 for (i = 0; ; ++i)
18778 {
18779 id = syn_get_stack_item(i);
18780 if (id < 0)
18781 break;
18782 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18783 break;
18784 }
18785 }
18786#endif
18787}
18788
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018790get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T *argvars;
18792 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018793 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018795 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018797 char_u *infile = NULL;
18798 char_u buf[NUMBUFLEN];
18799 int err = FALSE;
18800 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018801 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018802 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018804 rettv->v_type = VAR_STRING;
18805 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018806 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018807 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018808
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018809 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018810 {
18811 /*
18812 * Write the string to a temp file, to be used for input of the shell
18813 * command.
18814 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018815 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018816 {
18817 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018818 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018819 }
18820
18821 fd = mch_fopen((char *)infile, WRITEBIN);
18822 if (fd == NULL)
18823 {
18824 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018825 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018826 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018827 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018828 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018829 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18830 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018831 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018832 else
18833 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018834 size_t len;
18835
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018836 p = get_tv_string_buf_chk(&argvars[1], buf);
18837 if (p == NULL)
18838 {
18839 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018840 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018841 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018842 len = STRLEN(p);
18843 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018844 err = TRUE;
18845 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018846 if (fclose(fd) != 0)
18847 err = TRUE;
18848 if (err)
18849 {
18850 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018851 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018852 }
18853 }
18854
Bram Moolenaar52a72462014-08-29 15:53:52 +020018855 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18856 * echoes typeahead, that messes up the display. */
18857 if (!msg_silent)
18858 flags += SHELL_COOKED;
18859
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018860 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018862 int len;
18863 listitem_T *li;
18864 char_u *s = NULL;
18865 char_u *start;
18866 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018867 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868
Bram Moolenaar52a72462014-08-29 15:53:52 +020018869 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018870 if (res == NULL)
18871 goto errret;
18872
18873 list = list_alloc();
18874 if (list == NULL)
18875 goto errret;
18876
18877 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018879 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018880 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018881 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018882 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018883
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018884 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018885 if (s == NULL)
18886 goto errret;
18887
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018888 for (p = s; start < end; ++p, ++start)
18889 *p = *start == NUL ? NL : *start;
18890 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018891
18892 li = listitem_alloc();
18893 if (li == NULL)
18894 {
18895 vim_free(s);
18896 goto errret;
18897 }
18898 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018899 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018900 li->li_tv.vval.v_string = s;
18901 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018903
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018904 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018905 rettv->v_type = VAR_LIST;
18906 rettv->vval.v_list = list;
18907 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018909 else
18910 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018911 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018912#ifdef USE_CR
18913 /* translate <CR> into <NL> */
18914 if (res != NULL)
18915 {
18916 char_u *s;
18917
18918 for (s = res; *s; ++s)
18919 {
18920 if (*s == CAR)
18921 *s = NL;
18922 }
18923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924#else
18925# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018926 /* translate <CR><NL> into <NL> */
18927 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018929 char_u *s, *d;
18930
18931 d = res;
18932 for (s = res; *s; ++s)
18933 {
18934 if (s[0] == CAR && s[1] == NL)
18935 ++s;
18936 *d++ = *s;
18937 }
18938 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940# endif
18941#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018942 rettv->vval.v_string = res;
18943 res = NULL;
18944 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018945
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018946errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018947 if (infile != NULL)
18948 {
18949 mch_remove(infile);
18950 vim_free(infile);
18951 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018952 if (res != NULL)
18953 vim_free(res);
18954 if (list != NULL)
18955 list_free(list, TRUE);
18956}
18957
18958/*
18959 * "system()" function
18960 */
18961 static void
18962f_system(argvars, rettv)
18963 typval_T *argvars;
18964 typval_T *rettv;
18965{
18966 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18967}
18968
18969/*
18970 * "systemlist()" function
18971 */
18972 static void
18973f_systemlist(argvars, rettv)
18974 typval_T *argvars;
18975 typval_T *rettv;
18976{
18977 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978}
18979
18980/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018981 * "tabpagebuflist()" function
18982 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018983 static void
18984f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018985 typval_T *argvars UNUSED;
18986 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018987{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018988#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018989 tabpage_T *tp;
18990 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018991
18992 if (argvars[0].v_type == VAR_UNKNOWN)
18993 wp = firstwin;
18994 else
18995 {
18996 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18997 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018998 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018999 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019000 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019001 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019002 for (; wp != NULL; wp = wp->w_next)
19003 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019004 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019005 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019006 }
19007#endif
19008}
19009
19010
19011/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019012 * "tabpagenr()" function
19013 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019014 static void
19015f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019016 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019017 typval_T *rettv;
19018{
19019 int nr = 1;
19020#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019021 char_u *arg;
19022
19023 if (argvars[0].v_type != VAR_UNKNOWN)
19024 {
19025 arg = get_tv_string_chk(&argvars[0]);
19026 nr = 0;
19027 if (arg != NULL)
19028 {
19029 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019030 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019031 else
19032 EMSG2(_(e_invexpr2), arg);
19033 }
19034 }
19035 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019036 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019037#endif
19038 rettv->vval.v_number = nr;
19039}
19040
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019041
19042#ifdef FEAT_WINDOWS
19043static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19044
19045/*
19046 * Common code for tabpagewinnr() and winnr().
19047 */
19048 static int
19049get_winnr(tp, argvar)
19050 tabpage_T *tp;
19051 typval_T *argvar;
19052{
19053 win_T *twin;
19054 int nr = 1;
19055 win_T *wp;
19056 char_u *arg;
19057
19058 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19059 if (argvar->v_type != VAR_UNKNOWN)
19060 {
19061 arg = get_tv_string_chk(argvar);
19062 if (arg == NULL)
19063 nr = 0; /* type error; errmsg already given */
19064 else if (STRCMP(arg, "$") == 0)
19065 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19066 else if (STRCMP(arg, "#") == 0)
19067 {
19068 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19069 if (twin == NULL)
19070 nr = 0;
19071 }
19072 else
19073 {
19074 EMSG2(_(e_invexpr2), arg);
19075 nr = 0;
19076 }
19077 }
19078
19079 if (nr > 0)
19080 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19081 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019082 {
19083 if (wp == NULL)
19084 {
19085 /* didn't find it in this tabpage */
19086 nr = 0;
19087 break;
19088 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019089 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019090 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019091 return nr;
19092}
19093#endif
19094
19095/*
19096 * "tabpagewinnr()" function
19097 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019098 static void
19099f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019100 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019101 typval_T *rettv;
19102{
19103 int nr = 1;
19104#ifdef FEAT_WINDOWS
19105 tabpage_T *tp;
19106
19107 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19108 if (tp == NULL)
19109 nr = 0;
19110 else
19111 nr = get_winnr(tp, &argvars[1]);
19112#endif
19113 rettv->vval.v_number = nr;
19114}
19115
19116
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019117/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019118 * "tagfiles()" function
19119 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019120 static void
19121f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019122 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019123 typval_T *rettv;
19124{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019125 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019126 tagname_T tn;
19127 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019128
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019129 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019130 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019131 fname = alloc(MAXPATHL);
19132 if (fname == NULL)
19133 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019134
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019135 for (first = TRUE; ; first = FALSE)
19136 if (get_tagfname(&tn, first, fname) == FAIL
19137 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019138 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019139 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019140 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019141}
19142
19143/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019144 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019145 */
19146 static void
19147f_taglist(argvars, rettv)
19148 typval_T *argvars;
19149 typval_T *rettv;
19150{
19151 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019152
19153 tag_pattern = get_tv_string(&argvars[0]);
19154
19155 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019156 if (*tag_pattern == NUL)
19157 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019158
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019159 if (rettv_list_alloc(rettv) == OK)
19160 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019161}
19162
19163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 * "tempname()" function
19165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170{
19171 static int x = 'A';
19172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019174 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175
19176 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19177 * names. Skip 'I' and 'O', they are used for shell redirection. */
19178 do
19179 {
19180 if (x == 'Z')
19181 x = '0';
19182 else if (x == '9')
19183 x = 'A';
19184 else
19185 {
19186#ifdef EBCDIC
19187 if (x == 'I')
19188 x = 'J';
19189 else if (x == 'R')
19190 x = 'S';
19191 else
19192#endif
19193 ++x;
19194 }
19195 } while (x == 'I' || x == 'O');
19196}
19197
19198/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019199 * "test(list)" function: Just checking the walls...
19200 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019201 static void
19202f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019203 typval_T *argvars UNUSED;
19204 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019205{
19206 /* Used for unit testing. Change the code below to your liking. */
19207#if 0
19208 listitem_T *li;
19209 list_T *l;
19210 char_u *bad, *good;
19211
19212 if (argvars[0].v_type != VAR_LIST)
19213 return;
19214 l = argvars[0].vval.v_list;
19215 if (l == NULL)
19216 return;
19217 li = l->lv_first;
19218 if (li == NULL)
19219 return;
19220 bad = get_tv_string(&li->li_tv);
19221 li = li->li_next;
19222 if (li == NULL)
19223 return;
19224 good = get_tv_string(&li->li_tv);
19225 rettv->vval.v_number = test_edit_score(bad, good);
19226#endif
19227}
19228
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019229#ifdef FEAT_FLOAT
19230/*
19231 * "tan()" function
19232 */
19233 static void
19234f_tan(argvars, rettv)
19235 typval_T *argvars;
19236 typval_T *rettv;
19237{
19238 float_T f;
19239
19240 rettv->v_type = VAR_FLOAT;
19241 if (get_float_arg(argvars, &f) == OK)
19242 rettv->vval.v_float = tan(f);
19243 else
19244 rettv->vval.v_float = 0.0;
19245}
19246
19247/*
19248 * "tanh()" function
19249 */
19250 static void
19251f_tanh(argvars, rettv)
19252 typval_T *argvars;
19253 typval_T *rettv;
19254{
19255 float_T f;
19256
19257 rettv->v_type = VAR_FLOAT;
19258 if (get_float_arg(argvars, &f) == OK)
19259 rettv->vval.v_float = tanh(f);
19260 else
19261 rettv->vval.v_float = 0.0;
19262}
19263#endif
19264
Bram Moolenaard52d9742005-08-21 22:20:28 +000019265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 * "tolower(string)" function
19267 */
19268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019269f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019270 typval_T *argvars;
19271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272{
19273 char_u *p;
19274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 p = vim_strsave(get_tv_string(&argvars[0]));
19276 rettv->v_type = VAR_STRING;
19277 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278
19279 if (p != NULL)
19280 while (*p != NUL)
19281 {
19282#ifdef FEAT_MBYTE
19283 int l;
19284
19285 if (enc_utf8)
19286 {
19287 int c, lc;
19288
19289 c = utf_ptr2char(p);
19290 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019291 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 /* TODO: reallocate string when byte count changes. */
19293 if (utf_char2len(lc) == l)
19294 utf_char2bytes(lc, p);
19295 p += l;
19296 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019297 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 p += l; /* skip multi-byte character */
19299 else
19300#endif
19301 {
19302 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19303 ++p;
19304 }
19305 }
19306}
19307
19308/*
19309 * "toupper(string)" function
19310 */
19311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019312f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019313 typval_T *argvars;
19314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019316 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019317 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318}
19319
19320/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019321 * "tr(string, fromstr, tostr)" function
19322 */
19323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019325 typval_T *argvars;
19326 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019327{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019328 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019329 char_u *fromstr;
19330 char_u *tostr;
19331 char_u *p;
19332#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019333 int inlen;
19334 int fromlen;
19335 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019336 int idx;
19337 char_u *cpstr;
19338 int cplen;
19339 int first = TRUE;
19340#endif
19341 char_u buf[NUMBUFLEN];
19342 char_u buf2[NUMBUFLEN];
19343 garray_T ga;
19344
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019345 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019346 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19347 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019348
19349 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 rettv->v_type = VAR_STRING;
19351 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 if (fromstr == NULL || tostr == NULL)
19353 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019354 ga_init2(&ga, (int)sizeof(char), 80);
19355
19356#ifdef FEAT_MBYTE
19357 if (!has_mbyte)
19358#endif
19359 /* not multi-byte: fromstr and tostr must be the same length */
19360 if (STRLEN(fromstr) != STRLEN(tostr))
19361 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019362#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019363error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019364#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019365 EMSG2(_(e_invarg2), fromstr);
19366 ga_clear(&ga);
19367 return;
19368 }
19369
19370 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019371 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019372 {
19373#ifdef FEAT_MBYTE
19374 if (has_mbyte)
19375 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019376 inlen = (*mb_ptr2len)(in_str);
19377 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019378 cplen = inlen;
19379 idx = 0;
19380 for (p = fromstr; *p != NUL; p += fromlen)
19381 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019382 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019383 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019384 {
19385 for (p = tostr; *p != NUL; p += tolen)
19386 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019387 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019388 if (idx-- == 0)
19389 {
19390 cplen = tolen;
19391 cpstr = p;
19392 break;
19393 }
19394 }
19395 if (*p == NUL) /* tostr is shorter than fromstr */
19396 goto error;
19397 break;
19398 }
19399 ++idx;
19400 }
19401
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019402 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019403 {
19404 /* Check that fromstr and tostr have the same number of
19405 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019406 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019407 first = FALSE;
19408 for (p = tostr; *p != NUL; p += tolen)
19409 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019410 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019411 --idx;
19412 }
19413 if (idx != 0)
19414 goto error;
19415 }
19416
19417 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019418 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019419 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019420
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019421 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019422 }
19423 else
19424#endif
19425 {
19426 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019427 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019428 if (p != NULL)
19429 ga_append(&ga, tostr[p - fromstr]);
19430 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019431 ga_append(&ga, *in_str);
19432 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019433 }
19434 }
19435
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019436 /* add a terminating NUL */
19437 ga_grow(&ga, 1);
19438 ga_append(&ga, NUL);
19439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019440 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019441}
19442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019443#ifdef FEAT_FLOAT
19444/*
19445 * "trunc({float})" function
19446 */
19447 static void
19448f_trunc(argvars, rettv)
19449 typval_T *argvars;
19450 typval_T *rettv;
19451{
19452 float_T f;
19453
19454 rettv->v_type = VAR_FLOAT;
19455 if (get_float_arg(argvars, &f) == OK)
19456 /* trunc() is not in C90, use floor() or ceil() instead. */
19457 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19458 else
19459 rettv->vval.v_float = 0.0;
19460}
19461#endif
19462
Bram Moolenaar8299df92004-07-10 09:47:34 +000019463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 * "type(expr)" function
19465 */
19466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019468 typval_T *argvars;
19469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019471 int n;
19472
19473 switch (argvars[0].v_type)
19474 {
19475 case VAR_NUMBER: n = 0; break;
19476 case VAR_STRING: n = 1; break;
19477 case VAR_FUNC: n = 2; break;
19478 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019479 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019480#ifdef FEAT_FLOAT
19481 case VAR_FLOAT: n = 5; break;
19482#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019483 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19484 }
19485 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486}
19487
19488/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019489 * "undofile(name)" function
19490 */
19491 static void
19492f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019493 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019494 typval_T *rettv;
19495{
19496 rettv->v_type = VAR_STRING;
19497#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019498 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019499 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019500
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019501 if (*fname == NUL)
19502 {
19503 /* If there is no file name there will be no undo file. */
19504 rettv->vval.v_string = NULL;
19505 }
19506 else
19507 {
19508 char_u *ffname = FullName_save(fname, FALSE);
19509
19510 if (ffname != NULL)
19511 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19512 vim_free(ffname);
19513 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019514 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019515#else
19516 rettv->vval.v_string = NULL;
19517#endif
19518}
19519
19520/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019521 * "undotree()" function
19522 */
19523 static void
19524f_undotree(argvars, rettv)
19525 typval_T *argvars UNUSED;
19526 typval_T *rettv;
19527{
19528 if (rettv_dict_alloc(rettv) == OK)
19529 {
19530 dict_T *dict = rettv->vval.v_dict;
19531 list_T *list;
19532
Bram Moolenaar730cde92010-06-27 05:18:54 +020019533 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019534 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019535 dict_add_nr_str(dict, "save_last",
19536 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019537 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19538 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019539 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019540
19541 list = list_alloc();
19542 if (list != NULL)
19543 {
19544 u_eval_tree(curbuf->b_u_oldhead, list);
19545 dict_add_list(dict, "entries", list);
19546 }
19547 }
19548}
19549
19550/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019551 * "values(dict)" function
19552 */
19553 static void
19554f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 typval_T *argvars;
19556 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019557{
19558 dict_list(argvars, rettv, 1);
19559}
19560
19561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 * "virtcol(string)" function
19563 */
19564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019566 typval_T *argvars;
19567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568{
19569 colnr_T vcol = 0;
19570 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019571 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019573 fp = var2fpos(&argvars[0], FALSE, &fnum);
19574 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19575 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 {
19577 getvvcol(curwin, fp, NULL, NULL, &vcol);
19578 ++vcol;
19579 }
19580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582}
19583
19584/*
19585 * "visualmode()" function
19586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019588f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019589 typval_T *argvars UNUSED;
19590 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 char_u str[2];
19593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019594 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 str[0] = curbuf->b_visual_mode_eval;
19596 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598
19599 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019600 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602}
19603
19604/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019605 * "wildmenumode()" function
19606 */
19607 static void
19608f_wildmenumode(argvars, rettv)
19609 typval_T *argvars UNUSED;
19610 typval_T *rettv UNUSED;
19611{
19612#ifdef FEAT_WILDMENU
19613 if (wild_menu_showing)
19614 rettv->vval.v_number = 1;
19615#endif
19616}
19617
19618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 * "winbufnr(nr)" function
19620 */
19621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 typval_T *argvars;
19624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625{
19626 win_T *wp;
19627
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019628 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633}
19634
19635/*
19636 * "wincol()" function
19637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019639f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019640 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642{
19643 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019644 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645}
19646
19647/*
19648 * "winheight(nr)" function
19649 */
19650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019651f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 typval_T *argvars;
19653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654{
19655 win_T *wp;
19656
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019657 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662}
19663
19664/*
19665 * "winline()" function
19666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019669 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671{
19672 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019673 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674}
19675
19676/*
19677 * "winnr()" function
19678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019680f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683{
19684 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019685
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019687 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019689 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690}
19691
19692/*
19693 * "winrestcmd()" function
19694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019696f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699{
19700#ifdef FEAT_WINDOWS
19701 win_T *wp;
19702 int winnr = 1;
19703 garray_T ga;
19704 char_u buf[50];
19705
19706 ga_init2(&ga, (int)sizeof(char), 70);
19707 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19708 {
19709 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19710 ga_concat(&ga, buf);
19711# ifdef FEAT_VERTSPLIT
19712 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19713 ga_concat(&ga, buf);
19714# endif
19715 ++winnr;
19716 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019717 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019723 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724}
19725
19726/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019727 * "winrestview()" function
19728 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019729 static void
19730f_winrestview(argvars, rettv)
19731 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019732 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019733{
19734 dict_T *dict;
19735
19736 if (argvars[0].v_type != VAR_DICT
19737 || (dict = argvars[0].vval.v_dict) == NULL)
19738 EMSG(_(e_invarg));
19739 else
19740 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019741 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19742 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19743 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19744 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019745#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019746 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19747 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019748#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019749 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19750 {
19751 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19752 curwin->w_set_curswant = FALSE;
19753 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019754
Bram Moolenaar82c25852014-05-28 16:47:16 +020019755 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19756 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019757#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019758 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19759 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019760#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019761 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19762 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19763 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19764 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019765
19766 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019767 win_new_height(curwin, curwin->w_height);
19768# ifdef FEAT_VERTSPLIT
19769 win_new_width(curwin, W_WIDTH(curwin));
19770# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019771 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019772
Bram Moolenaarb851a962014-10-31 15:45:52 +010019773 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019774 curwin->w_topline = 1;
19775 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19776 curwin->w_topline = curbuf->b_ml.ml_line_count;
19777#ifdef FEAT_DIFF
19778 check_topfill(curwin, TRUE);
19779#endif
19780 }
19781}
19782
19783/*
19784 * "winsaveview()" function
19785 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019786 static void
19787f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019788 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019789 typval_T *rettv;
19790{
19791 dict_T *dict;
19792
Bram Moolenaara800b422010-06-27 01:15:55 +020019793 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019794 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019795 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019796
19797 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19798 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19799#ifdef FEAT_VIRTUALEDIT
19800 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19801#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019802 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019803 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19804
19805 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19806#ifdef FEAT_DIFF
19807 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19808#endif
19809 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19810 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19811}
19812
19813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 * "winwidth(nr)" function
19815 */
19816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 typval_T *argvars;
19819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820{
19821 win_T *wp;
19822
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019823 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 else
19827#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831#endif
19832}
19833
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019835 * Write list of strings to file
19836 */
19837 static int
19838write_list(fd, list, binary)
19839 FILE *fd;
19840 list_T *list;
19841 int binary;
19842{
19843 listitem_T *li;
19844 int c;
19845 int ret = OK;
19846 char_u *s;
19847
19848 for (li = list->lv_first; li != NULL; li = li->li_next)
19849 {
19850 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19851 {
19852 if (*s == '\n')
19853 c = putc(NUL, fd);
19854 else
19855 c = putc(*s, fd);
19856 if (c == EOF)
19857 {
19858 ret = FAIL;
19859 break;
19860 }
19861 }
19862 if (!binary || li->li_next != NULL)
19863 if (putc('\n', fd) == EOF)
19864 {
19865 ret = FAIL;
19866 break;
19867 }
19868 if (ret == FAIL)
19869 {
19870 EMSG(_(e_write));
19871 break;
19872 }
19873 }
19874 return ret;
19875}
19876
19877/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019878 * "writefile()" function
19879 */
19880 static void
19881f_writefile(argvars, rettv)
19882 typval_T *argvars;
19883 typval_T *rettv;
19884{
19885 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019886 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019887 char_u *fname;
19888 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019889 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019890
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019891 if (check_restricted() || check_secure())
19892 return;
19893
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019894 if (argvars[0].v_type != VAR_LIST)
19895 {
19896 EMSG2(_(e_listarg), "writefile()");
19897 return;
19898 }
19899 if (argvars[0].vval.v_list == NULL)
19900 return;
19901
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019902 if (argvars[2].v_type != VAR_UNKNOWN)
19903 {
19904 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19905 binary = TRUE;
19906 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19907 append = TRUE;
19908 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019909
19910 /* Always open the file in binary mode, library functions have a mind of
19911 * their own about CR-LF conversion. */
19912 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019913 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19914 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019915 {
19916 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19917 ret = -1;
19918 }
19919 else
19920 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019921 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19922 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019923 fclose(fd);
19924 }
19925
19926 rettv->vval.v_number = ret;
19927}
19928
19929/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019930 * "xor(expr, expr)" function
19931 */
19932 static void
19933f_xor(argvars, rettv)
19934 typval_T *argvars;
19935 typval_T *rettv;
19936{
19937 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19938 ^ get_tv_number_chk(&argvars[1], NULL);
19939}
19940
19941
19942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019944 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 */
19946 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019947var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019949 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019950 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019952 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019954 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955
Bram Moolenaara5525202006-03-02 22:52:09 +000019956 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019957 if (varp->v_type == VAR_LIST)
19958 {
19959 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019960 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019961 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019962 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019963
19964 l = varp->vval.v_list;
19965 if (l == NULL)
19966 return NULL;
19967
19968 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019969 pos.lnum = list_find_nr(l, 0L, &error);
19970 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019971 return NULL; /* invalid line number */
19972
19973 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019974 pos.col = list_find_nr(l, 1L, &error);
19975 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019976 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019977 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019978
19979 /* We accept "$" for the column number: last column. */
19980 li = list_find(l, 1L);
19981 if (li != NULL && li->li_tv.v_type == VAR_STRING
19982 && li->li_tv.vval.v_string != NULL
19983 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19984 pos.col = len + 1;
19985
Bram Moolenaara5525202006-03-02 22:52:09 +000019986 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019987 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019988 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019989 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019990
Bram Moolenaara5525202006-03-02 22:52:09 +000019991#ifdef FEAT_VIRTUALEDIT
19992 /* Get the virtual offset. Defaults to zero. */
19993 pos.coladd = list_find_nr(l, 2L, &error);
19994 if (error)
19995 pos.coladd = 0;
19996#endif
19997
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019998 return &pos;
19999 }
20000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020001 name = get_tv_string_chk(varp);
20002 if (name == NULL)
20003 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020004 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020006 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20007 {
20008 if (VIsual_active)
20009 return &VIsual;
20010 return &curwin->w_cursor;
20011 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020012 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020014 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20016 return NULL;
20017 return pp;
20018 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020019
20020#ifdef FEAT_VIRTUALEDIT
20021 pos.coladd = 0;
20022#endif
20023
Bram Moolenaar477933c2007-07-17 14:32:23 +000020024 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020025 {
20026 pos.col = 0;
20027 if (name[1] == '0') /* "w0": first visible line */
20028 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020029 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020030 pos.lnum = curwin->w_topline;
20031 return &pos;
20032 }
20033 else if (name[1] == '$') /* "w$": last visible line */
20034 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020035 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020036 pos.lnum = curwin->w_botline - 1;
20037 return &pos;
20038 }
20039 }
20040 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020042 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 {
20044 pos.lnum = curbuf->b_ml.ml_line_count;
20045 pos.col = 0;
20046 }
20047 else
20048 {
20049 pos.lnum = curwin->w_cursor.lnum;
20050 pos.col = (colnr_T)STRLEN(ml_get_curline());
20051 }
20052 return &pos;
20053 }
20054 return NULL;
20055}
20056
20057/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020058 * Convert list in "arg" into a position and optional file number.
20059 * When "fnump" is NULL there is no file number, only 3 items.
20060 * Note that the column is passed on as-is, the caller may want to decrement
20061 * it to use 1 for the first column.
20062 * Return FAIL when conversion is not possible, doesn't check the position for
20063 * validity.
20064 */
20065 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020066list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020067 typval_T *arg;
20068 pos_T *posp;
20069 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020070 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020071{
20072 list_T *l = arg->vval.v_list;
20073 long i = 0;
20074 long n;
20075
Bram Moolenaar493c1782014-05-28 14:34:46 +020020076 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20077 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020078 if (arg->v_type != VAR_LIST
20079 || l == NULL
20080 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020081 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020082 return FAIL;
20083
20084 if (fnump != NULL)
20085 {
20086 n = list_find_nr(l, i++, NULL); /* fnum */
20087 if (n < 0)
20088 return FAIL;
20089 if (n == 0)
20090 n = curbuf->b_fnum; /* current buffer */
20091 *fnump = n;
20092 }
20093
20094 n = list_find_nr(l, i++, NULL); /* lnum */
20095 if (n < 0)
20096 return FAIL;
20097 posp->lnum = n;
20098
20099 n = list_find_nr(l, i++, NULL); /* col */
20100 if (n < 0)
20101 return FAIL;
20102 posp->col = n;
20103
20104#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020105 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020106 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020107 posp->coladd = 0;
20108 else
20109 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020110#endif
20111
Bram Moolenaar493c1782014-05-28 14:34:46 +020020112 if (curswantp != NULL)
20113 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20114
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020115 return OK;
20116}
20117
20118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 * Get the length of an environment variable name.
20120 * Advance "arg" to the first character after the name.
20121 * Return 0 for error.
20122 */
20123 static int
20124get_env_len(arg)
20125 char_u **arg;
20126{
20127 char_u *p;
20128 int len;
20129
20130 for (p = *arg; vim_isIDc(*p); ++p)
20131 ;
20132 if (p == *arg) /* no name found */
20133 return 0;
20134
20135 len = (int)(p - *arg);
20136 *arg = p;
20137 return len;
20138}
20139
20140/*
20141 * Get the length of the name of a function or internal variable.
20142 * "arg" is advanced to the first non-white character after the name.
20143 * Return 0 if something is wrong.
20144 */
20145 static int
20146get_id_len(arg)
20147 char_u **arg;
20148{
20149 char_u *p;
20150 int len;
20151
20152 /* Find the end of the name. */
20153 for (p = *arg; eval_isnamec(*p); ++p)
20154 ;
20155 if (p == *arg) /* no name found */
20156 return 0;
20157
20158 len = (int)(p - *arg);
20159 *arg = skipwhite(p);
20160
20161 return len;
20162}
20163
20164/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020165 * Get the length of the name of a variable or function.
20166 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020168 * Return -1 if curly braces expansion failed.
20169 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 * If the name contains 'magic' {}'s, expand them and return the
20171 * expanded name in an allocated string via 'alias' - caller must free.
20172 */
20173 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020174get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 char_u **arg;
20176 char_u **alias;
20177 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020178 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179{
20180 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 char_u *p;
20182 char_u *expr_start;
20183 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184
20185 *alias = NULL; /* default to no alias */
20186
20187 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20188 && (*arg)[2] == (int)KE_SNR)
20189 {
20190 /* hard coded <SNR>, already translated */
20191 *arg += 3;
20192 return get_id_len(arg) + 3;
20193 }
20194 len = eval_fname_script(*arg);
20195 if (len > 0)
20196 {
20197 /* literal "<SID>", "s:" or "<SNR>" */
20198 *arg += len;
20199 }
20200
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020202 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020204 p = find_name_end(*arg, &expr_start, &expr_end,
20205 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 if (expr_start != NULL)
20207 {
20208 char_u *temp_string;
20209
20210 if (!evaluate)
20211 {
20212 len += (int)(p - *arg);
20213 *arg = skipwhite(p);
20214 return len;
20215 }
20216
20217 /*
20218 * Include any <SID> etc in the expanded string:
20219 * Thus the -len here.
20220 */
20221 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20222 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020223 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 *alias = temp_string;
20225 *arg = skipwhite(p);
20226 return (int)STRLEN(temp_string);
20227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228
20229 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020230 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 EMSG2(_(e_invexpr2), *arg);
20232
20233 return len;
20234}
20235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236/*
20237 * Find the end of a variable or function name, taking care of magic braces.
20238 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20239 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020240 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020241 * Return a pointer to just after the name. Equal to "arg" if there is no
20242 * valid name.
20243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020245find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 char_u *arg;
20247 char_u **expr_start;
20248 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020249 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020251 int mb_nest = 0;
20252 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 char_u *p;
20254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257 *expr_start = NULL;
20258 *expr_end = NULL;
20259 }
20260
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020261 /* Quick check for valid starting character. */
20262 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20263 return arg;
20264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 for (p = arg; *p != NUL
20266 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020267 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020268 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020270 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020271 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020272 if (*p == '\'')
20273 {
20274 /* skip over 'string' to avoid counting [ and ] inside it. */
20275 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20276 ;
20277 if (*p == NUL)
20278 break;
20279 }
20280 else if (*p == '"')
20281 {
20282 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20283 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20284 if (*p == '\\' && p[1] != NUL)
20285 ++p;
20286 if (*p == NUL)
20287 break;
20288 }
20289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020292 if (*p == '[')
20293 ++br_nest;
20294 else if (*p == ']')
20295 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300 if (*p == '{')
20301 {
20302 mb_nest++;
20303 if (expr_start != NULL && *expr_start == NULL)
20304 *expr_start = p;
20305 }
20306 else if (*p == '}')
20307 {
20308 mb_nest--;
20309 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20310 *expr_end = p;
20311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 }
20314
20315 return p;
20316}
20317
20318/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020319 * Expands out the 'magic' {}'s in a variable/function name.
20320 * Note that this can call itself recursively, to deal with
20321 * constructs like foo{bar}{baz}{bam}
20322 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20323 * "in_start" ^
20324 * "expr_start" ^
20325 * "expr_end" ^
20326 * "in_end" ^
20327 *
20328 * Returns a new allocated string, which the caller must free.
20329 * Returns NULL for failure.
20330 */
20331 static char_u *
20332make_expanded_name(in_start, expr_start, expr_end, in_end)
20333 char_u *in_start;
20334 char_u *expr_start;
20335 char_u *expr_end;
20336 char_u *in_end;
20337{
20338 char_u c1;
20339 char_u *retval = NULL;
20340 char_u *temp_result;
20341 char_u *nextcmd = NULL;
20342
20343 if (expr_end == NULL || in_end == NULL)
20344 return NULL;
20345 *expr_start = NUL;
20346 *expr_end = NUL;
20347 c1 = *in_end;
20348 *in_end = NUL;
20349
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020350 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020351 if (temp_result != NULL && nextcmd == NULL)
20352 {
20353 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20354 + (in_end - expr_end) + 1));
20355 if (retval != NULL)
20356 {
20357 STRCPY(retval, in_start);
20358 STRCAT(retval, temp_result);
20359 STRCAT(retval, expr_end + 1);
20360 }
20361 }
20362 vim_free(temp_result);
20363
20364 *in_end = c1; /* put char back for error messages */
20365 *expr_start = '{';
20366 *expr_end = '}';
20367
20368 if (retval != NULL)
20369 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020370 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020371 if (expr_start != NULL)
20372 {
20373 /* Further expansion! */
20374 temp_result = make_expanded_name(retval, expr_start,
20375 expr_end, temp_result);
20376 vim_free(retval);
20377 retval = temp_result;
20378 }
20379 }
20380
20381 return retval;
20382}
20383
20384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020386 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 */
20388 static int
20389eval_isnamec(c)
20390 int c;
20391{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020392 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20393}
20394
20395/*
20396 * Return TRUE if character "c" can be used as the first character in a
20397 * variable or function name (excluding '{' and '}').
20398 */
20399 static int
20400eval_isnamec1(c)
20401 int c;
20402{
20403 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404}
20405
20406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 * Set number v: variable to "val".
20408 */
20409 void
20410set_vim_var_nr(idx, val)
20411 int idx;
20412 long val;
20413{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020414 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415}
20416
20417/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020418 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 */
20420 long
20421get_vim_var_nr(idx)
20422 int idx;
20423{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020424 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020427/*
20428 * Get string v: variable value. Uses a static buffer, can only be used once.
20429 */
20430 char_u *
20431get_vim_var_str(idx)
20432 int idx;
20433{
20434 return get_tv_string(&vimvars[idx].vv_tv);
20435}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020436
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020438 * Get List v: variable value. Caller must take care of reference count when
20439 * needed.
20440 */
20441 list_T *
20442get_vim_var_list(idx)
20443 int idx;
20444{
20445 return vimvars[idx].vv_list;
20446}
20447
20448/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020449 * Set v:char to character "c".
20450 */
20451 void
20452set_vim_var_char(c)
20453 int c;
20454{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020455 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020456
20457#ifdef FEAT_MBYTE
20458 if (has_mbyte)
20459 buf[(*mb_char2bytes)(c, buf)] = NUL;
20460 else
20461#endif
20462 {
20463 buf[0] = c;
20464 buf[1] = NUL;
20465 }
20466 set_vim_var_string(VV_CHAR, buf, -1);
20467}
20468
20469/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020470 * Set v:count to "count" and v:count1 to "count1".
20471 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 */
20473 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020474set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 long count;
20476 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020477 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020479 if (set_prevcount)
20480 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020481 vimvars[VV_COUNT].vv_nr = count;
20482 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483}
20484
20485/*
20486 * Set string v: variable to a copy of "val".
20487 */
20488 void
20489set_vim_var_string(idx, val, len)
20490 int idx;
20491 char_u *val;
20492 int len; /* length of "val" to use or -1 (whole string) */
20493{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020494 /* Need to do this (at least) once, since we can't initialize a union.
20495 * Will always be invoked when "v:progname" is set. */
20496 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20497
Bram Moolenaare9a41262005-01-15 22:18:47 +000020498 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020500 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020502 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020504 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505}
20506
20507/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020508 * Set List v: variable to "val".
20509 */
20510 void
20511set_vim_var_list(idx, val)
20512 int idx;
20513 list_T *val;
20514{
20515 list_unref(vimvars[idx].vv_list);
20516 vimvars[idx].vv_list = val;
20517 if (val != NULL)
20518 ++val->lv_refcount;
20519}
20520
20521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 * Set v:register if needed.
20523 */
20524 void
20525set_reg_var(c)
20526 int c;
20527{
20528 char_u regname;
20529
20530 if (c == 0 || c == ' ')
20531 regname = '"';
20532 else
20533 regname = c;
20534 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020535 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 set_vim_var_string(VV_REG, &regname, 1);
20537}
20538
20539/*
20540 * Get or set v:exception. If "oldval" == NULL, return the current value.
20541 * Otherwise, restore the value to "oldval" and return NULL.
20542 * Must always be called in pairs to save and restore v:exception! Does not
20543 * take care of memory allocations.
20544 */
20545 char_u *
20546v_exception(oldval)
20547 char_u *oldval;
20548{
20549 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020550 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551
Bram Moolenaare9a41262005-01-15 22:18:47 +000020552 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 return NULL;
20554}
20555
20556/*
20557 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20558 * Otherwise, restore the value to "oldval" and return NULL.
20559 * Must always be called in pairs to save and restore v:throwpoint! Does not
20560 * take care of memory allocations.
20561 */
20562 char_u *
20563v_throwpoint(oldval)
20564 char_u *oldval;
20565{
20566 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020567 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568
Bram Moolenaare9a41262005-01-15 22:18:47 +000020569 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 return NULL;
20571}
20572
20573#if defined(FEAT_AUTOCMD) || defined(PROTO)
20574/*
20575 * Set v:cmdarg.
20576 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20577 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20578 * Must always be called in pairs!
20579 */
20580 char_u *
20581set_cmdarg(eap, oldarg)
20582 exarg_T *eap;
20583 char_u *oldarg;
20584{
20585 char_u *oldval;
20586 char_u *newval;
20587 unsigned len;
20588
Bram Moolenaare9a41262005-01-15 22:18:47 +000020589 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020590 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020592 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020593 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020594 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 }
20596
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020597 if (eap->force_bin == FORCE_BIN)
20598 len = 6;
20599 else if (eap->force_bin == FORCE_NOBIN)
20600 len = 8;
20601 else
20602 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020603
20604 if (eap->read_edit)
20605 len += 7;
20606
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020607 if (eap->force_ff != 0)
20608 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20609# ifdef FEAT_MBYTE
20610 if (eap->force_enc != 0)
20611 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020612 if (eap->bad_char != 0)
20613 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020614# endif
20615
20616 newval = alloc(len + 1);
20617 if (newval == NULL)
20618 return NULL;
20619
20620 if (eap->force_bin == FORCE_BIN)
20621 sprintf((char *)newval, " ++bin");
20622 else if (eap->force_bin == FORCE_NOBIN)
20623 sprintf((char *)newval, " ++nobin");
20624 else
20625 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020626
20627 if (eap->read_edit)
20628 STRCAT(newval, " ++edit");
20629
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020630 if (eap->force_ff != 0)
20631 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20632 eap->cmd + eap->force_ff);
20633# ifdef FEAT_MBYTE
20634 if (eap->force_enc != 0)
20635 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20636 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020637 if (eap->bad_char == BAD_KEEP)
20638 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20639 else if (eap->bad_char == BAD_DROP)
20640 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20641 else if (eap->bad_char != 0)
20642 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020643# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020644 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020645 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646}
20647#endif
20648
20649/*
20650 * Get the value of internal variable "name".
20651 * Return OK or FAIL.
20652 */
20653 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020654get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 char_u *name;
20656 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020657 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020658 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020659 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020660 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
20662 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 typval_T *tv = NULL;
20664 typval_T atv;
20665 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667
20668 /* truncate the name, so that we can use strcmp() */
20669 cc = name[len];
20670 name[len] = NUL;
20671
20672 /*
20673 * Check for "b:changedtick".
20674 */
20675 if (STRCMP(name, "b:changedtick") == 0)
20676 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020677 atv.v_type = VAR_NUMBER;
20678 atv.vval.v_number = curbuf->b_changedtick;
20679 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 }
20681
20682 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 * Check for user-defined variables.
20684 */
20685 else
20686 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020687 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020689 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020691 if (dip != NULL)
20692 *dip = v;
20693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 }
20695
Bram Moolenaare9a41262005-01-15 22:18:47 +000020696 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020698 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020699 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 ret = FAIL;
20701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020702 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020703 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704
20705 name[len] = cc;
20706
20707 return ret;
20708}
20709
20710/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020711 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20712 * Also handle function call with Funcref variable: func(expr)
20713 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20714 */
20715 static int
20716handle_subscript(arg, rettv, evaluate, verbose)
20717 char_u **arg;
20718 typval_T *rettv;
20719 int evaluate; /* do more than finding the end */
20720 int verbose; /* give error messages */
20721{
20722 int ret = OK;
20723 dict_T *selfdict = NULL;
20724 char_u *s;
20725 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020726 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020727
20728 while (ret == OK
20729 && (**arg == '['
20730 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020731 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020732 && !vim_iswhite(*(*arg - 1)))
20733 {
20734 if (**arg == '(')
20735 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020736 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020737 if (evaluate)
20738 {
20739 functv = *rettv;
20740 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020741
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020742 /* Invoke the function. Recursive! */
20743 s = functv.vval.v_string;
20744 }
20745 else
20746 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020747 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020748 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20749 &len, evaluate, selfdict);
20750
20751 /* Clear the funcref afterwards, so that deleting it while
20752 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020753 if (evaluate)
20754 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020755
20756 /* Stop the expression evaluation when immediately aborting on
20757 * error, or when an interrupt occurred or an exception was thrown
20758 * but not caught. */
20759 if (aborting())
20760 {
20761 if (ret == OK)
20762 clear_tv(rettv);
20763 ret = FAIL;
20764 }
20765 dict_unref(selfdict);
20766 selfdict = NULL;
20767 }
20768 else /* **arg == '[' || **arg == '.' */
20769 {
20770 dict_unref(selfdict);
20771 if (rettv->v_type == VAR_DICT)
20772 {
20773 selfdict = rettv->vval.v_dict;
20774 if (selfdict != NULL)
20775 ++selfdict->dv_refcount;
20776 }
20777 else
20778 selfdict = NULL;
20779 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20780 {
20781 clear_tv(rettv);
20782 ret = FAIL;
20783 }
20784 }
20785 }
20786 dict_unref(selfdict);
20787 return ret;
20788}
20789
20790/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020791 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020792 * value).
20793 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020794 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020795alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020796{
Bram Moolenaar33570922005-01-25 22:26:29 +000020797 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020798}
20799
20800/*
20801 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 * The string "s" must have been allocated, it is consumed.
20803 * Return NULL for out of memory, the variable otherwise.
20804 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020805 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020806alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 char_u *s;
20808{
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020811 rettv = alloc_tv();
20812 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 rettv->v_type = VAR_STRING;
20815 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 }
20817 else
20818 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020819 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820}
20821
20822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020823 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020825 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020826free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828{
20829 if (varp != NULL)
20830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020831 switch (varp->v_type)
20832 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020833 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020834 func_unref(varp->vval.v_string);
20835 /*FALLTHROUGH*/
20836 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020837 vim_free(varp->vval.v_string);
20838 break;
20839 case VAR_LIST:
20840 list_unref(varp->vval.v_list);
20841 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020842 case VAR_DICT:
20843 dict_unref(varp->vval.v_dict);
20844 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020845 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020846#ifdef FEAT_FLOAT
20847 case VAR_FLOAT:
20848#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020849 case VAR_UNKNOWN:
20850 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020851 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020852 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020853 break;
20854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 vim_free(varp);
20856 }
20857}
20858
20859/*
20860 * Free the memory for a variable value and set the value to NULL or 0.
20861 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020862 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020863clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020864 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865{
20866 if (varp != NULL)
20867 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020868 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020870 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020871 func_unref(varp->vval.v_string);
20872 /*FALLTHROUGH*/
20873 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020874 vim_free(varp->vval.v_string);
20875 varp->vval.v_string = NULL;
20876 break;
20877 case VAR_LIST:
20878 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020879 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020880 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020881 case VAR_DICT:
20882 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020883 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020884 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020885 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020886 varp->vval.v_number = 0;
20887 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020888#ifdef FEAT_FLOAT
20889 case VAR_FLOAT:
20890 varp->vval.v_float = 0.0;
20891 break;
20892#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 case VAR_UNKNOWN:
20894 break;
20895 default:
20896 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020898 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 }
20900}
20901
20902/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903 * Set the value of a variable to NULL without freeing items.
20904 */
20905 static void
20906init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020907 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908{
20909 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020910 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020911}
20912
20913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 * Get the number value of a variable.
20915 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020916 * For incompatible types, return 0.
20917 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20918 * caller of incompatible types: it sets *denote to TRUE if "denote"
20919 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 */
20921 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020922get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020923 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020925 int error = FALSE;
20926
20927 return get_tv_number_chk(varp, &error); /* return 0L on error */
20928}
20929
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020930 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020931get_tv_number_chk(varp, denote)
20932 typval_T *varp;
20933 int *denote;
20934{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020935 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020937 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020939 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020940 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020941#ifdef FEAT_FLOAT
20942 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020943 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020944 break;
20945#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020946 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020947 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020948 break;
20949 case VAR_STRING:
20950 if (varp->vval.v_string != NULL)
20951 vim_str2nr(varp->vval.v_string, NULL, NULL,
20952 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020953 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020954 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020955 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020956 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020957 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020958 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020959 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020960 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020961 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020962 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020964 if (denote == NULL) /* useful for values that must be unsigned */
20965 n = -1;
20966 else
20967 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020968 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969}
20970
20971/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020972 * Get the lnum from the first argument.
20973 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020974 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 */
20976 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020977get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979{
Bram Moolenaar33570922005-01-25 22:26:29 +000020980 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 linenr_T lnum;
20982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020983 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 if (lnum == 0) /* no valid number, try using line() */
20985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020986 rettv.v_type = VAR_NUMBER;
20987 f_line(argvars, &rettv);
20988 lnum = rettv.vval.v_number;
20989 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 }
20991 return lnum;
20992}
20993
20994/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020995 * Get the lnum from the first argument.
20996 * Also accepts "$", then "buf" is used.
20997 * Returns 0 on error.
20998 */
20999 static linenr_T
21000get_tv_lnum_buf(argvars, buf)
21001 typval_T *argvars;
21002 buf_T *buf;
21003{
21004 if (argvars[0].v_type == VAR_STRING
21005 && argvars[0].vval.v_string != NULL
21006 && argvars[0].vval.v_string[0] == '$'
21007 && buf != NULL)
21008 return buf->b_ml.ml_line_count;
21009 return get_tv_number_chk(&argvars[0], NULL);
21010}
21011
21012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 * Get the string value of a variable.
21014 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021015 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21016 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 * If the String variable has never been set, return an empty string.
21018 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021019 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21020 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 */
21022 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021024 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025{
21026 static char_u mybuf[NUMBUFLEN];
21027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029}
21030
21031 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021034 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021036 char_u *res = get_tv_string_buf_chk(varp, buf);
21037
21038 return res != NULL ? res : (char_u *)"";
21039}
21040
Bram Moolenaar7d647822014-04-05 21:28:56 +020021041/*
21042 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21043 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021044 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021045get_tv_string_chk(varp)
21046 typval_T *varp;
21047{
21048 static char_u mybuf[NUMBUFLEN];
21049
21050 return get_tv_string_buf_chk(varp, mybuf);
21051}
21052
21053 static char_u *
21054get_tv_string_buf_chk(varp, buf)
21055 typval_T *varp;
21056 char_u *buf;
21057{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021058 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021060 case VAR_NUMBER:
21061 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21062 return buf;
21063 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021064 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021065 break;
21066 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021067 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021068 break;
21069 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021070 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021071 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021072#ifdef FEAT_FLOAT
21073 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021074 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021075 break;
21076#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021077 case VAR_STRING:
21078 if (varp->vval.v_string != NULL)
21079 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021080 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021081 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021082 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021083 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021085 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086}
21087
21088/*
21089 * Find variable "name" in the list of variables.
21090 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021091 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021092 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021093 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021095 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021096find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021098 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021099 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021102 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103
Bram Moolenaara7043832005-01-21 11:56:39 +000021104 ht = find_var_ht(name, &varname);
21105 if (htp != NULL)
21106 *htp = ht;
21107 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021109 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111
21112/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021113 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021114 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021117find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021118 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021119 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021120 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021121 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021122{
Bram Moolenaar33570922005-01-25 22:26:29 +000021123 hashitem_T *hi;
21124
21125 if (*varname == NUL)
21126 {
21127 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021128 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021129 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021130 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 case 'g': return &globvars_var;
21132 case 'v': return &vimvars_var;
21133 case 'b': return &curbuf->b_bufvar;
21134 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021135#ifdef FEAT_WINDOWS
21136 case 't': return &curtab->tp_winvar;
21137#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021138 case 'l': return current_funccal == NULL
21139 ? NULL : &current_funccal->l_vars_var;
21140 case 'a': return current_funccal == NULL
21141 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 }
21143 return NULL;
21144 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021145
21146 hi = hash_find(ht, varname);
21147 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021148 {
21149 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021150 * worked find the variable again. Don't auto-load a script if it was
21151 * loaded already, otherwise it would be loaded every time when
21152 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021153 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021154 {
21155 /* Note: script_autoload() may make "hi" invalid. It must either
21156 * be obtained again or not used. */
21157 if (!script_autoload(varname, FALSE) || aborting())
21158 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021159 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021160 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021161 if (HASHITEM_EMPTY(hi))
21162 return NULL;
21163 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021164 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021165}
21166
21167/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021169 * Set "varname" to the start of name without ':'.
21170 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021171 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021172find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 char_u *name;
21174 char_u **varname;
21175{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021176 hashitem_T *hi;
21177
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 if (name[1] != ':')
21179 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021180 /* The name must not start with a colon or #. */
21181 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 return NULL;
21183 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021184
21185 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021186 hi = hash_find(&compat_hashtab, name);
21187 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021188 return &compat_hashtab;
21189
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021191 return &globvarht; /* global variable */
21192 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 }
21194 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021195 if (*name == 'g') /* global variable */
21196 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021197 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21198 */
21199 if (vim_strchr(name + 2, ':') != NULL
21200 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021201 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021203 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021205 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021206#ifdef FEAT_WINDOWS
21207 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021208 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021209#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021210 if (*name == 'v') /* v: variable */
21211 return &vimvarht;
21212 if (*name == 'a' && current_funccal != NULL) /* function argument */
21213 return &current_funccal->l_avars.dv_hashtab;
21214 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21215 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 if (*name == 's' /* script variable */
21217 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21218 return &SCRIPT_VARS(current_SID);
21219 return NULL;
21220}
21221
21222/*
21223 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021224 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 * Returns NULL when it doesn't exist.
21226 */
21227 char_u *
21228get_var_value(name)
21229 char_u *name;
21230{
Bram Moolenaar33570922005-01-25 22:26:29 +000021231 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021233 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 if (v == NULL)
21235 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021236 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237}
21238
21239/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021240 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 * sourcing this script and when executing functions defined in the script.
21242 */
21243 void
21244new_script_vars(id)
21245 scid_T id;
21246{
Bram Moolenaara7043832005-01-21 11:56:39 +000021247 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021248 hashtab_T *ht;
21249 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021250
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21252 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021253 /* Re-allocating ga_data means that an ht_array pointing to
21254 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021255 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021256 for (i = 1; i <= ga_scripts.ga_len; ++i)
21257 {
21258 ht = &SCRIPT_VARS(i);
21259 if (ht->ht_mask == HT_INIT_SIZE - 1)
21260 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021261 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021262 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021263 }
21264
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 while (ga_scripts.ga_len < id)
21266 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021267 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021268 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021269 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 }
21272 }
21273}
21274
21275/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21277 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 */
21279 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021280init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 dict_T *dict;
21282 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021283 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284{
Bram Moolenaar33570922005-01-25 22:26:29 +000021285 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021286 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021287 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021288 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021289 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021290 dict_var->di_tv.vval.v_dict = dict;
21291 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021292 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021293 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21294 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295}
21296
21297/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021298 * Unreference a dictionary initialized by init_var_dict().
21299 */
21300 void
21301unref_var_dict(dict)
21302 dict_T *dict;
21303{
21304 /* Now the dict needs to be freed if no one else is using it, go back to
21305 * normal reference counting. */
21306 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21307 dict_unref(dict);
21308}
21309
21310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021312 * Frees all allocated variables and the value they contain.
21313 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 */
21315 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021316vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021317 hashtab_T *ht;
21318{
21319 vars_clear_ext(ht, TRUE);
21320}
21321
21322/*
21323 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21324 */
21325 static void
21326vars_clear_ext(ht, free_val)
21327 hashtab_T *ht;
21328 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329{
Bram Moolenaara7043832005-01-21 11:56:39 +000021330 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021331 hashitem_T *hi;
21332 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021335 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021336 for (hi = ht->ht_array; todo > 0; ++hi)
21337 {
21338 if (!HASHITEM_EMPTY(hi))
21339 {
21340 --todo;
21341
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021343 * ht_array might change then. hash_clear() takes care of it
21344 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 v = HI2DI(hi);
21346 if (free_val)
21347 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021348 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021349 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021350 }
21351 }
21352 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021353 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354}
21355
Bram Moolenaara7043832005-01-21 11:56:39 +000021356/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021357 * Delete a variable from hashtab "ht" at item "hi".
21358 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021361delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021362 hashtab_T *ht;
21363 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364{
Bram Moolenaar33570922005-01-25 22:26:29 +000021365 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021366
21367 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021368 clear_tv(&di->di_tv);
21369 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370}
21371
21372/*
21373 * List the value of one internal variable.
21374 */
21375 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021376list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021379 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021381 char_u *tofree;
21382 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021383 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021384
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021385 current_copyID += COPYID_INC;
21386 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021388 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021389 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390}
21391
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021393list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 char_u *prefix;
21395 char_u *name;
21396 int type;
21397 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021398 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399{
Bram Moolenaar31859182007-08-14 20:41:13 +000021400 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21401 msg_start();
21402 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 if (name != NULL) /* "a:" vars don't have a name stored */
21404 msg_puts(name);
21405 msg_putchar(' ');
21406 msg_advance(22);
21407 if (type == VAR_NUMBER)
21408 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021409 else if (type == VAR_FUNC)
21410 msg_putchar('*');
21411 else if (type == VAR_LIST)
21412 {
21413 msg_putchar('[');
21414 if (*string == '[')
21415 ++string;
21416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021417 else if (type == VAR_DICT)
21418 {
21419 msg_putchar('{');
21420 if (*string == '{')
21421 ++string;
21422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 else
21424 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021425
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427
21428 if (type == VAR_FUNC)
21429 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021430 if (*first)
21431 {
21432 msg_clr_eos();
21433 *first = FALSE;
21434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435}
21436
21437/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021438 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 * If the variable already exists, the value is updated.
21440 * Otherwise the variable is created.
21441 */
21442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021443set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021445 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021446 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447{
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021450 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021452 ht = find_var_ht(name, &varname);
21453 if (ht == NULL || *varname == NUL)
21454 {
21455 EMSG2(_(e_illvar), name);
21456 return;
21457 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021458 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021459
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021460 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21461 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021462
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021466 if (var_check_ro(v->di_flags, name, FALSE)
21467 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021468 return;
21469 if (v->di_tv.v_type != tv->v_type
21470 && !((v->di_tv.v_type == VAR_STRING
21471 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021472 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021473 || tv->v_type == VAR_NUMBER))
21474#ifdef FEAT_FLOAT
21475 && !((v->di_tv.v_type == VAR_NUMBER
21476 || v->di_tv.v_type == VAR_FLOAT)
21477 && (tv->v_type == VAR_NUMBER
21478 || tv->v_type == VAR_FLOAT))
21479#endif
21480 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021481 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021482 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021483 return;
21484 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021485
21486 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021487 * Handle setting internal v: variables separately where needed to
21488 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 */
21490 if (ht == &vimvarht)
21491 {
21492 if (v->di_tv.v_type == VAR_STRING)
21493 {
21494 vim_free(v->di_tv.vval.v_string);
21495 if (copy || tv->v_type != VAR_STRING)
21496 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21497 else
21498 {
21499 /* Take over the string to avoid an extra alloc/free. */
21500 v->di_tv.vval.v_string = tv->vval.v_string;
21501 tv->vval.v_string = NULL;
21502 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021503 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021504 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021505 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021506 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021508 if (STRCMP(varname, "searchforward") == 0)
21509 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021510#ifdef FEAT_SEARCH_EXTRA
21511 else if (STRCMP(varname, "hlsearch") == 0)
21512 {
21513 no_hlsearch = !v->di_tv.vval.v_number;
21514 redraw_all_later(SOME_VALID);
21515 }
21516#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021517 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021518 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021519 else if (v->di_tv.v_type != tv->v_type)
21520 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 }
21522
21523 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 }
21525 else /* add a new variable */
21526 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021527 /* Can't add "v:" variable. */
21528 if (ht == &vimvarht)
21529 {
21530 EMSG2(_(e_illvar), name);
21531 return;
21532 }
21533
Bram Moolenaar92124a32005-06-17 22:03:40 +000021534 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021535 if (!valid_varname(varname))
21536 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021537
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021538 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21539 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021540 if (v == NULL)
21541 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021545 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 return;
21547 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021548 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021550
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021551 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021553 else
21554 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021556 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021557 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559}
21560
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021561/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021562 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 * Also give an error message.
21564 */
21565 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021566var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021567 int flags;
21568 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021569 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021570{
21571 if (flags & DI_FLAGS_RO)
21572 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021573 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021574 return TRUE;
21575 }
21576 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21577 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021578 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021579 return TRUE;
21580 }
21581 return FALSE;
21582}
21583
21584/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021585 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21586 * Also give an error message.
21587 */
21588 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021589var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021590 int flags;
21591 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021592 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021593{
21594 if (flags & DI_FLAGS_FIX)
21595 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021596 EMSG2(_("E795: Cannot delete variable %s"),
21597 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021598 return TRUE;
21599 }
21600 return FALSE;
21601}
21602
21603/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021604 * Check if a funcref is assigned to a valid variable name.
21605 * Return TRUE and give an error if not.
21606 */
21607 static int
21608var_check_func_name(name, new_var)
21609 char_u *name; /* points to start of variable name */
21610 int new_var; /* TRUE when creating the variable */
21611{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021612 /* Allow for w: b: s: and t:. */
21613 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021614 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21615 ? name[2] : name[0]))
21616 {
21617 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21618 name);
21619 return TRUE;
21620 }
21621 /* Don't allow hiding a function. When "v" is not NULL we might be
21622 * assigning another function to the same var, the type is checked
21623 * below. */
21624 if (new_var && function_exists(name))
21625 {
21626 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21627 name);
21628 return TRUE;
21629 }
21630 return FALSE;
21631}
21632
21633/*
21634 * Check if a variable name is valid.
21635 * Return FALSE and give an error if not.
21636 */
21637 static int
21638valid_varname(varname)
21639 char_u *varname;
21640{
21641 char_u *p;
21642
21643 for (p = varname; *p != NUL; ++p)
21644 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21645 && *p != AUTOLOAD_CHAR)
21646 {
21647 EMSG2(_(e_illvar), varname);
21648 return FALSE;
21649 }
21650 return TRUE;
21651}
21652
21653/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021654 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021655 * Also give an error message, using "name" or _("name") when use_gettext is
21656 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021657 */
21658 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021659tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021660 int lock;
21661 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021662 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021663{
21664 if (lock & VAR_LOCKED)
21665 {
21666 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021667 name == NULL ? (char_u *)_("Unknown")
21668 : use_gettext ? (char_u *)_(name)
21669 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021670 return TRUE;
21671 }
21672 if (lock & VAR_FIXED)
21673 {
21674 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021675 name == NULL ? (char_u *)_("Unknown")
21676 : use_gettext ? (char_u *)_(name)
21677 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021678 return TRUE;
21679 }
21680 return FALSE;
21681}
21682
21683/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021685 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021686 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021687 * It is OK for "from" and "to" to point to the same item. This is used to
21688 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021690 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021691copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021692 typval_T *from;
21693 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021695 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021696 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021697 switch (from->v_type)
21698 {
21699 case VAR_NUMBER:
21700 to->vval.v_number = from->vval.v_number;
21701 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021702#ifdef FEAT_FLOAT
21703 case VAR_FLOAT:
21704 to->vval.v_float = from->vval.v_float;
21705 break;
21706#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021707 case VAR_STRING:
21708 case VAR_FUNC:
21709 if (from->vval.v_string == NULL)
21710 to->vval.v_string = NULL;
21711 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021713 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021714 if (from->v_type == VAR_FUNC)
21715 func_ref(to->vval.v_string);
21716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021717 break;
21718 case VAR_LIST:
21719 if (from->vval.v_list == NULL)
21720 to->vval.v_list = NULL;
21721 else
21722 {
21723 to->vval.v_list = from->vval.v_list;
21724 ++to->vval.v_list->lv_refcount;
21725 }
21726 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021727 case VAR_DICT:
21728 if (from->vval.v_dict == NULL)
21729 to->vval.v_dict = NULL;
21730 else
21731 {
21732 to->vval.v_dict = from->vval.v_dict;
21733 ++to->vval.v_dict->dv_refcount;
21734 }
21735 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021736 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021738 break;
21739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740}
21741
21742/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021743 * Make a copy of an item.
21744 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021745 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21746 * reference to an already copied list/dict can be used.
21747 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021748 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021749 static int
21750item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 typval_T *from;
21752 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021753 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021754 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021755{
21756 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021757 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021758
Bram Moolenaar33570922005-01-25 22:26:29 +000021759 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021760 {
21761 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021762 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021763 }
21764 ++recurse;
21765
21766 switch (from->v_type)
21767 {
21768 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021769#ifdef FEAT_FLOAT
21770 case VAR_FLOAT:
21771#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021772 case VAR_STRING:
21773 case VAR_FUNC:
21774 copy_tv(from, to);
21775 break;
21776 case VAR_LIST:
21777 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021778 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021779 if (from->vval.v_list == NULL)
21780 to->vval.v_list = NULL;
21781 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21782 {
21783 /* use the copy made earlier */
21784 to->vval.v_list = from->vval.v_list->lv_copylist;
21785 ++to->vval.v_list->lv_refcount;
21786 }
21787 else
21788 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21789 if (to->vval.v_list == NULL)
21790 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021791 break;
21792 case VAR_DICT:
21793 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021794 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021795 if (from->vval.v_dict == NULL)
21796 to->vval.v_dict = NULL;
21797 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21798 {
21799 /* use the copy made earlier */
21800 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21801 ++to->vval.v_dict->dv_refcount;
21802 }
21803 else
21804 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21805 if (to->vval.v_dict == NULL)
21806 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021807 break;
21808 default:
21809 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021810 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021811 }
21812 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021813 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021814}
21815
21816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 * ":echo expr1 ..." print each argument separated with a space, add a
21818 * newline at the end.
21819 * ":echon expr1 ..." print each argument plain.
21820 */
21821 void
21822ex_echo(eap)
21823 exarg_T *eap;
21824{
21825 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 char_u *p;
21829 int needclr = TRUE;
21830 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021831 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832
21833 if (eap->skip)
21834 ++emsg_skip;
21835 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21836 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021837 /* If eval1() causes an error message the text from the command may
21838 * still need to be cleared. E.g., "echo 22,44". */
21839 need_clr_eos = needclr;
21840
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021842 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 {
21844 /*
21845 * Report the invalid expression unless the expression evaluation
21846 * has been cancelled due to an aborting error, an interrupt, or an
21847 * exception.
21848 */
21849 if (!aborting())
21850 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021851 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 break;
21853 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021854 need_clr_eos = FALSE;
21855
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 if (!eap->skip)
21857 {
21858 if (atstart)
21859 {
21860 atstart = FALSE;
21861 /* Call msg_start() after eval1(), evaluating the expression
21862 * may cause a message to appear. */
21863 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021864 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021865 /* Mark the saved text as finishing the line, so that what
21866 * follows is displayed on a new line when scrolling back
21867 * at the more prompt. */
21868 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 }
21872 else if (eap->cmdidx == CMD_echo)
21873 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021874 current_copyID += COPYID_INC;
21875 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021876 if (p != NULL)
21877 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021879 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021881 if (*p != TAB && needclr)
21882 {
21883 /* remove any text still there from the command */
21884 msg_clr_eos();
21885 needclr = FALSE;
21886 }
21887 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 }
21889 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021890 {
21891#ifdef FEAT_MBYTE
21892 if (has_mbyte)
21893 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021894 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021895
21896 (void)msg_outtrans_len_attr(p, i, echo_attr);
21897 p += i - 1;
21898 }
21899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021901 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021904 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 arg = skipwhite(arg);
21908 }
21909 eap->nextcmd = check_nextcmd(arg);
21910
21911 if (eap->skip)
21912 --emsg_skip;
21913 else
21914 {
21915 /* remove text that may still be there from the command */
21916 if (needclr)
21917 msg_clr_eos();
21918 if (eap->cmdidx == CMD_echo)
21919 msg_end();
21920 }
21921}
21922
21923/*
21924 * ":echohl {name}".
21925 */
21926 void
21927ex_echohl(eap)
21928 exarg_T *eap;
21929{
21930 int id;
21931
21932 id = syn_name2id(eap->arg);
21933 if (id == 0)
21934 echo_attr = 0;
21935 else
21936 echo_attr = syn_id2attr(id);
21937}
21938
21939/*
21940 * ":execute expr1 ..." execute the result of an expression.
21941 * ":echomsg expr1 ..." Print a message
21942 * ":echoerr expr1 ..." Print an error
21943 * Each gets spaces around each argument and a newline at the end for
21944 * echo commands
21945 */
21946 void
21947ex_execute(eap)
21948 exarg_T *eap;
21949{
21950 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 int ret = OK;
21953 char_u *p;
21954 garray_T ga;
21955 int len;
21956 int save_did_emsg;
21957
21958 ga_init2(&ga, 1, 80);
21959
21960 if (eap->skip)
21961 ++emsg_skip;
21962 while (*arg != NUL && *arg != '|' && *arg != '\n')
21963 {
21964 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021965 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 {
21967 /*
21968 * Report the invalid expression unless the expression evaluation
21969 * has been cancelled due to an aborting error, an interrupt, or an
21970 * exception.
21971 */
21972 if (!aborting())
21973 EMSG2(_(e_invexpr2), p);
21974 ret = FAIL;
21975 break;
21976 }
21977
21978 if (!eap->skip)
21979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021980 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 len = (int)STRLEN(p);
21982 if (ga_grow(&ga, len + 2) == FAIL)
21983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021984 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 ret = FAIL;
21986 break;
21987 }
21988 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 ga.ga_len += len;
21992 }
21993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021994 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 arg = skipwhite(arg);
21996 }
21997
21998 if (ret != FAIL && ga.ga_data != NULL)
21999 {
22000 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022003 out_flush();
22004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 else if (eap->cmdidx == CMD_echoerr)
22006 {
22007 /* We don't want to abort following commands, restore did_emsg. */
22008 save_did_emsg = did_emsg;
22009 EMSG((char_u *)ga.ga_data);
22010 if (!force_abort)
22011 did_emsg = save_did_emsg;
22012 }
22013 else if (eap->cmdidx == CMD_execute)
22014 do_cmdline((char_u *)ga.ga_data,
22015 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22016 }
22017
22018 ga_clear(&ga);
22019
22020 if (eap->skip)
22021 --emsg_skip;
22022
22023 eap->nextcmd = check_nextcmd(arg);
22024}
22025
22026/*
22027 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22028 * "arg" points to the "&" or '+' when called, to "option" when returning.
22029 * Returns NULL when no option name found. Otherwise pointer to the char
22030 * after the option name.
22031 */
22032 static char_u *
22033find_option_end(arg, opt_flags)
22034 char_u **arg;
22035 int *opt_flags;
22036{
22037 char_u *p = *arg;
22038
22039 ++p;
22040 if (*p == 'g' && p[1] == ':')
22041 {
22042 *opt_flags = OPT_GLOBAL;
22043 p += 2;
22044 }
22045 else if (*p == 'l' && p[1] == ':')
22046 {
22047 *opt_flags = OPT_LOCAL;
22048 p += 2;
22049 }
22050 else
22051 *opt_flags = 0;
22052
22053 if (!ASCII_ISALPHA(*p))
22054 return NULL;
22055 *arg = p;
22056
22057 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22058 p += 4; /* termcap option */
22059 else
22060 while (ASCII_ISALPHA(*p))
22061 ++p;
22062 return p;
22063}
22064
22065/*
22066 * ":function"
22067 */
22068 void
22069ex_function(eap)
22070 exarg_T *eap;
22071{
22072 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022073 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 int j;
22075 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022077 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 char_u *name = NULL;
22079 char_u *p;
22080 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022081 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 garray_T newargs;
22083 garray_T newlines;
22084 int varargs = FALSE;
22085 int mustend = FALSE;
22086 int flags = 0;
22087 ufunc_T *fp;
22088 int indent;
22089 int nesting;
22090 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022091 dictitem_T *v;
22092 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 static int func_nr = 0; /* number for nameless function */
22094 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022095 hashtab_T *ht;
22096 int todo;
22097 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022098 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099
22100 /*
22101 * ":function" without argument: list functions.
22102 */
22103 if (ends_excmd(*eap->arg))
22104 {
22105 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022106 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022107 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022108 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022109 {
22110 if (!HASHITEM_EMPTY(hi))
22111 {
22112 --todo;
22113 fp = HI2UF(hi);
22114 if (!isdigit(*fp->uf_name))
22115 list_func_head(fp, FALSE);
22116 }
22117 }
22118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 eap->nextcmd = check_nextcmd(eap->arg);
22120 return;
22121 }
22122
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022123 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022124 * ":function /pat": list functions matching pattern.
22125 */
22126 if (*eap->arg == '/')
22127 {
22128 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22129 if (!eap->skip)
22130 {
22131 regmatch_T regmatch;
22132
22133 c = *p;
22134 *p = NUL;
22135 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22136 *p = c;
22137 if (regmatch.regprog != NULL)
22138 {
22139 regmatch.rm_ic = p_ic;
22140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022141 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022142 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22143 {
22144 if (!HASHITEM_EMPTY(hi))
22145 {
22146 --todo;
22147 fp = HI2UF(hi);
22148 if (!isdigit(*fp->uf_name)
22149 && vim_regexec(&regmatch, fp->uf_name, 0))
22150 list_func_head(fp, FALSE);
22151 }
22152 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022153 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022154 }
22155 }
22156 if (*p == '/')
22157 ++p;
22158 eap->nextcmd = check_nextcmd(p);
22159 return;
22160 }
22161
22162 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022163 * Get the function name. There are these situations:
22164 * func normal function name
22165 * "name" == func, "fudi.fd_dict" == NULL
22166 * dict.func new dictionary entry
22167 * "name" == NULL, "fudi.fd_dict" set,
22168 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22169 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022170 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022171 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22172 * dict.func existing dict entry that's not a Funcref
22173 * "name" == NULL, "fudi.fd_dict" set,
22174 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022175 * s:func script-local function name
22176 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022179 name = trans_function_name(&p, eap->skip, 0, &fudi);
22180 paren = (vim_strchr(p, '(') != NULL);
22181 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 {
22183 /*
22184 * Return on an invalid expression in braces, unless the expression
22185 * evaluation has been cancelled due to an aborting error, an
22186 * interrupt, or an exception.
22187 */
22188 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022190 if (!eap->skip && fudi.fd_newkey != NULL)
22191 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022192 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 else
22196 eap->skip = TRUE;
22197 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022198
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 /* An error in a function call during evaluation of an expression in magic
22200 * braces should not cause the function not to be defined. */
22201 saved_did_emsg = did_emsg;
22202 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203
22204 /*
22205 * ":function func" with only function name: list function.
22206 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022207 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 {
22209 if (!ends_excmd(*skipwhite(p)))
22210 {
22211 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022212 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 }
22214 eap->nextcmd = check_nextcmd(p);
22215 if (eap->nextcmd != NULL)
22216 *p = NUL;
22217 if (!eap->skip && !got_int)
22218 {
22219 fp = find_func(name);
22220 if (fp != NULL)
22221 {
22222 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022223 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022225 if (FUNCLINE(fp, j) == NULL)
22226 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 msg_putchar('\n');
22228 msg_outnum((long)(j + 1));
22229 if (j < 9)
22230 msg_putchar(' ');
22231 if (j < 99)
22232 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022233 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 out_flush(); /* show a line at a time */
22235 ui_breakcheck();
22236 }
22237 if (!got_int)
22238 {
22239 msg_putchar('\n');
22240 msg_puts((char_u *)" endfunction");
22241 }
22242 }
22243 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022244 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022246 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 }
22248
22249 /*
22250 * ":function name(arg1, arg2)" Define function.
22251 */
22252 p = skipwhite(p);
22253 if (*p != '(')
22254 {
22255 if (!eap->skip)
22256 {
22257 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022258 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 }
22260 /* attempt to continue by skipping some text */
22261 if (vim_strchr(p, '(') != NULL)
22262 p = vim_strchr(p, '(');
22263 }
22264 p = skipwhite(p + 1);
22265
22266 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22267 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22268
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022269 if (!eap->skip)
22270 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022271 /* Check the name of the function. Unless it's a dictionary function
22272 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022273 if (name != NULL)
22274 arg = name;
22275 else
22276 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022277 if (arg != NULL && (fudi.fd_di == NULL
22278 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022279 {
22280 if (*arg == K_SPECIAL)
22281 j = 3;
22282 else
22283 j = 0;
22284 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22285 : eval_isnamec(arg[j])))
22286 ++j;
22287 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022288 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022289 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022290 /* Disallow using the g: dict. */
22291 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22292 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022293 }
22294
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 /*
22296 * Isolate the arguments: "arg1, arg2, ...)"
22297 */
22298 while (*p != ')')
22299 {
22300 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22301 {
22302 varargs = TRUE;
22303 p += 3;
22304 mustend = TRUE;
22305 }
22306 else
22307 {
22308 arg = p;
22309 while (ASCII_ISALNUM(*p) || *p == '_')
22310 ++p;
22311 if (arg == p || isdigit(*arg)
22312 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22313 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22314 {
22315 if (!eap->skip)
22316 EMSG2(_("E125: Illegal argument: %s"), arg);
22317 break;
22318 }
22319 if (ga_grow(&newargs, 1) == FAIL)
22320 goto erret;
22321 c = *p;
22322 *p = NUL;
22323 arg = vim_strsave(arg);
22324 if (arg == NULL)
22325 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022326
22327 /* Check for duplicate argument name. */
22328 for (i = 0; i < newargs.ga_len; ++i)
22329 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22330 {
22331 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022332 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022333 goto erret;
22334 }
22335
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22337 *p = c;
22338 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 if (*p == ',')
22340 ++p;
22341 else
22342 mustend = TRUE;
22343 }
22344 p = skipwhite(p);
22345 if (mustend && *p != ')')
22346 {
22347 if (!eap->skip)
22348 EMSG2(_(e_invarg2), eap->arg);
22349 break;
22350 }
22351 }
22352 ++p; /* skip the ')' */
22353
Bram Moolenaare9a41262005-01-15 22:18:47 +000022354 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 for (;;)
22356 {
22357 p = skipwhite(p);
22358 if (STRNCMP(p, "range", 5) == 0)
22359 {
22360 flags |= FC_RANGE;
22361 p += 5;
22362 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022363 else if (STRNCMP(p, "dict", 4) == 0)
22364 {
22365 flags |= FC_DICT;
22366 p += 4;
22367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 else if (STRNCMP(p, "abort", 5) == 0)
22369 {
22370 flags |= FC_ABORT;
22371 p += 5;
22372 }
22373 else
22374 break;
22375 }
22376
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022377 /* When there is a line break use what follows for the function body.
22378 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22379 if (*p == '\n')
22380 line_arg = p + 1;
22381 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 EMSG(_(e_trailing));
22383
22384 /*
22385 * Read the body of the function, until ":endfunction" is found.
22386 */
22387 if (KeyTyped)
22388 {
22389 /* Check if the function already exists, don't let the user type the
22390 * whole function before telling him it doesn't work! For a script we
22391 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022392 if (!eap->skip && !eap->forceit)
22393 {
22394 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22395 EMSG(_(e_funcdict));
22396 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022397 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022400 if (!eap->skip && did_emsg)
22401 goto erret;
22402
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 msg_putchar('\n'); /* don't overwrite the function name */
22404 cmdline_row = msg_row;
22405 }
22406
22407 indent = 2;
22408 nesting = 0;
22409 for (;;)
22410 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022411 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022412 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022413 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022414 saved_wait_return = FALSE;
22415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022417 sourcing_lnum_off = sourcing_lnum;
22418
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022419 if (line_arg != NULL)
22420 {
22421 /* Use eap->arg, split up in parts by line breaks. */
22422 theline = line_arg;
22423 p = vim_strchr(theline, '\n');
22424 if (p == NULL)
22425 line_arg += STRLEN(line_arg);
22426 else
22427 {
22428 *p = NUL;
22429 line_arg = p + 1;
22430 }
22431 }
22432 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433 theline = getcmdline(':', 0L, indent);
22434 else
22435 theline = eap->getline(':', eap->cookie, indent);
22436 if (KeyTyped)
22437 lines_left = Rows - 1;
22438 if (theline == NULL)
22439 {
22440 EMSG(_("E126: Missing :endfunction"));
22441 goto erret;
22442 }
22443
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022444 /* Detect line continuation: sourcing_lnum increased more than one. */
22445 if (sourcing_lnum > sourcing_lnum_off + 1)
22446 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22447 else
22448 sourcing_lnum_off = 0;
22449
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 if (skip_until != NULL)
22451 {
22452 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22453 * don't check for ":endfunc". */
22454 if (STRCMP(theline, skip_until) == 0)
22455 {
22456 vim_free(skip_until);
22457 skip_until = NULL;
22458 }
22459 }
22460 else
22461 {
22462 /* skip ':' and blanks*/
22463 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22464 ;
22465
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022466 /* Check for "endfunction". */
22467 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022469 if (line_arg == NULL)
22470 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 break;
22472 }
22473
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022474 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 * at "end". */
22476 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22477 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022478 else if (STRNCMP(p, "if", 2) == 0
22479 || STRNCMP(p, "wh", 2) == 0
22480 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 || STRNCMP(p, "try", 3) == 0)
22482 indent += 2;
22483
22484 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022485 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022487 if (*p == '!')
22488 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022490 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22491 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022493 ++nesting;
22494 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 }
22496 }
22497
22498 /* Check for ":append" or ":insert". */
22499 p = skip_range(p, NULL);
22500 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22501 || (p[0] == 'i'
22502 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22503 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22504 skip_until = vim_strsave((char_u *)".");
22505
22506 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22507 arg = skipwhite(skiptowhite(p));
22508 if (arg[0] == '<' && arg[1] =='<'
22509 && ((p[0] == 'p' && p[1] == 'y'
22510 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22511 || (p[0] == 'p' && p[1] == 'e'
22512 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22513 || (p[0] == 't' && p[1] == 'c'
22514 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022515 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22516 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22518 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022519 || (p[0] == 'm' && p[1] == 'z'
22520 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 ))
22522 {
22523 /* ":python <<" continues until a dot, like ":append" */
22524 p = skipwhite(arg + 2);
22525 if (*p == NUL)
22526 skip_until = vim_strsave((char_u *)".");
22527 else
22528 skip_until = vim_strsave(p);
22529 }
22530 }
22531
22532 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022533 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022534 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022535 if (line_arg == NULL)
22536 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022538 }
22539
22540 /* Copy the line to newly allocated memory. get_one_sourceline()
22541 * allocates 250 bytes per line, this saves 80% on average. The cost
22542 * is an extra alloc/free. */
22543 p = vim_strsave(theline);
22544 if (p != NULL)
22545 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022546 if (line_arg == NULL)
22547 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022548 theline = p;
22549 }
22550
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022551 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22552
22553 /* Add NULL lines for continuation lines, so that the line count is
22554 * equal to the index in the growarray. */
22555 while (sourcing_lnum_off-- > 0)
22556 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022557
22558 /* Check for end of eap->arg. */
22559 if (line_arg != NULL && *line_arg == NUL)
22560 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 }
22562
22563 /* Don't define the function when skipping commands or when an error was
22564 * detected. */
22565 if (eap->skip || did_emsg)
22566 goto erret;
22567
22568 /*
22569 * If there are no errors, add the function
22570 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022571 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022572 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022573 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022576 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022577 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022578 goto erret;
22579 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022580
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022581 fp = find_func(name);
22582 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022584 if (!eap->forceit)
22585 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022586 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022587 goto erret;
22588 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022589 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022590 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022591 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022592 name);
22593 goto erret;
22594 }
22595 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022596 ga_clear_strings(&(fp->uf_args));
22597 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022598 vim_free(name);
22599 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 }
22602 else
22603 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604 char numbuf[20];
22605
22606 fp = NULL;
22607 if (fudi.fd_newkey == NULL && !eap->forceit)
22608 {
22609 EMSG(_(e_funcdict));
22610 goto erret;
22611 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022612 if (fudi.fd_di == NULL)
22613 {
22614 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022615 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022616 goto erret;
22617 }
22618 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022619 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022620 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022621
22622 /* Give the function a sequential number. Can only be used with a
22623 * Funcref! */
22624 vim_free(name);
22625 sprintf(numbuf, "%d", ++func_nr);
22626 name = vim_strsave((char_u *)numbuf);
22627 if (name == NULL)
22628 goto erret;
22629 }
22630
22631 if (fp == NULL)
22632 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022633 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022634 {
22635 int slen, plen;
22636 char_u *scriptname;
22637
22638 /* Check that the autoload name matches the script name. */
22639 j = FAIL;
22640 if (sourcing_name != NULL)
22641 {
22642 scriptname = autoload_name(name);
22643 if (scriptname != NULL)
22644 {
22645 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022646 plen = (int)STRLEN(p);
22647 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022648 if (slen > plen && fnamecmp(p,
22649 sourcing_name + slen - plen) == 0)
22650 j = OK;
22651 vim_free(scriptname);
22652 }
22653 }
22654 if (j == FAIL)
22655 {
22656 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22657 goto erret;
22658 }
22659 }
22660
22661 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 if (fp == NULL)
22663 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022664
22665 if (fudi.fd_dict != NULL)
22666 {
22667 if (fudi.fd_di == NULL)
22668 {
22669 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022670 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 if (fudi.fd_di == NULL)
22672 {
22673 vim_free(fp);
22674 goto erret;
22675 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022676 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22677 {
22678 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022679 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022680 goto erret;
22681 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022682 }
22683 else
22684 /* overwrite existing dict entry */
22685 clear_tv(&fudi.fd_di->di_tv);
22686 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022687 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022688 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022689 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022690
22691 /* behave like "dict" was used */
22692 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022693 }
22694
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022696 STRCPY(fp->uf_name, name);
22697 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022699 fp->uf_args = newargs;
22700 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022701#ifdef FEAT_PROFILE
22702 fp->uf_tml_count = NULL;
22703 fp->uf_tml_total = NULL;
22704 fp->uf_tml_self = NULL;
22705 fp->uf_profiling = FALSE;
22706 if (prof_def_func())
22707 func_do_profile(fp);
22708#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022709 fp->uf_varargs = varargs;
22710 fp->uf_flags = flags;
22711 fp->uf_calls = 0;
22712 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022713 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714
22715erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 ga_clear_strings(&newargs);
22717 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022718ret_free:
22719 vim_free(skip_until);
22720 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022723 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724}
22725
22726/*
22727 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022728 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022730 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022731 * TFN_INT: internal function name OK
22732 * TFN_QUIET: be quiet
22733 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 * Advances "pp" to just after the function name (if no error).
22735 */
22736 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022737trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 char_u **pp;
22739 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022743 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 char_u *start;
22745 char_u *end;
22746 int lead;
22747 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022749 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022750
22751 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022752 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022754
22755 /* Check for hard coded <SNR>: already translated function ID (from a user
22756 * command). */
22757 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22758 && (*pp)[2] == (int)KE_SNR)
22759 {
22760 *pp += 3;
22761 len = get_id_len(pp) + 3;
22762 return vim_strnsave(start, len);
22763 }
22764
22765 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22766 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022768 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022770
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022771 /* Note that TFN_ flags use the same values as GLV_ flags. */
22772 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022773 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022774 if (end == start)
22775 {
22776 if (!skip)
22777 EMSG(_("E129: Function name required"));
22778 goto theend;
22779 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022780 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022781 {
22782 /*
22783 * Report an invalid expression in braces, unless the expression
22784 * evaluation has been cancelled due to an aborting error, an
22785 * interrupt, or an exception.
22786 */
22787 if (!aborting())
22788 {
22789 if (end != NULL)
22790 EMSG2(_(e_invarg2), start);
22791 }
22792 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022793 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022794 goto theend;
22795 }
22796
22797 if (lv.ll_tv != NULL)
22798 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022799 if (fdp != NULL)
22800 {
22801 fdp->fd_dict = lv.ll_dict;
22802 fdp->fd_newkey = lv.ll_newkey;
22803 lv.ll_newkey = NULL;
22804 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022805 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022806 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22807 {
22808 name = vim_strsave(lv.ll_tv->vval.v_string);
22809 *pp = end;
22810 }
22811 else
22812 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022813 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22814 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022815 EMSG(_(e_funcref));
22816 else
22817 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022818 name = NULL;
22819 }
22820 goto theend;
22821 }
22822
22823 if (lv.ll_name == NULL)
22824 {
22825 /* Error found, but continue after the function name. */
22826 *pp = end;
22827 goto theend;
22828 }
22829
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022830 /* Check if the name is a Funcref. If so, use the value. */
22831 if (lv.ll_exp_name != NULL)
22832 {
22833 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022834 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022835 if (name == lv.ll_exp_name)
22836 name = NULL;
22837 }
22838 else
22839 {
22840 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022841 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022842 if (name == *pp)
22843 name = NULL;
22844 }
22845 if (name != NULL)
22846 {
22847 name = vim_strsave(name);
22848 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022849 if (STRNCMP(name, "<SNR>", 5) == 0)
22850 {
22851 /* Change "<SNR>" to the byte sequence. */
22852 name[0] = K_SPECIAL;
22853 name[1] = KS_EXTRA;
22854 name[2] = (int)KE_SNR;
22855 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22856 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022857 goto theend;
22858 }
22859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022860 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022861 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022862 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022863 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22864 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22865 {
22866 /* When there was "s:" already or the name expanded to get a
22867 * leading "s:" then remove it. */
22868 lv.ll_name += 2;
22869 len -= 2;
22870 lead = 2;
22871 }
22872 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022873 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022874 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022875 /* skip over "s:" and "g:" */
22876 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022877 lv.ll_name += 2;
22878 len = (int)(end - lv.ll_name);
22879 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022880
22881 /*
22882 * Copy the function name to allocated memory.
22883 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22884 * Accept <SNR>123_name() outside a script.
22885 */
22886 if (skip)
22887 lead = 0; /* do nothing */
22888 else if (lead > 0)
22889 {
22890 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022891 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22892 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022893 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022894 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022895 if (current_SID <= 0)
22896 {
22897 EMSG(_(e_usingsid));
22898 goto theend;
22899 }
22900 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22901 lead += (int)STRLEN(sid_buf);
22902 }
22903 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022904 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022905 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022906 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022907 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022908 goto theend;
22909 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022910 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022911 {
22912 char_u *cp = vim_strchr(lv.ll_name, ':');
22913
22914 if (cp != NULL && cp < end)
22915 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022916 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022917 goto theend;
22918 }
22919 }
22920
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022921 name = alloc((unsigned)(len + lead + 1));
22922 if (name != NULL)
22923 {
22924 if (lead > 0)
22925 {
22926 name[0] = K_SPECIAL;
22927 name[1] = KS_EXTRA;
22928 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022929 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022930 STRCPY(name + 3, sid_buf);
22931 }
22932 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022933 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022934 }
22935 *pp = end;
22936
22937theend:
22938 clear_lval(&lv);
22939 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940}
22941
22942/*
22943 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22944 * Return 2 if "p" starts with "s:".
22945 * Return 0 otherwise.
22946 */
22947 static int
22948eval_fname_script(p)
22949 char_u *p;
22950{
22951 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22952 || STRNICMP(p + 1, "SNR>", 4) == 0))
22953 return 5;
22954 if (p[0] == 's' && p[1] == ':')
22955 return 2;
22956 return 0;
22957}
22958
22959/*
22960 * Return TRUE if "p" starts with "<SID>" or "s:".
22961 * Only works if eval_fname_script() returned non-zero for "p"!
22962 */
22963 static int
22964eval_fname_sid(p)
22965 char_u *p;
22966{
22967 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22968}
22969
22970/*
22971 * List the head of the function: "name(arg1, arg2)".
22972 */
22973 static void
22974list_func_head(fp, indent)
22975 ufunc_T *fp;
22976 int indent;
22977{
22978 int j;
22979
22980 msg_start();
22981 if (indent)
22982 MSG_PUTS(" ");
22983 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022984 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 {
22986 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022987 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 }
22989 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022990 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022992 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 {
22994 if (j)
22995 MSG_PUTS(", ");
22996 msg_puts(FUNCARG(fp, j));
22997 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022998 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 {
23000 if (j)
23001 MSG_PUTS(", ");
23002 MSG_PUTS("...");
23003 }
23004 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023005 if (fp->uf_flags & FC_ABORT)
23006 MSG_PUTS(" abort");
23007 if (fp->uf_flags & FC_RANGE)
23008 MSG_PUTS(" range");
23009 if (fp->uf_flags & FC_DICT)
23010 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023011 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023012 if (p_verbose > 0)
23013 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014}
23015
23016/*
23017 * Find a function by name, return pointer to it in ufuncs.
23018 * Return NULL for unknown function.
23019 */
23020 static ufunc_T *
23021find_func(name)
23022 char_u *name;
23023{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023024 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023026 hi = hash_find(&func_hashtab, name);
23027 if (!HASHITEM_EMPTY(hi))
23028 return HI2UF(hi);
23029 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030}
23031
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023032#if defined(EXITFREE) || defined(PROTO)
23033 void
23034free_all_functions()
23035{
23036 hashitem_T *hi;
23037
23038 /* Need to start all over every time, because func_free() may change the
23039 * hash table. */
23040 while (func_hashtab.ht_used > 0)
23041 for (hi = func_hashtab.ht_array; ; ++hi)
23042 if (!HASHITEM_EMPTY(hi))
23043 {
23044 func_free(HI2UF(hi));
23045 break;
23046 }
23047}
23048#endif
23049
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023050 int
23051translated_function_exists(name)
23052 char_u *name;
23053{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023054 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023055 return find_internal_func(name) >= 0;
23056 return find_func(name) != NULL;
23057}
23058
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023059/*
23060 * Return TRUE if a function "name" exists.
23061 */
23062 static int
23063function_exists(name)
23064 char_u *name;
23065{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023066 char_u *nm = name;
23067 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023068 int n = FALSE;
23069
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023070 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23071 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023072 nm = skipwhite(nm);
23073
23074 /* Only accept "funcname", "funcname ", "funcname (..." and
23075 * "funcname(...", not "funcname!...". */
23076 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023077 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023078 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023079 return n;
23080}
23081
Bram Moolenaara1544c02013-05-30 12:35:52 +020023082 char_u *
23083get_expanded_name(name, check)
23084 char_u *name;
23085 int check;
23086{
23087 char_u *nm = name;
23088 char_u *p;
23089
23090 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23091
23092 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023093 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023094 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023095
Bram Moolenaara1544c02013-05-30 12:35:52 +020023096 vim_free(p);
23097 return NULL;
23098}
23099
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023100/*
23101 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023102 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23103 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023104 */
23105 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023106builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023107 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023108 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023109{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023110 char_u *p;
23111
23112 if (!ASCII_ISLOWER(name[0]))
23113 return FALSE;
23114 p = vim_strchr(name, AUTOLOAD_CHAR);
23115 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023116}
23117
Bram Moolenaar05159a02005-02-26 23:04:13 +000023118#if defined(FEAT_PROFILE) || defined(PROTO)
23119/*
23120 * Start profiling function "fp".
23121 */
23122 static void
23123func_do_profile(fp)
23124 ufunc_T *fp;
23125{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023126 int len = fp->uf_lines.ga_len;
23127
23128 if (len == 0)
23129 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023130 fp->uf_tm_count = 0;
23131 profile_zero(&fp->uf_tm_self);
23132 profile_zero(&fp->uf_tm_total);
23133 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023134 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023135 if (fp->uf_tml_total == NULL)
23136 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023137 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023138 if (fp->uf_tml_self == NULL)
23139 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023140 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023141 fp->uf_tml_idx = -1;
23142 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23143 || fp->uf_tml_self == NULL)
23144 return; /* out of memory */
23145
23146 fp->uf_profiling = TRUE;
23147}
23148
23149/*
23150 * Dump the profiling results for all functions in file "fd".
23151 */
23152 void
23153func_dump_profile(fd)
23154 FILE *fd;
23155{
23156 hashitem_T *hi;
23157 int todo;
23158 ufunc_T *fp;
23159 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023160 ufunc_T **sorttab;
23161 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023163 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023164 if (todo == 0)
23165 return; /* nothing to dump */
23166
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023167 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023168
Bram Moolenaar05159a02005-02-26 23:04:13 +000023169 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23170 {
23171 if (!HASHITEM_EMPTY(hi))
23172 {
23173 --todo;
23174 fp = HI2UF(hi);
23175 if (fp->uf_profiling)
23176 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023177 if (sorttab != NULL)
23178 sorttab[st_len++] = fp;
23179
Bram Moolenaar05159a02005-02-26 23:04:13 +000023180 if (fp->uf_name[0] == K_SPECIAL)
23181 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23182 else
23183 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23184 if (fp->uf_tm_count == 1)
23185 fprintf(fd, "Called 1 time\n");
23186 else
23187 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23188 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23189 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23190 fprintf(fd, "\n");
23191 fprintf(fd, "count total (s) self (s)\n");
23192
23193 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23194 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023195 if (FUNCLINE(fp, i) == NULL)
23196 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023197 prof_func_line(fd, fp->uf_tml_count[i],
23198 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023199 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23200 }
23201 fprintf(fd, "\n");
23202 }
23203 }
23204 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023205
23206 if (sorttab != NULL && st_len > 0)
23207 {
23208 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23209 prof_total_cmp);
23210 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23211 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23212 prof_self_cmp);
23213 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23214 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023215
23216 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023217}
Bram Moolenaar73830342005-02-28 22:48:19 +000023218
23219 static void
23220prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23221 FILE *fd;
23222 ufunc_T **sorttab;
23223 int st_len;
23224 char *title;
23225 int prefer_self; /* when equal print only self time */
23226{
23227 int i;
23228 ufunc_T *fp;
23229
23230 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23231 fprintf(fd, "count total (s) self (s) function\n");
23232 for (i = 0; i < 20 && i < st_len; ++i)
23233 {
23234 fp = sorttab[i];
23235 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23236 prefer_self);
23237 if (fp->uf_name[0] == K_SPECIAL)
23238 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23239 else
23240 fprintf(fd, " %s()\n", fp->uf_name);
23241 }
23242 fprintf(fd, "\n");
23243}
23244
23245/*
23246 * Print the count and times for one function or function line.
23247 */
23248 static void
23249prof_func_line(fd, count, total, self, prefer_self)
23250 FILE *fd;
23251 int count;
23252 proftime_T *total;
23253 proftime_T *self;
23254 int prefer_self; /* when equal print only self time */
23255{
23256 if (count > 0)
23257 {
23258 fprintf(fd, "%5d ", count);
23259 if (prefer_self && profile_equal(total, self))
23260 fprintf(fd, " ");
23261 else
23262 fprintf(fd, "%s ", profile_msg(total));
23263 if (!prefer_self && profile_equal(total, self))
23264 fprintf(fd, " ");
23265 else
23266 fprintf(fd, "%s ", profile_msg(self));
23267 }
23268 else
23269 fprintf(fd, " ");
23270}
23271
23272/*
23273 * Compare function for total time sorting.
23274 */
23275 static int
23276#ifdef __BORLANDC__
23277_RTLENTRYF
23278#endif
23279prof_total_cmp(s1, s2)
23280 const void *s1;
23281 const void *s2;
23282{
23283 ufunc_T *p1, *p2;
23284
23285 p1 = *(ufunc_T **)s1;
23286 p2 = *(ufunc_T **)s2;
23287 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23288}
23289
23290/*
23291 * Compare function for self time sorting.
23292 */
23293 static int
23294#ifdef __BORLANDC__
23295_RTLENTRYF
23296#endif
23297prof_self_cmp(s1, s2)
23298 const void *s1;
23299 const void *s2;
23300{
23301 ufunc_T *p1, *p2;
23302
23303 p1 = *(ufunc_T **)s1;
23304 p2 = *(ufunc_T **)s2;
23305 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23306}
23307
Bram Moolenaar05159a02005-02-26 23:04:13 +000023308#endif
23309
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023310/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023311 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023312 * Return TRUE if a package was loaded.
23313 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023314 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023315script_autoload(name, reload)
23316 char_u *name;
23317 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023318{
23319 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023320 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023321 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023322 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023323
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023324 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023325 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023326 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023327 return FALSE;
23328
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023329 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023330
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023331 /* Find the name in the list of previously loaded package names. Skip
23332 * "autoload/", it's always the same. */
23333 for (i = 0; i < ga_loaded.ga_len; ++i)
23334 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23335 break;
23336 if (!reload && i < ga_loaded.ga_len)
23337 ret = FALSE; /* was loaded already */
23338 else
23339 {
23340 /* Remember the name if it wasn't loaded already. */
23341 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23342 {
23343 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23344 tofree = NULL;
23345 }
23346
23347 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023348 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023349 ret = TRUE;
23350 }
23351
23352 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023353 return ret;
23354}
23355
23356/*
23357 * Return the autoload script name for a function or variable name.
23358 * Returns NULL when out of memory.
23359 */
23360 static char_u *
23361autoload_name(name)
23362 char_u *name;
23363{
23364 char_u *p;
23365 char_u *scriptname;
23366
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023367 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023368 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23369 if (scriptname == NULL)
23370 return FALSE;
23371 STRCPY(scriptname, "autoload/");
23372 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023373 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023374 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023375 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023376 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023377 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023378}
23379
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23381
23382/*
23383 * Function given to ExpandGeneric() to obtain the list of user defined
23384 * function names.
23385 */
23386 char_u *
23387get_user_func_name(xp, idx)
23388 expand_T *xp;
23389 int idx;
23390{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023391 static long_u done;
23392 static hashitem_T *hi;
23393 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394
23395 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023397 done = 0;
23398 hi = func_hashtab.ht_array;
23399 }
23400 if (done < func_hashtab.ht_used)
23401 {
23402 if (done++ > 0)
23403 ++hi;
23404 while (HASHITEM_EMPTY(hi))
23405 ++hi;
23406 fp = HI2UF(hi);
23407
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023408 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023409 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023410
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023411 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23412 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413
23414 cat_func_name(IObuff, fp);
23415 if (xp->xp_context != EXPAND_USER_FUNC)
23416 {
23417 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023418 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 STRCAT(IObuff, ")");
23420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 return IObuff;
23422 }
23423 return NULL;
23424}
23425
23426#endif /* FEAT_CMDL_COMPL */
23427
23428/*
23429 * Copy the function name of "fp" to buffer "buf".
23430 * "buf" must be able to hold the function name plus three bytes.
23431 * Takes care of script-local function names.
23432 */
23433 static void
23434cat_func_name(buf, fp)
23435 char_u *buf;
23436 ufunc_T *fp;
23437{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023438 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 {
23440 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023441 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 }
23443 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023444 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445}
23446
23447/*
23448 * ":delfunction {name}"
23449 */
23450 void
23451ex_delfunction(eap)
23452 exarg_T *eap;
23453{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023454 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 char_u *p;
23456 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458
23459 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023460 name = trans_function_name(&p, eap->skip, 0, &fudi);
23461 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023463 {
23464 if (fudi.fd_dict != NULL && !eap->skip)
23465 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 if (!ends_excmd(*skipwhite(p)))
23469 {
23470 vim_free(name);
23471 EMSG(_(e_trailing));
23472 return;
23473 }
23474 eap->nextcmd = check_nextcmd(p);
23475 if (eap->nextcmd != NULL)
23476 *p = NUL;
23477
23478 if (!eap->skip)
23479 fp = find_func(name);
23480 vim_free(name);
23481
23482 if (!eap->skip)
23483 {
23484 if (fp == NULL)
23485 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023486 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 return;
23488 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023489 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 {
23491 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23492 return;
23493 }
23494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023497 /* Delete the dict item that refers to the function, it will
23498 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023499 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023501 else
23502 func_free(fp);
23503 }
23504}
23505
23506/*
23507 * Free a function and remove it from the list of functions.
23508 */
23509 static void
23510func_free(fp)
23511 ufunc_T *fp;
23512{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023513 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023514
23515 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023516 ga_clear_strings(&(fp->uf_args));
23517 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023518#ifdef FEAT_PROFILE
23519 vim_free(fp->uf_tml_count);
23520 vim_free(fp->uf_tml_total);
23521 vim_free(fp->uf_tml_self);
23522#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023523
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023524 /* remove the function from the function hashtable */
23525 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23526 if (HASHITEM_EMPTY(hi))
23527 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023528 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023529 hash_remove(&func_hashtab, hi);
23530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023531 vim_free(fp);
23532}
23533
23534/*
23535 * Unreference a Function: decrement the reference count and free it when it
23536 * becomes zero. Only for numbered functions.
23537 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023538 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023539func_unref(name)
23540 char_u *name;
23541{
23542 ufunc_T *fp;
23543
23544 if (name != NULL && isdigit(*name))
23545 {
23546 fp = find_func(name);
23547 if (fp == NULL)
23548 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023549 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023550 {
23551 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023552 * when "uf_calls" becomes zero. */
23553 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023554 func_free(fp);
23555 }
23556 }
23557}
23558
23559/*
23560 * Count a reference to a Function.
23561 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023562 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023563func_ref(name)
23564 char_u *name;
23565{
23566 ufunc_T *fp;
23567
23568 if (name != NULL && isdigit(*name))
23569 {
23570 fp = find_func(name);
23571 if (fp == NULL)
23572 EMSG2(_(e_intern2), "func_ref()");
23573 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023574 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 }
23576}
23577
23578/*
23579 * Call a user function.
23580 */
23581 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023582call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 ufunc_T *fp; /* pointer to function */
23584 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023585 typval_T *argvars; /* arguments */
23586 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 linenr_T firstline; /* first line of range */
23588 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023589 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590{
Bram Moolenaar33570922005-01-25 22:26:29 +000023591 char_u *save_sourcing_name;
23592 linenr_T save_sourcing_lnum;
23593 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023594 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023595 int save_did_emsg;
23596 static int depth = 0;
23597 dictitem_T *v;
23598 int fixvar_idx = 0; /* index in fixvar[] */
23599 int i;
23600 int ai;
23601 char_u numbuf[NUMBUFLEN];
23602 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023603#ifdef FEAT_PROFILE
23604 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023605 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607
23608 /* If depth of calling is getting too high, don't execute the function */
23609 if (depth >= p_mfd)
23610 {
23611 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023612 rettv->v_type = VAR_NUMBER;
23613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 return;
23615 }
23616 ++depth;
23617
23618 line_breakcheck(); /* check for CTRL-C hit */
23619
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023620 fc = (funccall_T *)alloc(sizeof(funccall_T));
23621 fc->caller = current_funccal;
23622 current_funccal = fc;
23623 fc->func = fp;
23624 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023625 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023626 fc->linenr = 0;
23627 fc->returned = FALSE;
23628 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023630 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23631 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632
Bram Moolenaar33570922005-01-25 22:26:29 +000023633 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023634 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023635 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23636 * each argument variable and saves a lot of time.
23637 */
23638 /*
23639 * Init l: variables.
23640 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023641 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023642 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023643 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023644 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23645 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023646 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023647 name = v->di_key;
23648 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023649 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023650 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023651 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023652 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023653 v->di_tv.vval.v_dict = selfdict;
23654 ++selfdict->dv_refcount;
23655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023656
Bram Moolenaar33570922005-01-25 22:26:29 +000023657 /*
23658 * Init a: variables.
23659 * Set a:0 to "argcount".
23660 * Set a:000 to a list with room for the "..." arguments.
23661 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023662 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023663 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023664 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023665 /* Use "name" to avoid a warning from some compiler that checks the
23666 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023667 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023668 name = v->di_key;
23669 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023670 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023671 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023672 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023673 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023674 v->di_tv.vval.v_list = &fc->l_varlist;
23675 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23676 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23677 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023678
23679 /*
23680 * Set a:firstline to "firstline" and a:lastline to "lastline".
23681 * Set a:name to named arguments.
23682 * Set a:N to the "..." arguments.
23683 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023684 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023685 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023686 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023687 (varnumber_T)lastline);
23688 for (i = 0; i < argcount; ++i)
23689 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023690 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023691 if (ai < 0)
23692 /* named argument a:name */
23693 name = FUNCARG(fp, i);
23694 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023695 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023696 /* "..." argument a:1, a:2, etc. */
23697 sprintf((char *)numbuf, "%d", ai + 1);
23698 name = numbuf;
23699 }
23700 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23701 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023702 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023703 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23704 }
23705 else
23706 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023707 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23708 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023709 if (v == NULL)
23710 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023711 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023712 }
23713 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023714 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023715
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023716 /* Note: the values are copied directly to avoid alloc/free.
23717 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023718 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023719 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023720
23721 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23722 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023723 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23724 fc->l_listitems[ai].li_tv = argvars[i];
23725 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023726 }
23727 }
23728
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 /* Don't redraw while executing the function. */
23730 ++RedrawingDisabled;
23731 save_sourcing_name = sourcing_name;
23732 save_sourcing_lnum = sourcing_lnum;
23733 sourcing_lnum = 1;
23734 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023735 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 if (sourcing_name != NULL)
23737 {
23738 if (save_sourcing_name != NULL
23739 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23740 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23741 else
23742 STRCPY(sourcing_name, "function ");
23743 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23744
23745 if (p_verbose >= 12)
23746 {
23747 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023748 verbose_enter_scroll();
23749
Bram Moolenaar555b2802005-05-19 21:08:39 +000023750 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 if (p_verbose >= 14)
23752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023754 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023755 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023756 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757
23758 msg_puts((char_u *)"(");
23759 for (i = 0; i < argcount; ++i)
23760 {
23761 if (i > 0)
23762 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023763 if (argvars[i].v_type == VAR_NUMBER)
23764 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 else
23766 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023767 /* Do not want errors such as E724 here. */
23768 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023769 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023770 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023771 if (s != NULL)
23772 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023773 if (vim_strsize(s) > MSG_BUF_CLEN)
23774 {
23775 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23776 s = buf;
23777 }
23778 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023779 vim_free(tofree);
23780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 }
23782 }
23783 msg_puts((char_u *)")");
23784 }
23785 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023786
23787 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 --no_wait_return;
23789 }
23790 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023791#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023792 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023793 {
23794 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23795 func_do_profile(fp);
23796 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023797 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023798 {
23799 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023800 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023801 profile_zero(&fp->uf_tm_children);
23802 }
23803 script_prof_save(&wait_start);
23804 }
23805#endif
23806
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023808 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 save_did_emsg = did_emsg;
23810 did_emsg = FALSE;
23811
23812 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023813 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23815
23816 --RedrawingDisabled;
23817
23818 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023819 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023821 clear_tv(rettv);
23822 rettv->v_type = VAR_NUMBER;
23823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 }
23825
Bram Moolenaar05159a02005-02-26 23:04:13 +000023826#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023827 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023828 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023829 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023830 profile_end(&call_start);
23831 profile_sub_wait(&wait_start, &call_start);
23832 profile_add(&fp->uf_tm_total, &call_start);
23833 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023834 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023835 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023836 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23837 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023838 }
23839 }
23840#endif
23841
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 /* when being verbose, mention the return value */
23843 if (p_verbose >= 12)
23844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023846 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023849 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023850 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023851 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023852 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023855 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023856 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023857 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023858 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023859
Bram Moolenaar555b2802005-05-19 21:08:39 +000023860 /* The value may be very long. Skip the middle part, so that we
23861 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023862 * truncate it at the end. Don't want errors such as E724 here. */
23863 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023864 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023865 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023866 if (s != NULL)
23867 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023868 if (vim_strsize(s) > MSG_BUF_CLEN)
23869 {
23870 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23871 s = buf;
23872 }
23873 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023874 vim_free(tofree);
23875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 }
23877 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023878
23879 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 --no_wait_return;
23881 }
23882
23883 vim_free(sourcing_name);
23884 sourcing_name = save_sourcing_name;
23885 sourcing_lnum = save_sourcing_lnum;
23886 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023887#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023888 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023889 script_prof_restore(&wait_start);
23890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891
23892 if (p_verbose >= 12 && sourcing_name != NULL)
23893 {
23894 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023895 verbose_enter_scroll();
23896
Bram Moolenaar555b2802005-05-19 21:08:39 +000023897 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023899
23900 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 --no_wait_return;
23902 }
23903
23904 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023905 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023907
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023908 /* If the a:000 list and the l: and a: dicts are not referenced we can
23909 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023910 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23911 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23912 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23913 {
23914 free_funccal(fc, FALSE);
23915 }
23916 else
23917 {
23918 hashitem_T *hi;
23919 listitem_T *li;
23920 int todo;
23921
23922 /* "fc" is still in use. This can happen when returning "a:000" or
23923 * assigning "l:" to a global variable.
23924 * Link "fc" in the list for garbage collection later. */
23925 fc->caller = previous_funccal;
23926 previous_funccal = fc;
23927
23928 /* Make a copy of the a: variables, since we didn't do that above. */
23929 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23930 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23931 {
23932 if (!HASHITEM_EMPTY(hi))
23933 {
23934 --todo;
23935 v = HI2DI(hi);
23936 copy_tv(&v->di_tv, &v->di_tv);
23937 }
23938 }
23939
23940 /* Make a copy of the a:000 items, since we didn't do that above. */
23941 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23942 copy_tv(&li->li_tv, &li->li_tv);
23943 }
23944}
23945
23946/*
23947 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023948 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023949 */
23950 static int
23951can_free_funccal(fc, copyID)
23952 funccall_T *fc;
23953 int copyID;
23954{
23955 return (fc->l_varlist.lv_copyID != copyID
23956 && fc->l_vars.dv_copyID != copyID
23957 && fc->l_avars.dv_copyID != copyID);
23958}
23959
23960/*
23961 * Free "fc" and what it contains.
23962 */
23963 static void
23964free_funccal(fc, free_val)
23965 funccall_T *fc;
23966 int free_val; /* a: vars were allocated */
23967{
23968 listitem_T *li;
23969
23970 /* The a: variables typevals may not have been allocated, only free the
23971 * allocated variables. */
23972 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23973
23974 /* free all l: variables */
23975 vars_clear(&fc->l_vars.dv_hashtab);
23976
23977 /* Free the a:000 variables if they were allocated. */
23978 if (free_val)
23979 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23980 clear_tv(&li->li_tv);
23981
23982 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983}
23984
23985/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023986 * Add a number variable "name" to dict "dp" with value "nr".
23987 */
23988 static void
23989add_nr_var(dp, v, name, nr)
23990 dict_T *dp;
23991 dictitem_T *v;
23992 char *name;
23993 varnumber_T nr;
23994{
23995 STRCPY(v->di_key, name);
23996 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23997 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23998 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023999 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024000 v->di_tv.vval.v_number = nr;
24001}
24002
24003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 * ":return [expr]"
24005 */
24006 void
24007ex_return(eap)
24008 exarg_T *eap;
24009{
24010 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024011 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 int returning = FALSE;
24013
24014 if (current_funccal == NULL)
24015 {
24016 EMSG(_("E133: :return not inside a function"));
24017 return;
24018 }
24019
24020 if (eap->skip)
24021 ++emsg_skip;
24022
24023 eap->nextcmd = NULL;
24024 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024025 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 {
24027 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024028 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024030 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 }
24032 /* It's safer to return also on error. */
24033 else if (!eap->skip)
24034 {
24035 /*
24036 * Return unless the expression evaluation has been cancelled due to an
24037 * aborting error, an interrupt, or an exception.
24038 */
24039 if (!aborting())
24040 returning = do_return(eap, FALSE, TRUE, NULL);
24041 }
24042
24043 /* When skipping or the return gets pending, advance to the next command
24044 * in this line (!returning). Otherwise, ignore the rest of the line.
24045 * Following lines will be ignored by get_func_line(). */
24046 if (returning)
24047 eap->nextcmd = NULL;
24048 else if (eap->nextcmd == NULL) /* no argument */
24049 eap->nextcmd = check_nextcmd(arg);
24050
24051 if (eap->skip)
24052 --emsg_skip;
24053}
24054
24055/*
24056 * Return from a function. Possibly makes the return pending. Also called
24057 * for a pending return at the ":endtry" or after returning from an extra
24058 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024059 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024060 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 * FALSE when the return gets pending.
24062 */
24063 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024064do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 exarg_T *eap;
24066 int reanimate;
24067 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024068 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069{
24070 int idx;
24071 struct condstack *cstack = eap->cstack;
24072
24073 if (reanimate)
24074 /* Undo the return. */
24075 current_funccal->returned = FALSE;
24076
24077 /*
24078 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24079 * not in its finally clause (which then is to be executed next) is found.
24080 * In this case, make the ":return" pending for execution at the ":endtry".
24081 * Otherwise, return normally.
24082 */
24083 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24084 if (idx >= 0)
24085 {
24086 cstack->cs_pending[idx] = CSTP_RETURN;
24087
24088 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024089 /* A pending return again gets pending. "rettv" points to an
24090 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024092 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 else
24094 {
24095 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024096 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024098 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024100 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 {
24102 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024103 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024104 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105 else
24106 EMSG(_(e_outofmem));
24107 }
24108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024109 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110
24111 if (reanimate)
24112 {
24113 /* The pending return value could be overwritten by a ":return"
24114 * without argument in a finally clause; reset the default
24115 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024116 current_funccal->rettv->v_type = VAR_NUMBER;
24117 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 }
24119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024120 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 }
24122 else
24123 {
24124 current_funccal->returned = TRUE;
24125
24126 /* If the return is carried out now, store the return value. For
24127 * a return immediately after reanimation, the value is already
24128 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024129 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024131 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024132 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024134 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 }
24136 }
24137
24138 return idx < 0;
24139}
24140
24141/*
24142 * Free the variable with a pending return value.
24143 */
24144 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024145discard_pending_return(rettv)
24146 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147{
Bram Moolenaar33570922005-01-25 22:26:29 +000024148 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149}
24150
24151/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024152 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 * is an allocated string. Used by report_pending() for verbose messages.
24154 */
24155 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024156get_return_cmd(rettv)
24157 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024159 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024160 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024161 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024163 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024164 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024165 if (s == NULL)
24166 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024167
24168 STRCPY(IObuff, ":return ");
24169 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24170 if (STRLEN(s) + 8 >= IOSIZE)
24171 STRCPY(IObuff + IOSIZE - 4, "...");
24172 vim_free(tofree);
24173 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174}
24175
24176/*
24177 * Get next function line.
24178 * Called by do_cmdline() to get the next line.
24179 * Returns allocated string, or NULL for end of function.
24180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 char_u *
24182get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024183 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024185 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186{
Bram Moolenaar33570922005-01-25 22:26:29 +000024187 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024188 ufunc_T *fp = fcp->func;
24189 char_u *retval;
24190 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191
24192 /* If breakpoints have been added/deleted need to check for it. */
24193 if (fcp->dbg_tick != debug_tick)
24194 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024195 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 sourcing_lnum);
24197 fcp->dbg_tick = debug_tick;
24198 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024199#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024200 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024201 func_line_end(cookie);
24202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203
Bram Moolenaar05159a02005-02-26 23:04:13 +000024204 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024205 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24206 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 retval = NULL;
24208 else
24209 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024210 /* Skip NULL lines (continuation lines). */
24211 while (fcp->linenr < gap->ga_len
24212 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24213 ++fcp->linenr;
24214 if (fcp->linenr >= gap->ga_len)
24215 retval = NULL;
24216 else
24217 {
24218 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24219 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024220#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024221 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024222 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024223#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 }
24226
24227 /* Did we encounter a breakpoint? */
24228 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24229 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024230 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024232 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 sourcing_lnum);
24234 fcp->dbg_tick = debug_tick;
24235 }
24236
24237 return retval;
24238}
24239
Bram Moolenaar05159a02005-02-26 23:04:13 +000024240#if defined(FEAT_PROFILE) || defined(PROTO)
24241/*
24242 * Called when starting to read a function line.
24243 * "sourcing_lnum" must be correct!
24244 * When skipping lines it may not actually be executed, but we won't find out
24245 * until later and we need to store the time now.
24246 */
24247 void
24248func_line_start(cookie)
24249 void *cookie;
24250{
24251 funccall_T *fcp = (funccall_T *)cookie;
24252 ufunc_T *fp = fcp->func;
24253
24254 if (fp->uf_profiling && sourcing_lnum >= 1
24255 && sourcing_lnum <= fp->uf_lines.ga_len)
24256 {
24257 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024258 /* Skip continuation lines. */
24259 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24260 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024261 fp->uf_tml_execed = FALSE;
24262 profile_start(&fp->uf_tml_start);
24263 profile_zero(&fp->uf_tml_children);
24264 profile_get_wait(&fp->uf_tml_wait);
24265 }
24266}
24267
24268/*
24269 * Called when actually executing a function line.
24270 */
24271 void
24272func_line_exec(cookie)
24273 void *cookie;
24274{
24275 funccall_T *fcp = (funccall_T *)cookie;
24276 ufunc_T *fp = fcp->func;
24277
24278 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24279 fp->uf_tml_execed = TRUE;
24280}
24281
24282/*
24283 * Called when done with a function line.
24284 */
24285 void
24286func_line_end(cookie)
24287 void *cookie;
24288{
24289 funccall_T *fcp = (funccall_T *)cookie;
24290 ufunc_T *fp = fcp->func;
24291
24292 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24293 {
24294 if (fp->uf_tml_execed)
24295 {
24296 ++fp->uf_tml_count[fp->uf_tml_idx];
24297 profile_end(&fp->uf_tml_start);
24298 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024299 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024300 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24301 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024302 }
24303 fp->uf_tml_idx = -1;
24304 }
24305}
24306#endif
24307
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308/*
24309 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024310 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 */
24312 int
24313func_has_ended(cookie)
24314 void *cookie;
24315{
Bram Moolenaar33570922005-01-25 22:26:29 +000024316 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317
24318 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24319 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024320 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 || fcp->returned);
24322}
24323
24324/*
24325 * return TRUE if cookie indicates a function which "abort"s on errors.
24326 */
24327 int
24328func_has_abort(cookie)
24329 void *cookie;
24330{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024331 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332}
24333
24334#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24335typedef enum
24336{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024337 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24338 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24339 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340} var_flavour_T;
24341
24342static var_flavour_T var_flavour __ARGS((char_u *varname));
24343
24344 static var_flavour_T
24345var_flavour(varname)
24346 char_u *varname;
24347{
24348 char_u *p = varname;
24349
24350 if (ASCII_ISUPPER(*p))
24351 {
24352 while (*(++p))
24353 if (ASCII_ISLOWER(*p))
24354 return VAR_FLAVOUR_SESSION;
24355 return VAR_FLAVOUR_VIMINFO;
24356 }
24357 else
24358 return VAR_FLAVOUR_DEFAULT;
24359}
24360#endif
24361
24362#if defined(FEAT_VIMINFO) || defined(PROTO)
24363/*
24364 * Restore global vars that start with a capital from the viminfo file
24365 */
24366 int
24367read_viminfo_varlist(virp, writing)
24368 vir_T *virp;
24369 int writing;
24370{
24371 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024372 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374
24375 if (!writing && (find_viminfo_parameter('!') != NULL))
24376 {
24377 tab = vim_strchr(virp->vir_line + 1, '\t');
24378 if (tab != NULL)
24379 {
24380 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024381 switch (*tab)
24382 {
24383 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024384#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024385 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024386#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024387 case 'D': type = VAR_DICT; break;
24388 case 'L': type = VAR_LIST; break;
24389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390
24391 tab = vim_strchr(tab, '\t');
24392 if (tab != NULL)
24393 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024394 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024395 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024396 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024398#ifdef FEAT_FLOAT
24399 else if (type == VAR_FLOAT)
24400 (void)string2float(tab + 1, &tv.vval.v_float);
24401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024403 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024404 if (type == VAR_DICT || type == VAR_LIST)
24405 {
24406 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24407
24408 if (etv == NULL)
24409 /* Failed to parse back the dict or list, use it as a
24410 * string. */
24411 tv.v_type = VAR_STRING;
24412 else
24413 {
24414 vim_free(tv.vval.v_string);
24415 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024416 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024417 }
24418 }
24419
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024420 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024421
24422 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024423 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024424 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24425 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 }
24427 }
24428 }
24429
24430 return viminfo_readline(virp);
24431}
24432
24433/*
24434 * Write global vars that start with a capital to the viminfo file
24435 */
24436 void
24437write_viminfo_varlist(fp)
24438 FILE *fp;
24439{
Bram Moolenaar33570922005-01-25 22:26:29 +000024440 hashitem_T *hi;
24441 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024442 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024443 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024444 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024445 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024446 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447
24448 if (find_viminfo_parameter('!') == NULL)
24449 return;
24450
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024451 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024452
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024453 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024454 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024456 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024458 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024459 this_var = HI2DI(hi);
24460 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024461 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024462 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024463 {
24464 case VAR_STRING: s = "STR"; break;
24465 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024466#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024467 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024468#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024469 case VAR_DICT: s = "DIC"; break;
24470 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024471 default: continue;
24472 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024473 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024474 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024475 if (p != NULL)
24476 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024477 vim_free(tofree);
24478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 }
24480 }
24481}
24482#endif
24483
24484#if defined(FEAT_SESSION) || defined(PROTO)
24485 int
24486store_session_globals(fd)
24487 FILE *fd;
24488{
Bram Moolenaar33570922005-01-25 22:26:29 +000024489 hashitem_T *hi;
24490 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024491 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 char_u *p, *t;
24493
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024494 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024495 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024497 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024499 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024500 this_var = HI2DI(hi);
24501 if ((this_var->di_tv.v_type == VAR_NUMBER
24502 || this_var->di_tv.v_type == VAR_STRING)
24503 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024504 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024505 /* Escape special characters with a backslash. Turn a LF and
24506 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024507 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024508 (char_u *)"\\\"\n\r");
24509 if (p == NULL) /* out of memory */
24510 break;
24511 for (t = p; *t != NUL; ++t)
24512 if (*t == '\n')
24513 *t = 'n';
24514 else if (*t == '\r')
24515 *t = 'r';
24516 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024517 this_var->di_key,
24518 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24519 : ' ',
24520 p,
24521 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24522 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024523 || put_eol(fd) == FAIL)
24524 {
24525 vim_free(p);
24526 return FAIL;
24527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 vim_free(p);
24529 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024530#ifdef FEAT_FLOAT
24531 else if (this_var->di_tv.v_type == VAR_FLOAT
24532 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24533 {
24534 float_T f = this_var->di_tv.vval.v_float;
24535 int sign = ' ';
24536
24537 if (f < 0)
24538 {
24539 f = -f;
24540 sign = '-';
24541 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024542 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024543 this_var->di_key, sign, f) < 0)
24544 || put_eol(fd) == FAIL)
24545 return FAIL;
24546 }
24547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 }
24549 }
24550 return OK;
24551}
24552#endif
24553
Bram Moolenaar661b1822005-07-28 22:36:45 +000024554/*
24555 * Display script name where an item was last set.
24556 * Should only be invoked when 'verbose' is non-zero.
24557 */
24558 void
24559last_set_msg(scriptID)
24560 scid_T scriptID;
24561{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024562 char_u *p;
24563
Bram Moolenaar661b1822005-07-28 22:36:45 +000024564 if (scriptID != 0)
24565 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024566 p = home_replace_save(NULL, get_scriptname(scriptID));
24567 if (p != NULL)
24568 {
24569 verbose_enter();
24570 MSG_PUTS(_("\n\tLast set from "));
24571 MSG_PUTS(p);
24572 vim_free(p);
24573 verbose_leave();
24574 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024575 }
24576}
24577
Bram Moolenaard812df62008-11-09 12:46:09 +000024578/*
24579 * List v:oldfiles in a nice way.
24580 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024581 void
24582ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024583 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024584{
24585 list_T *l = vimvars[VV_OLDFILES].vv_list;
24586 listitem_T *li;
24587 int nr = 0;
24588
24589 if (l == NULL)
24590 msg((char_u *)_("No old files"));
24591 else
24592 {
24593 msg_start();
24594 msg_scroll = TRUE;
24595 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24596 {
24597 msg_outnum((long)++nr);
24598 MSG_PUTS(": ");
24599 msg_outtrans(get_tv_string(&li->li_tv));
24600 msg_putchar('\n');
24601 out_flush(); /* output one line at a time */
24602 ui_breakcheck();
24603 }
24604 /* Assume "got_int" was set to truncate the listing. */
24605 got_int = FALSE;
24606
24607#ifdef FEAT_BROWSE_CMD
24608 if (cmdmod.browse)
24609 {
24610 quit_more = FALSE;
24611 nr = prompt_for_number(FALSE);
24612 msg_starthere();
24613 if (nr > 0)
24614 {
24615 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24616 (long)nr);
24617
24618 if (p != NULL)
24619 {
24620 p = expand_env_save(p);
24621 eap->arg = p;
24622 eap->cmdidx = CMD_edit;
24623 cmdmod.browse = FALSE;
24624 do_exedit(eap, NULL);
24625 vim_free(p);
24626 }
24627 }
24628 }
24629#endif
24630 }
24631}
24632
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633#endif /* FEAT_EVAL */
24634
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024636#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637
24638#ifdef WIN3264
24639/*
24640 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24641 */
24642static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24643static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24644static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24645
24646/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024647 * Get the short path (8.3) for the filename in "fnamep".
24648 * Only works for a valid file name.
24649 * When the path gets longer "fnamep" is changed and the allocated buffer
24650 * is put in "bufp".
24651 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24652 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 */
24654 static int
24655get_short_pathname(fnamep, bufp, fnamelen)
24656 char_u **fnamep;
24657 char_u **bufp;
24658 int *fnamelen;
24659{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024660 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 char_u *newbuf;
24662
24663 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 l = GetShortPathName(*fnamep, *fnamep, len);
24665 if (l > len - 1)
24666 {
24667 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024668 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 newbuf = vim_strnsave(*fnamep, l);
24670 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024671 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672
24673 vim_free(*bufp);
24674 *fnamep = *bufp = newbuf;
24675
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024676 /* Really should always succeed, as the buffer is big enough. */
24677 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 }
24679
24680 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024681 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682}
24683
24684/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024685 * Get the short path (8.3) for the filename in "fname". The converted
24686 * path is returned in "bufp".
24687 *
24688 * Some of the directories specified in "fname" may not exist. This function
24689 * will shorten the existing directories at the beginning of the path and then
24690 * append the remaining non-existing path.
24691 *
24692 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024693 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024694 * bufp - Pointer to an allocated buffer for the filename.
24695 * fnamelen - Length of the filename pointed to by fname
24696 *
24697 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698 */
24699 static int
24700shortpath_for_invalid_fname(fname, bufp, fnamelen)
24701 char_u **fname;
24702 char_u **bufp;
24703 int *fnamelen;
24704{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024705 char_u *short_fname, *save_fname, *pbuf_unused;
24706 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024708 int old_len, len;
24709 int new_len, sfx_len;
24710 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711
24712 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024713 old_len = *fnamelen;
24714 save_fname = vim_strnsave(*fname, old_len);
24715 pbuf_unused = NULL;
24716 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024718 endp = save_fname + old_len - 1; /* Find the end of the copy */
24719 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024721 /*
24722 * Try shortening the supplied path till it succeeds by removing one
24723 * directory at a time from the tail of the path.
24724 */
24725 len = 0;
24726 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024728 /* go back one path-separator */
24729 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24730 --endp;
24731 if (endp <= save_fname)
24732 break; /* processed the complete path */
24733
24734 /*
24735 * Replace the path separator with a NUL and try to shorten the
24736 * resulting path.
24737 */
24738 ch = *endp;
24739 *endp = 0;
24740 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024741 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024742 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24743 {
24744 retval = FAIL;
24745 goto theend;
24746 }
24747 *endp = ch; /* preserve the string */
24748
24749 if (len > 0)
24750 break; /* successfully shortened the path */
24751
24752 /* failed to shorten the path. Skip the path separator */
24753 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 }
24755
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024756 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024758 /*
24759 * Succeeded in shortening the path. Now concatenate the shortened
24760 * path with the remaining path at the tail.
24761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024763 /* Compute the length of the new path. */
24764 sfx_len = (int)(save_endp - endp) + 1;
24765 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024767 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024769 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024771 /* There is not enough space in the currently allocated string,
24772 * copy it to a buffer big enough. */
24773 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024775 {
24776 retval = FAIL;
24777 goto theend;
24778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 }
24780 else
24781 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024782 /* Transfer short_fname to the main buffer (it's big enough),
24783 * unless get_short_pathname() did its work in-place. */
24784 *fname = *bufp = save_fname;
24785 if (short_fname != save_fname)
24786 vim_strncpy(save_fname, short_fname, len);
24787 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024789
24790 /* concat the not-shortened part of the path */
24791 vim_strncpy(*fname + len, endp, sfx_len);
24792 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024794
24795theend:
24796 vim_free(pbuf_unused);
24797 vim_free(save_fname);
24798
24799 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800}
24801
24802/*
24803 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024804 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805 */
24806 static int
24807shortpath_for_partial(fnamep, bufp, fnamelen)
24808 char_u **fnamep;
24809 char_u **bufp;
24810 int *fnamelen;
24811{
24812 int sepcount, len, tflen;
24813 char_u *p;
24814 char_u *pbuf, *tfname;
24815 int hasTilde;
24816
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024817 /* Count up the path separators from the RHS.. so we know which part
24818 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024820 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821 if (vim_ispathsep(*p))
24822 ++sepcount;
24823
24824 /* Need full path first (use expand_env() to remove a "~/") */
24825 hasTilde = (**fnamep == '~');
24826 if (hasTilde)
24827 pbuf = tfname = expand_env_save(*fnamep);
24828 else
24829 pbuf = tfname = FullName_save(*fnamep, FALSE);
24830
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024831 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024833 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835
24836 if (len == 0)
24837 {
24838 /* Don't have a valid filename, so shorten the rest of the
24839 * path if we can. This CAN give us invalid 8.3 filenames, but
24840 * there's not a lot of point in guessing what it might be.
24841 */
24842 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024843 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24844 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845 }
24846
24847 /* Count the paths backward to find the beginning of the desired string. */
24848 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024849 {
24850#ifdef FEAT_MBYTE
24851 if (has_mbyte)
24852 p -= mb_head_off(tfname, p);
24853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 if (vim_ispathsep(*p))
24855 {
24856 if (sepcount == 0 || (hasTilde && sepcount == 1))
24857 break;
24858 else
24859 sepcount --;
24860 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 if (hasTilde)
24863 {
24864 --p;
24865 if (p >= tfname)
24866 *p = '~';
24867 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024868 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 }
24870 else
24871 ++p;
24872
24873 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24874 vim_free(*bufp);
24875 *fnamelen = (int)STRLEN(p);
24876 *bufp = pbuf;
24877 *fnamep = p;
24878
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024879 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880}
24881#endif /* WIN3264 */
24882
24883/*
24884 * Adjust a filename, according to a string of modifiers.
24885 * *fnamep must be NUL terminated when called. When returning, the length is
24886 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024887 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 * When there is an error, *fnamep is set to NULL.
24889 */
24890 int
24891modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24892 char_u *src; /* string with modifiers */
24893 int *usedlen; /* characters after src that are used */
24894 char_u **fnamep; /* file name so far */
24895 char_u **bufp; /* buffer for allocated file name or NULL */
24896 int *fnamelen; /* length of fnamep */
24897{
24898 int valid = 0;
24899 char_u *tail;
24900 char_u *s, *p, *pbuf;
24901 char_u dirname[MAXPATHL];
24902 int c;
24903 int has_fullname = 0;
24904#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024905 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 int has_shortname = 0;
24907#endif
24908
24909repeat:
24910 /* ":p" - full path/file_name */
24911 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24912 {
24913 has_fullname = 1;
24914
24915 valid |= VALID_PATH;
24916 *usedlen += 2;
24917
24918 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24919 if ((*fnamep)[0] == '~'
24920#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24921 && ((*fnamep)[1] == '/'
24922# ifdef BACKSLASH_IN_FILENAME
24923 || (*fnamep)[1] == '\\'
24924# endif
24925 || (*fnamep)[1] == NUL)
24926
24927#endif
24928 )
24929 {
24930 *fnamep = expand_env_save(*fnamep);
24931 vim_free(*bufp); /* free any allocated file name */
24932 *bufp = *fnamep;
24933 if (*fnamep == NULL)
24934 return -1;
24935 }
24936
24937 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024938 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 {
24940 if (vim_ispathsep(*p)
24941 && p[1] == '.'
24942 && (p[2] == NUL
24943 || vim_ispathsep(p[2])
24944 || (p[2] == '.'
24945 && (p[3] == NUL || vim_ispathsep(p[3])))))
24946 break;
24947 }
24948
24949 /* FullName_save() is slow, don't use it when not needed. */
24950 if (*p != NUL || !vim_isAbsName(*fnamep))
24951 {
24952 *fnamep = FullName_save(*fnamep, *p != NUL);
24953 vim_free(*bufp); /* free any allocated file name */
24954 *bufp = *fnamep;
24955 if (*fnamep == NULL)
24956 return -1;
24957 }
24958
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024959#ifdef WIN3264
24960# if _WIN32_WINNT >= 0x0500
24961 if (vim_strchr(*fnamep, '~') != NULL)
24962 {
24963 /* Expand 8.3 filename to full path. Needed to make sure the same
24964 * file does not have two different names.
24965 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24966 p = alloc(_MAX_PATH + 1);
24967 if (p != NULL)
24968 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024969 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024970 {
24971 vim_free(*bufp);
24972 *bufp = *fnamep = p;
24973 }
24974 else
24975 vim_free(p);
24976 }
24977 }
24978# endif
24979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 /* Append a path separator to a directory. */
24981 if (mch_isdir(*fnamep))
24982 {
24983 /* Make room for one or two extra characters. */
24984 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24985 vim_free(*bufp); /* free any allocated file name */
24986 *bufp = *fnamep;
24987 if (*fnamep == NULL)
24988 return -1;
24989 add_pathsep(*fnamep);
24990 }
24991 }
24992
24993 /* ":." - path relative to the current directory */
24994 /* ":~" - path relative to the home directory */
24995 /* ":8" - shortname path - postponed till after */
24996 while (src[*usedlen] == ':'
24997 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24998 {
24999 *usedlen += 2;
25000 if (c == '8')
25001 {
25002#ifdef WIN3264
25003 has_shortname = 1; /* Postpone this. */
25004#endif
25005 continue;
25006 }
25007 pbuf = NULL;
25008 /* Need full path first (use expand_env() to remove a "~/") */
25009 if (!has_fullname)
25010 {
25011 if (c == '.' && **fnamep == '~')
25012 p = pbuf = expand_env_save(*fnamep);
25013 else
25014 p = pbuf = FullName_save(*fnamep, FALSE);
25015 }
25016 else
25017 p = *fnamep;
25018
25019 has_fullname = 0;
25020
25021 if (p != NULL)
25022 {
25023 if (c == '.')
25024 {
25025 mch_dirname(dirname, MAXPATHL);
25026 s = shorten_fname(p, dirname);
25027 if (s != NULL)
25028 {
25029 *fnamep = s;
25030 if (pbuf != NULL)
25031 {
25032 vim_free(*bufp); /* free any allocated file name */
25033 *bufp = pbuf;
25034 pbuf = NULL;
25035 }
25036 }
25037 }
25038 else
25039 {
25040 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25041 /* Only replace it when it starts with '~' */
25042 if (*dirname == '~')
25043 {
25044 s = vim_strsave(dirname);
25045 if (s != NULL)
25046 {
25047 *fnamep = s;
25048 vim_free(*bufp);
25049 *bufp = s;
25050 }
25051 }
25052 }
25053 vim_free(pbuf);
25054 }
25055 }
25056
25057 tail = gettail(*fnamep);
25058 *fnamelen = (int)STRLEN(*fnamep);
25059
25060 /* ":h" - head, remove "/file_name", can be repeated */
25061 /* Don't remove the first "/" or "c:\" */
25062 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25063 {
25064 valid |= VALID_HEAD;
25065 *usedlen += 2;
25066 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025067 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025068 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069 *fnamelen = (int)(tail - *fnamep);
25070#ifdef VMS
25071 if (*fnamelen > 0)
25072 *fnamelen += 1; /* the path separator is part of the path */
25073#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025074 if (*fnamelen == 0)
25075 {
25076 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25077 p = vim_strsave((char_u *)".");
25078 if (p == NULL)
25079 return -1;
25080 vim_free(*bufp);
25081 *bufp = *fnamep = tail = p;
25082 *fnamelen = 1;
25083 }
25084 else
25085 {
25086 while (tail > s && !after_pathsep(s, tail))
25087 mb_ptr_back(*fnamep, tail);
25088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 }
25090
25091 /* ":8" - shortname */
25092 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25093 {
25094 *usedlen += 2;
25095#ifdef WIN3264
25096 has_shortname = 1;
25097#endif
25098 }
25099
25100#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025101 /*
25102 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103 */
25104 if (has_shortname)
25105 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025106 /* Copy the string if it is shortened by :h and when it wasn't copied
25107 * yet, because we are going to change it in place. Avoids changing
25108 * the buffer name for "%:8". */
25109 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 {
25111 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025112 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 return -1;
25114 vim_free(*bufp);
25115 *bufp = *fnamep = p;
25116 }
25117
25118 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025119 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 if (!has_fullname && !vim_isAbsName(*fnamep))
25121 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025122 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 return -1;
25124 }
25125 else
25126 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025127 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128
Bram Moolenaardc935552011-08-17 15:23:23 +020025129 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025131 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132 return -1;
25133
25134 if (l == 0)
25135 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025136 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025138 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 return -1;
25140 }
25141 *fnamelen = l;
25142 }
25143 }
25144#endif /* WIN3264 */
25145
25146 /* ":t" - tail, just the basename */
25147 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25148 {
25149 *usedlen += 2;
25150 *fnamelen -= (int)(tail - *fnamep);
25151 *fnamep = tail;
25152 }
25153
25154 /* ":e" - extension, can be repeated */
25155 /* ":r" - root, without extension, can be repeated */
25156 while (src[*usedlen] == ':'
25157 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25158 {
25159 /* find a '.' in the tail:
25160 * - for second :e: before the current fname
25161 * - otherwise: The last '.'
25162 */
25163 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25164 s = *fnamep - 2;
25165 else
25166 s = *fnamep + *fnamelen - 1;
25167 for ( ; s > tail; --s)
25168 if (s[0] == '.')
25169 break;
25170 if (src[*usedlen + 1] == 'e') /* :e */
25171 {
25172 if (s > tail)
25173 {
25174 *fnamelen += (int)(*fnamep - (s + 1));
25175 *fnamep = s + 1;
25176#ifdef VMS
25177 /* cut version from the extension */
25178 s = *fnamep + *fnamelen - 1;
25179 for ( ; s > *fnamep; --s)
25180 if (s[0] == ';')
25181 break;
25182 if (s > *fnamep)
25183 *fnamelen = s - *fnamep;
25184#endif
25185 }
25186 else if (*fnamep <= tail)
25187 *fnamelen = 0;
25188 }
25189 else /* :r */
25190 {
25191 if (s > tail) /* remove one extension */
25192 *fnamelen = (int)(s - *fnamep);
25193 }
25194 *usedlen += 2;
25195 }
25196
25197 /* ":s?pat?foo?" - substitute */
25198 /* ":gs?pat?foo?" - global substitute */
25199 if (src[*usedlen] == ':'
25200 && (src[*usedlen + 1] == 's'
25201 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25202 {
25203 char_u *str;
25204 char_u *pat;
25205 char_u *sub;
25206 int sep;
25207 char_u *flags;
25208 int didit = FALSE;
25209
25210 flags = (char_u *)"";
25211 s = src + *usedlen + 2;
25212 if (src[*usedlen + 1] == 'g')
25213 {
25214 flags = (char_u *)"g";
25215 ++s;
25216 }
25217
25218 sep = *s++;
25219 if (sep)
25220 {
25221 /* find end of pattern */
25222 p = vim_strchr(s, sep);
25223 if (p != NULL)
25224 {
25225 pat = vim_strnsave(s, (int)(p - s));
25226 if (pat != NULL)
25227 {
25228 s = p + 1;
25229 /* find end of substitution */
25230 p = vim_strchr(s, sep);
25231 if (p != NULL)
25232 {
25233 sub = vim_strnsave(s, (int)(p - s));
25234 str = vim_strnsave(*fnamep, *fnamelen);
25235 if (sub != NULL && str != NULL)
25236 {
25237 *usedlen = (int)(p + 1 - src);
25238 s = do_string_sub(str, pat, sub, flags);
25239 if (s != NULL)
25240 {
25241 *fnamep = s;
25242 *fnamelen = (int)STRLEN(s);
25243 vim_free(*bufp);
25244 *bufp = s;
25245 didit = TRUE;
25246 }
25247 }
25248 vim_free(sub);
25249 vim_free(str);
25250 }
25251 vim_free(pat);
25252 }
25253 }
25254 /* after using ":s", repeat all the modifiers */
25255 if (didit)
25256 goto repeat;
25257 }
25258 }
25259
Bram Moolenaar26df0922014-02-23 23:39:13 +010025260 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25261 {
25262 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25263 if (p == NULL)
25264 return -1;
25265 vim_free(*bufp);
25266 *bufp = *fnamep = p;
25267 *fnamelen = (int)STRLEN(p);
25268 *usedlen += 2;
25269 }
25270
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 return valid;
25272}
25273
25274/*
25275 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25276 * "flags" can be "g" to do a global substitute.
25277 * Returns an allocated string, NULL for error.
25278 */
25279 char_u *
25280do_string_sub(str, pat, sub, flags)
25281 char_u *str;
25282 char_u *pat;
25283 char_u *sub;
25284 char_u *flags;
25285{
25286 int sublen;
25287 regmatch_T regmatch;
25288 int i;
25289 int do_all;
25290 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025291 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 garray_T ga;
25293 char_u *ret;
25294 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025295 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296
25297 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25298 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025299 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300
25301 ga_init2(&ga, 1, 200);
25302
25303 do_all = (flags[0] == 'g');
25304
25305 regmatch.rm_ic = p_ic;
25306 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25307 if (regmatch.regprog != NULL)
25308 {
25309 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025310 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025311 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25312 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025313 /* Skip empty match except for first match. */
25314 if (regmatch.startp[0] == regmatch.endp[0])
25315 {
25316 if (zero_width == regmatch.startp[0])
25317 {
25318 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025319 i = MB_PTR2LEN(tail);
25320 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25321 (size_t)i);
25322 ga.ga_len += i;
25323 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025324 continue;
25325 }
25326 zero_width = regmatch.startp[0];
25327 }
25328
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329 /*
25330 * Get some space for a temporary buffer to do the substitution
25331 * into. It will contain:
25332 * - The text up to where the match is.
25333 * - The substituted text.
25334 * - The text after the match.
25335 */
25336 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025337 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25339 {
25340 ga_clear(&ga);
25341 break;
25342 }
25343
25344 /* copy the text up to where the match is */
25345 i = (int)(regmatch.startp[0] - tail);
25346 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25347 /* add the substituted text */
25348 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25349 + ga.ga_len + i, TRUE, TRUE, FALSE);
25350 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025351 tail = regmatch.endp[0];
25352 if (*tail == NUL)
25353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 if (!do_all)
25355 break;
25356 }
25357
25358 if (ga.ga_data != NULL)
25359 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25360
Bram Moolenaar473de612013-06-08 18:19:48 +020025361 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 }
25363
25364 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25365 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025366 if (p_cpo == empty_option)
25367 p_cpo = save_cpo;
25368 else
25369 /* Darn, evaluating {sub} expression changed the value. */
25370 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371
25372 return ret;
25373}
25374
25375#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */