blob: ac1058cf0cfb77c5c5c4b89cb3765ed2c189fdfd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static 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 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static 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 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static 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 +0200461static 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 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100470static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100471static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200475static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100477static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100478static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara260b872016-01-15 20:48:22 +0100479static void f_assert_fails __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100480static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200485static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100496static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100498static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000503static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000506static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000508#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000509static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000510static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
512#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#ifdef FEAT_FLOAT
516static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
522static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200532static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200565static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000568static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200569static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000577static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000578static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200579static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000580static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000581static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200584static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000585static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100591static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000594static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000608static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100613static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000615static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000627#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200628static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200631#ifdef FEAT_LUA
632static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200639static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000640static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000641static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000643static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000647#ifdef vim_mkdir
648static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100651#ifdef FEAT_MZSCHEME
652static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100656static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000657static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
659static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
660#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000662static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000663static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200664#ifdef FEAT_PYTHON3
665static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
667#ifdef FEAT_PYTHON
668static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000671static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000672static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000684#ifdef FEAT_FLOAT
685static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
686#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200687static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100689static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000692static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000694static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200699static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000702static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000703static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000704static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000705static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200707static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000708static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100710#ifdef FEAT_CRYPT
711static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
712#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000713static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200714static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
717static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200718static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000721static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000722static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000725#ifdef FEAT_FLOAT
726static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
728#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000729static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200730static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000731#ifdef HAVE_STRFTIME
732static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
734static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200740static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200741static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000747static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200748static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200750static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000751static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000752static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000753static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000754static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000755static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000757static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200758#ifdef FEAT_FLOAT
759static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
761#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#ifdef FEAT_FLOAT
766static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
767#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200769static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200770static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100771static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100775static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
781static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000782static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
783static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100786static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100787static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788
Bram Moolenaar493c1782014-05-28 14:34:46 +0200789static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000790static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static int get_env_len __ARGS((char_u **arg));
792static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000793static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000794static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
795#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
796#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
797 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000798static 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 +0000799static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000800static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200801static 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 +0000802static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static typval_T *alloc_tv __ARGS((void));
804static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static void init_tv __ARGS((typval_T *varp));
806static long get_tv_number __ARGS((typval_T *varp));
807static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000808static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static char_u *get_tv_string __ARGS((typval_T *varp));
810static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000811static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100812static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
813static 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 +0000814static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
815static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
816static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000817static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
818static 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 +0000819static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200820static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
821static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200822static int var_check_func_name __ARGS((char_u *name, int new_var));
823static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200824static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000825static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
827static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
828static int eval_fname_script __ARGS((char_u *p));
829static int eval_fname_sid __ARGS((char_u *p));
830static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static ufunc_T *find_func __ARGS((char_u *name));
832static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200833static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834#ifdef FEAT_PROFILE
835static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000836static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
837static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
838static int
839# ifdef __BORLANDC__
840 _RTLENTRYF
841# endif
842 prof_total_cmp __ARGS((const void *s1, const void *s2));
843static int
844# ifdef __BORLANDC__
845 _RTLENTRYF
846# endif
847 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200849static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000850static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000851static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000853static 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 +0000854static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
855static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000857static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
858static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000859static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000860static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000861static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200862static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200863static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200865
866#ifdef EBCDIC
867static int compare_func_name __ARGS((const void *s1, const void *s2));
868static void sortFunctions __ARGS(());
869#endif
870
Bram Moolenaar33570922005-01-25 22:26:29 +0000871/*
872 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000873 */
874 void
875eval_init()
876{
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 int i;
878 struct vimvar *p;
879
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200880 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
881 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200882 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000883 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000884 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885
886 for (i = 0; i < VV_LEN; ++i)
887 {
888 p = &vimvars[i];
889 STRCPY(p->vv_di.di_key, p->vv_name);
890 if (p->vv_flags & VV_RO)
891 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
892 else if (p->vv_flags & VV_RO_SBX)
893 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
894 else
895 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000896
897 /* add to v: scope dict, unless the value is not always available */
898 if (p->vv_type != VAR_UNKNOWN)
899 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000900 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000901 /* add to compat scope dict */
902 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000904 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100905 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200906 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100907 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200908 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909
910#ifdef EBCDIC
911 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100912 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913 */
914 sortFunctions();
915#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000916}
917
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918#if defined(EXITFREE) || defined(PROTO)
919 void
920eval_clear()
921{
922 int i;
923 struct vimvar *p;
924
925 for (i = 0; i < VV_LEN; ++i)
926 {
927 p = &vimvars[i];
928 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000929 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000930 vim_free(p->vv_str);
931 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000932 }
933 else if (p->vv_di.di_tv.v_type == VAR_LIST)
934 {
935 list_unref(p->vv_list);
936 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000937 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938 }
939 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000940 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000941 hash_clear(&compat_hashtab);
942
Bram Moolenaard9fba312005-06-26 22:34:35 +0000943 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100944# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200945 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100946# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000947
948 /* global variables */
949 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000951 /* autoloaded script names */
952 ga_clear_strings(&ga_loaded);
953
Bram Moolenaarcca74132013-09-25 21:00:28 +0200954 /* Script-local variables. First clear all the variables and in a second
955 * loop free the scriptvar_T, because a variable in one script might hold
956 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200958 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200959 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200960 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200961 ga_clear(&ga_scripts);
962
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000963 /* unreferenced lists and dicts */
964 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000965
966 /* functions */
967 free_all_functions();
968 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969}
970#endif
971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * Return the name of the executed function.
974 */
975 char_u *
976func_name(cookie)
977 void *cookie;
978{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000979 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/*
983 * Return the address holding the next breakpoint line for a funccall cookie.
984 */
985 linenr_T *
986func_breakpoint(cookie)
987 void *cookie;
988{
Bram Moolenaar33570922005-01-25 22:26:29 +0000989 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990}
991
992/*
993 * Return the address holding the debug tick for a funccall cookie.
994 */
995 int *
996func_dbg_tick(cookie)
997 void *cookie;
998{
Bram Moolenaar33570922005-01-25 22:26:29 +0000999 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000}
1001
1002/*
1003 * Return the nesting level for a funccall cookie.
1004 */
1005 int
1006func_level(cookie)
1007 void *cookie;
1008{
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011
1012/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001013funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001015/* pointer to list of previously used funccal, still around because some
1016 * item in it is still being used. */
1017funccall_T *previous_funccal = NULL;
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
1020 * Return TRUE when a function was ended by a ":return" command.
1021 */
1022 int
1023current_func_returned()
1024{
1025 return current_funccal->returned;
1026}
1027
1028
1029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 * Set an internal variable to a string value. Creates the variable if it does
1031 * not already exist.
1032 */
1033 void
1034set_internal_string_var(name, value)
1035 char_u *name;
1036 char_u *value;
1037{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001038 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041 val = vim_strsave(value);
1042 if (val != NULL)
1043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001044 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001045 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001047 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001048 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 }
1051}
1052
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001054static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055static char_u *redir_endp = NULL;
1056static char_u *redir_varname = NULL;
1057
1058/*
1059 * Start recording command output to a variable
1060 * Returns OK if successfully completed the setup. FAIL otherwise.
1061 */
1062 int
1063var_redir_start(name, append)
1064 char_u *name;
1065 int append; /* append to an existing variable */
1066{
1067 int save_emsg;
1068 int err;
1069 typval_T tv;
1070
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001072 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 {
1074 EMSG(_(e_invarg));
1075 return FAIL;
1076 }
1077
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001078 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 redir_varname = vim_strsave(name);
1080 if (redir_varname == NULL)
1081 return FAIL;
1082
1083 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1084 if (redir_lval == NULL)
1085 {
1086 var_redir_stop();
1087 return FAIL;
1088 }
1089
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001090 /* The output is stored in growarray "redir_ga" until redirection ends. */
1091 ga_init2(&redir_ga, (int)sizeof(char), 500);
1092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001094 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001095 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1097 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001098 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099 if (redir_endp != NULL && *redir_endp != NUL)
1100 /* Trailing characters are present after the variable name */
1101 EMSG(_(e_trailing));
1102 else
1103 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001104 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 var_redir_stop();
1106 return FAIL;
1107 }
1108
1109 /* check if we can write to the variable: set it to or append an empty
1110 * string */
1111 save_emsg = did_emsg;
1112 did_emsg = FALSE;
1113 tv.v_type = VAR_STRING;
1114 tv.vval.v_string = (char_u *)"";
1115 if (append)
1116 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1117 else
1118 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001119 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001121 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 if (err)
1123 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001124 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
1126 return FAIL;
1127 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128
1129 return OK;
1130}
1131
1132/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 * Append "value[value_len]" to the variable set by var_redir_start().
1134 * The actual appending is postponed until redirection ends, because the value
1135 * appended may in fact be the string we write to, changing it may cause freed
1136 * memory to be used:
1137 * :redir => foo
1138 * :let foo
1139 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 */
1141 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001144 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001146 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147
1148 if (redir_lval == NULL)
1149 return;
1150
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001152 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001154 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001156 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 {
1158 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001159 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160 }
1161 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163}
1164
1165/*
1166 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 */
1169 void
1170var_redir_stop()
1171{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 typval_T tv;
1173
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 if (redir_lval != NULL)
1175 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001176 /* If there was no error: assign the text to the variable. */
1177 if (redir_endp != NULL)
1178 {
1179 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1180 tv.v_type = VAR_STRING;
1181 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001182 /* Call get_lval() again, if it's inside a Dict or List it may
1183 * have changed. */
1184 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001185 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001186 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1187 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1188 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001189 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 /* free the collected output */
1192 vim_free(redir_ga.ga_data);
1193 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195 vim_free(redir_lval);
1196 redir_lval = NULL;
1197 }
1198 vim_free(redir_varname);
1199 redir_varname = NULL;
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202# if defined(FEAT_MBYTE) || defined(PROTO)
1203 int
1204eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1205 char_u *enc_from;
1206 char_u *enc_to;
1207 char_u *fname_from;
1208 char_u *fname_to;
1209{
1210 int err = FALSE;
1211
1212 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1213 set_vim_var_string(VV_CC_TO, enc_to, -1);
1214 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1215 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1216 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1217 err = TRUE;
1218 set_vim_var_string(VV_CC_FROM, NULL, -1);
1219 set_vim_var_string(VV_CC_TO, NULL, -1);
1220 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1221 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1222
1223 if (err)
1224 return FAIL;
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1230 int
1231eval_printexpr(fname, args)
1232 char_u *fname;
1233 char_u *args;
1234{
1235 int err = FALSE;
1236
1237 set_vim_var_string(VV_FNAME_IN, fname, -1);
1238 set_vim_var_string(VV_CMDARG, args, -1);
1239 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1240 err = TRUE;
1241 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1242 set_vim_var_string(VV_CMDARG, NULL, -1);
1243
1244 if (err)
1245 {
1246 mch_remove(fname);
1247 return FAIL;
1248 }
1249 return OK;
1250}
1251# endif
1252
1253# if defined(FEAT_DIFF) || defined(PROTO)
1254 void
1255eval_diff(origfile, newfile, outfile)
1256 char_u *origfile;
1257 char_u *newfile;
1258 char_u *outfile;
1259{
1260 int err = FALSE;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270
1271 void
1272eval_patch(origfile, difffile, outfile)
1273 char_u *origfile;
1274 char_u *difffile;
1275 char_u *outfile;
1276{
1277 int err;
1278
1279 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1280 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1281 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1282 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1283 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1284 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1285 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1286}
1287# endif
1288
1289/*
1290 * Top level evaluation function, returning a boolean.
1291 * Sets "error" to TRUE if there was an error.
1292 * Return TRUE or FALSE.
1293 */
1294 int
1295eval_to_bool(arg, error, nextcmd, skip)
1296 char_u *arg;
1297 int *error;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 int retval = FALSE;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 else
1309 {
1310 *error = FALSE;
1311 if (!skip)
1312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001313 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 }
1317 if (skip)
1318 --emsg_skip;
1319
1320 return retval;
1321}
1322
1323/*
1324 * Top level evaluation function, returning a string. If "skip" is TRUE,
1325 * only parsing to "nextcmd" is done, without reporting errors. Return
1326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1327 */
1328 char_u *
1329eval_to_string_skip(arg, nextcmd, skip)
1330 char_u *arg;
1331 char_u **nextcmd;
1332 int skip; /* only parse, don't execute */
1333{
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *retval;
1336
1337 if (skip)
1338 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 retval = NULL;
1341 else
1342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 retval = vim_strsave(get_tv_string(&tv));
1344 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 if (skip)
1347 --emsg_skip;
1348
1349 return retval;
1350}
1351
1352/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001353 * Skip over an expression at "*pp".
1354 * Return FAIL for an error, OK otherwise.
1355 */
1356 int
1357skip_expr(pp)
1358 char_u **pp;
1359{
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001361
1362 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001364}
1365
1366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368 * When "convert" is TRUE convert a List into a sequence of lines and convert
1369 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 * Return pointer to allocated memory, or NULL for failure.
1371 */
1372 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 char_u *arg;
1375 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001376 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001380 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001381#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001382 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001385 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 retval = NULL;
1387 else
1388 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001389 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 {
1391 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001392 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001393 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001394 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001395 if (tv.vval.v_list->lv_len > 0)
1396 ga_append(&ga, NL);
1397 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001398 ga_append(&ga, NUL);
1399 retval = (char_u *)ga.ga_data;
1400 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401#ifdef FEAT_FLOAT
1402 else if (convert && tv.v_type == VAR_FLOAT)
1403 {
1404 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1405 retval = vim_strsave(numbuf);
1406 }
1407#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001408 else
1409 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001410 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412
1413 return retval;
1414}
1415
1416/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 * Call eval_to_string() without using current local variables and using
1418 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 */
1420 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 char_u *arg;
1423 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001424 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 char_u *retval;
1427 void *save_funccalp;
1428
1429 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001430 if (use_sandbox)
1431 ++sandbox;
1432 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001433 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001434 if (use_sandbox)
1435 --sandbox;
1436 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 restore_funccal(save_funccalp);
1438 return retval;
1439}
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441/*
1442 * Top level evaluation function, returning a number.
1443 * Evaluates "expr" silently.
1444 * Returns -1 for an error.
1445 */
1446 int
1447eval_to_number(expr)
1448 char_u *expr;
1449{
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001452 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454 ++emsg_off;
1455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = -1;
1458 else
1459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001460 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 --emsg_off;
1464
1465 return retval;
1466}
1467
Bram Moolenaara40058a2005-07-11 22:42:07 +00001468/*
1469 * Prepare v: variable "idx" to be used.
1470 * Save the current typeval in "save_tv".
1471 * When not used yet add the variable to the v: hashtable.
1472 */
1473 static void
1474prepare_vimvar(idx, save_tv)
1475 int idx;
1476 typval_T *save_tv;
1477{
1478 *save_tv = vimvars[idx].vv_tv;
1479 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1480 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1481}
1482
1483/*
1484 * Restore v: variable "idx" to typeval "save_tv".
1485 * When no longer defined, remove the variable from the v: hashtable.
1486 */
1487 static void
1488restore_vimvar(idx, save_tv)
1489 int idx;
1490 typval_T *save_tv;
1491{
1492 hashitem_T *hi;
1493
Bram Moolenaara40058a2005-07-11 22:42:07 +00001494 vimvars[idx].vv_tv = *save_tv;
1495 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1496 {
1497 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1498 if (HASHITEM_EMPTY(hi))
1499 EMSG2(_(e_intern2), "restore_vimvar()");
1500 else
1501 hash_remove(&vimvarht, hi);
1502 }
1503}
1504
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001505#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001506/*
1507 * Evaluate an expression to a list with suggestions.
1508 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001509 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510 */
1511 list_T *
1512eval_spell_expr(badword, expr)
1513 char_u *badword;
1514 char_u *expr;
1515{
1516 typval_T save_val;
1517 typval_T rettv;
1518 list_T *list = NULL;
1519 char_u *p = skipwhite(expr);
1520
1521 /* Set "v:val" to the bad word. */
1522 prepare_vimvar(VV_VAL, &save_val);
1523 vimvars[VV_VAL].vv_type = VAR_STRING;
1524 vimvars[VV_VAL].vv_str = badword;
1525 if (p_verbose == 0)
1526 ++emsg_off;
1527
1528 if (eval1(&p, &rettv, TRUE) == OK)
1529 {
1530 if (rettv.v_type != VAR_LIST)
1531 clear_tv(&rettv);
1532 else
1533 list = rettv.vval.v_list;
1534 }
1535
1536 if (p_verbose == 0)
1537 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538 restore_vimvar(VV_VAL, &save_val);
1539
1540 return list;
1541}
1542
1543/*
1544 * "list" is supposed to contain two items: a word and a number. Return the
1545 * word in "pp" and the number as the return value.
1546 * Return -1 if anything isn't right.
1547 * Used to get the good word and score from the eval_spell_expr() result.
1548 */
1549 int
1550get_spellword(list, pp)
1551 list_T *list;
1552 char_u **pp;
1553{
1554 listitem_T *li;
1555
1556 li = list->lv_first;
1557 if (li == NULL)
1558 return -1;
1559 *pp = get_tv_string(&li->li_tv);
1560
1561 li = li->li_next;
1562 if (li == NULL)
1563 return -1;
1564 return get_tv_number(&li->li_tv);
1565}
1566#endif
1567
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001569 * Top level evaluation function.
1570 * Returns an allocated typval_T with the result.
1571 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001572 */
1573 typval_T *
1574eval_expr(arg, nextcmd)
1575 char_u *arg;
1576 char_u **nextcmd;
1577{
1578 typval_T *tv;
1579
1580 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001581 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001582 {
1583 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001584 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001585 }
1586
1587 return tv;
1588}
1589
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001593 * Uses argv[argc] for the function arguments. Only Number and String
1594 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001597 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 char_u *func;
1600 int argc;
1601 char_u **argv;
1602 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001603 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaar33570922005-01-25 22:26:29 +00001606 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 long n;
1608 int len;
1609 int i;
1610 int doesrange;
1611 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001614 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 for (i = 0; i < argc; i++)
1619 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001620 /* Pass a NULL or empty argument as an empty string */
1621 if (argv[i] == NULL || *argv[i] == NUL)
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001625 continue;
1626 }
1627
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001628 if (str_arg_only)
1629 len = 0;
1630 else
1631 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001632 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (len != 0 && len == (int)STRLEN(argv[i]))
1634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001635 argvars[i].v_type = VAR_NUMBER;
1636 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638 else
1639 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001640 argvars[i].v_type = VAR_STRING;
1641 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 }
1643 }
1644
1645 if (safe)
1646 {
1647 save_funccalp = save_funccal();
1648 ++sandbox;
1649 }
1650
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1652 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (safe)
1656 {
1657 --sandbox;
1658 restore_funccal(save_funccalp);
1659 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 vim_free(argvars);
1661
1662 if (ret == FAIL)
1663 clear_tv(rettv);
1664
1665 return ret;
1666}
1667
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001668/*
1669 * Call vimL function "func" and return the result as a number.
1670 * Returns -1 when calling the function fails.
1671 * Uses argv[argc] for the function arguments.
1672 */
1673 long
1674call_func_retnr(func, argc, argv, safe)
1675 char_u *func;
1676 int argc;
1677 char_u **argv;
1678 int safe; /* use the sandbox */
1679{
1680 typval_T rettv;
1681 long retval;
1682
1683 /* All arguments are passed as strings, no conversion to number. */
1684 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1685 return -1;
1686
1687 retval = get_tv_number_chk(&rettv, NULL);
1688 clear_tv(&rettv);
1689 return retval;
1690}
1691
1692#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1693 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1694
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697 * Call vimL function "func" and return the result as a string.
1698 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
1700 */
1701 void *
1702call_func_retstr(func, argc, argv, safe)
1703 char_u *func;
1704 int argc;
1705 char_u **argv;
1706 int safe; /* use the sandbox */
1707{
1708 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001709 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 return retval;
1718}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001719# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001721/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001722 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001724 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 */
1726 void *
1727call_func_retlist(func, argc, argv, safe)
1728 char_u *func;
1729 int argc;
1730 char_u **argv;
1731 int safe; /* use the sandbox */
1732{
1733 typval_T rettv;
1734
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001735 /* All arguments are passed as strings, no conversion to number. */
1736 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 return NULL;
1738
1739 if (rettv.v_type != VAR_LIST)
1740 {
1741 clear_tv(&rettv);
1742 return NULL;
1743 }
1744
1745 return rettv.vval.v_list;
1746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#endif
1748
1749/*
1750 * Save the current function call pointer, and set it to NULL.
1751 * Used when executing autocommands and for ":source".
1752 */
1753 void *
1754save_funccal()
1755{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001756 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 current_funccal = NULL;
1759 return (void *)fc;
1760}
1761
1762 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763restore_funccal(vfc)
1764 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001766 funccall_T *fc = (funccall_T *)vfc;
1767
1768 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
Bram Moolenaar05159a02005-02-26 23:04:13 +00001771#if defined(FEAT_PROFILE) || defined(PROTO)
1772/*
1773 * Prepare profiling for entering a child or something else that is not
1774 * counted for the script/function itself.
1775 * Should always be called in pair with prof_child_exit().
1776 */
1777 void
1778prof_child_enter(tm)
1779 proftime_T *tm; /* place to store waittime */
1780{
1781 funccall_T *fc = current_funccal;
1782
1783 if (fc != NULL && fc->func->uf_profiling)
1784 profile_start(&fc->prof_child);
1785 script_prof_save(tm);
1786}
1787
1788/*
1789 * Take care of time spent in a child.
1790 * Should always be called after prof_child_enter().
1791 */
1792 void
1793prof_child_exit(tm)
1794 proftime_T *tm; /* where waittime was stored */
1795{
1796 funccall_T *fc = current_funccal;
1797
1798 if (fc != NULL && fc->func->uf_profiling)
1799 {
1800 profile_end(&fc->prof_child);
1801 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1802 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1803 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1804 }
1805 script_prof_restore(tm);
1806}
1807#endif
1808
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#ifdef FEAT_FOLDING
1811/*
1812 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1813 * it in "*cp". Doesn't give error messages.
1814 */
1815 int
1816eval_foldexpr(arg, cp)
1817 char_u *arg;
1818 int *cp;
1819{
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 int retval;
1822 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001823 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1824 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001827 if (use_sandbox)
1828 ++sandbox;
1829 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 retval = 0;
1833 else
1834 {
1835 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 if (tv.v_type == VAR_NUMBER)
1837 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001838 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 retval = 0;
1840 else
1841 {
1842 /* If the result is a string, check if there is a non-digit before
1843 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (!VIM_ISDIGIT(*s) && *s != '-')
1846 *cp = *s++;
1847 retval = atol((char *)s);
1848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001852 if (use_sandbox)
1853 --sandbox;
1854 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856 return retval;
1857}
1858#endif
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 * ":let" list all variable values
1862 * ":let var1 var2" list variable values
1863 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001864 * ":let var += expr" assignment command.
1865 * ":let var -= expr" assignment command.
1866 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869 void
1870ex_let(eap)
1871 exarg_T *eap;
1872{
1873 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001875 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 int var_count = 0;
1878 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001881 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
Bram Moolenaardb552d602006-03-23 22:59:57 +00001883 argend = skip_var_list(arg, &var_count, &semicolon);
1884 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001886 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1887 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001888 expr = skipwhite(argend);
1889 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1890 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001892 /*
1893 * ":let" without "=": list variables
1894 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895 if (*arg == '[')
1896 EMSG(_(e_invarg));
1897 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_glob_vars(&first);
1904 list_buf_vars(&first);
1905 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001906#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001907 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001908#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001909 list_script_vars(&first);
1910 list_func_vars(&first);
1911 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 eap->nextcmd = check_nextcmd(arg);
1914 }
1915 else
1916 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 op[0] = '=';
1918 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001921 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1922 op[0] = *expr; /* +=, -= or .= */
1923 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001925 else
1926 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (eap->skip)
1929 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (eap->skip)
1932 {
1933 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 --emsg_skip;
1936 }
1937 else if (i != FAIL)
1938 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 }
1944}
1945
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946/*
1947 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1948 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 * When "nextchars" is not NULL it points to a string with characters that
1950 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1951 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 * Returns OK or FAIL;
1953 */
1954 static int
1955ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1956 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 int copy; /* copy values from "tv", don't move */
1959 int semicolon; /* from skip_var_list() */
1960 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962{
1963 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001966 listitem_T *item;
1967 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968
1969 if (*arg != '[')
1970 {
1971 /*
1972 * ":let var = expr" or ":for var in list"
1973 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 return OK;
1977 }
1978
1979 /*
1980 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1981 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001982 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 {
1984 EMSG(_(e_listreq));
1985 return FAIL;
1986 }
1987
1988 i = list_len(l);
1989 if (semicolon == 0 && var_count < i)
1990 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001991 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 return FAIL;
1993 }
1994 if (var_count - semicolon > i)
1995 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001996 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 return FAIL;
1998 }
1999
2000 item = l->lv_first;
2001 while (*arg != ']')
2002 {
2003 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 item = item->li_next;
2006 if (arg == NULL)
2007 return FAIL;
2008
2009 arg = skipwhite(arg);
2010 if (*arg == ';')
2011 {
2012 /* Put the rest of the list (may be empty) in the var after ';'.
2013 * Create a new list for this. */
2014 l = list_alloc();
2015 if (l == NULL)
2016 return FAIL;
2017 while (item != NULL)
2018 {
2019 list_append_tv(l, &item->li_tv);
2020 item = item->li_next;
2021 }
2022
2023 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002024 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 ltv.vval.v_list = l;
2026 l->lv_refcount = 1;
2027
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2029 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 clear_tv(&ltv);
2031 if (arg == NULL)
2032 return FAIL;
2033 break;
2034 }
2035 else if (*arg != ',' && *arg != ']')
2036 {
2037 EMSG2(_(e_intern2), "ex_let_vars()");
2038 return FAIL;
2039 }
2040 }
2041
2042 return OK;
2043}
2044
2045/*
2046 * Skip over assignable variable "var" or list of variables "[var, var]".
2047 * Used for ":let varvar = expr" and ":for varvar in expr".
2048 * For "[var, var]" increment "*var_count" for each variable.
2049 * for "[var, var; var]" set "semicolon".
2050 * Return NULL for an error.
2051 */
2052 static char_u *
2053skip_var_list(arg, var_count, semicolon)
2054 char_u *arg;
2055 int *var_count;
2056 int *semicolon;
2057{
2058 char_u *p, *s;
2059
2060 if (*arg == '[')
2061 {
2062 /* "[var, var]": find the matching ']'. */
2063 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002064 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 {
2066 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2067 s = skip_var_one(p);
2068 if (s == p)
2069 {
2070 EMSG2(_(e_invarg2), p);
2071 return NULL;
2072 }
2073 ++*var_count;
2074
2075 p = skipwhite(s);
2076 if (*p == ']')
2077 break;
2078 else if (*p == ';')
2079 {
2080 if (*semicolon == 1)
2081 {
2082 EMSG(_("Double ; in list of variables"));
2083 return NULL;
2084 }
2085 *semicolon = 1;
2086 }
2087 else if (*p != ',')
2088 {
2089 EMSG2(_(e_invarg2), p);
2090 return NULL;
2091 }
2092 }
2093 return p + 1;
2094 }
2095 else
2096 return skip_var_one(arg);
2097}
2098
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002100 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002101 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002102 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103 static char_u *
2104skip_var_one(arg)
2105 char_u *arg;
2106{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002107 if (*arg == '@' && arg[1] != NUL)
2108 return arg + 2;
2109 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2110 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111}
2112
Bram Moolenaara7043832005-01-21 11:56:39 +00002113/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 * List variables for hashtab "ht" with prefix "prefix".
2115 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002119 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123{
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 hashitem_T *hi;
2125 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126 int todo;
2127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002128 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2130 {
2131 if (!HASHITEM_EMPTY(hi))
2132 {
2133 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 di = HI2DI(hi);
2135 if (empty || di->di_tv.v_type != VAR_STRING
2136 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138 }
2139 }
2140}
2141
2142/*
2143 * List global variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_glob_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List buffer variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_buf_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159 char_u numbuf[NUMBUFLEN];
2160
Bram Moolenaar429fa852013-04-15 12:27:36 +02002161 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163
2164 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2166 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002167}
2168
2169/*
2170 * List window variables.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_win_vars(first)
2174 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002175{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002176 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002178}
2179
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002180#ifdef FEAT_WINDOWS
2181/*
2182 * List tab page variables.
2183 */
2184 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_tab_vars(first)
2186 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002188 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190}
2191#endif
2192
Bram Moolenaara7043832005-01-21 11:56:39 +00002193/*
2194 * List Vim variables.
2195 */
2196 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197list_vim_vars(first)
2198 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201}
2202
2203/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204 * List script-local variables, if there is a script.
2205 */
2206 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207list_script_vars(first)
2208 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002209{
2210 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2212 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002213}
2214
2215/*
2216 * List function variables, if there is a function.
2217 */
2218 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219list_func_vars(first)
2220 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221{
2222 if (current_funccal != NULL)
2223 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002224 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225}
2226
2227/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 * List variables in "arg".
2229 */
2230 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 exarg_T *eap;
2233 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235{
2236 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 char_u *name_start;
2240 char_u *arg_subsc;
2241 char_u *tofree;
2242 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243
2244 while (!ends_excmd(*arg) && !got_int)
2245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002248 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2250 {
2251 emsg_severe = TRUE;
2252 EMSG(_(e_trailing));
2253 break;
2254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 /* get_name_len() takes care of expanding curly braces */
2259 name_start = name = arg;
2260 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2261 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* This is mainly to keep test 49 working: when expanding
2264 * curly braces fails overrule the exception error message. */
2265 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 emsg_severe = TRUE;
2268 EMSG2(_(e_invarg2), arg);
2269 break;
2270 }
2271 error = TRUE;
2272 }
2273 else
2274 {
2275 if (tofree != NULL)
2276 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002277 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 else
2280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 /* handle d.key, l[idx], f(expr) */
2282 arg_subsc = arg;
2283 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002291 case 'g': list_glob_vars(first); break;
2292 case 'b': list_buf_vars(first); break;
2293 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002294#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002295 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002297 case 'v': list_vim_vars(first); break;
2298 case 's': list_script_vars(first); break;
2299 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 default:
2301 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 }
2304 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 {
2306 char_u numbuf[NUMBUFLEN];
2307 char_u *tf;
2308 int c;
2309 char_u *s;
2310
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002311 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 c = *arg;
2313 *arg = NUL;
2314 list_one_var_a((char_u *)"",
2315 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002316 tv.v_type,
2317 s == NULL ? (char_u *)"" : s,
2318 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 *arg = c;
2320 vim_free(tf);
2321 }
2322 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
2325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326
2327 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329
2330 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332
2333 return arg;
2334}
2335
2336/*
2337 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2338 * Returns a pointer to the char just after the var name.
2339 * Returns NULL if there is an error.
2340 */
2341 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002344 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348{
2349 int c1;
2350 char_u *name;
2351 char_u *p;
2352 char_u *arg_end = NULL;
2353 int len;
2354 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356
2357 /*
2358 * ":let $VAR = expr": Set environment variable.
2359 */
2360 if (*arg == '$')
2361 {
2362 /* Find the end of the name. */
2363 ++arg;
2364 name = arg;
2365 len = get_env_len(&arg);
2366 if (len == 0)
2367 EMSG2(_(e_invarg2), name - 1);
2368 else
2369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 if (op != NULL && (*op == '+' || *op == '-'))
2371 EMSG2(_(e_letwrong), op);
2372 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002375 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 {
2377 c1 = name[len];
2378 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 p = get_tv_string_chk(tv);
2380 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 {
2382 int mustfree = FALSE;
2383 char_u *s = vim_getenv(name, &mustfree);
2384
2385 if (s != NULL)
2386 {
2387 p = tofree = concat_str(s, p);
2388 if (mustfree)
2389 vim_free(s);
2390 }
2391 }
2392 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 if (STRICMP(name, "HOME") == 0)
2396 init_homedir();
2397 else if (didset_vim && STRICMP(name, "VIM") == 0)
2398 didset_vim = FALSE;
2399 else if (didset_vimruntime
2400 && STRICMP(name, "VIMRUNTIME") == 0)
2401 didset_vimruntime = FALSE;
2402 arg_end = arg;
2403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 }
2407 }
2408 }
2409
2410 /*
2411 * ":let &option = expr": Set option value.
2412 * ":let &l:option = expr": Set local option value.
2413 * ":let &g:option = expr": Set global option value.
2414 */
2415 else if (*arg == '&')
2416 {
2417 /* Find the end of the name. */
2418 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 if (p == NULL || (endchars != NULL
2420 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 EMSG(_(e_letunexp));
2422 else
2423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 long n;
2425 int opt_type;
2426 long numval;
2427 char_u *stringval = NULL;
2428 char_u *s;
2429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 c1 = *p;
2431 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432
2433 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002434 s = get_tv_string_chk(tv); /* != NULL if number or string */
2435 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 {
2437 opt_type = get_option_value(arg, &numval,
2438 &stringval, opt_flags);
2439 if ((opt_type == 1 && *op == '.')
2440 || (opt_type == 0 && *op != '.'))
2441 EMSG2(_(e_letwrong), op);
2442 else
2443 {
2444 if (opt_type == 1) /* number */
2445 {
2446 if (*op == '+')
2447 n = numval + n;
2448 else
2449 n = numval - n;
2450 }
2451 else if (opt_type == 0 && stringval != NULL) /* string */
2452 {
2453 s = concat_str(stringval, s);
2454 vim_free(stringval);
2455 stringval = s;
2456 }
2457 }
2458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 if (s != NULL)
2460 {
2461 set_option_value(arg, n, s, opt_flags);
2462 arg_end = p;
2463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
2467 }
2468
2469 /*
2470 * ":let @r = expr": Set register contents.
2471 */
2472 else if (*arg == '@')
2473 {
2474 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 if (op != NULL && (*op == '+' || *op == '-'))
2476 EMSG2(_(e_letwrong), op);
2477 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002478 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 char_u *s;
2484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 p = get_tv_string_chk(tv);
2486 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002488 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 if (s != NULL)
2490 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 vim_free(s);
2493 }
2494 }
2495 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 arg_end = arg + 1;
2499 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 }
2502 }
2503
2504 /*
2505 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002508 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002512 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2516 EMSG(_(e_letunexp));
2517 else
2518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 arg_end = p;
2521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525
2526 else
2527 EMSG2(_(e_invarg2), arg);
2528
2529 return arg_end;
2530}
2531
2532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2534 */
2535 static int
2536check_changedtick(arg)
2537 char_u *arg;
2538{
2539 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2540 {
2541 EMSG2(_(e_readonlyvar), arg);
2542 return TRUE;
2543 }
2544 return FALSE;
2545}
2546
2547/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 * Get an lval: variable, Dict item or List item that can be assigned a value
2549 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2550 * "name.key", "name.key[expr]" etc.
2551 * Indexing only works if "name" is an existing List or Dictionary.
2552 * "name" points to the start of the name.
2553 * If "rettv" is not NULL it points to the value to be assigned.
2554 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2555 * wrong; must end in space or cmd separator.
2556 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 * flags:
2558 * GLV_QUIET: do not give error messages
2559 * GLV_NO_AUTOLOAD: do not use script autoloading
2560 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002562 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 */
2565 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002566get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 typval_T *rettv;
2569 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 int unlet;
2571 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002572 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002573 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 char_u *expr_start, *expr_end;
2577 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 dictitem_T *v;
2579 typval_T var1;
2580 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002586 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590
2591 if (skip)
2592 {
2593 /* When skipping just find the end of the name. */
2594 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002595 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 }
2597
2598 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002599 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (expr_start != NULL)
2601 {
2602 /* Don't expand the name when we already know there is an error. */
2603 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2604 && *p != '[' && *p != '.')
2605 {
2606 EMSG(_(e_trailing));
2607 return NULL;
2608 }
2609
2610 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2611 if (lp->ll_exp_name == NULL)
2612 {
2613 /* Report an invalid expression in braces, unless the
2614 * expression evaluation has been cancelled due to an
2615 * aborting error, an interrupt, or an exception. */
2616 if (!aborting() && !quiet)
2617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002618 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 EMSG2(_(e_invarg2), name);
2620 return NULL;
2621 }
2622 }
2623 lp->ll_name = lp->ll_exp_name;
2624 }
2625 else
2626 lp->ll_name = name;
2627
2628 /* Without [idx] or .key we are done. */
2629 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2630 return p;
2631
2632 cc = *p;
2633 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002634 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (v == NULL && !quiet)
2636 EMSG2(_(e_undefvar), lp->ll_name);
2637 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 if (v == NULL)
2639 return NULL;
2640
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 /*
2642 * Loop until no more [idx] or .key is following.
2643 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2648 && !(lp->ll_tv->v_type == VAR_DICT
2649 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!quiet)
2652 EMSG(_("E689: Can only index a List or Dictionary"));
2653 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E708: [:] must come last"));
2659 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 len = -1;
2663 if (*p == '.')
2664 {
2665 key = p + 1;
2666 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2667 ;
2668 if (len == 0)
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (!quiet)
2671 EMSG(_(e_emptykey));
2672 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 }
2674 p = key + len;
2675 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 else
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (*p == ':')
2681 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 else
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 empty1 = FALSE;
2685 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 if (get_tv_string_chk(&var1) == NULL)
2688 {
2689 /* not a number or string */
2690 clear_tv(&var1);
2691 return NULL;
2692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
2694
2695 /* Optionally get the second index [ :expr]. */
2696 if (*p == ':')
2697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002702 if (!empty1)
2703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (rettv != NULL && (rettv->v_type != VAR_LIST
2707 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
2710 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715 p = skipwhite(p + 1);
2716 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 else
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (!empty1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002727 if (get_tv_string_chk(&var2) == NULL)
2728 {
2729 /* not a number or string */
2730 if (!empty1)
2731 clear_tv(&var1);
2732 clear_tv(&var2);
2733 return NULL;
2734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (!quiet)
2744 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (!empty1)
2746 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751
2752 /* Skip to past ']'. */
2753 ++p;
2754 }
2755
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
2758 if (len == -1)
2759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 if (*key == NUL)
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (!quiet)
2765 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 lp->ll_list = NULL;
2771 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002772 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 /* When assigning to a scope dictionary check that a function and
2775 * variable name is valid (only variable name unless it is l: or
2776 * g: dictionary). Disallow overwriting a builtin function. */
2777 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002779 int prevval;
2780 int wrong;
2781
2782 if (len != -1)
2783 {
2784 prevval = key[len];
2785 key[len] = NUL;
2786 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002787 else
2788 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2790 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002792 || !valid_varname(key);
2793 if (len != -1)
2794 key[len] = prevval;
2795 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796 return NULL;
2797 }
2798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 /* Can't add "v:" variable. */
2802 if (lp->ll_dict == &vimvardict)
2803 {
2804 EMSG2(_(e_illvar), name);
2805 return NULL;
2806 }
2807
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002808 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (len == -1)
2822 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 p = NULL;
2825 break;
2826 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002827 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002828 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 return NULL;
2830
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 if (len == -1)
2832 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 }
2835 else
2836 {
2837 /*
2838 * Get the number and item for the only or first index of the List.
2839 */
2840 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 else
2843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002844 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 clear_tv(&var1);
2846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_list = lp->ll_tv->vval.v_list;
2849 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2850 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002852 if (lp->ll_n1 < 0)
2853 {
2854 lp->ll_n1 = 0;
2855 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2856 }
2857 }
2858 if (lp->ll_li == NULL)
2859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002862 if (!quiet)
2863 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 }
2866
2867 /*
2868 * May need to find the item or absolute index for the second
2869 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 * When no index given: "lp->ll_empty2" is TRUE.
2871 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002875 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002881 {
2882 if (!quiet)
2883 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 }
2888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2890 if (lp->ll_n1 < 0)
2891 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2892 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 {
2894 if (!quiet)
2895 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 return p;
2905}
2906
2907/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 */
2910 static void
2911clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002912 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 vim_free(lp->ll_exp_name);
2915 vim_free(lp->ll_newkey);
2916}
2917
2918/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 */
2923 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930{
2931 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 listitem_T *ri;
2933 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934
2935 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 cc = *endp;
2940 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 if (op != NULL && *op != '=')
2942 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002946 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002947 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002948 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002950 if ((di == NULL
2951 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2952 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2953 FALSE)))
2954 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 set_var(lp->ll_name, &tv, FALSE);
2956 clear_tv(&tv);
2957 }
2958 }
2959 else
2960 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002962 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 else if (tv_check_lock(lp->ll_newkey == NULL
2965 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002966 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 else if (lp->ll_range)
2969 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002970 listitem_T *ll_li = lp->ll_li;
2971 int ll_n1 = lp->ll_n1;
2972
2973 /*
2974 * Check whether any of the list items is locked
2975 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002976 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002978 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002979 return;
2980 ri = ri->li_next;
2981 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2982 break;
2983 ll_li = ll_li->li_next;
2984 ++ll_n1;
2985 }
2986
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /*
2988 * Assign the List values to the list items.
2989 */
2990 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002991 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002992 if (op != NULL && *op != '=')
2993 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2994 else
2995 {
2996 clear_tv(&lp->ll_li->li_tv);
2997 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2998 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 ri = ri->li_next;
3000 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3001 break;
3002 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003005 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ri = NULL;
3008 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 lp->ll_li = lp->ll_li->li_next;
3012 ++lp->ll_n1;
3013 }
3014 if (ri != NULL)
3015 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 else if (lp->ll_empty2
3017 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 : lp->ll_n1 != lp->ll_n2)
3019 EMSG(_("E711: List value has not enough items"));
3020 }
3021 else
3022 {
3023 /*
3024 * Assign to a List or Dictionary item.
3025 */
3026 if (lp->ll_newkey != NULL)
3027 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 if (op != NULL && *op != '=')
3029 {
3030 EMSG2(_(e_letwrong), op);
3031 return;
3032 }
3033
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003035 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 if (di == NULL)
3037 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003038 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3039 {
3040 vim_free(di);
3041 return;
3042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003044 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 else if (op != NULL && *op != '=')
3046 {
3047 tv_op(lp->ll_tv, rettv, op);
3048 return;
3049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003050 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003052
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 /*
3054 * Assign the value to the variable or list item.
3055 */
3056 if (copy)
3057 copy_tv(rettv, lp->ll_tv);
3058 else
3059 {
3060 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003061 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 }
3064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065}
3066
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003067/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3069 * Returns OK or FAIL.
3070 */
3071 static int
3072tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003073 typval_T *tv1;
3074 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 char_u *op;
3076{
3077 long n;
3078 char_u numbuf[NUMBUFLEN];
3079 char_u *s;
3080
3081 /* Can't do anything with a Funcref or a Dict on the right. */
3082 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3083 {
3084 switch (tv1->v_type)
3085 {
3086 case VAR_DICT:
3087 case VAR_FUNC:
3088 break;
3089
3090 case VAR_LIST:
3091 if (*op != '+' || tv2->v_type != VAR_LIST)
3092 break;
3093 /* List += List */
3094 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3095 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3096 return OK;
3097
3098 case VAR_NUMBER:
3099 case VAR_STRING:
3100 if (tv2->v_type == VAR_LIST)
3101 break;
3102 if (*op == '+' || *op == '-')
3103 {
3104 /* nr += nr or nr -= nr*/
3105 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003106#ifdef FEAT_FLOAT
3107 if (tv2->v_type == VAR_FLOAT)
3108 {
3109 float_T f = n;
3110
3111 if (*op == '+')
3112 f += tv2->vval.v_float;
3113 else
3114 f -= tv2->vval.v_float;
3115 clear_tv(tv1);
3116 tv1->v_type = VAR_FLOAT;
3117 tv1->vval.v_float = f;
3118 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120#endif
3121 {
3122 if (*op == '+')
3123 n += get_tv_number(tv2);
3124 else
3125 n -= get_tv_number(tv2);
3126 clear_tv(tv1);
3127 tv1->v_type = VAR_NUMBER;
3128 tv1->vval.v_number = n;
3129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 }
3131 else
3132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133 if (tv2->v_type == VAR_FLOAT)
3134 break;
3135
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 /* str .= str */
3137 s = get_tv_string(tv1);
3138 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3139 clear_tv(tv1);
3140 tv1->v_type = VAR_STRING;
3141 tv1->vval.v_string = s;
3142 }
3143 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144
3145#ifdef FEAT_FLOAT
3146 case VAR_FLOAT:
3147 {
3148 float_T f;
3149
3150 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3151 && tv2->v_type != VAR_NUMBER
3152 && tv2->v_type != VAR_STRING))
3153 break;
3154 if (tv2->v_type == VAR_FLOAT)
3155 f = tv2->vval.v_float;
3156 else
3157 f = get_tv_number(tv2);
3158 if (*op == '+')
3159 tv1->vval.v_float += f;
3160 else
3161 tv1->vval.v_float -= f;
3162 }
3163 return OK;
3164#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003165 }
3166 }
3167
3168 EMSG2(_(e_letwrong), op);
3169 return FAIL;
3170}
3171
3172/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 * Add a watcher to a list.
3174 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003175 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 list_T *l;
3178 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179{
3180 lw->lw_next = l->lv_watch;
3181 l->lv_watch = lw;
3182}
3183
3184/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003185 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186 * No warning when it isn't found...
3187 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003188 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 list_T *l;
3191 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 lwp = &l->lv_watch;
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 {
3198 if (lw == lwrem)
3199 {
3200 *lwp = lw->lw_next;
3201 break;
3202 }
3203 lwp = &lw->lw_next;
3204 }
3205}
3206
3207/*
3208 * Just before removing an item from a list: advance watchers to the next
3209 * item.
3210 */
3211 static void
3212list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 list_T *l;
3214 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217
3218 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3219 if (lw->lw_item == item)
3220 lw->lw_item = item->li_next;
3221}
3222
3223/*
3224 * Evaluate the expression used in a ":for var in expr" command.
3225 * "arg" points to "var".
3226 * Set "*errp" to TRUE for an error, FALSE otherwise;
3227 * Return a pointer that holds the info. Null when there is an error.
3228 */
3229 void *
3230eval_for_line(arg, errp, nextcmdp, skip)
3231 char_u *arg;
3232 int *errp;
3233 char_u **nextcmdp;
3234 int skip;
3235{
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 typval_T tv;
3239 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240
3241 *errp = TRUE; /* default: there is an error */
3242
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 if (fi == NULL)
3245 return NULL;
3246
3247 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3248 if (expr == NULL)
3249 return fi;
3250
3251 expr = skipwhite(expr);
3252 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3253 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003254 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 return fi;
3256 }
3257
3258 if (skip)
3259 ++emsg_skip;
3260 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3261 {
3262 *errp = FALSE;
3263 if (!skip)
3264 {
3265 l = tv.vval.v_list;
3266 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 clear_tv(&tv);
3270 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 else
3272 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003273 /* No need to increment the refcount, it's already set for the
3274 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 fi->fi_list = l;
3276 list_add_watch(l, &fi->fi_lw);
3277 fi->fi_lw.lw_item = l->lv_first;
3278 }
3279 }
3280 }
3281 if (skip)
3282 --emsg_skip;
3283
3284 return fi;
3285}
3286
3287/*
3288 * Use the first item in a ":for" list. Advance to the next.
3289 * Assign the values to the variable (list). "arg" points to the first one.
3290 * Return TRUE when a valid item was found, FALSE when at end of list or
3291 * something wrong.
3292 */
3293 int
3294next_for_item(fi_void, arg)
3295 void *fi_void;
3296 char_u *arg;
3297{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003298 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301
3302 item = fi->fi_lw.lw_item;
3303 if (item == NULL)
3304 result = FALSE;
3305 else
3306 {
3307 fi->fi_lw.lw_item = item->li_next;
3308 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3309 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3310 }
3311 return result;
3312}
3313
3314/*
3315 * Free the structure used to store info used by ":for".
3316 */
3317 void
3318free_for_info(fi_void)
3319 void *fi_void;
3320{
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003323 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 list_unref(fi->fi_list);
3327 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 vim_free(fi);
3329}
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3332
3333 void
3334set_context_for_expression(xp, arg, cmdidx)
3335 expand_T *xp;
3336 char_u *arg;
3337 cmdidx_T cmdidx;
3338{
3339 int got_eq = FALSE;
3340 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 if (cmdidx == CMD_let)
3344 {
3345 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003346 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 {
3348 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003349 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 {
3351 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353 if (vim_iswhite(*p))
3354 break;
3355 }
3356 return;
3357 }
3358 }
3359 else
3360 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3361 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 while ((xp->xp_pattern = vim_strpbrk(arg,
3363 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3364 {
3365 c = *xp->xp_pattern;
3366 if (c == '&')
3367 {
3368 c = xp->xp_pattern[1];
3369 if (c == '&')
3370 {
3371 ++xp->xp_pattern;
3372 xp->xp_context = cmdidx != CMD_let || got_eq
3373 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3374 }
3375 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3379 xp->xp_pattern += 2;
3380
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383 else if (c == '$')
3384 {
3385 /* environment variable */
3386 xp->xp_context = EXPAND_ENV_VARS;
3387 }
3388 else if (c == '=')
3389 {
3390 got_eq = TRUE;
3391 xp->xp_context = EXPAND_EXPRESSION;
3392 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 && xp->xp_context == EXPAND_FUNCTIONS
3395 && vim_strchr(xp->xp_pattern, '(') == NULL)
3396 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003397 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 break;
3399 }
3400 else if (cmdidx != CMD_let || got_eq)
3401 {
3402 if (c == '"') /* string */
3403 {
3404 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3405 if (c == '\\' && xp->xp_pattern[1] != NUL)
3406 ++xp->xp_pattern;
3407 xp->xp_context = EXPAND_NOTHING;
3408 }
3409 else if (c == '\'') /* literal string */
3410 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003411 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3413 /* skip */ ;
3414 xp->xp_context = EXPAND_NOTHING;
3415 }
3416 else if (c == '|')
3417 {
3418 if (xp->xp_pattern[1] == '|')
3419 {
3420 ++xp->xp_pattern;
3421 xp->xp_context = EXPAND_EXPRESSION;
3422 }
3423 else
3424 xp->xp_context = EXPAND_COMMANDS;
3425 }
3426 else
3427 xp->xp_context = EXPAND_EXPRESSION;
3428 }
3429 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003430 /* Doesn't look like something valid, expand as an expression
3431 * anyway. */
3432 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 arg = xp->xp_pattern;
3434 if (*arg != NUL)
3435 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3436 /* skip */ ;
3437 }
3438 xp->xp_pattern = arg;
3439}
3440
3441#endif /* FEAT_CMDL_COMPL */
3442
3443/*
3444 * ":1,25call func(arg1, arg2)" function call.
3445 */
3446 void
3447ex_call(eap)
3448 exarg_T *eap;
3449{
3450 char_u *arg = eap->arg;
3451 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 linenr_T lnum;
3457 int doesrange;
3458 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 if (eap->skip)
3462 {
3463 /* trans_function_name() doesn't work well when skipping, use eval0()
3464 * instead to skip to any following command, e.g. for:
3465 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003466 ++emsg_skip;
3467 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3468 clear_tv(&rettv);
3469 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 return;
3471 }
3472
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003474 if (fudi.fd_newkey != NULL)
3475 {
3476 /* Still need to give an error message for missing key. */
3477 EMSG2(_(e_dictkey), fudi.fd_newkey);
3478 vim_free(fudi.fd_newkey);
3479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 if (tofree == NULL)
3481 return;
3482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 /* Increase refcount on dictionary, it could get deleted when evaluating
3484 * the arguments. */
3485 if (fudi.fd_dict != NULL)
3486 ++fudi.fd_dict->dv_refcount;
3487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003489 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003490 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar532c7802005-01-27 14:44:31 +00003492 /* Skip white space to allow ":call func ()". Not good, but required for
3493 * backward compatibility. */
3494 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 if (*startarg != '(')
3498 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003499 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 goto end;
3501 }
3502
3503 /*
3504 * When skipping, evaluate the function once, to find the end of the
3505 * arguments.
3506 * When the function takes a range, this is discovered after the first
3507 * call, and the loop is broken.
3508 */
3509 if (eap->skip)
3510 {
3511 ++emsg_skip;
3512 lnum = eap->line2; /* do it once, also with an invalid range */
3513 }
3514 else
3515 lnum = eap->line1;
3516 for ( ; lnum <= eap->line2; ++lnum)
3517 {
3518 if (!eap->skip && eap->addr_count > 0)
3519 {
3520 curwin->w_cursor.lnum = lnum;
3521 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003522#ifdef FEAT_VIRTUALEDIT
3523 curwin->w_cursor.coladd = 0;
3524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 eap->line1, eap->line2, &doesrange,
3529 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 failed = TRUE;
3532 break;
3533 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
3535 /* Handle a function returning a Funcref, Dictionary or List. */
3536 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3537 {
3538 failed = TRUE;
3539 break;
3540 }
3541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (doesrange || eap->skip)
3544 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (aborting())
3551 break;
3552 }
3553 if (eap->skip)
3554 --emsg_skip;
3555
3556 if (!failed)
3557 {
3558 /* Check for trailing illegal characters and a following command. */
3559 if (!ends_excmd(*arg))
3560 {
3561 emsg_severe = TRUE;
3562 EMSG(_(e_trailing));
3563 }
3564 else
3565 eap->nextcmd = check_nextcmd(arg);
3566 }
3567
3568end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
3574 * ":unlet[!] var1 ... " command.
3575 */
3576 void
3577ex_unlet(eap)
3578 exarg_T *eap;
3579{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 ex_unletlock(eap, eap->arg, 0);
3581}
3582
3583/*
3584 * ":lockvar" and ":unlockvar" commands
3585 */
3586 void
3587ex_lockvar(eap)
3588 exarg_T *eap;
3589{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003591 int deep = 2;
3592
3593 if (eap->forceit)
3594 deep = -1;
3595 else if (vim_isdigit(*arg))
3596 {
3597 deep = getdigits(&arg);
3598 arg = skipwhite(arg);
3599 }
3600
3601 ex_unletlock(eap, arg, deep);
3602}
3603
3604/*
3605 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3606 */
3607 static void
3608ex_unletlock(eap, argstart, deep)
3609 exarg_T *eap;
3610 char_u *argstart;
3611 int deep;
3612{
3613 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 do
3619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003621 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003622 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (lv.ll_name == NULL)
3624 error = TRUE; /* error but continue parsing */
3625 if (name_end == NULL || (!vim_iswhite(*name_end)
3626 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 if (name_end != NULL)
3629 {
3630 emsg_severe = TRUE;
3631 EMSG(_(e_trailing));
3632 }
3633 if (!(eap->skip || error))
3634 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 break;
3636 }
3637
3638 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 {
3640 if (eap->cmdidx == CMD_unlet)
3641 {
3642 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3643 error = TRUE;
3644 }
3645 else
3646 {
3647 if (do_lock_var(&lv, name_end, deep,
3648 eap->cmdidx == CMD_lockvar) == FAIL)
3649 error = TRUE;
3650 }
3651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 if (!eap->skip)
3654 clear_lval(&lv);
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 arg = skipwhite(name_end);
3657 } while (!ends_excmd(*arg));
3658
3659 eap->nextcmd = check_nextcmd(arg);
3660}
3661
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 int forceit;
3667{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 int ret = OK;
3669 int cc;
3670
3671 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 cc = *name_end;
3674 *name_end = NUL;
3675
3676 /* Normal name or expanded name. */
3677 if (check_changedtick(lp->ll_name))
3678 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003682 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003685 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003686 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003687 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 else if (lp->ll_range)
3689 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003690 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691 listitem_T *ll_li = lp->ll_li;
3692 int ll_n1 = lp->ll_n1;
3693
3694 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3695 {
3696 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003697 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003698 return FAIL;
3699 ll_li = li;
3700 ++ll_n1;
3701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702
3703 /* Delete a range of List items. */
3704 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3705 {
3706 li = lp->ll_li->li_next;
3707 listitem_remove(lp->ll_list, lp->ll_li);
3708 lp->ll_li = li;
3709 ++lp->ll_n1;
3710 }
3711 }
3712 else
3713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 /* unlet a List item. */
3716 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003719 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 }
3721
3722 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003723}
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725/*
3726 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 */
3729 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003730do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003732 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
Bram Moolenaar33570922005-01-25 22:26:29 +00003734 hashtab_T *ht;
3735 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003736 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003737 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003738 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 ht = find_var_ht(name, &varname);
3741 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003743 if (ht == &globvarht)
3744 d = &globvardict;
3745 else if (current_funccal != NULL
3746 && ht == &current_funccal->l_vars.dv_hashtab)
3747 d = &current_funccal->l_vars;
3748 else if (ht == &compat_hashtab)
3749 d = &vimvardict;
3750 else
3751 {
3752 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3753 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3754 }
3755 if (d == NULL)
3756 {
3757 EMSG2(_(e_intern2), "do_unlet()");
3758 return FAIL;
3759 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 hi = hash_find(ht, varname);
3761 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003763 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003764 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003765 || var_check_ro(di->di_flags, name, FALSE)
3766 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003767 return FAIL;
3768
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003769 delete_var(ht, hi);
3770 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003773 if (forceit)
3774 return OK;
3775 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return FAIL;
3777}
3778
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779/*
3780 * Lock or unlock variable indicated by "lp".
3781 * "deep" is the levels to go (-1 for unlimited);
3782 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3783 */
3784 static int
3785do_lock_var(lp, name_end, deep, lock)
3786 lval_T *lp;
3787 char_u *name_end;
3788 int deep;
3789 int lock;
3790{
3791 int ret = OK;
3792 int cc;
3793 dictitem_T *di;
3794
3795 if (deep == 0) /* nothing to do */
3796 return OK;
3797
3798 if (lp->ll_tv == NULL)
3799 {
3800 cc = *name_end;
3801 *name_end = NUL;
3802
3803 /* Normal name or expanded name. */
3804 if (check_changedtick(lp->ll_name))
3805 ret = FAIL;
3806 else
3807 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003808 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003809 if (di == NULL)
3810 ret = FAIL;
3811 else
3812 {
3813 if (lock)
3814 di->di_flags |= DI_FLAGS_LOCK;
3815 else
3816 di->di_flags &= ~DI_FLAGS_LOCK;
3817 item_lock(&di->di_tv, deep, lock);
3818 }
3819 }
3820 *name_end = cc;
3821 }
3822 else if (lp->ll_range)
3823 {
3824 listitem_T *li = lp->ll_li;
3825
3826 /* (un)lock a range of List items. */
3827 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3828 {
3829 item_lock(&li->li_tv, deep, lock);
3830 li = li->li_next;
3831 ++lp->ll_n1;
3832 }
3833 }
3834 else if (lp->ll_list != NULL)
3835 /* (un)lock a List item. */
3836 item_lock(&lp->ll_li->li_tv, deep, lock);
3837 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003838 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003839 item_lock(&lp->ll_di->di_tv, deep, lock);
3840
3841 return ret;
3842}
3843
3844/*
3845 * Lock or unlock an item. "deep" is nr of levels to go.
3846 */
3847 static void
3848item_lock(tv, deep, lock)
3849 typval_T *tv;
3850 int deep;
3851 int lock;
3852{
3853 static int recurse = 0;
3854 list_T *l;
3855 listitem_T *li;
3856 dict_T *d;
3857 hashitem_T *hi;
3858 int todo;
3859
3860 if (recurse >= DICT_MAXNEST)
3861 {
3862 EMSG(_("E743: variable nested too deep for (un)lock"));
3863 return;
3864 }
3865 if (deep == 0)
3866 return;
3867 ++recurse;
3868
3869 /* lock/unlock the item itself */
3870 if (lock)
3871 tv->v_lock |= VAR_LOCKED;
3872 else
3873 tv->v_lock &= ~VAR_LOCKED;
3874
3875 switch (tv->v_type)
3876 {
3877 case VAR_LIST:
3878 if ((l = tv->vval.v_list) != NULL)
3879 {
3880 if (lock)
3881 l->lv_lock |= VAR_LOCKED;
3882 else
3883 l->lv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 /* recursive: lock/unlock the items the List contains */
3886 for (li = l->lv_first; li != NULL; li = li->li_next)
3887 item_lock(&li->li_tv, deep - 1, lock);
3888 }
3889 break;
3890 case VAR_DICT:
3891 if ((d = tv->vval.v_dict) != NULL)
3892 {
3893 if (lock)
3894 d->dv_lock |= VAR_LOCKED;
3895 else
3896 d->dv_lock &= ~VAR_LOCKED;
3897 if (deep < 0 || deep > 1)
3898 {
3899 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003900 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003901 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3902 {
3903 if (!HASHITEM_EMPTY(hi))
3904 {
3905 --todo;
3906 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3907 }
3908 }
3909 }
3910 }
3911 }
3912 --recurse;
3913}
3914
Bram Moolenaara40058a2005-07-11 22:42:07 +00003915/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003916 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3917 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918 */
3919 static int
3920tv_islocked(tv)
3921 typval_T *tv;
3922{
3923 return (tv->v_lock & VAR_LOCKED)
3924 || (tv->v_type == VAR_LIST
3925 && tv->vval.v_list != NULL
3926 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3927 || (tv->v_type == VAR_DICT
3928 && tv->vval.v_dict != NULL
3929 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3930}
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3933/*
3934 * Delete all "menutrans_" variables.
3935 */
3936 void
3937del_menutrans_vars()
3938{
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003943 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 {
3946 if (!HASHITEM_EMPTY(hi))
3947 {
3948 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3950 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 }
3952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954}
3955#endif
3956
3957#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3958
3959/*
3960 * Local string buffer for the next two functions to store a variable name
3961 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3962 * get_user_var_name().
3963 */
3964
3965static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3966
3967static char_u *varnamebuf = NULL;
3968static int varnamebuflen = 0;
3969
3970/*
3971 * Function to concatenate a prefix and a variable name.
3972 */
3973 static char_u *
3974cat_prefix_varname(prefix, name)
3975 int prefix;
3976 char_u *name;
3977{
3978 int len;
3979
3980 len = (int)STRLEN(name) + 3;
3981 if (len > varnamebuflen)
3982 {
3983 vim_free(varnamebuf);
3984 len += 10; /* some additional space */
3985 varnamebuf = alloc(len);
3986 if (varnamebuf == NULL)
3987 {
3988 varnamebuflen = 0;
3989 return NULL;
3990 }
3991 varnamebuflen = len;
3992 }
3993 *varnamebuf = prefix;
3994 varnamebuf[1] = ':';
3995 STRCPY(varnamebuf + 2, name);
3996 return varnamebuf;
3997}
3998
3999/*
4000 * Function given to ExpandGeneric() to obtain the list of user defined
4001 * (global/buffer/window/built-in) variable names.
4002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *
4004get_user_var_name(xp, idx)
4005 expand_T *xp;
4006 int idx;
4007{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004008 static long_u gdone;
4009 static long_u bdone;
4010 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011#ifdef FEAT_WINDOWS
4012 static long_u tdone;
4013#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 static int vidx;
4015 static hashitem_T *hi;
4016 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021#ifdef FEAT_WINDOWS
4022 tdone = 0;
4023#endif
4024 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004025
4026 /* Global variables */
4027 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 else
4032 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 while (HASHITEM_EMPTY(hi))
4034 ++hi;
4035 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4036 return cat_prefix_varname('g', hi->hi_key);
4037 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004039
4040 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004041 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004042 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004044 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004046 else
4047 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 while (HASHITEM_EMPTY(hi))
4049 ++hi;
4050 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return (char_u *)"b:changedtick";
4056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004057
4058 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004059 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004062 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004063 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004064 else
4065 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004066 while (HASHITEM_EMPTY(hi))
4067 ++hi;
4068 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004071#ifdef FEAT_WINDOWS
4072 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004073 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074 if (tdone < ht->ht_used)
4075 {
4076 if (tdone++ == 0)
4077 hi = ht->ht_array;
4078 else
4079 ++hi;
4080 while (HASHITEM_EMPTY(hi))
4081 ++hi;
4082 return cat_prefix_varname('t', hi->hi_key);
4083 }
4084#endif
4085
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 /* v: variables */
4087 if (vidx < VV_LEN)
4088 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 vim_free(varnamebuf);
4091 varnamebuf = NULL;
4092 varnamebuflen = 0;
4093 return NULL;
4094}
4095
4096#endif /* FEAT_CMDL_COMPL */
4097
4098/*
4099 * types for expressions.
4100 */
4101typedef enum
4102{
4103 TYPE_UNKNOWN = 0
4104 , TYPE_EQUAL /* == */
4105 , TYPE_NEQUAL /* != */
4106 , TYPE_GREATER /* > */
4107 , TYPE_GEQUAL /* >= */
4108 , TYPE_SMALLER /* < */
4109 , TYPE_SEQUAL /* <= */
4110 , TYPE_MATCH /* =~ */
4111 , TYPE_NOMATCH /* !~ */
4112} exptype_T;
4113
4114/*
4115 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4118 */
4119
4120/*
4121 * Handle zero level expression.
4122 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004124 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 char_u **nextcmd;
4132 int evaluate;
4133{
4134 int ret;
4135 char_u *p;
4136
4137 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (ret == FAIL || !ends_excmd(*p))
4140 {
4141 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /*
4144 * Report the invalid expression unless the expression evaluation has
4145 * been cancelled due to an aborting error, an interrupt, or an
4146 * exception.
4147 */
4148 if (!aborting())
4149 EMSG2(_(e_invexpr2), arg);
4150 ret = FAIL;
4151 }
4152 if (nextcmd != NULL)
4153 *nextcmd = check_nextcmd(p);
4154
4155 return ret;
4156}
4157
4158/*
4159 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004160 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 *
4162 * "arg" must point to the first non-white of the expression.
4163 * "arg" is advanced to the next non-white after the recognized expression.
4164 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004165 * Note: "rettv.v_lock" is not set.
4166 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Return OK or FAIL.
4168 */
4169 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int evaluate;
4174{
4175 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004176 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 /*
4179 * Get the first variable.
4180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
4183
4184 if ((*arg)[0] == '?')
4185 {
4186 result = FALSE;
4187 if (evaluate)
4188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 int error = FALSE;
4190
4191 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 if (error)
4195 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197
4198 /*
4199 * Get the second variable.
4200 */
4201 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204
4205 /*
4206 * Check for the ":".
4207 */
4208 if ((*arg)[0] != ':')
4209 {
4210 EMSG(_("E109: Missing ':' after '?'"));
4211 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214 }
4215
4216 /*
4217 * Get the third variable.
4218 */
4219 *arg = skipwhite(*arg + 1);
4220 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4221 {
4222 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return FAIL;
4225 }
4226 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229
4230 return OK;
4231}
4232
4233/*
4234 * Handle first level expression:
4235 * expr2 || expr2 || expr2 logical OR
4236 *
4237 * "arg" must point to the first non-white of the expression.
4238 * "arg" is advanced to the next non-white after the recognized expression.
4239 *
4240 * Return OK or FAIL.
4241 */
4242 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int evaluate;
4247{
Bram Moolenaar33570922005-01-25 22:26:29 +00004248 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 long result;
4250 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 /*
4254 * Get the first variable.
4255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 return FAIL;
4258
4259 /*
4260 * Repeat until there is no following "||".
4261 */
4262 first = TRUE;
4263 result = FALSE;
4264 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4265 {
4266 if (evaluate && first)
4267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (error)
4272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 first = FALSE;
4274 }
4275
4276 /*
4277 * Get the second variable.
4278 */
4279 *arg = skipwhite(*arg + 2);
4280 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4281 return FAIL;
4282
4283 /*
4284 * Compute the result.
4285 */
4286 if (evaluate && !result)
4287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (error)
4292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 if (evaluate)
4295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 rettv->v_type = VAR_NUMBER;
4297 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 }
4300
4301 return OK;
4302}
4303
4304/*
4305 * Handle second level expression:
4306 * expr3 && expr3 && expr3 logical AND
4307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 int evaluate;
4318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 long result;
4321 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Get the first variable.
4326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 return FAIL;
4329
4330 /*
4331 * Repeat until there is no following "&&".
4332 */
4333 first = TRUE;
4334 result = TRUE;
4335 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4336 {
4337 if (evaluate && first)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 first = FALSE;
4345 }
4346
4347 /*
4348 * Get the second variable.
4349 */
4350 *arg = skipwhite(*arg + 2);
4351 if (eval4(arg, &var2, evaluate && result) == FAIL)
4352 return FAIL;
4353
4354 /*
4355 * Compute the result.
4356 */
4357 if (evaluate && result)
4358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 if (error)
4363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 if (evaluate)
4366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 rettv->v_type = VAR_NUMBER;
4368 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
4371
4372 return OK;
4373}
4374
4375/*
4376 * Handle third level expression:
4377 * var1 == var2
4378 * var1 =~ var2
4379 * var1 != var2
4380 * var1 !~ var2
4381 * var1 > var2
4382 * var1 >= var2
4383 * var1 < var2
4384 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004385 * var1 is var2
4386 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 *
4388 * "arg" must point to the first non-white of the expression.
4389 * "arg" is advanced to the next non-white after the recognized expression.
4390 *
4391 * Return OK or FAIL.
4392 */
4393 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int evaluate;
4398{
Bram Moolenaar33570922005-01-25 22:26:29 +00004399 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 char_u *p;
4401 int i;
4402 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 int len = 2;
4405 long n1, n2;
4406 char_u *s1, *s2;
4407 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4408 regmatch_T regmatch;
4409 int ic;
4410 char_u *save_cpo;
4411
4412 /*
4413 * Get the first variable.
4414 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 return FAIL;
4417
4418 p = *arg;
4419 switch (p[0])
4420 {
4421 case '=': if (p[1] == '=')
4422 type = TYPE_EQUAL;
4423 else if (p[1] == '~')
4424 type = TYPE_MATCH;
4425 break;
4426 case '!': if (p[1] == '=')
4427 type = TYPE_NEQUAL;
4428 else if (p[1] == '~')
4429 type = TYPE_NOMATCH;
4430 break;
4431 case '>': if (p[1] != '=')
4432 {
4433 type = TYPE_GREATER;
4434 len = 1;
4435 }
4436 else
4437 type = TYPE_GEQUAL;
4438 break;
4439 case '<': if (p[1] != '=')
4440 {
4441 type = TYPE_SMALLER;
4442 len = 1;
4443 }
4444 else
4445 type = TYPE_SEQUAL;
4446 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 case 'i': if (p[1] == 's')
4448 {
4449 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4450 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004451 i = p[len];
4452 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 {
4454 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4455 type_is = TRUE;
4456 }
4457 }
4458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460
4461 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004462 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 */
4464 if (type != TYPE_UNKNOWN)
4465 {
4466 /* extra question mark appended: ignore case */
4467 if (p[len] == '?')
4468 {
4469 ic = TRUE;
4470 ++len;
4471 }
4472 /* extra '#' appended: match case */
4473 else if (p[len] == '#')
4474 {
4475 ic = FALSE;
4476 ++len;
4477 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004478 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 else
4480 ic = p_ic;
4481
4482 /*
4483 * Get the second variable.
4484 */
4485 *arg = skipwhite(p + len);
4486 if (eval5(arg, &var2, evaluate) == FAIL)
4487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 return FAIL;
4490 }
4491
4492 if (evaluate)
4493 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 if (type_is && rettv->v_type != var2.v_type)
4495 {
4496 /* For "is" a different type always means FALSE, for "notis"
4497 * it means TRUE. */
4498 n1 = (type == TYPE_NEQUAL);
4499 }
4500 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4501 {
4502 if (type_is)
4503 {
4504 n1 = (rettv->v_type == var2.v_type
4505 && rettv->vval.v_list == var2.vval.v_list);
4506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 else if (rettv->v_type != var2.v_type
4510 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4511 {
4512 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004513 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004515 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 clear_tv(rettv);
4517 clear_tv(&var2);
4518 return FAIL;
4519 }
4520 else
4521 {
4522 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004523 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4524 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 if (type == TYPE_NEQUAL)
4526 n1 = !n1;
4527 }
4528 }
4529
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004530 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4531 {
4532 if (type_is)
4533 {
4534 n1 = (rettv->v_type == var2.v_type
4535 && rettv->vval.v_dict == var2.vval.v_dict);
4536 if (type == TYPE_NEQUAL)
4537 n1 = !n1;
4538 }
4539 else if (rettv->v_type != var2.v_type
4540 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4541 {
4542 if (rettv->v_type != var2.v_type)
4543 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4544 else
4545 EMSG(_("E736: Invalid operation for Dictionary"));
4546 clear_tv(rettv);
4547 clear_tv(&var2);
4548 return FAIL;
4549 }
4550 else
4551 {
4552 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004553 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4554 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004555 if (type == TYPE_NEQUAL)
4556 n1 = !n1;
4557 }
4558 }
4559
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4561 {
4562 if (rettv->v_type != var2.v_type
4563 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4564 {
4565 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004566 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004567 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004568 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 clear_tv(rettv);
4570 clear_tv(&var2);
4571 return FAIL;
4572 }
4573 else
4574 {
4575 /* Compare two Funcrefs for being equal or unequal. */
4576 if (rettv->vval.v_string == NULL
4577 || var2.vval.v_string == NULL)
4578 n1 = FALSE;
4579 else
4580 n1 = STRCMP(rettv->vval.v_string,
4581 var2.vval.v_string) == 0;
4582 if (type == TYPE_NEQUAL)
4583 n1 = !n1;
4584 }
4585 }
4586
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004587#ifdef FEAT_FLOAT
4588 /*
4589 * If one of the two variables is a float, compare as a float.
4590 * When using "=~" or "!~", always compare as string.
4591 */
4592 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4593 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4594 {
4595 float_T f1, f2;
4596
4597 if (rettv->v_type == VAR_FLOAT)
4598 f1 = rettv->vval.v_float;
4599 else
4600 f1 = get_tv_number(rettv);
4601 if (var2.v_type == VAR_FLOAT)
4602 f2 = var2.vval.v_float;
4603 else
4604 f2 = get_tv_number(&var2);
4605 n1 = FALSE;
4606 switch (type)
4607 {
4608 case TYPE_EQUAL: n1 = (f1 == f2); break;
4609 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4610 case TYPE_GREATER: n1 = (f1 > f2); break;
4611 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4612 case TYPE_SMALLER: n1 = (f1 < f2); break;
4613 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4614 case TYPE_UNKNOWN:
4615 case TYPE_MATCH:
4616 case TYPE_NOMATCH: break; /* avoid gcc warning */
4617 }
4618 }
4619#endif
4620
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 /*
4622 * If one of the two variables is a number, compare as a number.
4623 * When using "=~" or "!~", always compare as string.
4624 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 n1 = get_tv_number(rettv);
4629 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 switch (type)
4631 {
4632 case TYPE_EQUAL: n1 = (n1 == n2); break;
4633 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4634 case TYPE_GREATER: n1 = (n1 > n2); break;
4635 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4636 case TYPE_SMALLER: n1 = (n1 < n2); break;
4637 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4638 case TYPE_UNKNOWN:
4639 case TYPE_MATCH:
4640 case TYPE_NOMATCH: break; /* avoid gcc warning */
4641 }
4642 }
4643 else
4644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 s1 = get_tv_string_buf(rettv, buf1);
4646 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4648 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4649 else
4650 i = 0;
4651 n1 = FALSE;
4652 switch (type)
4653 {
4654 case TYPE_EQUAL: n1 = (i == 0); break;
4655 case TYPE_NEQUAL: n1 = (i != 0); break;
4656 case TYPE_GREATER: n1 = (i > 0); break;
4657 case TYPE_GEQUAL: n1 = (i >= 0); break;
4658 case TYPE_SMALLER: n1 = (i < 0); break;
4659 case TYPE_SEQUAL: n1 = (i <= 0); break;
4660
4661 case TYPE_MATCH:
4662 case TYPE_NOMATCH:
4663 /* avoid 'l' flag in 'cpoptions' */
4664 save_cpo = p_cpo;
4665 p_cpo = (char_u *)"";
4666 regmatch.regprog = vim_regcomp(s2,
4667 RE_MAGIC + RE_STRING);
4668 regmatch.rm_ic = ic;
4669 if (regmatch.regprog != NULL)
4670 {
4671 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004672 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (type == TYPE_NOMATCH)
4674 n1 = !n1;
4675 }
4676 p_cpo = save_cpo;
4677 break;
4678
4679 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4680 }
4681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004682 clear_tv(rettv);
4683 clear_tv(&var2);
4684 rettv->v_type = VAR_NUMBER;
4685 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 }
4688
4689 return OK;
4690}
4691
4692/*
4693 * Handle fourth level expression:
4694 * + number addition
4695 * - number subtraction
4696 * . string concatenation
4697 *
4698 * "arg" must point to the first non-white of the expression.
4699 * "arg" is advanced to the next non-white after the recognized expression.
4700 *
4701 * Return OK or FAIL.
4702 */
4703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 int evaluate;
4708{
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T var2;
4710 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 int op;
4712 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 float_T f1 = 0, f2 = 0;
4715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 char_u *s1, *s2;
4717 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4718 char_u *p;
4719
4720 /*
4721 * Get the first variable.
4722 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004723 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return FAIL;
4725
4726 /*
4727 * Repeat computing, until no '+', '-' or '.' is following.
4728 */
4729 for (;;)
4730 {
4731 op = **arg;
4732 if (op != '+' && op != '-' && op != '.')
4733 break;
4734
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735 if ((op != '+' || rettv->v_type != VAR_LIST)
4736#ifdef FEAT_FLOAT
4737 && (op == '.' || rettv->v_type != VAR_FLOAT)
4738#endif
4739 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 {
4741 /* For "list + ...", an illegal use of the first operand as
4742 * a number cannot be determined before evaluating the 2nd
4743 * operand: if this is also a list, all is ok.
4744 * For "something . ...", "something - ..." or "non-list + ...",
4745 * we know that the first operand needs to be a string or number
4746 * without evaluating the 2nd operand. So check before to avoid
4747 * side effects after an error. */
4748 if (evaluate && get_tv_string_chk(rettv) == NULL)
4749 {
4750 clear_tv(rettv);
4751 return FAIL;
4752 }
4753 }
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 /*
4756 * Get the second variable.
4757 */
4758 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004759 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 return FAIL;
4763 }
4764
4765 if (evaluate)
4766 {
4767 /*
4768 * Compute the result.
4769 */
4770 if (op == '.')
4771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4773 s2 = get_tv_string_buf_chk(&var2, buf2);
4774 if (s2 == NULL) /* type error ? */
4775 {
4776 clear_tv(rettv);
4777 clear_tv(&var2);
4778 return FAIL;
4779 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004780 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(rettv);
4782 rettv->v_type = VAR_STRING;
4783 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004785 else if (op == '+' && rettv->v_type == VAR_LIST
4786 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004787 {
4788 /* concatenate Lists */
4789 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4790 &var3) == FAIL)
4791 {
4792 clear_tv(rettv);
4793 clear_tv(&var2);
4794 return FAIL;
4795 }
4796 clear_tv(rettv);
4797 *rettv = var3;
4798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 else
4800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004801 int error = FALSE;
4802
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803#ifdef FEAT_FLOAT
4804 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806 f1 = rettv->vval.v_float;
4807 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 else
4810#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812 n1 = get_tv_number_chk(rettv, &error);
4813 if (error)
4814 {
4815 /* This can only happen for "list + non-list". For
4816 * "non-list + ..." or "something - ...", we returned
4817 * before evaluating the 2nd operand. */
4818 clear_tv(rettv);
4819 return FAIL;
4820 }
4821#ifdef FEAT_FLOAT
4822 if (var2.v_type == VAR_FLOAT)
4823 f1 = n1;
4824#endif
4825 }
4826#ifdef FEAT_FLOAT
4827 if (var2.v_type == VAR_FLOAT)
4828 {
4829 f2 = var2.vval.v_float;
4830 n2 = 0;
4831 }
4832 else
4833#endif
4834 {
4835 n2 = get_tv_number_chk(&var2, &error);
4836 if (error)
4837 {
4838 clear_tv(rettv);
4839 clear_tv(&var2);
4840 return FAIL;
4841 }
4842#ifdef FEAT_FLOAT
4843 if (rettv->v_type == VAR_FLOAT)
4844 f2 = n2;
4845#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848
4849#ifdef FEAT_FLOAT
4850 /* If there is a float on either side the result is a float. */
4851 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4852 {
4853 if (op == '+')
4854 f1 = f1 + f2;
4855 else
4856 f1 = f1 - f2;
4857 rettv->v_type = VAR_FLOAT;
4858 rettv->vval.v_float = f1;
4859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861#endif
4862 {
4863 if (op == '+')
4864 n1 = n1 + n2;
4865 else
4866 n1 = n1 - n2;
4867 rettv->v_type = VAR_NUMBER;
4868 rettv->vval.v_number = n1;
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 }
4874 return OK;
4875}
4876
4877/*
4878 * Handle fifth level expression:
4879 * * number multiplication
4880 * / number division
4881 * % number modulo
4882 *
4883 * "arg" must point to the first non-white of the expression.
4884 * "arg" is advanced to the next non-white after the recognized expression.
4885 *
4886 * Return OK or FAIL.
4887 */
4888 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004889eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004893 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
Bram Moolenaar33570922005-01-25 22:26:29 +00004895 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 int op;
4897 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898#ifdef FEAT_FLOAT
4899 int use_float = FALSE;
4900 float_T f1 = 0, f2;
4901#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
4904 /*
4905 * Get the first variable.
4906 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004907 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 return FAIL;
4909
4910 /*
4911 * Repeat computing, until no '*', '/' or '%' is following.
4912 */
4913 for (;;)
4914 {
4915 op = **arg;
4916 if (op != '*' && op != '/' && op != '%')
4917 break;
4918
4919 if (evaluate)
4920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921#ifdef FEAT_FLOAT
4922 if (rettv->v_type == VAR_FLOAT)
4923 {
4924 f1 = rettv->vval.v_float;
4925 use_float = TRUE;
4926 n1 = 0;
4927 }
4928 else
4929#endif
4930 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004932 if (error)
4933 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 else
4936 n1 = 0;
4937
4938 /*
4939 * Get the second variable.
4940 */
4941 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004942 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return FAIL;
4944
4945 if (evaluate)
4946 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947#ifdef FEAT_FLOAT
4948 if (var2.v_type == VAR_FLOAT)
4949 {
4950 if (!use_float)
4951 {
4952 f1 = n1;
4953 use_float = TRUE;
4954 }
4955 f2 = var2.vval.v_float;
4956 n2 = 0;
4957 }
4958 else
4959#endif
4960 {
4961 n2 = get_tv_number_chk(&var2, &error);
4962 clear_tv(&var2);
4963 if (error)
4964 return FAIL;
4965#ifdef FEAT_FLOAT
4966 if (use_float)
4967 f2 = n2;
4968#endif
4969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /*
4972 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975#ifdef FEAT_FLOAT
4976 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 if (op == '*')
4979 f1 = f1 * f2;
4980 else if (op == '/')
4981 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# ifdef VMS
4983 /* VMS crashes on divide by zero, work around it */
4984 if (f2 == 0.0)
4985 {
4986 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004987 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004988 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004989 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004991 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004992 }
4993 else
4994 f1 = f1 / f2;
4995# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 /* We rely on the floating point library to handle divide
4997 * by zero to result in "inf" and not a crash. */
4998 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004999# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005003 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 return FAIL;
5005 }
5006 rettv->v_type = VAR_FLOAT;
5007 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 if (op == '*')
5013 n1 = n1 * n2;
5014 else if (op == '/')
5015 {
5016 if (n2 == 0) /* give an error message? */
5017 {
5018 if (n1 == 0)
5019 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5020 else if (n1 < 0)
5021 n1 = -0x7fffffffL;
5022 else
5023 n1 = 0x7fffffffL;
5024 }
5025 else
5026 n1 = n1 / n2;
5027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 {
5030 if (n2 == 0) /* give an error message? */
5031 n1 = 0;
5032 else
5033 n1 = n1 % n2;
5034 }
5035 rettv->v_type = VAR_NUMBER;
5036 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 }
5040
5041 return OK;
5042}
5043
5044/*
5045 * Handle sixth level expression:
5046 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005047 * "string" string constant
5048 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 * &option-name option value
5050 * @r register contents
5051 * identifier variable value
5052 * function() function call
5053 * $VAR environment variable
5054 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005055 * [expr, expr] List
5056 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 *
5058 * Also handle:
5059 * ! in front logical NOT
5060 * - in front unary minus
5061 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 * trailing [] subscript in String or List
5063 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 *
5065 * "arg" must point to the first non-white of the expression.
5066 * "arg" is advanced to the next non-white after the recognized expression.
5067 *
5068 * Return OK or FAIL.
5069 */
5070 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005071eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005075 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 long n;
5078 int len;
5079 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 char_u *start_leader, *end_leader;
5081 int ret = OK;
5082 char_u *alias;
5083
5084 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
5090 /*
5091 * Skip '!' and '-' characters. They are handled later.
5092 */
5093 start_leader = *arg;
5094 while (**arg == '!' || **arg == '-' || **arg == '+')
5095 *arg = skipwhite(*arg + 1);
5096 end_leader = *arg;
5097
5098 switch (**arg)
5099 {
5100 /*
5101 * Number constant.
5102 */
5103 case '0':
5104 case '1':
5105 case '2':
5106 case '3':
5107 case '4':
5108 case '5':
5109 case '6':
5110 case '7':
5111 case '8':
5112 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005113 {
5114#ifdef FEAT_FLOAT
5115 char_u *p = skipdigits(*arg + 1);
5116 int get_float = FALSE;
5117
5118 /* We accept a float when the format matches
5119 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005120 * strict to avoid backwards compatibility problems.
5121 * Don't look for a float after the "." operator, so that
5122 * ":let vers = 1.2.3" doesn't fail. */
5123 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005125 get_float = TRUE;
5126 p = skipdigits(p + 2);
5127 if (*p == 'e' || *p == 'E')
5128 {
5129 ++p;
5130 if (*p == '-' || *p == '+')
5131 ++p;
5132 if (!vim_isdigit(*p))
5133 get_float = FALSE;
5134 else
5135 p = skipdigits(p + 1);
5136 }
5137 if (ASCII_ISALPHA(*p) || *p == '.')
5138 get_float = FALSE;
5139 }
5140 if (get_float)
5141 {
5142 float_T f;
5143
5144 *arg += string2float(*arg, &f);
5145 if (evaluate)
5146 {
5147 rettv->v_type = VAR_FLOAT;
5148 rettv->vval.v_float = f;
5149 }
5150 }
5151 else
5152#endif
5153 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005154 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005155 *arg += len;
5156 if (evaluate)
5157 {
5158 rettv->v_type = VAR_NUMBER;
5159 rettv->vval.v_number = n;
5160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165 /*
5166 * String constant: "string".
5167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170
5171 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 break;
5176
5177 /*
5178 * List: [expr, expr]
5179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 * Dictionary: {key: val, key: val}
5185 */
5186 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5187 break;
5188
5189 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005190 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005192 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 break;
5194
5195 /*
5196 * Environment variable: $VAR.
5197 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 break;
5200
5201 /*
5202 * Register contents: @r.
5203 */
5204 case '@': ++*arg;
5205 if (evaluate)
5206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005208 rettv->vval.v_string = get_reg_contents(**arg,
5209 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211 if (**arg != NUL)
5212 ++*arg;
5213 break;
5214
5215 /*
5216 * nested expression: (expression).
5217 */
5218 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005219 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (**arg == ')')
5221 ++*arg;
5222 else if (ret == OK)
5223 {
5224 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 ret = FAIL;
5227 }
5228 break;
5229
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 break;
5232 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233
5234 if (ret == NOTDONE)
5235 {
5236 /*
5237 * Must be a variable or function name.
5238 * Can also be a curly-braces kind of name: {expr}.
5239 */
5240 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005241 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242 if (alias != NULL)
5243 s = alias;
5244
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005245 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 ret = FAIL;
5247 else
5248 {
5249 if (**arg == '(') /* recursive! */
5250 {
5251 /* If "s" is the name of a variable of type VAR_FUNC
5252 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005253 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254
5255 /* Invoke the function. */
5256 ret = get_func_tv(s, len, rettv, arg,
5257 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005258 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005259
5260 /* If evaluate is FALSE rettv->v_type was not set in
5261 * get_func_tv, but it's needed in handle_subscript() to parse
5262 * what follows. So set it here. */
5263 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5264 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005265 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005266 rettv->v_type = VAR_FUNC;
5267 }
5268
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 /* Stop the expression evaluation when immediately
5270 * aborting on error, or when an interrupt occurred or
5271 * an exception was thrown but not caught. */
5272 if (aborting())
5273 {
5274 if (ret == OK)
5275 clear_tv(rettv);
5276 ret = FAIL;
5277 }
5278 }
5279 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005280 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005281 else
5282 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005284 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 }
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 *arg = skipwhite(*arg);
5288
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5290 * expr(expr). */
5291 if (ret == OK)
5292 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 /*
5295 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5296 */
5297 if (ret == OK && evaluate && end_leader > start_leader)
5298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 int val = 0;
5301#ifdef FEAT_FLOAT
5302 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 if (rettv->v_type == VAR_FLOAT)
5305 f = rettv->vval.v_float;
5306 else
5307#endif
5308 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 clear_tv(rettv);
5312 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 else
5315 {
5316 while (end_leader > start_leader)
5317 {
5318 --end_leader;
5319 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320 {
5321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 f = !f;
5324 else
5325#endif
5326 val = !val;
5327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005329 {
5330#ifdef FEAT_FLOAT
5331 if (rettv->v_type == VAR_FLOAT)
5332 f = -f;
5333 else
5334#endif
5335 val = -val;
5336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005338#ifdef FEAT_FLOAT
5339 if (rettv->v_type == VAR_FLOAT)
5340 {
5341 clear_tv(rettv);
5342 rettv->vval.v_float = f;
5343 }
5344 else
5345#endif
5346 {
5347 clear_tv(rettv);
5348 rettv->v_type = VAR_NUMBER;
5349 rettv->vval.v_number = val;
5350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353
5354 return ret;
5355}
5356
5357/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005358 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5359 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5361 */
5362 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005363eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005365 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368{
5369 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 long len = -1;
5373 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005377 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005379 if (verbose)
5380 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 return FAIL;
5382 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005383#ifdef FEAT_FLOAT
5384 else if (rettv->v_type == VAR_FLOAT)
5385 {
5386 if (verbose)
5387 EMSG(_(e_float_as_string));
5388 return FAIL;
5389 }
5390#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005392 init_tv(&var1);
5393 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 /*
5397 * dict.name
5398 */
5399 key = *arg + 1;
5400 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5401 ;
5402 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 }
5406 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 /*
5409 * something[idx]
5410 *
5411 * Get the (first) variable from inside the [].
5412 */
5413 *arg = skipwhite(*arg + 1);
5414 if (**arg == ':')
5415 empty1 = TRUE;
5416 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5417 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005418 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5419 {
5420 /* not a number or string */
5421 clear_tv(&var1);
5422 return FAIL;
5423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424
5425 /*
5426 * Get the second variable from inside the [:].
5427 */
5428 if (**arg == ':')
5429 {
5430 range = TRUE;
5431 *arg = skipwhite(*arg + 1);
5432 if (**arg == ']')
5433 empty2 = TRUE;
5434 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436 if (!empty1)
5437 clear_tv(&var1);
5438 return FAIL;
5439 }
5440 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5441 {
5442 /* not a number or string */
5443 if (!empty1)
5444 clear_tv(&var1);
5445 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 return FAIL;
5447 }
5448 }
5449
5450 /* Check for the ']'. */
5451 if (**arg != ']')
5452 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005453 if (verbose)
5454 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 clear_tv(&var1);
5456 if (range)
5457 clear_tv(&var2);
5458 return FAIL;
5459 }
5460 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462
5463 if (evaluate)
5464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 n1 = 0;
5466 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 n1 = get_tv_number(&var1);
5469 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 if (range)
5472 {
5473 if (empty2)
5474 n2 = -1;
5475 else
5476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 n2 = get_tv_number(&var2);
5478 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 }
5481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 {
5484 case VAR_NUMBER:
5485 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 len = (long)STRLEN(s);
5488 if (range)
5489 {
5490 /* The resulting variable is a substring. If the indexes
5491 * are out of range the result is empty. */
5492 if (n1 < 0)
5493 {
5494 n1 = len + n1;
5495 if (n1 < 0)
5496 n1 = 0;
5497 }
5498 if (n2 < 0)
5499 n2 = len + n2;
5500 else if (n2 >= len)
5501 n2 = len;
5502 if (n1 >= len || n2 < 0 || n1 > n2)
5503 s = NULL;
5504 else
5505 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5506 }
5507 else
5508 {
5509 /* The resulting variable is a string of a single
5510 * character. If the index is too big or negative the
5511 * result is empty. */
5512 if (n1 >= len || n1 < 0)
5513 s = NULL;
5514 else
5515 s = vim_strnsave(s + n1, 1);
5516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 clear_tv(rettv);
5518 rettv->v_type = VAR_STRING;
5519 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 break;
5521
5522 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 if (n1 < 0)
5525 n1 = len + n1;
5526 if (!empty1 && (n1 < 0 || n1 >= len))
5527 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005528 /* For a range we allow invalid values and return an empty
5529 * list. A list index out of range is an error. */
5530 if (!range)
5531 {
5532 if (verbose)
5533 EMSGN(_(e_listidx), n1);
5534 return FAIL;
5535 }
5536 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 }
5538 if (range)
5539 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005540 list_T *l;
5541 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542
5543 if (n2 < 0)
5544 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005545 else if (n2 >= len)
5546 n2 = len - 1;
5547 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005548 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 l = list_alloc();
5550 if (l == NULL)
5551 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 n1 <= n2; ++n1)
5554 {
5555 if (list_append_tv(l, &item->li_tv) == FAIL)
5556 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005557 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 return FAIL;
5559 }
5560 item = item->li_next;
5561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 clear_tv(rettv);
5563 rettv->v_type = VAR_LIST;
5564 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005565 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 }
5567 else
5568 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005569 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 clear_tv(rettv);
5571 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
5575 case VAR_DICT:
5576 if (range)
5577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005578 if (verbose)
5579 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 if (len == -1)
5581 clear_tv(&var1);
5582 return FAIL;
5583 }
5584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586
5587 if (len == -1)
5588 {
5589 key = get_tv_string(&var1);
5590 if (*key == NUL)
5591 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005592 if (verbose)
5593 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 clear_tv(&var1);
5595 return FAIL;
5596 }
5597 }
5598
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005599 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005601 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005602 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 if (len == -1)
5604 clear_tv(&var1);
5605 if (item == NULL)
5606 return FAIL;
5607
5608 copy_tv(&item->di_tv, &var1);
5609 clear_tv(rettv);
5610 *rettv = var1;
5611 }
5612 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 }
5614 }
5615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 return OK;
5617}
5618
5619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 * Get an option value.
5621 * "arg" points to the '&' or '+' before the option name.
5622 * "arg" is advanced to character after the option name.
5623 * Return OK or FAIL.
5624 */
5625 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int evaluate;
5630{
5631 char_u *option_end;
5632 long numval;
5633 char_u *stringval;
5634 int opt_type;
5635 int c;
5636 int working = (**arg == '+'); /* has("+option") */
5637 int ret = OK;
5638 int opt_flags;
5639
5640 /*
5641 * Isolate the option name and find its value.
5642 */
5643 option_end = find_option_end(arg, &opt_flags);
5644 if (option_end == NULL)
5645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 EMSG2(_("E112: Option name missing: %s"), *arg);
5648 return FAIL;
5649 }
5650
5651 if (!evaluate)
5652 {
5653 *arg = option_end;
5654 return OK;
5655 }
5656
5657 c = *option_end;
5658 *option_end = NUL;
5659 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 if (opt_type == -3) /* invalid name */
5663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 EMSG2(_("E113: Unknown option: %s"), *arg);
5666 ret = FAIL;
5667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
5670 if (opt_type == -2) /* hidden string option */
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else if (opt_type == -1) /* hidden number option */
5676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 rettv->v_type = VAR_NUMBER;
5678 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else if (opt_type == 1) /* number option */
5681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv->v_type = VAR_NUMBER;
5683 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else /* string option */
5686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 rettv->v_type = VAR_STRING;
5688 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 }
5691 else if (working && (opt_type == -2 || opt_type == -1))
5692 ret = FAIL;
5693
5694 *option_end = c; /* put back for error messages */
5695 *arg = option_end;
5696
5697 return ret;
5698}
5699
5700/*
5701 * Allocate a variable for a string constant.
5702 * Return OK or FAIL.
5703 */
5704 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 int evaluate;
5709{
5710 char_u *p;
5711 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 int extra = 0;
5713
5714 /*
5715 * Find the end of the string, skipping backslashed characters.
5716 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (*p == '\\' && p[1] != NUL)
5720 {
5721 ++p;
5722 /* A "\<x>" form occupies at least 4 characters, and produces up
5723 * to 6 characters: reserve space for 2 extra */
5724 if (*p == '<')
5725 extra += 2;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728
5729 if (*p != '"')
5730 {
5731 EMSG2(_("E114: Missing quote: %s"), *arg);
5732 return FAIL;
5733 }
5734
5735 /* If only parsing, set *arg and return here */
5736 if (!evaluate)
5737 {
5738 *arg = p + 1;
5739 return OK;
5740 }
5741
5742 /*
5743 * Copy the string into allocated memory, handling backslashed
5744 * characters.
5745 */
5746 name = alloc((unsigned)(p - *arg + extra));
5747 if (name == NULL)
5748 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 rettv->v_type = VAR_STRING;
5750 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (*p == '\\')
5755 {
5756 switch (*++p)
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 case 'b': *name++ = BS; ++p; break;
5759 case 'e': *name++ = ESC; ++p; break;
5760 case 'f': *name++ = FF; ++p; break;
5761 case 'n': *name++ = NL; ++p; break;
5762 case 'r': *name++ = CAR; ++p; break;
5763 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 case 'X': /* hex: "\x1", "\x12" */
5766 case 'x':
5767 case 'u': /* Unicode: "\u0023" */
5768 case 'U':
5769 if (vim_isxdigit(p[1]))
5770 {
5771 int n, nr;
5772 int c = toupper(*p);
5773
5774 if (c == 'X')
5775 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005778 else
5779 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 nr = 0;
5781 while (--n >= 0 && vim_isxdigit(p[1]))
5782 {
5783 ++p;
5784 nr = (nr << 4) + hex2nr(*p);
5785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787#ifdef FEAT_MBYTE
5788 /* For "\u" store the number according to
5789 * 'encoding'. */
5790 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797
5798 /* octal: "\1", "\12", "\123" */
5799 case '0':
5800 case '1':
5801 case '2':
5802 case '3':
5803 case '4':
5804 case '5':
5805 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case '7': *name = *p++ - '0';
5807 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 *name = (*name << 3) + *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
5811 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815
5816 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (extra != 0)
5819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823 /* FALLTHROUGH */
5824
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 }
5828 }
5829 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 *arg = p + 1;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return OK;
5837}
5838
5839/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Return OK or FAIL.
5842 */
5843 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005844get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 int evaluate;
5848{
5849 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 int reduce = 0;
5852
5853 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 */
5856 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 break;
5862 ++reduce;
5863 ++p;
5864 }
5865 }
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 return FAIL;
5871 }
5872
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 if (!evaluate)
5875 {
5876 *arg = p + 1;
5877 return OK;
5878 }
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 str = alloc((unsigned)((p - *arg) - reduce));
5884 if (str == NULL)
5885 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 break;
5895 ++p;
5896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 *arg = p + 1;
5901
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Allocate a variable for a List and fill it from "*arg".
5907 * Return OK or FAIL.
5908 */
5909 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 int evaluate;
5914{
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 list_T *l = NULL;
5916 typval_T tv;
5917 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918
5919 if (evaluate)
5920 {
5921 l = list_alloc();
5922 if (l == NULL)
5923 return FAIL;
5924 }
5925
5926 *arg = skipwhite(*arg + 1);
5927 while (**arg != ']' && **arg != NUL)
5928 {
5929 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5930 goto failret;
5931 if (evaluate)
5932 {
5933 item = listitem_alloc();
5934 if (item != NULL)
5935 {
5936 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005937 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 list_append(l, item);
5939 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005940 else
5941 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 }
5943
5944 if (**arg == ']')
5945 break;
5946 if (**arg != ',')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 goto failret;
5950 }
5951 *arg = skipwhite(*arg + 1);
5952 }
5953
5954 if (**arg != ']')
5955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957failret:
5958 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005959 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 return FAIL;
5961 }
5962
5963 *arg = skipwhite(*arg + 1);
5964 if (evaluate)
5965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005966 rettv->v_type = VAR_LIST;
5967 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 ++l->lv_refcount;
5969 }
5970
5971 return OK;
5972}
5973
5974/*
5975 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005976 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005978 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979list_alloc()
5980{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005981 list_T *l;
5982
5983 l = (list_T *)alloc_clear(sizeof(list_T));
5984 if (l != NULL)
5985 {
5986 /* Prepend the list to the list of lists for garbage collection. */
5987 if (first_list != NULL)
5988 first_list->lv_used_prev = l;
5989 l->lv_used_prev = NULL;
5990 l->lv_used_next = first_list;
5991 first_list = l;
5992 }
5993 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994}
5995
5996/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005997 * Allocate an empty list for a return value.
5998 * Returns OK or FAIL.
5999 */
6000 static int
6001rettv_list_alloc(rettv)
6002 typval_T *rettv;
6003{
6004 list_T *l = list_alloc();
6005
6006 if (l == NULL)
6007 return FAIL;
6008
6009 rettv->vval.v_list = l;
6010 rettv->v_type = VAR_LIST;
6011 ++l->lv_refcount;
6012 return OK;
6013}
6014
6015/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 * Unreference a list: decrement the reference count and free it when it
6017 * becomes zero.
6018 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006019 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006023 if (l != NULL && --l->lv_refcount <= 0)
6024 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025}
6026
6027/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006028 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 * Ignores the reference count.
6030 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006031 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006032list_free(l, recurse)
6033 list_T *l;
6034 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006038 /* Remove the list from the list of lists for garbage collection. */
6039 if (l->lv_used_prev == NULL)
6040 first_list = l->lv_used_next;
6041 else
6042 l->lv_used_prev->lv_used_next = l->lv_used_next;
6043 if (l->lv_used_next != NULL)
6044 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6045
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006048 /* Remove the item before deleting it. */
6049 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006050 if (recurse || (item->li_tv.v_type != VAR_LIST
6051 && item->li_tv.v_type != VAR_DICT))
6052 clear_tv(&item->li_tv);
6053 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 }
6055 vim_free(l);
6056}
6057
6058/*
6059 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006060 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006062 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063listitem_alloc()
6064{
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066}
6067
6068/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006069 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006071 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006075 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 vim_free(item);
6077}
6078
6079/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 * Remove a list item from a List and free it. Also clears the value.
6081 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006082 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006083listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 list_T *l;
6085 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006086{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006087 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006088 listitem_free(item);
6089}
6090
6091/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 * Get the number of items in a list.
6093 */
6094 static long
6095list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 if (l == NULL)
6099 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006100 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101}
6102
6103/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 * Return TRUE when two lists have exactly the same values.
6105 */
6106 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 list_T *l1;
6109 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112{
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006115 if (l1 == NULL || l2 == NULL)
6116 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 if (l1 == l2)
6118 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006119 if (list_len(l1) != list_len(l2))
6120 return FALSE;
6121
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006122 for (item1 = l1->lv_first, item2 = l2->lv_first;
6123 item1 != NULL && item2 != NULL;
6124 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006125 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126 return FALSE;
6127 return item1 == NULL && item2 == NULL;
6128}
6129
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006130#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6131 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006132/*
6133 * Return the dictitem that an entry in a hashtable points to.
6134 */
6135 dictitem_T *
6136dict_lookup(hi)
6137 hashitem_T *hi;
6138{
6139 return HI2DI(hi);
6140}
6141#endif
6142
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 * Return TRUE when two dictionaries have exactly the same key/values.
6145 */
6146 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 dict_T *d1;
6149 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006151 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152{
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 hashitem_T *hi;
6154 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006157 if (d1 == NULL || d2 == NULL)
6158 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006159 if (d1 == d2)
6160 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 if (dict_len(d1) != dict_len(d2))
6162 return FALSE;
6163
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006164 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006167 if (!HASHITEM_EMPTY(hi))
6168 {
6169 item2 = dict_find(d2, hi->hi_key, -1);
6170 if (item2 == NULL)
6171 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006172 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006173 return FALSE;
6174 --todo;
6175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006176 }
6177 return TRUE;
6178}
6179
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180static int tv_equal_recurse_limit;
6181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006182/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006185 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 */
6187 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 typval_T *tv1;
6190 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 int ic; /* ignore case */
6192 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193{
6194 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006195 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006197 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006199 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006202 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 * recursiveness to a limit. We guess they are equal then.
6204 * A fixed limit has the problem of still taking an awful long time.
6205 * Reduce the limit every time running into it. That should work fine for
6206 * deeply linked structures that are not recursively linked and catch
6207 * recursiveness quickly. */
6208 if (!recursive)
6209 tv_equal_recurse_limit = 1000;
6210 if (recursive_cnt >= tv_equal_recurse_limit)
6211 {
6212 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006213 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006215
6216 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006217 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006219 ++recursive_cnt;
6220 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6221 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006222 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223
6224 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006225 ++recursive_cnt;
6226 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6227 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006228 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006229
6230 case VAR_FUNC:
6231 return (tv1->vval.v_string != NULL
6232 && tv2->vval.v_string != NULL
6233 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6234
6235 case VAR_NUMBER:
6236 return tv1->vval.v_number == tv2->vval.v_number;
6237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006238#ifdef FEAT_FLOAT
6239 case VAR_FLOAT:
6240 return tv1->vval.v_float == tv2->vval.v_float;
6241#endif
6242
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243 case VAR_STRING:
6244 s1 = get_tv_string_buf(tv1, buf1);
6245 s2 = get_tv_string_buf(tv2, buf2);
6246 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248
6249 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250 return TRUE;
6251}
6252
6253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 * Locate item with index "n" in list "l" and return it.
6255 * A negative index is counted from the end; -1 is the last item.
6256 * Returns NULL when "n" is out of range.
6257 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006258 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 long n;
6262{
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 long idx;
6265
6266 if (l == NULL)
6267 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268
6269 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271 n = l->lv_len + n;
6272
6273 /* Check for index out of range. */
6274 if (n < 0 || n >= l->lv_len)
6275 return NULL;
6276
6277 /* When there is a cached index may start search from there. */
6278 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006280 if (n < l->lv_idx / 2)
6281 {
6282 /* closest to the start of the list */
6283 item = l->lv_first;
6284 idx = 0;
6285 }
6286 else if (n > (l->lv_idx + l->lv_len) / 2)
6287 {
6288 /* closest to the end of the list */
6289 item = l->lv_last;
6290 idx = l->lv_len - 1;
6291 }
6292 else
6293 {
6294 /* closest to the cached index */
6295 item = l->lv_idx_item;
6296 idx = l->lv_idx;
6297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 }
6299 else
6300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 if (n < l->lv_len / 2)
6302 {
6303 /* closest to the start of the list */
6304 item = l->lv_first;
6305 idx = 0;
6306 }
6307 else
6308 {
6309 /* closest to the end of the list */
6310 item = l->lv_last;
6311 idx = l->lv_len - 1;
6312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314
6315 while (n > idx)
6316 {
6317 /* search forward */
6318 item = item->li_next;
6319 ++idx;
6320 }
6321 while (n < idx)
6322 {
6323 /* search backward */
6324 item = item->li_prev;
6325 --idx;
6326 }
6327
6328 /* cache the used index */
6329 l->lv_idx = idx;
6330 l->lv_idx_item = item;
6331
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 return item;
6333}
6334
6335/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006336 * Get list item "l[idx]" as a number.
6337 */
6338 static long
6339list_find_nr(l, idx, errorp)
6340 list_T *l;
6341 long idx;
6342 int *errorp; /* set to TRUE when something wrong */
6343{
6344 listitem_T *li;
6345
6346 li = list_find(l, idx);
6347 if (li == NULL)
6348 {
6349 if (errorp != NULL)
6350 *errorp = TRUE;
6351 return -1L;
6352 }
6353 return get_tv_number_chk(&li->li_tv, errorp);
6354}
6355
6356/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006357 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6358 */
6359 char_u *
6360list_find_str(l, idx)
6361 list_T *l;
6362 long idx;
6363{
6364 listitem_T *li;
6365
6366 li = list_find(l, idx - 1);
6367 if (li == NULL)
6368 {
6369 EMSGN(_(e_listidx), idx);
6370 return NULL;
6371 }
6372 return get_tv_string(&li->li_tv);
6373}
6374
6375/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376 * Locate "item" list "l" and return its index.
6377 * Returns -1 when "item" is not in the list.
6378 */
6379 static long
6380list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 list_T *l;
6382 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006383{
6384 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006386
6387 if (l == NULL)
6388 return -1;
6389 idx = 0;
6390 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6391 ++idx;
6392 if (li == NULL)
6393 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006394 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006395}
6396
6397/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398 * Append item "item" to the end of list "l".
6399 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006400 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 list_T *l;
6403 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404{
6405 if (l->lv_last == NULL)
6406 {
6407 /* empty list */
6408 l->lv_first = item;
6409 l->lv_last = item;
6410 item->li_prev = NULL;
6411 }
6412 else
6413 {
6414 l->lv_last->li_next = item;
6415 item->li_prev = l->lv_last;
6416 l->lv_last = item;
6417 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 item->li_next = NULL;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006426 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 list_T *l;
6429 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006431 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435 copy_tv(tv, &li->li_tv);
6436 list_append(l, li);
6437 return OK;
6438}
6439
6440/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006441 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006442 * Return FAIL when out of memory.
6443 */
6444 int
6445list_append_dict(list, dict)
6446 list_T *list;
6447 dict_T *dict;
6448{
6449 listitem_T *li = listitem_alloc();
6450
6451 if (li == NULL)
6452 return FAIL;
6453 li->li_tv.v_type = VAR_DICT;
6454 li->li_tv.v_lock = 0;
6455 li->li_tv.vval.v_dict = dict;
6456 list_append(list, li);
6457 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 return OK;
6459}
6460
6461/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006462 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006463 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 * Returns FAIL when out of memory.
6465 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006466 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006467list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006468 list_T *l;
6469 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006470 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006471{
6472 listitem_T *li = listitem_alloc();
6473
6474 if (li == NULL)
6475 return FAIL;
6476 list_append(l, li);
6477 li->li_tv.v_type = VAR_STRING;
6478 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006479 if (str == NULL)
6480 li->li_tv.vval.v_string = NULL;
6481 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006482 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006483 return FAIL;
6484 return OK;
6485}
6486
6487/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006488 * Append "n" to list "l".
6489 * Returns FAIL when out of memory.
6490 */
6491 static int
6492list_append_number(l, n)
6493 list_T *l;
6494 varnumber_T n;
6495{
6496 listitem_T *li;
6497
6498 li = listitem_alloc();
6499 if (li == NULL)
6500 return FAIL;
6501 li->li_tv.v_type = VAR_NUMBER;
6502 li->li_tv.v_lock = 0;
6503 li->li_tv.vval.v_number = n;
6504 list_append(l, li);
6505 return OK;
6506}
6507
6508/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 * If "item" is NULL append at the end.
6511 * Return FAIL when out of memory.
6512 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006513 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 list_T *l;
6516 typval_T *tv;
6517 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518{
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520
6521 if (ni == NULL)
6522 return FAIL;
6523 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006524 list_insert(l, ni, item);
6525 return OK;
6526}
6527
6528 void
6529list_insert(l, ni, item)
6530 list_T *l;
6531 listitem_T *ni;
6532 listitem_T *item;
6533{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 if (item == NULL)
6535 /* Append new item at end of list. */
6536 list_append(l, ni);
6537 else
6538 {
6539 /* Insert new item before existing item. */
6540 ni->li_prev = item->li_prev;
6541 ni->li_next = item;
6542 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 ++l->lv_idx;
6546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006548 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006550 l->lv_idx_item = NULL;
6551 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006553 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555}
6556
6557/*
6558 * Extend "l1" with "l2".
6559 * If "bef" is NULL append at the end, otherwise insert before this item.
6560 * Returns FAIL when out of memory.
6561 */
6562 static int
6563list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 list_T *l1;
6565 list_T *l2;
6566 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567{
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006569 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006571 /* We also quit the loop when we have inserted the original item count of
6572 * the list, avoid a hang when we extend a list with itself. */
6573 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6575 return FAIL;
6576 return OK;
6577}
6578
6579/*
6580 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6581 * Return FAIL when out of memory.
6582 */
6583 static int
6584list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *l1;
6586 list_T *l2;
6587 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588{
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006591 if (l1 == NULL || l2 == NULL)
6592 return FAIL;
6593
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006594 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 if (l == NULL)
6597 return FAIL;
6598 tv->v_type = VAR_LIST;
6599 tv->vval.v_list = l;
6600
6601 /* append all items from the second list */
6602 return list_extend(l, l2, NULL);
6603}
6604
6605/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006606 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 * Returns NULL when out of memory.
6610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006612list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006613 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616{
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 list_T *copy;
6618 listitem_T *item;
6619 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620
6621 if (orig == NULL)
6622 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623
6624 copy = list_alloc();
6625 if (copy != NULL)
6626 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006627 if (copyID != 0)
6628 {
6629 /* Do this before adding the items, because one of the items may
6630 * refer back to this list. */
6631 orig->lv_copyID = copyID;
6632 orig->lv_copylist = copy;
6633 }
6634 for (item = orig->lv_first; item != NULL && !got_int;
6635 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 {
6637 ni = listitem_alloc();
6638 if (ni == NULL)
6639 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006641 {
6642 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6643 {
6644 vim_free(ni);
6645 break;
6646 }
6647 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006649 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 list_append(copy, ni);
6651 }
6652 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006653 if (item != NULL)
6654 {
6655 list_unref(copy);
6656 copy = NULL;
6657 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 }
6659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 return copy;
6661}
6662
6663/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006665 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006666 * This used to be called list_remove, but that conflicts with a Sun header
6667 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006669 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006670vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 list_T *l;
6672 listitem_T *item;
6673 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674{
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677 /* notify watchers */
6678 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006680 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006681 list_fix_watch(l, ip);
6682 if (ip == item2)
6683 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006685
6686 if (item2->li_next == NULL)
6687 l->lv_last = item->li_prev;
6688 else
6689 item2->li_next->li_prev = item->li_prev;
6690 if (item->li_prev == NULL)
6691 l->lv_first = item2->li_next;
6692 else
6693 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006694 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695}
6696
6697/*
6698 * Return an allocated string with the string representation of a list.
6699 * May return NULL.
6700 */
6701 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006702list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006703 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006704 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705{
6706 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707
6708 if (tv->vval.v_list == NULL)
6709 return NULL;
6710 ga_init2(&ga, (int)sizeof(char), 80);
6711 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006712 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006713 {
6714 vim_free(ga.ga_data);
6715 return NULL;
6716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006717 ga_append(&ga, ']');
6718 ga_append(&ga, NUL);
6719 return (char_u *)ga.ga_data;
6720}
6721
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006722typedef struct join_S {
6723 char_u *s;
6724 char_u *tofree;
6725} join_T;
6726
6727 static int
6728list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6729 garray_T *gap; /* to store the result in */
6730 list_T *l;
6731 char_u *sep;
6732 int echo_style;
6733 int copyID;
6734 garray_T *join_gap; /* to keep each list item string */
6735{
6736 int i;
6737 join_T *p;
6738 int len;
6739 int sumlen = 0;
6740 int first = TRUE;
6741 char_u *tofree;
6742 char_u numbuf[NUMBUFLEN];
6743 listitem_T *item;
6744 char_u *s;
6745
6746 /* Stringify each item in the list. */
6747 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6748 {
6749 if (echo_style)
6750 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6751 else
6752 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6753 if (s == NULL)
6754 return FAIL;
6755
6756 len = (int)STRLEN(s);
6757 sumlen += len;
6758
Bram Moolenaarcde88542015-08-11 19:14:00 +02006759 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006760 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6761 if (tofree != NULL || s != numbuf)
6762 {
6763 p->s = s;
6764 p->tofree = tofree;
6765 }
6766 else
6767 {
6768 p->s = vim_strnsave(s, len);
6769 p->tofree = p->s;
6770 }
6771
6772 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006773 if (did_echo_string_emsg) /* recursion error, bail out */
6774 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006775 }
6776
6777 /* Allocate result buffer with its total size, avoid re-allocation and
6778 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6779 if (join_gap->ga_len >= 2)
6780 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6781 if (ga_grow(gap, sumlen + 2) == FAIL)
6782 return FAIL;
6783
6784 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6785 {
6786 if (first)
6787 first = FALSE;
6788 else
6789 ga_concat(gap, sep);
6790 p = ((join_T *)join_gap->ga_data) + i;
6791
6792 if (p->s != NULL)
6793 ga_concat(gap, p->s);
6794 line_breakcheck();
6795 }
6796
6797 return OK;
6798}
6799
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006800/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006802 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006803 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006804 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006805 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006806list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006808 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006810 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006811 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006812{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006813 garray_T join_ga;
6814 int retval;
6815 join_T *p;
6816 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817
Bram Moolenaard39a7512015-04-16 22:51:22 +02006818 if (l->lv_len < 1)
6819 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006820 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6821 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6822
6823 /* Dispose each item in join_ga. */
6824 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826 p = (join_T *)join_ga.ga_data;
6827 for (i = 0; i < join_ga.ga_len; ++i)
6828 {
6829 vim_free(p->tofree);
6830 ++p;
6831 }
6832 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006833 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006834
6835 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006836}
6837
6838/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 * Garbage collection for lists and dictionaries.
6840 *
6841 * We use reference counts to be able to free most items right away when they
6842 * are no longer used. But for composite items it's possible that it becomes
6843 * unused while the reference count is > 0: When there is a recursive
6844 * reference. Example:
6845 * :let l = [1, 2, 3]
6846 * :let d = {9: l}
6847 * :let l[1] = d
6848 *
6849 * Since this is quite unusual we handle this with garbage collection: every
6850 * once in a while find out which lists and dicts are not referenced from any
6851 * variable.
6852 *
6853 * Here is a good reference text about garbage collection (refers to Python
6854 * but it applies to all reference-counting mechanisms):
6855 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006857
6858/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 * Do garbage collection for lists and dicts.
6860 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006861 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 int
6863garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006864{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 buf_T *buf;
6868 win_T *wp;
6869 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006870 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006871 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006873#ifdef FEAT_WINDOWS
6874 tabpage_T *tp;
6875#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006877 /* Only do this once. */
6878 want_garbage_collect = FALSE;
6879 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006880 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006881
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006882 /* We advance by two because we add one for items referenced through
6883 * previous_funccal. */
6884 current_copyID += COPYID_INC;
6885 copyID = current_copyID;
6886
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 /*
6888 * 1. Go through all accessible variables and mark all lists and dicts
6889 * with copyID.
6890 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891
6892 /* Don't free variables in the previous_funccal list unless they are only
6893 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006894 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6896 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6898 NULL);
6899 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6900 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006901 }
6902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 /* script-local variables */
6904 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
6907 /* buffer-local variables */
6908 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6910 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911
6912 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006913 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6915 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006916#ifdef FEAT_AUTOCMD
6917 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6919 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006920#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006922#ifdef FEAT_WINDOWS
6923 /* tabpage-local variables */
6924 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6926 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927#endif
6928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931
6932 /* function-local variables */
6933 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6934 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6936 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 }
6938
Bram Moolenaard812df62008-11-09 12:46:09 +00006939 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006941
Bram Moolenaar1dced572012-04-05 16:54:08 +02006942#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006944#endif
6945
Bram Moolenaardb913952012-06-29 12:54:53 +02006946#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006948#endif
6949
6950#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006952#endif
6953
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 /*
6957 * 2. Free lists and dictionaries that are not referenced.
6958 */
6959 did_free = free_unref_items(copyID);
6960
6961 /*
6962 * 3. Check if any funccal can be freed now.
6963 */
6964 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 if (can_free_funccal(*pfc, copyID))
6967 {
6968 fc = *pfc;
6969 *pfc = fc->caller;
6970 free_funccal(fc, TRUE);
6971 did_free = TRUE;
6972 did_free_funccal = TRUE;
6973 }
6974 else
6975 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 if (did_free_funccal)
6978 /* When a funccal was freed some more items might be garbage
6979 * collected, so run again. */
6980 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 else if (p_verbose > 0)
6983 {
6984 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6985 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006986
6987 return did_free;
6988}
6989
6990/*
6991 * Free lists and dictionaries that are no longer referenced.
6992 */
6993 static int
6994free_unref_items(copyID)
6995 int copyID;
6996{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006997 dict_T *dd, *dd_next;
6998 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 int did_free = FALSE;
7000
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 */
7004 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 {
7006 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007009 /* Free the Dictionary and ordinary items it contains, but don't
7010 * recurse into Lists and Dictionaries, they will be in the list
7011 * of dicts or list of lists. */
7012 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007015 dd = dd_next;
7016 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017
7018 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007019 * Go through the list of lists and free items without the copyID.
7020 * But don't free a list that has a watcher (used in a for loop), these
7021 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 */
7023 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007024 {
7025 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007026 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7027 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007029 /* Free the List and ordinary items it contains, but don't recurse
7030 * into Lists and Dictionaries, they will be in the list of dicts
7031 * or list of lists. */
7032 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007035 ll = ll_next;
7036 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 return did_free;
7038}
7039
7040/*
7041 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 * "list_stack" is used to add lists to be marked. Can be NULL.
7043 *
7044 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int
7047set_ref_in_ht(ht, copyID, list_stack)
7048 hashtab_T *ht;
7049 int copyID;
7050 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051{
7052 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 hashtab_T *cur_ht;
7056 ht_stack_T *ht_stack = NULL;
7057 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 cur_ht = ht;
7060 for (;;)
7061 {
7062 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 /* Mark each item in the hashtab. If the item contains a hashtab
7065 * it is added to ht_stack, if it contains a list it is added to
7066 * list_stack. */
7067 todo = (int)cur_ht->ht_used;
7068 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7069 if (!HASHITEM_EMPTY(hi))
7070 {
7071 --todo;
7072 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7073 &ht_stack, list_stack);
7074 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076
7077 if (ht_stack == NULL)
7078 break;
7079
7080 /* take an item from the stack */
7081 cur_ht = ht_stack->ht;
7082 tempitem = ht_stack;
7083 ht_stack = ht_stack->prev;
7084 free(tempitem);
7085 }
7086
7087 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088}
7089
7090/*
7091 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7093 *
7094 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 int
7097set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098 list_T *l;
7099 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 listitem_T *li;
7103 int abort = FALSE;
7104 list_T *cur_l;
7105 list_stack_T *list_stack = NULL;
7106 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 cur_l = l;
7109 for (;;)
7110 {
7111 if (!abort)
7112 /* Mark each item in the list. If the item contains a hashtab
7113 * it is added to ht_stack, if it contains a list it is added to
7114 * list_stack. */
7115 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7116 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7117 ht_stack, &list_stack);
7118 if (list_stack == NULL)
7119 break;
7120
7121 /* take an item from the stack */
7122 cur_l = list_stack->list;
7123 tempitem = list_stack;
7124 list_stack = list_stack->prev;
7125 free(tempitem);
7126 }
7127
7128 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129}
7130
7131/*
7132 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 * "list_stack" is used to add lists to be marked. Can be NULL.
7134 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7135 *
7136 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007137 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 int
7139set_ref_in_item(tv, copyID, ht_stack, list_stack)
7140 typval_T *tv;
7141 int copyID;
7142 ht_stack_T **ht_stack;
7143 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007144{
7145 dict_T *dd;
7146 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007147 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148
7149 switch (tv->v_type)
7150 {
7151 case VAR_DICT:
7152 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007153 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007155 /* Didn't see this dict yet. */
7156 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 if (ht_stack == NULL)
7158 {
7159 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7160 }
7161 else
7162 {
7163 ht_stack_T *newitem = (ht_stack_T*)malloc(
7164 sizeof(ht_stack_T));
7165 if (newitem == NULL)
7166 abort = TRUE;
7167 else
7168 {
7169 newitem->ht = &dd->dv_hashtab;
7170 newitem->prev = *ht_stack;
7171 *ht_stack = newitem;
7172 }
7173 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007175 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007176
7177 case VAR_LIST:
7178 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007179 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007181 /* Didn't see this list yet. */
7182 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 if (list_stack == NULL)
7184 {
7185 abort = set_ref_in_list(ll, copyID, ht_stack);
7186 }
7187 else
7188 {
7189 list_stack_T *newitem = (list_stack_T*)malloc(
7190 sizeof(list_stack_T));
7191 if (newitem == NULL)
7192 abort = TRUE;
7193 else
7194 {
7195 newitem->list = ll;
7196 newitem->prev = *list_stack;
7197 *list_stack = newitem;
7198 }
7199 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007200 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007202 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007203 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007204}
7205
7206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207 * Allocate an empty header for a dictionary.
7208 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007209 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210dict_alloc()
7211{
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007217 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218 if (first_dict != NULL)
7219 first_dict->dv_used_prev = d;
7220 d->dv_used_next = first_dict;
7221 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007222 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007225 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007226 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007227 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007228 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007230 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231}
7232
7233/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007234 * Allocate an empty dict for a return value.
7235 * Returns OK or FAIL.
7236 */
7237 static int
7238rettv_dict_alloc(rettv)
7239 typval_T *rettv;
7240{
7241 dict_T *d = dict_alloc();
7242
7243 if (d == NULL)
7244 return FAIL;
7245
7246 rettv->vval.v_dict = d;
7247 rettv->v_type = VAR_DICT;
7248 ++d->dv_refcount;
7249 return OK;
7250}
7251
7252
7253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 * Unreference a Dictionary: decrement the reference count and free it when it
7255 * becomes zero.
7256 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007257 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 if (d != NULL && --d->dv_refcount <= 0)
7262 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263}
7264
7265/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007266 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 * Ignores the reference count.
7268 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007269 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007270dict_free(d, recurse)
7271 dict_T *d;
7272 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007276 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007278 /* Remove the dict from the list of dicts for garbage collection. */
7279 if (d->dv_used_prev == NULL)
7280 first_dict = d->dv_used_next;
7281 else
7282 d->dv_used_prev->dv_used_next = d->dv_used_next;
7283 if (d->dv_used_next != NULL)
7284 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7285
7286 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007287 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007288 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 if (!HASHITEM_EMPTY(hi))
7292 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007293 /* Remove the item before deleting it, just in case there is
7294 * something recursive causing trouble. */
7295 di = HI2DI(hi);
7296 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007297 if (recurse || (di->di_tv.v_type != VAR_LIST
7298 && di->di_tv.v_type != VAR_DICT))
7299 clear_tv(&di->di_tv);
7300 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 --todo;
7302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 vim_free(d);
7306}
7307
7308/*
7309 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 * The "key" is copied to the new item.
7311 * Note that the value of the item "di_tv" still needs to be initialized!
7312 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007314 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315dictitem_alloc(key)
7316 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317{
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007320 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007322 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007324 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327}
7328
7329/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330 * Make a copy of a Dictionary item.
7331 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335{
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007338 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7339 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 if (di != NULL)
7341 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007343 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344 copy_tv(&org->di_tv, &di->di_tv);
7345 }
7346 return di;
7347}
7348
7349/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350 * Remove item "item" from Dictionary "dict" and free it.
7351 */
7352 static void
7353dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 dict_T *dict;
7355 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356{
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 if (HASHITEM_EMPTY(hi))
7361 EMSG2(_(e_intern2), "dictitem_remove()");
7362 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 dictitem_free(item);
7365}
7366
7367/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368 * Free a dict item. Also clears the value.
7369 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007370 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007375 if (item->di_flags & DI_FLAGS_ALLOC)
7376 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007377}
7378
7379/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7381 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007382 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 * Returns NULL when out of memory.
7384 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007386dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390{
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 dict_T *copy;
7392 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395
7396 if (orig == NULL)
7397 return NULL;
7398
7399 copy = dict_alloc();
7400 if (copy != NULL)
7401 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007402 if (copyID != 0)
7403 {
7404 orig->dv_copyID = copyID;
7405 orig->dv_copydict = copy;
7406 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007407 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007408 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007412 --todo;
7413
7414 di = dictitem_alloc(hi->hi_key);
7415 if (di == NULL)
7416 break;
7417 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 {
7419 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7420 copyID) == FAIL)
7421 {
7422 vim_free(di);
7423 break;
7424 }
7425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426 else
7427 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7428 if (dict_add(copy, di) == FAIL)
7429 {
7430 dictitem_free(di);
7431 break;
7432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435
Bram Moolenaare9a41262005-01-15 22:18:47 +00007436 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 if (todo > 0)
7438 {
7439 dict_unref(copy);
7440 copy = NULL;
7441 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007442 }
7443
7444 return copy;
7445}
7446
7447/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007449 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007451 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 dict_T *d;
7454 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455{
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457}
7458
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007460 * Add a number or string entry to dictionary "d".
7461 * When "str" is NULL use number "nr", otherwise use "str".
7462 * Returns FAIL when out of memory and when key already exists.
7463 */
7464 int
7465dict_add_nr_str(d, key, nr, str)
7466 dict_T *d;
7467 char *key;
7468 long nr;
7469 char_u *str;
7470{
7471 dictitem_T *item;
7472
7473 item = dictitem_alloc((char_u *)key);
7474 if (item == NULL)
7475 return FAIL;
7476 item->di_tv.v_lock = 0;
7477 if (str == NULL)
7478 {
7479 item->di_tv.v_type = VAR_NUMBER;
7480 item->di_tv.vval.v_number = nr;
7481 }
7482 else
7483 {
7484 item->di_tv.v_type = VAR_STRING;
7485 item->di_tv.vval.v_string = vim_strsave(str);
7486 }
7487 if (dict_add(d, item) == FAIL)
7488 {
7489 dictitem_free(item);
7490 return FAIL;
7491 }
7492 return OK;
7493}
7494
7495/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007496 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007497 * Returns FAIL when out of memory and when key already exists.
7498 */
7499 int
7500dict_add_list(d, key, list)
7501 dict_T *d;
7502 char *key;
7503 list_T *list;
7504{
7505 dictitem_T *item;
7506
7507 item = dictitem_alloc((char_u *)key);
7508 if (item == NULL)
7509 return FAIL;
7510 item->di_tv.v_lock = 0;
7511 item->di_tv.v_type = VAR_LIST;
7512 item->di_tv.vval.v_list = list;
7513 if (dict_add(d, item) == FAIL)
7514 {
7515 dictitem_free(item);
7516 return FAIL;
7517 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007518 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007519 return OK;
7520}
7521
7522/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007523 * Get the number of items in a Dictionary.
7524 */
7525 static long
7526dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 if (d == NULL)
7530 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007531 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007532}
7533
7534/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 * Find item "key[len]" in Dictionary "d".
7536 * If "len" is negative use strlen(key).
7537 * Returns NULL when not found.
7538 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007539 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007540dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 char_u *key;
7543 int len;
7544{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545#define AKEYLEN 200
7546 char_u buf[AKEYLEN];
7547 char_u *akey;
7548 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 if (len < 0)
7552 akey = key;
7553 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007554 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 tofree = akey = vim_strnsave(key, len);
7556 if (akey == NULL)
7557 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 else
7560 {
7561 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007562 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 akey = buf;
7564 }
7565
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 vim_free(tofree);
7568 if (HASHITEM_EMPTY(hi))
7569 return NULL;
7570 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571}
7572
7573/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007574 * Get a string item from a dictionary.
7575 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007576 * Returns NULL if the entry doesn't exist or out of memory.
7577 */
7578 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007579get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007580 dict_T *d;
7581 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007582 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007583{
7584 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007585 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007586
7587 di = dict_find(d, key, -1);
7588 if (di == NULL)
7589 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007590 s = get_tv_string(&di->di_tv);
7591 if (save && s != NULL)
7592 s = vim_strsave(s);
7593 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007594}
7595
7596/*
7597 * Get a number item from a dictionary.
7598 * Returns 0 if the entry doesn't exist or out of memory.
7599 */
7600 long
7601get_dict_number(d, key)
7602 dict_T *d;
7603 char_u *key;
7604{
7605 dictitem_T *di;
7606
7607 di = dict_find(d, key, -1);
7608 if (di == NULL)
7609 return 0;
7610 return get_tv_number(&di->di_tv);
7611}
7612
7613/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 * Return an allocated string with the string representation of a Dictionary.
7615 * May return NULL.
7616 */
7617 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621{
7622 garray_T ga;
7623 int first = TRUE;
7624 char_u *tofree;
7625 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 return NULL;
7633 ga_init2(&ga, (int)sizeof(char), 80);
7634 ga_append(&ga, '{');
7635
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007636 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007637 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641 --todo;
7642
7643 if (first)
7644 first = FALSE;
7645 else
7646 ga_concat(&ga, (char_u *)", ");
7647
7648 tofree = string_quote(hi->hi_key, FALSE);
7649 if (tofree != NULL)
7650 {
7651 ga_concat(&ga, tofree);
7652 vim_free(tofree);
7653 }
7654 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007655 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 if (s != NULL)
7657 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007659 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007660 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007661 line_breakcheck();
7662
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007665 if (todo > 0)
7666 {
7667 vim_free(ga.ga_data);
7668 return NULL;
7669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670
7671 ga_append(&ga, '}');
7672 ga_append(&ga, NUL);
7673 return (char_u *)ga.ga_data;
7674}
7675
7676/*
7677 * Allocate a variable for a Dictionary and fill it from "*arg".
7678 * Return OK or FAIL. Returns NOTDONE for {expr}.
7679 */
7680 static int
7681get_dict_tv(arg, rettv, evaluate)
7682 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 int evaluate;
7685{
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 dict_T *d = NULL;
7687 typval_T tvkey;
7688 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007689 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693
7694 /*
7695 * First check if it's not a curly-braces thing: {expr}.
7696 * Must do this without evaluating, otherwise a function may be called
7697 * twice. Unfortunately this means we need to call eval1() twice for the
7698 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 if (*start != '}')
7702 {
7703 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7704 return FAIL;
7705 if (*start == '}')
7706 return NOTDONE;
7707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708
7709 if (evaluate)
7710 {
7711 d = dict_alloc();
7712 if (d == NULL)
7713 return FAIL;
7714 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 tvkey.v_type = VAR_UNKNOWN;
7716 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717
7718 *arg = skipwhite(*arg + 1);
7719 while (**arg != '}' && **arg != NUL)
7720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 goto failret;
7723 if (**arg != ':')
7724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007725 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007726 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 goto failret;
7728 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007729 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007731 key = get_tv_string_buf_chk(&tvkey, buf);
7732 if (key == NULL || *key == NUL)
7733 {
7734 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7735 if (key != NULL)
7736 EMSG(_(e_emptykey));
7737 clear_tv(&tvkey);
7738 goto failret;
7739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741
7742 *arg = skipwhite(*arg + 1);
7743 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7744 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007745 if (evaluate)
7746 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 goto failret;
7748 }
7749 if (evaluate)
7750 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 if (item != NULL)
7753 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007754 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007755 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 clear_tv(&tv);
7757 goto failret;
7758 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007759 item = dictitem_alloc(key);
7760 clear_tv(&tvkey);
7761 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007764 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007765 if (dict_add(d, item) == FAIL)
7766 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 }
7768 }
7769
7770 if (**arg == '}')
7771 break;
7772 if (**arg != ',')
7773 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007774 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 goto failret;
7776 }
7777 *arg = skipwhite(*arg + 1);
7778 }
7779
7780 if (**arg != '}')
7781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007782 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007783failret:
7784 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007785 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786 return FAIL;
7787 }
7788
7789 *arg = skipwhite(*arg + 1);
7790 if (evaluate)
7791 {
7792 rettv->v_type = VAR_DICT;
7793 rettv->vval.v_dict = d;
7794 ++d->dv_refcount;
7795 }
7796
7797 return OK;
7798}
7799
Bram Moolenaar8c711452005-01-14 21:53:12 +00007800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 * Return a string with the string representation of a variable.
7802 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007803 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007804 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007805 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007806 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 */
7808 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007809echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007812 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007813 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 static int recurse = 0;
7816 char_u *r = NULL;
7817
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007819 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007820 if (!did_echo_string_emsg)
7821 {
7822 /* Only give this message once for a recursive call to avoid
7823 * flooding the user with errors. And stop iterating over lists
7824 * and dicts. */
7825 did_echo_string_emsg = TRUE;
7826 EMSG(_("E724: variable nested too deep for displaying"));
7827 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007829 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 }
7831 ++recurse;
7832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 switch (tv->v_type)
7834 {
7835 case VAR_FUNC:
7836 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 r = tv->vval.v_string;
7838 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841 if (tv->vval.v_list == NULL)
7842 {
7843 *tofree = NULL;
7844 r = NULL;
7845 }
7846 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7847 {
7848 *tofree = NULL;
7849 r = (char_u *)"[...]";
7850 }
7851 else
7852 {
7853 tv->vval.v_list->lv_copyID = copyID;
7854 *tofree = list2string(tv, copyID);
7855 r = *tofree;
7856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007858
Bram Moolenaar8c711452005-01-14 21:53:12 +00007859 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860 if (tv->vval.v_dict == NULL)
7861 {
7862 *tofree = NULL;
7863 r = NULL;
7864 }
7865 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7866 {
7867 *tofree = NULL;
7868 r = (char_u *)"{...}";
7869 }
7870 else
7871 {
7872 tv->vval.v_dict->dv_copyID = copyID;
7873 *tofree = dict2string(tv, copyID);
7874 r = *tofree;
7875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 case VAR_STRING:
7879 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007880 *tofree = NULL;
7881 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007882 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007883
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007884#ifdef FEAT_FLOAT
7885 case VAR_FLOAT:
7886 *tofree = NULL;
7887 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7888 r = numbuf;
7889 break;
7890#endif
7891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896
Bram Moolenaar8502c702014-06-17 12:51:16 +02007897 if (--recurse == 0)
7898 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900}
7901
7902/*
7903 * Return a string with the string representation of a variable.
7904 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7905 * "numbuf" is used for a number.
7906 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007907 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007908 */
7909 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007910tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912 char_u **tofree;
7913 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007914 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915{
7916 switch (tv->v_type)
7917 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 case VAR_FUNC:
7919 *tofree = string_quote(tv->vval.v_string, TRUE);
7920 return *tofree;
7921 case VAR_STRING:
7922 *tofree = string_quote(tv->vval.v_string, FALSE);
7923 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007924#ifdef FEAT_FLOAT
7925 case VAR_FLOAT:
7926 *tofree = NULL;
7927 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7928 return numbuf;
7929#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007930 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007933 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007934 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007936 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007937 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938}
7939
7940/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 * Return string "str" in ' quotes, doubling ' characters.
7942 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 */
7945 static char_u *
7946string_quote(str, function)
7947 char_u *str;
7948 int function;
7949{
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007951 char_u *p, *r, *s;
7952
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 len = (function ? 13 : 3);
7954 if (str != NULL)
7955 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007956 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007957 for (p = str; *p != NUL; mb_ptr_adv(p))
7958 if (*p == '\'')
7959 ++len;
7960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 s = r = alloc(len);
7962 if (r != NULL)
7963 {
7964 if (function)
7965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 r += 10;
7968 }
7969 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 if (str != NULL)
7972 for (p = str; *p != NUL; )
7973 {
7974 if (*p == '\'')
7975 *r++ = '\'';
7976 MB_COPY_CHAR(p, r);
7977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007979 if (function)
7980 *r++ = ')';
7981 *r++ = NUL;
7982 }
7983 return s;
7984}
7985
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986#ifdef FEAT_FLOAT
7987/*
7988 * Convert the string "text" to a floating point number.
7989 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7990 * this always uses a decimal point.
7991 * Returns the length of the text that was consumed.
7992 */
7993 static int
7994string2float(text, value)
7995 char_u *text;
7996 float_T *value; /* result stored here */
7997{
7998 char *s = (char *)text;
7999 float_T f;
8000
8001 f = strtod(s, &s);
8002 *value = f;
8003 return (int)((char_u *)s - text);
8004}
8005#endif
8006
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 * Get the value of an environment variable.
8009 * "arg" is pointing to the '$'. It is advanced to after the name.
8010 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008011 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 */
8013 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008014get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 int evaluate;
8018{
8019 char_u *string = NULL;
8020 int len;
8021 int cc;
8022 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008023 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024
8025 ++*arg;
8026 name = *arg;
8027 len = get_env_len(arg);
8028 if (evaluate)
8029 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008030 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008031 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008032
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008033 cc = name[len];
8034 name[len] = NUL;
8035 /* first try vim_getenv(), fast for normal environment vars */
8036 string = vim_getenv(name, &mustfree);
8037 if (string != NULL && *string != NUL)
8038 {
8039 if (!mustfree)
8040 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008042 else
8043 {
8044 if (mustfree)
8045 vim_free(string);
8046
8047 /* next try expanding things like $VIM and ${HOME} */
8048 string = expand_env_save(name - 1);
8049 if (string != NULL && *string == '$')
8050 {
8051 vim_free(string);
8052 string = NULL;
8053 }
8054 }
8055 name[len] = cc;
8056
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008057 rettv->v_type = VAR_STRING;
8058 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 }
8060
8061 return OK;
8062}
8063
8064/*
8065 * Array with names and number of arguments of all internal functions
8066 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8067 */
8068static struct fst
8069{
8070 char *f_name; /* function name */
8071 char f_min_argc; /* minimal number of arguments */
8072 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008074 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075} functions[] =
8076{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008081 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008082 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008083 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"append", 2, 2, f_append},
8085 {"argc", 0, 0, f_argc},
8086 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008087 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008088 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008089#ifdef FEAT_FLOAT
8090 {"asin", 1, 1, f_asin}, /* WJMc */
8091#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008092 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008093 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008094 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008095 {"assert_false", 1, 2, f_assert_false},
8096 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097#ifdef FEAT_FLOAT
8098 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008099 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008102 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"bufexists", 1, 1, f_bufexists},
8104 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8105 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8106 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8107 {"buflisted", 1, 1, f_buflisted},
8108 {"bufloaded", 1, 1, f_bufloaded},
8109 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008110 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"bufwinnr", 1, 1, f_bufwinnr},
8112 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008113 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008114 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"ceil", 1, 1, f_ceil},
8118#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008119 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008120 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008122 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008124#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008125 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008126 {"complete_add", 1, 1, f_complete_add},
8127 {"complete_check", 0, 0, f_complete_check},
8128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008131#ifdef FEAT_FLOAT
8132 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008133 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008135 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008137 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008138 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"delete", 1, 1, f_delete},
8140 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008141 {"diff_filler", 1, 1, f_diff_filler},
8142 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008143 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008145 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"eventhandler", 0, 0, f_eventhandler},
8147 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008148 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150#ifdef FEAT_FLOAT
8151 {"exp", 1, 1, f_exp},
8152#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008153 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008154 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008155 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8157 {"filereadable", 1, 1, f_filereadable},
8158 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008159 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008160 {"finddir", 1, 3, f_finddir},
8161 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#ifdef FEAT_FLOAT
8163 {"float2nr", 1, 1, f_float2nr},
8164 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008165 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008166#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008167 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"fnamemodify", 2, 2, f_fnamemodify},
8169 {"foldclosed", 1, 1, f_foldclosed},
8170 {"foldclosedend", 1, 1, f_foldclosedend},
8171 {"foldlevel", 1, 1, f_foldlevel},
8172 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008173 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008175 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008176 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008177 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008178 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008179 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"getchar", 0, 1, f_getchar},
8181 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008182 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getcmdline", 0, 0, f_getcmdline},
8184 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008185 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008186 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008187 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008189 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008190 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"getfsize", 1, 1, f_getfsize},
8192 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008193 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008194 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008195 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008196 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008197 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008198 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008199 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008200 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008202 {"gettabvar", 2, 3, f_gettabvar},
8203 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"getwinposx", 0, 0, f_getwinposx},
8205 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008206 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008207 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008208 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008209 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008211 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008212 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008213 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8215 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8216 {"histadd", 2, 2, f_histadd},
8217 {"histdel", 1, 2, f_histdel},
8218 {"histget", 1, 2, f_histget},
8219 {"histnr", 1, 1, f_histnr},
8220 {"hlID", 1, 1, f_hlID},
8221 {"hlexists", 1, 1, f_hlexists},
8222 {"hostname", 0, 0, f_hostname},
8223 {"iconv", 3, 3, f_iconv},
8224 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008225 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008226 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008228 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"inputrestore", 0, 0, f_inputrestore},
8230 {"inputsave", 0, 0, f_inputsave},
8231 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008233 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008235 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008236 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008237 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"libcall", 3, 3, f_libcall},
8242 {"libcallnr", 3, 3, f_libcallnr},
8243 {"line", 1, 1, f_line},
8244 {"line2byte", 1, 1, f_line2byte},
8245 {"lispindent", 1, 1, f_lispindent},
8246 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008248 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008249 {"log10", 1, 1, f_log10},
8250#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008251#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008252 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008253#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008254 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008255 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008256 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008257 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008258 {"matchadd", 2, 5, f_matchadd},
8259 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008260 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008261 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008262 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008263 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008264 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008265 {"max", 1, 1, f_max},
8266 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008267#ifdef vim_mkdir
8268 {"mkdir", 1, 3, f_mkdir},
8269#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008270 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008271#ifdef FEAT_MZSCHEME
8272 {"mzeval", 1, 1, f_mzeval},
8273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008275 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008276 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008277 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008278#ifdef FEAT_FLOAT
8279 {"pow", 2, 2, f_pow},
8280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008282 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008283 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008284#ifdef FEAT_PYTHON3
8285 {"py3eval", 1, 1, f_py3eval},
8286#endif
8287#ifdef FEAT_PYTHON
8288 {"pyeval", 1, 1, f_pyeval},
8289#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008290 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008291 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008292 {"reltime", 0, 2, f_reltime},
8293 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"remote_expr", 2, 3, f_remote_expr},
8295 {"remote_foreground", 1, 1, f_remote_foreground},
8296 {"remote_peek", 1, 2, f_remote_peek},
8297 {"remote_read", 1, 1, f_remote_read},
8298 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008299 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008301 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008303 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008304#ifdef FEAT_FLOAT
8305 {"round", 1, 1, f_round},
8306#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008307 {"screenattr", 2, 2, f_screenattr},
8308 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008309 {"screencol", 0, 0, f_screencol},
8310 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008311 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008312 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008313 {"searchpair", 3, 7, f_searchpair},
8314 {"searchpairpos", 3, 7, f_searchpairpos},
8315 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"server2client", 2, 2, f_server2client},
8317 {"serverlist", 0, 0, f_serverlist},
8318 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008319 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"setcmdpos", 1, 1, f_setcmdpos},
8321 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008322 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008323 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008324 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008325 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008327 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008328 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008330#ifdef FEAT_CRYPT
8331 {"sha256", 1, 1, f_sha256},
8332#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008333 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008334 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336#ifdef FEAT_FLOAT
8337 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008338 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008339#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008340 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008341 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008342 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008343 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008344 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008345#ifdef FEAT_FLOAT
8346 {"sqrt", 1, 1, f_sqrt},
8347 {"str2float", 1, 1, f_str2float},
8348#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008349 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008350 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008351 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352#ifdef HAVE_STRFTIME
8353 {"strftime", 1, 2, f_strftime},
8354#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008355 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008356 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"strlen", 1, 1, f_strlen},
8358 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008359 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008361 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008362 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"substitute", 4, 4, f_substitute},
8364 {"synID", 3, 3, f_synID},
8365 {"synIDattr", 2, 3, f_synIDattr},
8366 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008367 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008368 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008369 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008370 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008371 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008372 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008373 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008374 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008375 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008376#ifdef FEAT_FLOAT
8377 {"tan", 1, 1, f_tan},
8378 {"tanh", 1, 1, f_tanh},
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008381 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"tolower", 1, 1, f_tolower},
8383 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008384 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008385#ifdef FEAT_FLOAT
8386 {"trunc", 1, 1, f_trunc},
8387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008389 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008390 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008391 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008392 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"virtcol", 1, 1, f_virtcol},
8394 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008395 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"winbufnr", 1, 1, f_winbufnr},
8397 {"wincol", 0, 0, f_wincol},
8398 {"winheight", 1, 1, f_winheight},
8399 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008400 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008402 {"winrestview", 1, 1, f_winrestview},
8403 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008405 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008406 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008407 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408};
8409
8410#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8411
8412/*
8413 * Function given to ExpandGeneric() to obtain the list of internal
8414 * or user defined function names.
8415 */
8416 char_u *
8417get_function_name(xp, idx)
8418 expand_T *xp;
8419 int idx;
8420{
8421 static int intidx = -1;
8422 char_u *name;
8423
8424 if (idx == 0)
8425 intidx = -1;
8426 if (intidx < 0)
8427 {
8428 name = get_user_func_name(xp, idx);
8429 if (name != NULL)
8430 return name;
8431 }
8432 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8433 {
8434 STRCPY(IObuff, functions[intidx].f_name);
8435 STRCAT(IObuff, "(");
8436 if (functions[intidx].f_max_argc == 0)
8437 STRCAT(IObuff, ")");
8438 return IObuff;
8439 }
8440
8441 return NULL;
8442}
8443
8444/*
8445 * Function given to ExpandGeneric() to obtain the list of internal or
8446 * user defined variable or function names.
8447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 char_u *
8449get_expr_name(xp, idx)
8450 expand_T *xp;
8451 int idx;
8452{
8453 static int intidx = -1;
8454 char_u *name;
8455
8456 if (idx == 0)
8457 intidx = -1;
8458 if (intidx < 0)
8459 {
8460 name = get_function_name(xp, idx);
8461 if (name != NULL)
8462 return name;
8463 }
8464 return get_user_var_name(xp, ++intidx);
8465}
8466
8467#endif /* FEAT_CMDL_COMPL */
8468
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008469#if defined(EBCDIC) || defined(PROTO)
8470/*
8471 * Compare struct fst by function name.
8472 */
8473 static int
8474compare_func_name(s1, s2)
8475 const void *s1;
8476 const void *s2;
8477{
8478 struct fst *p1 = (struct fst *)s1;
8479 struct fst *p2 = (struct fst *)s2;
8480
8481 return STRCMP(p1->f_name, p2->f_name);
8482}
8483
8484/*
8485 * Sort the function table by function name.
8486 * The sorting of the table above is ASCII dependant.
8487 * On machines using EBCDIC we have to sort it.
8488 */
8489 static void
8490sortFunctions()
8491{
8492 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8493
8494 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8495}
8496#endif
8497
8498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499/*
8500 * Find internal function in table above.
8501 * Return index, or -1 if not found
8502 */
8503 static int
8504find_internal_func(name)
8505 char_u *name; /* name of the function */
8506{
8507 int first = 0;
8508 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8509 int cmp;
8510 int x;
8511
8512 /*
8513 * Find the function name in the table. Binary search.
8514 */
8515 while (first <= last)
8516 {
8517 x = first + ((unsigned)(last - first) >> 1);
8518 cmp = STRCMP(name, functions[x].f_name);
8519 if (cmp < 0)
8520 last = x - 1;
8521 else if (cmp > 0)
8522 first = x + 1;
8523 else
8524 return x;
8525 }
8526 return -1;
8527}
8528
8529/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8531 * name it contains, otherwise return "name".
8532 */
8533 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008534deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 char_u *name;
8536 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008537 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538{
Bram Moolenaar33570922005-01-25 22:26:29 +00008539 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540 int cc;
8541
8542 cc = name[*lenp];
8543 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008544 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008549 {
8550 *lenp = 0;
8551 return (char_u *)""; /* just in case */
8552 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008553 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008554 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008555 }
8556
8557 return name;
8558}
8559
8560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 * Allocate a variable for the result of a function.
8562 * Return OK or FAIL.
8563 */
8564 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8566 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 char_u *name; /* name of the function */
8568 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 char_u **arg; /* argument, pointing to the '(' */
8571 linenr_T firstline; /* first line of range */
8572 linenr_T lastline; /* last line of range */
8573 int *doesrange; /* return: function handled range */
8574 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 char_u *argp;
8578 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008579 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 int argcount = 0; /* number of arguments found */
8581
8582 /*
8583 * Get the arguments.
8584 */
8585 argp = *arg;
8586 while (argcount < MAX_FUNC_ARGS)
8587 {
8588 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8589 if (*argp == ')' || *argp == ',' || *argp == NUL)
8590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8592 {
8593 ret = FAIL;
8594 break;
8595 }
8596 ++argcount;
8597 if (*argp != ',')
8598 break;
8599 }
8600 if (*argp == ')')
8601 ++argp;
8602 else
8603 ret = FAIL;
8604
8605 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008607 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008609 {
8610 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008611 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008613 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
8616 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 *arg = skipwhite(argp);
8620 return ret;
8621}
8622
8623
8624/*
8625 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008626 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008627 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 */
8629 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008630call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008631 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008632 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008636 typval_T *argvars; /* vars for arguments, must have "argcount"
8637 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 linenr_T firstline; /* first line of range */
8639 linenr_T lastline; /* last line of range */
8640 int *doesrange; /* return: function handled range */
8641 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#define ERROR_UNKNOWN 0
8646#define ERROR_TOOMANY 1
8647#define ERROR_TOOFEW 2
8648#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008649#define ERROR_DICT 4
8650#define ERROR_NONE 5
8651#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 int error = ERROR_NONE;
8653 int i;
8654 int llen;
8655 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656#define FLEN_FIXED 40
8657 char_u fname_buf[FLEN_FIXED + 1];
8658 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008659 char_u *name;
8660
8661 /* Make a copy of the name, if it comes from a funcref variable it could
8662 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008663 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008664 if (name == NULL)
8665 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
8667 /*
8668 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8669 * Change <SNR>123_name() to K_SNR 123_name().
8670 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 llen = eval_fname_script(name);
8673 if (llen > 0)
8674 {
8675 fname_buf[0] = K_SPECIAL;
8676 fname_buf[1] = KS_EXTRA;
8677 fname_buf[2] = (int)KE_SNR;
8678 i = 3;
8679 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8680 {
8681 if (current_SID <= 0)
8682 error = ERROR_SCRIPT;
8683 else
8684 {
8685 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8686 i = (int)STRLEN(fname_buf);
8687 }
8688 }
8689 if (i + STRLEN(name + llen) < FLEN_FIXED)
8690 {
8691 STRCPY(fname_buf + i, name + llen);
8692 fname = fname_buf;
8693 }
8694 else
8695 {
8696 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8697 if (fname == NULL)
8698 error = ERROR_OTHER;
8699 else
8700 {
8701 mch_memmove(fname, fname_buf, (size_t)i);
8702 STRCPY(fname + i, name + llen);
8703 }
8704 }
8705 }
8706 else
8707 fname = name;
8708
8709 *doesrange = FALSE;
8710
8711
8712 /* execute the function if no errors detected and executing */
8713 if (evaluate && error == ERROR_NONE)
8714 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008715 char_u *rfname = fname;
8716
8717 /* Ignore "g:" before a function name. */
8718 if (fname[0] == 'g' && fname[1] == ':')
8719 rfname = fname + 2;
8720
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008721 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8722 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 error = ERROR_UNKNOWN;
8724
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008725 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {
8727 /*
8728 * User defined function.
8729 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008730 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008733 /* Trigger FuncUndefined event, may load the function. */
8734 if (fp == NULL
8735 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008737 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008739 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008740 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 }
8742#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008743 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008744 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008745 {
8746 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008747 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008748 }
8749
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 if (fp != NULL)
8751 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008754 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008756 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008758 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008759 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 else
8761 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008762 int did_save_redo = FALSE;
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 /*
8765 * Call the user function.
8766 * Save and restore search patterns, script variables and
8767 * redo buffer.
8768 */
8769 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008770#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008771 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008772#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008773 {
8774 saveRedobuff();
8775 did_save_redo = TRUE;
8776 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008779 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008780 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8781 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8782 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008783 /* Function was unreferenced while being used, free it
8784 * now. */
8785 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008786 if (did_save_redo)
8787 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 restore_search_patterns();
8789 error = ERROR_NONE;
8790 }
8791 }
8792 }
8793 else
8794 {
8795 /*
8796 * Find the function name in the table, call its implementation.
8797 */
8798 i = find_internal_func(fname);
8799 if (i >= 0)
8800 {
8801 if (argcount < functions[i].f_min_argc)
8802 error = ERROR_TOOFEW;
8803 else if (argcount > functions[i].f_max_argc)
8804 error = ERROR_TOOMANY;
8805 else
8806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008807 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 error = ERROR_NONE;
8810 }
8811 }
8812 }
8813 /*
8814 * The function call (or "FuncUndefined" autocommand sequence) might
8815 * have been aborted by an error, an interrupt, or an explicitly thrown
8816 * exception that has not been caught so far. This situation can be
8817 * tested for by calling aborting(). For an error in an internal
8818 * function or for the "E132" error in call_user_func(), however, the
8819 * throw point at which the "force_abort" flag (temporarily reset by
8820 * emsg()) is normally updated has not been reached yet. We need to
8821 * update that flag first to make aborting() reliable.
8822 */
8823 update_force_abort();
8824 }
8825 if (error == ERROR_NONE)
8826 ret = OK;
8827
8828 /*
8829 * Report an error unless the argument evaluation or function call has been
8830 * cancelled due to an aborting error, an interrupt, or an exception.
8831 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008832 if (!aborting())
8833 {
8834 switch (error)
8835 {
8836 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008837 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008838 break;
8839 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008840 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008841 break;
8842 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008843 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008844 name);
8845 break;
8846 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008847 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008848 name);
8849 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008851 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 name);
8853 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008854 }
8855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 if (fname != name && fname != fname_buf)
8858 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008859 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
8861 return ret;
8862}
8863
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008864/*
8865 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008866 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008867 */
8868 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008869emsg_funcname(ermsg, name)
8870 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008871 char_u *name;
8872{
8873 char_u *p;
8874
8875 if (*name == K_SPECIAL)
8876 p = concat_str((char_u *)"<SNR>", name + 3);
8877 else
8878 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008879 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008880 if (p != name)
8881 vim_free(p);
8882}
8883
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008884/*
8885 * Return TRUE for a non-zero Number and a non-empty String.
8886 */
8887 static int
8888non_zero_arg(argvars)
8889 typval_T *argvars;
8890{
8891 return ((argvars[0].v_type == VAR_NUMBER
8892 && argvars[0].vval.v_number != 0)
8893 || (argvars[0].v_type == VAR_STRING
8894 && argvars[0].vval.v_string != NULL
8895 && *argvars[0].vval.v_string != NUL));
8896}
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898/*********************************************
8899 * Implementation of the built-in functions
8900 */
8901
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008902#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008903static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8904
8905/*
8906 * Get the float value of "argvars[0]" into "f".
8907 * Returns FAIL when the argument is not a Number or Float.
8908 */
8909 static int
8910get_float_arg(argvars, f)
8911 typval_T *argvars;
8912 float_T *f;
8913{
8914 if (argvars[0].v_type == VAR_FLOAT)
8915 {
8916 *f = argvars[0].vval.v_float;
8917 return OK;
8918 }
8919 if (argvars[0].v_type == VAR_NUMBER)
8920 {
8921 *f = (float_T)argvars[0].vval.v_number;
8922 return OK;
8923 }
8924 EMSG(_("E808: Number or Float required"));
8925 return FAIL;
8926}
8927
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008928/*
8929 * "abs(expr)" function
8930 */
8931 static void
8932f_abs(argvars, rettv)
8933 typval_T *argvars;
8934 typval_T *rettv;
8935{
8936 if (argvars[0].v_type == VAR_FLOAT)
8937 {
8938 rettv->v_type = VAR_FLOAT;
8939 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8940 }
8941 else
8942 {
8943 varnumber_T n;
8944 int error = FALSE;
8945
8946 n = get_tv_number_chk(&argvars[0], &error);
8947 if (error)
8948 rettv->vval.v_number = -1;
8949 else if (n > 0)
8950 rettv->vval.v_number = n;
8951 else
8952 rettv->vval.v_number = -n;
8953 }
8954}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008955
8956/*
8957 * "acos()" function
8958 */
8959 static void
8960f_acos(argvars, rettv)
8961 typval_T *argvars;
8962 typval_T *rettv;
8963{
8964 float_T f;
8965
8966 rettv->v_type = VAR_FLOAT;
8967 if (get_float_arg(argvars, &f) == OK)
8968 rettv->vval.v_float = acos(f);
8969 else
8970 rettv->vval.v_float = 0.0;
8971}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008972#endif
8973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008975 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 */
8977 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 typval_T *argvars;
8980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
Bram Moolenaar33570922005-01-25 22:26:29 +00008982 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008985 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008987 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008988 && !tv_check_lock(l->lv_lock,
8989 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008990 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008992 }
8993 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 EMSG(_(e_listreq));
8995}
8996
8997/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008998 * "alloc_fail(id, countdown, repeat)" function
8999 */
9000 static void
9001f_alloc_fail(argvars, rettv)
9002 typval_T *argvars;
9003 typval_T *rettv UNUSED;
9004{
9005 if (argvars[0].v_type != VAR_NUMBER
9006 || argvars[0].vval.v_number <= 0
9007 || argvars[1].v_type != VAR_NUMBER
9008 || argvars[1].vval.v_number < 0
9009 || argvars[2].v_type != VAR_NUMBER)
9010 EMSG(_(e_invarg));
9011 else
9012 {
9013 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009014 if (alloc_fail_id >= aid_last)
9015 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009016 alloc_fail_countdown = argvars[1].vval.v_number;
9017 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009018 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009019 }
9020}
9021
9022/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009023 * "and(expr, expr)" function
9024 */
9025 static void
9026f_and(argvars, rettv)
9027 typval_T *argvars;
9028 typval_T *rettv;
9029{
9030 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9031 & get_tv_number_chk(&argvars[1], NULL);
9032}
9033
9034/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009035 * "append(lnum, string/list)" function
9036 */
9037 static void
9038f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 typval_T *argvars;
9040 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009041{
9042 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 list_T *l = NULL;
9045 listitem_T *li = NULL;
9046 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009047 long added = 0;
9048
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009049 /* When coming here from Insert mode, sync undo, so that this can be
9050 * undone separately from what was previously inserted. */
9051 if (u_sync_once == 2)
9052 {
9053 u_sync_once = 1; /* notify that u_sync() was called */
9054 u_sync(TRUE);
9055 }
9056
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 lnum = get_tv_lnum(argvars);
9058 if (lnum >= 0
9059 && lnum <= curbuf->b_ml.ml_line_count
9060 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009061 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009062 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 l = argvars[1].vval.v_list;
9065 if (l == NULL)
9066 return;
9067 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009069 for (;;)
9070 {
9071 if (l == NULL)
9072 tv = &argvars[1]; /* append a string */
9073 else if (li == NULL)
9074 break; /* end of list */
9075 else
9076 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 line = get_tv_string_chk(tv);
9078 if (line == NULL) /* type error */
9079 {
9080 rettv->vval.v_number = 1; /* Failed */
9081 break;
9082 }
9083 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009084 ++added;
9085 if (l == NULL)
9086 break;
9087 li = li->li_next;
9088 }
9089
9090 appended_lines_mark(lnum, added);
9091 if (curwin->w_cursor.lnum > lnum)
9092 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 else
9095 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096}
9097
9098/*
9099 * "argc()" function
9100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108
9109/*
9110 * "argidx()" function
9111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
9120/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009121 * "arglistid()" function
9122 */
9123 static void
9124f_arglistid(argvars, rettv)
9125 typval_T *argvars UNUSED;
9126 typval_T *rettv;
9127{
9128 win_T *wp;
9129 tabpage_T *tp = NULL;
9130 long n;
9131
9132 rettv->vval.v_number = -1;
9133 if (argvars[0].v_type != VAR_UNKNOWN)
9134 {
9135 if (argvars[1].v_type != VAR_UNKNOWN)
9136 {
9137 n = get_tv_number(&argvars[1]);
9138 if (n >= 0)
9139 tp = find_tabpage(n);
9140 }
9141 else
9142 tp = curtab;
9143
9144 if (tp != NULL)
9145 {
9146 wp = find_win_by_nr(&argvars[0], tp);
9147 if (wp != NULL)
9148 rettv->vval.v_number = wp->w_alist->id;
9149 }
9150 }
9151 else
9152 rettv->vval.v_number = curwin->w_alist->id;
9153}
9154
9155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 * "argv(nr)" function
9157 */
9158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009160 typval_T *argvars;
9161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
9163 int idx;
9164
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009165 if (argvars[0].v_type != VAR_UNKNOWN)
9166 {
9167 idx = get_tv_number_chk(&argvars[0], NULL);
9168 if (idx >= 0 && idx < ARGCOUNT)
9169 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9170 else
9171 rettv->vval.v_string = NULL;
9172 rettv->v_type = VAR_STRING;
9173 }
9174 else if (rettv_list_alloc(rettv) == OK)
9175 for (idx = 0; idx < ARGCOUNT; ++idx)
9176 list_append_string(rettv->vval.v_list,
9177 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178}
9179
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009180static void prepare_assert_error __ARGS((garray_T*gap));
9181static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9182static void assert_error __ARGS((garray_T *gap));
9183static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9184
9185/*
9186 * Prepare "gap" for an assert error and add the sourcing position.
9187 */
9188 static void
9189prepare_assert_error(gap)
9190 garray_T *gap;
9191{
9192 char buf[NUMBUFLEN];
9193
9194 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009195 if (sourcing_name != NULL)
9196 {
9197 ga_concat(gap, sourcing_name);
9198 if (sourcing_lnum > 0)
9199 ga_concat(gap, (char_u *)" ");
9200 }
9201 if (sourcing_lnum > 0)
9202 {
9203 sprintf(buf, "line %ld", (long)sourcing_lnum);
9204 ga_concat(gap, (char_u *)buf);
9205 }
9206 if (sourcing_name != NULL || sourcing_lnum > 0)
9207 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009208}
9209
9210/*
9211 * Fill "gap" with information about an assert error.
9212 */
9213 static void
9214fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9215 garray_T *gap;
9216 typval_T *opt_msg_tv;
9217 char_u *exp_str;
9218 typval_T *exp_tv;
9219 typval_T *got_tv;
9220{
9221 char_u numbuf[NUMBUFLEN];
9222 char_u *tofree;
9223
9224 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9225 {
9226 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9227 vim_free(tofree);
9228 }
9229 else
9230 {
9231 ga_concat(gap, (char_u *)"Expected ");
9232 if (exp_str == NULL)
9233 {
9234 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9235 vim_free(tofree);
9236 }
9237 else
9238 ga_concat(gap, exp_str);
9239 ga_concat(gap, (char_u *)" but got ");
9240 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9241 vim_free(tofree);
9242 }
9243}
Bram Moolenaar43345542015-11-29 17:35:35 +01009244
9245/*
9246 * Add an assert error to v:errors.
9247 */
9248 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009249assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009250 garray_T *gap;
9251{
9252 struct vimvar *vp = &vimvars[VV_ERRORS];
9253
9254 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9255 /* Make sure v:errors is a list. */
9256 set_vim_var_list(VV_ERRORS, list_alloc());
9257 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9258}
9259
Bram Moolenaar43345542015-11-29 17:35:35 +01009260/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009261 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009262 */
9263 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009264f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009265 typval_T *argvars;
9266 typval_T *rettv UNUSED;
9267{
9268 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009269
9270 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9271 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 prepare_assert_error(&ga);
9273 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9274 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009275 ga_clear(&ga);
9276 }
9277}
9278
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009279/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009280 * "assert_exception(string[, msg])" function
9281 */
9282 static void
9283f_assert_exception(argvars, rettv)
9284 typval_T *argvars;
9285 typval_T *rettv UNUSED;
9286{
9287 garray_T ga;
9288 char *error;
9289
9290 error = (char *)get_tv_string_chk(&argvars[0]);
9291 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9292 {
9293 prepare_assert_error(&ga);
9294 ga_concat(&ga, (char_u *)"v:exception is not set");
9295 assert_error(&ga);
9296 ga_clear(&ga);
9297 }
9298 else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9299 {
9300 prepare_assert_error(&ga);
9301 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9302 &vimvars[VV_EXCEPTION].vv_tv);
9303 assert_error(&ga);
9304 ga_clear(&ga);
9305 }
9306}
9307
9308/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009309 * "assert_fails(cmd [, error])" function
9310 */
9311 static void
9312f_assert_fails(argvars, rettv)
9313 typval_T *argvars;
9314 typval_T *rettv UNUSED;
9315{
9316 char_u *cmd = get_tv_string_chk(&argvars[0]);
9317 garray_T ga;
9318
9319 called_emsg = FALSE;
9320 suppress_errthrow = TRUE;
9321 emsg_silent = TRUE;
9322 do_cmdline_cmd(cmd);
9323 if (!called_emsg)
9324 {
9325 prepare_assert_error(&ga);
9326 ga_concat(&ga, (char_u *)"command did not fail: ");
9327 ga_concat(&ga, cmd);
9328 assert_error(&ga);
9329 ga_clear(&ga);
9330 }
9331 else if (argvars[1].v_type != VAR_UNKNOWN)
9332 {
9333 char_u buf[NUMBUFLEN];
9334 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9335
9336 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9337 {
9338 prepare_assert_error(&ga);
9339 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9340 &vimvars[VV_ERRMSG].vv_tv);
9341 assert_error(&ga);
9342 ga_clear(&ga);
9343 }
9344 }
9345
9346 called_emsg = FALSE;
9347 suppress_errthrow = FALSE;
9348 emsg_silent = FALSE;
9349 emsg_on_display = FALSE;
9350 set_vim_var_string(VV_ERRMSG, NULL, 0);
9351}
9352
9353/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009354 * Common for assert_true() and assert_false().
9355 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009356 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009357assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009358 typval_T *argvars;
9359 int isTrue;
9360{
9361 int error = FALSE;
9362 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009363
9364 if (argvars[0].v_type != VAR_NUMBER
9365 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9366 || error)
9367 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368 prepare_assert_error(&ga);
9369 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009370 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009371 NULL, &argvars[0]);
9372 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009373 ga_clear(&ga);
9374 }
9375}
9376
9377/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009378 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009379 */
9380 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009381f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009382 typval_T *argvars;
9383 typval_T *rettv UNUSED;
9384{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009385 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009386}
9387
9388/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009389 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009390 */
9391 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009392f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009393 typval_T *argvars;
9394 typval_T *rettv UNUSED;
9395{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009396 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009397}
9398
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009399#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009400/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009401 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009403 static void
9404f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009405 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009406 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009407{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009408 float_T f;
9409
9410 rettv->v_type = VAR_FLOAT;
9411 if (get_float_arg(argvars, &f) == OK)
9412 rettv->vval.v_float = asin(f);
9413 else
9414 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009415}
9416
9417/*
9418 * "atan()" function
9419 */
9420 static void
9421f_atan(argvars, rettv)
9422 typval_T *argvars;
9423 typval_T *rettv;
9424{
9425 float_T f;
9426
9427 rettv->v_type = VAR_FLOAT;
9428 if (get_float_arg(argvars, &f) == OK)
9429 rettv->vval.v_float = atan(f);
9430 else
9431 rettv->vval.v_float = 0.0;
9432}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009433
9434/*
9435 * "atan2()" function
9436 */
9437 static void
9438f_atan2(argvars, rettv)
9439 typval_T *argvars;
9440 typval_T *rettv;
9441{
9442 float_T fx, fy;
9443
9444 rettv->v_type = VAR_FLOAT;
9445 if (get_float_arg(argvars, &fx) == OK
9446 && get_float_arg(&argvars[1], &fy) == OK)
9447 rettv->vval.v_float = atan2(fx, fy);
9448 else
9449 rettv->vval.v_float = 0.0;
9450}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009451#endif
9452
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453/*
9454 * "browse(save, title, initdir, default)" function
9455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
9461#ifdef FEAT_BROWSE
9462 int save;
9463 char_u *title;
9464 char_u *initdir;
9465 char_u *defname;
9466 char_u buf[NUMBUFLEN];
9467 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009468 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 save = get_tv_number_chk(&argvars[0], &error);
9471 title = get_tv_string_chk(&argvars[1]);
9472 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9473 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 if (error || title == NULL || initdir == NULL || defname == NULL)
9476 rettv->vval.v_string = NULL;
9477 else
9478 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009479 do_browse(save ? BROWSE_SAVE : 0,
9480 title, defname, NULL, initdir, NULL, curbuf);
9481#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485}
9486
9487/*
9488 * "browsedir(title, initdir)" function
9489 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009493 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009494{
9495#ifdef FEAT_BROWSE
9496 char_u *title;
9497 char_u *initdir;
9498 char_u buf[NUMBUFLEN];
9499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009500 title = get_tv_string_chk(&argvars[0]);
9501 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 if (title == NULL || initdir == NULL)
9504 rettv->vval.v_string = NULL;
9505 else
9506 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009507 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512}
9513
Bram Moolenaar33570922005-01-25 22:26:29 +00009514static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009515
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516/*
9517 * Find a buffer by number or exact name.
9518 */
9519 static buf_T *
9520find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009521 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
9523 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 if (avar->v_type == VAR_NUMBER)
9526 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009527 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009530 if (buf == NULL)
9531 {
9532 /* No full path name match, try a match with a URL or a "nofile"
9533 * buffer, these don't use the full path. */
9534 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9535 if (buf->b_fname != NULL
9536 && (path_with_url(buf->b_fname)
9537#ifdef FEAT_QUICKFIX
9538 || bt_nofile(buf)
9539#endif
9540 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009542 break;
9543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 }
9545 return buf;
9546}
9547
9548/*
9549 * "bufexists(expr)" function
9550 */
9551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557}
9558
9559/*
9560 * "buflisted(expr)" function
9561 */
9562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009564 typval_T *argvars;
9565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 buf_T *buf;
9568
9569 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
9573/*
9574 * "bufloaded(expr)" function
9575 */
9576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009578 typval_T *argvars;
9579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 buf_T *buf;
9582
9583 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009587static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589/*
9590 * Get buffer by number or pattern.
9591 */
9592 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009593get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009595 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 int save_magic;
9599 char_u *save_cpo;
9600 buf_T *buf;
9601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 if (tv->v_type == VAR_NUMBER)
9603 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009604 if (tv->v_type != VAR_STRING)
9605 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 if (name == NULL || *name == NUL)
9607 return curbuf;
9608 if (name[0] == '$' && name[1] == NUL)
9609 return lastbuf;
9610
9611 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9612 save_magic = p_magic;
9613 p_magic = TRUE;
9614 save_cpo = p_cpo;
9615 p_cpo = (char_u *)"";
9616
9617 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009618 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619
9620 p_magic = save_magic;
9621 p_cpo = save_cpo;
9622
9623 /* If not found, try expanding the name, like done for bufexists(). */
9624 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626
9627 return buf;
9628}
9629
9630/*
9631 * "bufname(expr)" function
9632 */
9633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638 buf_T *buf;
9639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009642 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 --emsg_off;
9649}
9650
9651/*
9652 * "bufnr(expr)" function
9653 */
9654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 typval_T *argvars;
9657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009660 int error = FALSE;
9661 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009665 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009666 --emsg_off;
9667
9668 /* If the buffer isn't found and the second argument is not zero create a
9669 * new buffer. */
9670 if (buf == NULL
9671 && argvars[1].v_type != VAR_UNKNOWN
9672 && get_tv_number_chk(&argvars[1], &error) != 0
9673 && !error
9674 && (name = get_tv_string_chk(&argvars[0])) != NULL
9675 && !error)
9676 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9677
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682}
9683
9684/*
9685 * "bufwinnr(nr)" function
9686 */
9687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 typval_T *argvars;
9690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
9692#ifdef FEAT_WINDOWS
9693 win_T *wp;
9694 int winnr = 0;
9695#endif
9696 buf_T *buf;
9697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009698 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009700 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#ifdef FEAT_WINDOWS
9702 for (wp = firstwin; wp; wp = wp->w_next)
9703 {
9704 ++winnr;
9705 if (wp->w_buffer == buf)
9706 break;
9707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#endif
9712 --emsg_off;
9713}
9714
9715/*
9716 * "byte2line(byte)" function
9717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009720 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725#else
9726 long boff = 0;
9727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009728 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 (linenr_T)0, &boff);
9734#endif
9735}
9736
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009737 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009738byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009741 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009742{
9743#ifdef FEAT_MBYTE
9744 char_u *t;
9745#endif
9746 char_u *str;
9747 long idx;
9748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 str = get_tv_string_chk(&argvars[0]);
9750 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009753 return;
9754
9755#ifdef FEAT_MBYTE
9756 t = str;
9757 for ( ; idx > 0; idx--)
9758 {
9759 if (*t == NUL) /* EOL reached */
9760 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009761 if (enc_utf8 && comp)
9762 t += utf_ptr2len(t);
9763 else
9764 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009765 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009766 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009767#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009768 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009770#endif
9771}
9772
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009773/*
9774 * "byteidx()" function
9775 */
9776 static void
9777f_byteidx(argvars, rettv)
9778 typval_T *argvars;
9779 typval_T *rettv;
9780{
9781 byteidx(argvars, rettv, FALSE);
9782}
9783
9784/*
9785 * "byteidxcomp()" function
9786 */
9787 static void
9788f_byteidxcomp(argvars, rettv)
9789 typval_T *argvars;
9790 typval_T *rettv;
9791{
9792 byteidx(argvars, rettv, TRUE);
9793}
9794
Bram Moolenaardb913952012-06-29 12:54:53 +02009795 int
9796func_call(name, args, selfdict, rettv)
9797 char_u *name;
9798 typval_T *args;
9799 dict_T *selfdict;
9800 typval_T *rettv;
9801{
9802 listitem_T *item;
9803 typval_T argv[MAX_FUNC_ARGS + 1];
9804 int argc = 0;
9805 int dummy;
9806 int r = 0;
9807
9808 for (item = args->vval.v_list->lv_first; item != NULL;
9809 item = item->li_next)
9810 {
9811 if (argc == MAX_FUNC_ARGS)
9812 {
9813 EMSG(_("E699: Too many arguments"));
9814 break;
9815 }
9816 /* Make a copy of each argument. This is needed to be able to set
9817 * v_lock to VAR_FIXED in the copy without changing the original list.
9818 */
9819 copy_tv(&item->li_tv, &argv[argc++]);
9820 }
9821
9822 if (item == NULL)
9823 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9824 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9825 &dummy, TRUE, selfdict);
9826
9827 /* Free the arguments. */
9828 while (argc > 0)
9829 clear_tv(&argv[--argc]);
9830
9831 return r;
9832}
9833
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009834/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009835 * "call(func, arglist)" function
9836 */
9837 static void
9838f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009839 typval_T *argvars;
9840 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009841{
9842 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009844
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009845 if (argvars[1].v_type != VAR_LIST)
9846 {
9847 EMSG(_(e_listreq));
9848 return;
9849 }
9850 if (argvars[1].vval.v_list == NULL)
9851 return;
9852
9853 if (argvars[0].v_type == VAR_FUNC)
9854 func = argvars[0].vval.v_string;
9855 else
9856 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 if (*func == NUL)
9858 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009859
Bram Moolenaare9a41262005-01-15 22:18:47 +00009860 if (argvars[2].v_type != VAR_UNKNOWN)
9861 {
9862 if (argvars[2].v_type != VAR_DICT)
9863 {
9864 EMSG(_(e_dictreq));
9865 return;
9866 }
9867 selfdict = argvars[2].vval.v_dict;
9868 }
9869
Bram Moolenaardb913952012-06-29 12:54:53 +02009870 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009871}
9872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009873#ifdef FEAT_FLOAT
9874/*
9875 * "ceil({float})" function
9876 */
9877 static void
9878f_ceil(argvars, rettv)
9879 typval_T *argvars;
9880 typval_T *rettv;
9881{
9882 float_T f;
9883
9884 rettv->v_type = VAR_FLOAT;
9885 if (get_float_arg(argvars, &f) == OK)
9886 rettv->vval.v_float = ceil(f);
9887 else
9888 rettv->vval.v_float = 0.0;
9889}
9890#endif
9891
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009893 * "changenr()" function
9894 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009895 static void
9896f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009897 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009898 typval_T *rettv;
9899{
9900 rettv->vval.v_number = curbuf->b_u_seq_cur;
9901}
9902
9903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 * "char2nr(string)" function
9905 */
9906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911#ifdef FEAT_MBYTE
9912 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009913 {
9914 int utf8 = 0;
9915
9916 if (argvars[1].v_type != VAR_UNKNOWN)
9917 utf8 = get_tv_number_chk(&argvars[1], NULL);
9918
9919 if (utf8)
9920 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9921 else
9922 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 else
9925#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927}
9928
9929/*
9930 * "cindent(lnum)" function
9931 */
9932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936{
9937#ifdef FEAT_CINDENT
9938 pos_T pos;
9939 linenr_T lnum;
9940
9941 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9944 {
9945 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 curwin->w_cursor = pos;
9948 }
9949 else
9950#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952}
9953
9954/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009955 * "clearmatches()" function
9956 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009957 static void
9958f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009959 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009960 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009961{
9962#ifdef FEAT_SEARCH_EXTRA
9963 clear_matches(curwin);
9964#endif
9965}
9966
9967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 * "col(string)" function
9969 */
9970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 typval_T *argvars;
9973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974{
9975 colnr_T col = 0;
9976 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009977 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009979 fp = var2fpos(&argvars[0], FALSE, &fnum);
9980 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 {
9982 if (fp->col == MAXCOL)
9983 {
9984 /* '> can be MAXCOL, get the length of the line then */
9985 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009986 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 else
9988 col = MAXCOL;
9989 }
9990 else
9991 {
9992 col = fp->col + 1;
9993#ifdef FEAT_VIRTUALEDIT
9994 /* col(".") when the cursor is on the NUL at the end of the line
9995 * because of "coladd" can be seen as an extra column. */
9996 if (virtual_active() && fp == &curwin->w_cursor)
9997 {
9998 char_u *p = ml_get_cursor();
9999
10000 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10001 curwin->w_virtcol - curwin->w_cursor.coladd))
10002 {
10003# ifdef FEAT_MBYTE
10004 int l;
10005
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010006 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 col += l;
10008# else
10009 if (*p != NUL && p[1] == NUL)
10010 ++col;
10011# endif
10012 }
10013 }
10014#endif
10015 }
10016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018}
10019
Bram Moolenaar572cb562005-08-05 21:35:02 +000010020#if defined(FEAT_INS_EXPAND)
10021/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010022 * "complete()" function
10023 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010024 static void
10025f_complete(argvars, rettv)
10026 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010027 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010028{
10029 int startcol;
10030
10031 if ((State & INSERT) == 0)
10032 {
10033 EMSG(_("E785: complete() can only be used in Insert mode"));
10034 return;
10035 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010036
10037 /* Check for undo allowed here, because if something was already inserted
10038 * the line was already saved for undo and this check isn't done. */
10039 if (!undo_allowed())
10040 return;
10041
Bram Moolenaarade00832006-03-10 21:46:58 +000010042 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10043 {
10044 EMSG(_(e_invarg));
10045 return;
10046 }
10047
10048 startcol = get_tv_number_chk(&argvars[0], NULL);
10049 if (startcol <= 0)
10050 return;
10051
10052 set_completion(startcol - 1, argvars[1].vval.v_list);
10053}
10054
10055/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010056 * "complete_add()" function
10057 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010058 static void
10059f_complete_add(argvars, rettv)
10060 typval_T *argvars;
10061 typval_T *rettv;
10062{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010063 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010064}
10065
10066/*
10067 * "complete_check()" function
10068 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010069 static void
10070f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010071 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010072 typval_T *rettv;
10073{
10074 int saved = RedrawingDisabled;
10075
10076 RedrawingDisabled = 0;
10077 ins_compl_check_keys(0);
10078 rettv->vval.v_number = compl_interrupted;
10079 RedrawingDisabled = saved;
10080}
10081#endif
10082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083/*
10084 * "confirm(message, buttons[, default [, type]])" function
10085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010088 typval_T *argvars UNUSED;
10089 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10092 char_u *message;
10093 char_u *buttons = NULL;
10094 char_u buf[NUMBUFLEN];
10095 char_u buf2[NUMBUFLEN];
10096 int def = 1;
10097 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 char_u *typestr;
10099 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 message = get_tv_string_chk(&argvars[0]);
10102 if (message == NULL)
10103 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010104 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10107 if (buttons == NULL)
10108 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010112 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10115 if (typestr == NULL)
10116 error = TRUE;
10117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 switch (TOUPPER_ASC(*typestr))
10120 {
10121 case 'E': type = VIM_ERROR; break;
10122 case 'Q': type = VIM_QUESTION; break;
10123 case 'I': type = VIM_INFO; break;
10124 case 'W': type = VIM_WARNING; break;
10125 case 'G': type = VIM_GENERIC; break;
10126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 }
10128 }
10129 }
10130 }
10131
10132 if (buttons == NULL || *buttons == NUL)
10133 buttons = (char_u *)_("&Ok");
10134
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010135 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010137 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#endif
10139}
10140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010141/*
10142 * "copy()" function
10143 */
10144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010146 typval_T *argvars;
10147 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010148{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010149 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010150}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010152#ifdef FEAT_FLOAT
10153/*
10154 * "cos()" function
10155 */
10156 static void
10157f_cos(argvars, rettv)
10158 typval_T *argvars;
10159 typval_T *rettv;
10160{
10161 float_T f;
10162
10163 rettv->v_type = VAR_FLOAT;
10164 if (get_float_arg(argvars, &f) == OK)
10165 rettv->vval.v_float = cos(f);
10166 else
10167 rettv->vval.v_float = 0.0;
10168}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010169
10170/*
10171 * "cosh()" function
10172 */
10173 static void
10174f_cosh(argvars, rettv)
10175 typval_T *argvars;
10176 typval_T *rettv;
10177{
10178 float_T f;
10179
10180 rettv->v_type = VAR_FLOAT;
10181 if (get_float_arg(argvars, &f) == OK)
10182 rettv->vval.v_float = cosh(f);
10183 else
10184 rettv->vval.v_float = 0.0;
10185}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010186#endif
10187
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010189 * "count()" function
10190 */
10191 static void
10192f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 typval_T *argvars;
10194 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010195{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010196 long n = 0;
10197 int ic = FALSE;
10198
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 listitem_T *li;
10202 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010204
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 if ((l = argvars[0].vval.v_list) != NULL)
10206 {
10207 li = l->lv_first;
10208 if (argvars[2].v_type != VAR_UNKNOWN)
10209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 int error = FALSE;
10211
10212 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010213 if (argvars[3].v_type != VAR_UNKNOWN)
10214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 idx = get_tv_number_chk(&argvars[3], &error);
10216 if (!error)
10217 {
10218 li = list_find(l, idx);
10219 if (li == NULL)
10220 EMSGN(_(e_listidx), idx);
10221 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 if (error)
10224 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 }
10226
10227 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010228 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 ++n;
10230 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 else if (argvars[0].v_type == VAR_DICT)
10233 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 int todo;
10235 dict_T *d;
10236 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010238 if ((d = argvars[0].vval.v_dict) != NULL)
10239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 int error = FALSE;
10241
Bram Moolenaare9a41262005-01-15 22:18:47 +000010242 if (argvars[2].v_type != VAR_UNKNOWN)
10243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 if (argvars[3].v_type != VAR_UNKNOWN)
10246 EMSG(_(e_invarg));
10247 }
10248
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010249 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 {
10252 if (!HASHITEM_EMPTY(hi))
10253 {
10254 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010255 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010256 ++n;
10257 }
10258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010259 }
10260 }
10261 else
10262 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010263 rettv->vval.v_number = n;
10264}
10265
10266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10268 *
10269 * Checks the existence of a cscope connection.
10270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010272f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010273 typval_T *argvars UNUSED;
10274 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275{
10276#ifdef FEAT_CSCOPE
10277 int num = 0;
10278 char_u *dbpath = NULL;
10279 char_u *prepend = NULL;
10280 char_u buf[NUMBUFLEN];
10281
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010282 if (argvars[0].v_type != VAR_UNKNOWN
10283 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 num = (int)get_tv_number(&argvars[0]);
10286 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010287 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 }
10290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292#endif
10293}
10294
10295/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010296 * "cursor(lnum, col)" function, or
10297 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010299 * Moves the cursor to the specified line and column.
10300 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010304 typval_T *argvars;
10305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306{
10307 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010308#ifdef FEAT_VIRTUALEDIT
10309 long coladd = 0;
10310#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010311 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010313 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010314 if (argvars[1].v_type == VAR_UNKNOWN)
10315 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010316 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010317 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010318
Bram Moolenaar493c1782014-05-28 14:34:46 +020010319 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010320 {
10321 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010322 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010323 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010324 line = pos.lnum;
10325 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010326#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010327 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010328#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010329 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010330 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010331 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010332 set_curswant = FALSE;
10333 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010334 }
10335 else
10336 {
10337 line = get_tv_lnum(argvars);
10338 col = get_tv_number_chk(&argvars[1], NULL);
10339#ifdef FEAT_VIRTUALEDIT
10340 if (argvars[2].v_type != VAR_UNKNOWN)
10341 coladd = get_tv_number_chk(&argvars[2], NULL);
10342#endif
10343 }
10344 if (line < 0 || col < 0
10345#ifdef FEAT_VIRTUALEDIT
10346 || coladd < 0
10347#endif
10348 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010349 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 if (line > 0)
10351 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 if (col > 0)
10353 curwin->w_cursor.col = col - 1;
10354#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010355 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356#endif
10357
10358 /* Make sure the cursor is in a valid position. */
10359 check_cursor();
10360#ifdef FEAT_MBYTE
10361 /* Correct cursor for multi-byte character. */
10362 if (has_mbyte)
10363 mb_adjust_cursor();
10364#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010365
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010366 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010367 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368}
10369
10370/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 */
10373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 typval_T *argvars;
10376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010378 int noref = 0;
10379
10380 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010381 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010382 if (noref < 0 || noref > 1)
10383 EMSG(_(e_invarg));
10384 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010385 {
10386 current_copyID += COPYID_INC;
10387 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389}
10390
10391/*
10392 * "delete()" function
10393 */
10394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *argvars;
10397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
10399 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010402 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403}
10404
10405/*
10406 * "did_filetype()" function
10407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010410 typval_T *argvars UNUSED;
10411 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412{
10413#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415#endif
10416}
10417
10418/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010419 * "diff_filler()" function
10420 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010423 typval_T *argvars UNUSED;
10424 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010425{
10426#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010428#endif
10429}
10430
10431/*
10432 * "diff_hlID()" function
10433 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010436 typval_T *argvars UNUSED;
10437 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010438{
10439#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010441 static linenr_T prev_lnum = 0;
10442 static int changedtick = 0;
10443 static int fnum = 0;
10444 static int change_start = 0;
10445 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010446 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010447 int filler_lines;
10448 int col;
10449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 if (lnum < 0) /* ignore type error in {lnum} arg */
10451 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010452 if (lnum != prev_lnum
10453 || changedtick != curbuf->b_changedtick
10454 || fnum != curbuf->b_fnum)
10455 {
10456 /* New line, buffer, change: need to get the values. */
10457 filler_lines = diff_check(curwin, lnum);
10458 if (filler_lines < 0)
10459 {
10460 if (filler_lines == -1)
10461 {
10462 change_start = MAXCOL;
10463 change_end = -1;
10464 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10465 hlID = HLF_ADD; /* added line */
10466 else
10467 hlID = HLF_CHD; /* changed line */
10468 }
10469 else
10470 hlID = HLF_ADD; /* added line */
10471 }
10472 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010473 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010474 prev_lnum = lnum;
10475 changedtick = curbuf->b_changedtick;
10476 fnum = curbuf->b_fnum;
10477 }
10478
10479 if (hlID == HLF_CHD || hlID == HLF_TXD)
10480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010482 if (col >= change_start && col <= change_end)
10483 hlID = HLF_TXD; /* changed text */
10484 else
10485 hlID = HLF_CHD; /* changed line */
10486 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010487 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010488#endif
10489}
10490
10491/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010492 * "empty({expr})" function
10493 */
10494 static void
10495f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010498{
10499 int n;
10500
10501 switch (argvars[0].v_type)
10502 {
10503 case VAR_STRING:
10504 case VAR_FUNC:
10505 n = argvars[0].vval.v_string == NULL
10506 || *argvars[0].vval.v_string == NUL;
10507 break;
10508 case VAR_NUMBER:
10509 n = argvars[0].vval.v_number == 0;
10510 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010511#ifdef FEAT_FLOAT
10512 case VAR_FLOAT:
10513 n = argvars[0].vval.v_float == 0.0;
10514 break;
10515#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010516 case VAR_LIST:
10517 n = argvars[0].vval.v_list == NULL
10518 || argvars[0].vval.v_list->lv_first == NULL;
10519 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520 case VAR_DICT:
10521 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010524 default:
10525 EMSG2(_(e_intern2), "f_empty()");
10526 n = 0;
10527 }
10528
10529 rettv->vval.v_number = n;
10530}
10531
10532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 * "escape({string}, {chars})" function
10534 */
10535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540 char_u buf[NUMBUFLEN];
10541
Bram Moolenaar758711c2005-02-02 23:11:38 +000010542 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10543 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545}
10546
10547/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548 * "eval()" function
10549 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010550 static void
10551f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 typval_T *argvars;
10553 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010555 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 s = get_tv_string_chk(&argvars[0]);
10558 if (s != NULL)
10559 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560
Bram Moolenaar615b9972015-01-14 17:15:05 +010010561 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10563 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010564 if (p != NULL && !aborting())
10565 EMSG2(_(e_invexpr2), p);
10566 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010568 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570 else if (*s != NUL)
10571 EMSG(_(e_trailing));
10572}
10573
10574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 * "eventhandler()" function
10576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010579 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583}
10584
10585/*
10586 * "executable()" function
10587 */
10588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010590 typval_T *argvars;
10591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010593 char_u *name = get_tv_string(&argvars[0]);
10594
10595 /* Check in $PATH and also check directly if there is a directory name. */
10596 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10597 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010598}
10599
10600/*
10601 * "exepath()" function
10602 */
10603 static void
10604f_exepath(argvars, rettv)
10605 typval_T *argvars;
10606 typval_T *rettv;
10607{
10608 char_u *p = NULL;
10609
Bram Moolenaarb5971142015-03-21 17:32:19 +010010610 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010611 rettv->v_type = VAR_STRING;
10612 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613}
10614
10615/*
10616 * "exists()" function
10617 */
10618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010620 typval_T *argvars;
10621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 char_u *p;
10624 char_u *name;
10625 int n = FALSE;
10626 int len = 0;
10627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 if (*p == '$') /* environment variable */
10630 {
10631 /* first try "normal" environment variables (fast) */
10632 if (mch_getenv(p + 1) != NULL)
10633 n = TRUE;
10634 else
10635 {
10636 /* try expanding things like $VIM and ${HOME} */
10637 p = expand_env_save(p);
10638 if (p != NULL && *p != '$')
10639 n = TRUE;
10640 vim_free(p);
10641 }
10642 }
10643 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010646 if (*skipwhite(p) != NUL)
10647 n = FALSE; /* trailing garbage */
10648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 else if (*p == '*') /* internal or user defined function */
10650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010651 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 }
10653 else if (*p == ':')
10654 {
10655 n = cmd_exists(p + 1);
10656 }
10657 else if (*p == '#')
10658 {
10659#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010660 if (p[1] == '#')
10661 n = autocmd_supported(p + 2);
10662 else
10663 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664#endif
10665 }
10666 else /* internal variable */
10667 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010668 char_u *tofree;
10669 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010671 /* get_name_len() takes care of expanding curly braces */
10672 name = p;
10673 len = get_name_len(&p, &tofree, TRUE, FALSE);
10674 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010676 if (tofree != NULL)
10677 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010678 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010679 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010681 /* handle d.key, l[idx], f(expr) */
10682 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10683 if (n)
10684 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 }
10686 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010687 if (*p != NUL)
10688 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010690 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 }
10692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694}
10695
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010696#ifdef FEAT_FLOAT
10697/*
10698 * "exp()" function
10699 */
10700 static void
10701f_exp(argvars, rettv)
10702 typval_T *argvars;
10703 typval_T *rettv;
10704{
10705 float_T f;
10706
10707 rettv->v_type = VAR_FLOAT;
10708 if (get_float_arg(argvars, &f) == OK)
10709 rettv->vval.v_float = exp(f);
10710 else
10711 rettv->vval.v_float = 0.0;
10712}
10713#endif
10714
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715/*
10716 * "expand()" function
10717 */
10718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *argvars;
10721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
10723 char_u *s;
10724 int len;
10725 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010726 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010729 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010732 if (argvars[1].v_type != VAR_UNKNOWN
10733 && argvars[2].v_type != VAR_UNKNOWN
10734 && get_tv_number_chk(&argvars[2], &error)
10735 && !error)
10736 {
10737 rettv->v_type = VAR_LIST;
10738 rettv->vval.v_list = NULL;
10739 }
10740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 if (*s == '%' || *s == '#' || *s == '<')
10743 {
10744 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010745 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010747 if (rettv->v_type == VAR_LIST)
10748 {
10749 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10750 list_append_string(rettv->vval.v_list, result, -1);
10751 else
10752 vim_free(result);
10753 }
10754 else
10755 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 }
10757 else
10758 {
10759 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010760 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 if (argvars[1].v_type != VAR_UNKNOWN
10762 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010763 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 if (!error)
10765 {
10766 ExpandInit(&xpc);
10767 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010768 if (p_wic)
10769 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010770 if (rettv->v_type == VAR_STRING)
10771 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10772 options, WILD_ALL);
10773 else if (rettv_list_alloc(rettv) != FAIL)
10774 {
10775 int i;
10776
10777 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10778 for (i = 0; i < xpc.xp_numfiles; i++)
10779 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10780 ExpandCleanup(&xpc);
10781 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010782 }
10783 else
10784 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 }
10786}
10787
10788/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010789 * Go over all entries in "d2" and add them to "d1".
10790 * When "action" is "error" then a duplicate key is an error.
10791 * When "action" is "force" then a duplicate key is overwritten.
10792 * Otherwise duplicate keys are ignored ("action" is "keep").
10793 */
10794 void
10795dict_extend(d1, d2, action)
10796 dict_T *d1;
10797 dict_T *d2;
10798 char_u *action;
10799{
10800 dictitem_T *di1;
10801 hashitem_T *hi2;
10802 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010803 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010804
10805 todo = (int)d2->dv_hashtab.ht_used;
10806 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10807 {
10808 if (!HASHITEM_EMPTY(hi2))
10809 {
10810 --todo;
10811 di1 = dict_find(d1, hi2->hi_key, -1);
10812 if (d1->dv_scope != 0)
10813 {
10814 /* Disallow replacing a builtin function in l: and g:.
10815 * Check the key to be valid when adding to any
10816 * scope. */
10817 if (d1->dv_scope == VAR_DEF_SCOPE
10818 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10819 && var_check_func_name(hi2->hi_key,
10820 di1 == NULL))
10821 break;
10822 if (!valid_varname(hi2->hi_key))
10823 break;
10824 }
10825 if (di1 == NULL)
10826 {
10827 di1 = dictitem_copy(HI2DI(hi2));
10828 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10829 dictitem_free(di1);
10830 }
10831 else if (*action == 'e')
10832 {
10833 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10834 break;
10835 }
10836 else if (*action == 'f' && HI2DI(hi2) != di1)
10837 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010838 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10839 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010840 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010841 clear_tv(&di1->di_tv);
10842 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10843 }
10844 }
10845 }
10846}
10847
10848/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010849 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010851 */
10852 static void
10853f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *argvars;
10855 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010856{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010857 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010858
Bram Moolenaare9a41262005-01-15 22:18:47 +000010859 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010861 list_T *l1, *l2;
10862 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010863 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010864 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010865
Bram Moolenaare9a41262005-01-15 22:18:47 +000010866 l1 = argvars[0].vval.v_list;
10867 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010868 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010869 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 {
10871 if (argvars[2].v_type != VAR_UNKNOWN)
10872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 before = get_tv_number_chk(&argvars[2], &error);
10874 if (error)
10875 return; /* type error; errmsg already given */
10876
Bram Moolenaar758711c2005-02-02 23:11:38 +000010877 if (before == l1->lv_len)
10878 item = NULL;
10879 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010880 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010881 item = list_find(l1, before);
10882 if (item == NULL)
10883 {
10884 EMSGN(_(e_listidx), before);
10885 return;
10886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010887 }
10888 }
10889 else
10890 item = NULL;
10891 list_extend(l1, l2, item);
10892
Bram Moolenaare9a41262005-01-15 22:18:47 +000010893 copy_tv(&argvars[0], rettv);
10894 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010895 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010896 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10897 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010898 dict_T *d1, *d2;
10899 char_u *action;
10900 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901
10902 d1 = argvars[0].vval.v_dict;
10903 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010904 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010905 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010906 {
10907 /* Check the third argument. */
10908 if (argvars[2].v_type != VAR_UNKNOWN)
10909 {
10910 static char *(av[]) = {"keep", "force", "error"};
10911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 action = get_tv_string_chk(&argvars[2]);
10913 if (action == NULL)
10914 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010915 for (i = 0; i < 3; ++i)
10916 if (STRCMP(action, av[i]) == 0)
10917 break;
10918 if (i == 3)
10919 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010920 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010921 return;
10922 }
10923 }
10924 else
10925 action = (char_u *)"force";
10926
Bram Moolenaara9922d62013-05-30 13:01:18 +020010927 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010928
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 copy_tv(&argvars[0], rettv);
10930 }
10931 }
10932 else
10933 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010934}
10935
10936/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010937 * "feedkeys()" function
10938 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010939 static void
10940f_feedkeys(argvars, rettv)
10941 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010942 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010943{
10944 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010945 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010946 char_u *keys, *flags;
10947 char_u nbuf[NUMBUFLEN];
10948 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010949 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010950
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010951 /* This is not allowed in the sandbox. If the commands would still be
10952 * executed in the sandbox it would be OK, but it probably happens later,
10953 * when "sandbox" is no longer set. */
10954 if (check_secure())
10955 return;
10956
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010957 keys = get_tv_string(&argvars[0]);
10958 if (*keys != NUL)
10959 {
10960 if (argvars[1].v_type != VAR_UNKNOWN)
10961 {
10962 flags = get_tv_string_buf(&argvars[1], nbuf);
10963 for ( ; *flags != NUL; ++flags)
10964 {
10965 switch (*flags)
10966 {
10967 case 'n': remap = FALSE; break;
10968 case 'm': remap = TRUE; break;
10969 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010970 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010971 }
10972 }
10973 }
10974
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010975 /* Need to escape K_SPECIAL and CSI before putting the string in the
10976 * typeahead buffer. */
10977 keys_esc = vim_strsave_escape_csi(keys);
10978 if (keys_esc != NULL)
10979 {
10980 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010981 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010982 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010983 if (vgetc_busy)
10984 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010985 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010986 }
10987}
10988
10989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 * "filereadable()" function
10991 */
10992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010993f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010994 typval_T *argvars;
10995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010997 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 char_u *p;
10999 int n;
11000
Bram Moolenaarc236c162008-07-13 17:41:49 +000011001#ifndef O_NONBLOCK
11002# define O_NONBLOCK 0
11003#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011005 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11006 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 {
11008 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011009 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 }
11011 else
11012 n = FALSE;
11013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015}
11016
11017/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011018 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 * rights to write into.
11020 */
11021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011023 typval_T *argvars;
11024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011026 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027}
11028
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011029static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011030
11031 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011032findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011035 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011036{
11037#ifdef FEAT_SEARCHPATH
11038 char_u *fname;
11039 char_u *fresult = NULL;
11040 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11041 char_u *p;
11042 char_u pathbuf[NUMBUFLEN];
11043 int count = 1;
11044 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011045 int error = FALSE;
11046#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011047
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011048 rettv->vval.v_string = NULL;
11049 rettv->v_type = VAR_STRING;
11050
11051#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011053
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11057 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011058 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 else
11060 {
11061 if (*p != NUL)
11062 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011065 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011067 }
11068
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011069 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11070 error = TRUE;
11071
11072 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 do
11075 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011076 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011077 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 fresult = find_file_in_path_option(first ? fname : NULL,
11079 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011080 0, first, path,
11081 find_what,
11082 curbuf->b_ffname,
11083 find_what == FINDFILE_DIR
11084 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011086
11087 if (fresult != NULL && rettv->v_type == VAR_LIST)
11088 list_append_string(rettv->vval.v_list, fresult, -1);
11089
11090 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011092
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011093 if (rettv->v_type == VAR_STRING)
11094 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011095#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011096}
11097
Bram Moolenaar33570922005-01-25 22:26:29 +000011098static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11099static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011100
11101/*
11102 * Implementation of map() and filter().
11103 */
11104 static void
11105filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011106 typval_T *argvars;
11107 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011108 int map;
11109{
11110 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 listitem_T *li, *nli;
11113 list_T *l = NULL;
11114 dictitem_T *di;
11115 hashtab_T *ht;
11116 hashitem_T *hi;
11117 dict_T *d = NULL;
11118 typval_T save_val;
11119 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011121 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011122 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011123 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011124 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011125 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011126 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011127
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011129 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011130 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011131 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 return;
11133 }
11134 else if (argvars[0].v_type == VAR_DICT)
11135 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011136 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011137 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 return;
11139 }
11140 else
11141 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011142 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143 return;
11144 }
11145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 expr = get_tv_string_buf_chk(&argvars[1], buf);
11147 /* On type errors, the preceding call has already displayed an error
11148 * message. Avoid a misleading error message for an empty string that
11149 * was not passed as argument. */
11150 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 prepare_vimvar(VV_VAL, &save_val);
11153 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011154
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011155 /* We reset "did_emsg" to be able to detect whether an error
11156 * occurred during evaluation of the expression. */
11157 save_did_emsg = did_emsg;
11158 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011159
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011160 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011161 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 vimvars[VV_KEY].vv_type = VAR_STRING;
11164
11165 ht = &d->dv_hashtab;
11166 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011167 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011168 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 if (!HASHITEM_EMPTY(hi))
11171 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011172 int r;
11173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 --todo;
11175 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011176 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011177 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11178 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 break;
11180 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011181 r = filter_map_one(&di->di_tv, expr, map, &rem);
11182 clear_tv(&vimvars[VV_KEY].vv_tv);
11183 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 break;
11185 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011186 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011187 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11188 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011189 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011191 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 }
11193 }
11194 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 }
11196 else
11197 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011198 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 for (li = l->lv_first; li != NULL; li = nli)
11201 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011202 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011203 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011205 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011206 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011207 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011208 break;
11209 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011211 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011212 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011213 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011214
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011215 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011217
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011218 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011219 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220
11221 copy_tv(&argvars[0], rettv);
11222}
11223
11224 static int
11225filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 char_u *expr;
11228 int map;
11229 int *remp;
11230{
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011233 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 s = expr;
11237 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011238 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 if (*s != NUL) /* check for trailing chars after expr */
11240 {
11241 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011242 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011243 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244 }
11245 if (map)
11246 {
11247 /* map(): replace the list item value */
11248 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011249 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 *tv = rettv;
11251 }
11252 else
11253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 int error = FALSE;
11255
Bram Moolenaare9a41262005-01-15 22:18:47 +000011256 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011259 /* On type error, nothing has been removed; return FAIL to stop the
11260 * loop. The error message was given by get_tv_number_chk(). */
11261 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011262 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011263 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011264 retval = OK;
11265theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011267 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011268}
11269
11270/*
11271 * "filter()" function
11272 */
11273 static void
11274f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 typval_T *argvars;
11276 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011277{
11278 filter_map(argvars, rettv, FALSE);
11279}
11280
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011282 * "finddir({fname}[, {path}[, {count}]])" function
11283 */
11284 static void
11285f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011288{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011289 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290}
11291
11292/*
11293 * "findfile({fname}[, {path}[, {count}]])" function
11294 */
11295 static void
11296f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 typval_T *argvars;
11298 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011299{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011300 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011301}
11302
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011303#ifdef FEAT_FLOAT
11304/*
11305 * "float2nr({float})" function
11306 */
11307 static void
11308f_float2nr(argvars, rettv)
11309 typval_T *argvars;
11310 typval_T *rettv;
11311{
11312 float_T f;
11313
11314 if (get_float_arg(argvars, &f) == OK)
11315 {
11316 if (f < -0x7fffffff)
11317 rettv->vval.v_number = -0x7fffffff;
11318 else if (f > 0x7fffffff)
11319 rettv->vval.v_number = 0x7fffffff;
11320 else
11321 rettv->vval.v_number = (varnumber_T)f;
11322 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011323}
11324
11325/*
11326 * "floor({float})" function
11327 */
11328 static void
11329f_floor(argvars, rettv)
11330 typval_T *argvars;
11331 typval_T *rettv;
11332{
11333 float_T f;
11334
11335 rettv->v_type = VAR_FLOAT;
11336 if (get_float_arg(argvars, &f) == OK)
11337 rettv->vval.v_float = floor(f);
11338 else
11339 rettv->vval.v_float = 0.0;
11340}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011341
11342/*
11343 * "fmod()" function
11344 */
11345 static void
11346f_fmod(argvars, rettv)
11347 typval_T *argvars;
11348 typval_T *rettv;
11349{
11350 float_T fx, fy;
11351
11352 rettv->v_type = VAR_FLOAT;
11353 if (get_float_arg(argvars, &fx) == OK
11354 && get_float_arg(&argvars[1], &fy) == OK)
11355 rettv->vval.v_float = fmod(fx, fy);
11356 else
11357 rettv->vval.v_float = 0.0;
11358}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011359#endif
11360
Bram Moolenaar0d660222005-01-07 21:51:51 +000011361/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011362 * "fnameescape({string})" function
11363 */
11364 static void
11365f_fnameescape(argvars, rettv)
11366 typval_T *argvars;
11367 typval_T *rettv;
11368{
11369 rettv->vval.v_string = vim_strsave_fnameescape(
11370 get_tv_string(&argvars[0]), FALSE);
11371 rettv->v_type = VAR_STRING;
11372}
11373
11374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 * "fnamemodify({fname}, {mods})" function
11376 */
11377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011379 typval_T *argvars;
11380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381{
11382 char_u *fname;
11383 char_u *mods;
11384 int usedlen = 0;
11385 int len;
11386 char_u *fbuf = NULL;
11387 char_u buf[NUMBUFLEN];
11388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 fname = get_tv_string_chk(&argvars[0]);
11390 mods = get_tv_string_buf_chk(&argvars[1], buf);
11391 if (fname == NULL || mods == NULL)
11392 fname = NULL;
11393 else
11394 {
11395 len = (int)STRLEN(fname);
11396 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 vim_free(fbuf);
11405}
11406
Bram Moolenaar33570922005-01-25 22:26:29 +000011407static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408
11409/*
11410 * "foldclosed()" function
11411 */
11412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011414 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011415 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011416 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
11418#ifdef FEAT_FOLDING
11419 linenr_T lnum;
11420 linenr_T first, last;
11421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11424 {
11425 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11426 {
11427 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 return;
11432 }
11433 }
11434#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436}
11437
11438/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011439 * "foldclosed()" function
11440 */
11441 static void
11442f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 typval_T *argvars;
11444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011445{
11446 foldclosed_both(argvars, rettv, FALSE);
11447}
11448
11449/*
11450 * "foldclosedend()" function
11451 */
11452 static void
11453f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456{
11457 foldclosed_both(argvars, rettv, TRUE);
11458}
11459
11460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 * "foldlevel()" function
11462 */
11463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011465 typval_T *argvars UNUSED;
11466 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
11468#ifdef FEAT_FOLDING
11469 linenr_T lnum;
11470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475}
11476
11477/*
11478 * "foldtext()" function
11479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011482 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484{
11485#ifdef FEAT_FOLDING
11486 linenr_T lnum;
11487 char_u *s;
11488 char_u *r;
11489 int len;
11490 char *txt;
11491#endif
11492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->v_type = VAR_STRING;
11494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11497 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11498 <= curbuf->b_ml.ml_line_count
11499 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 {
11501 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11503 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 {
11505 if (!linewhite(lnum))
11506 break;
11507 ++lnum;
11508 }
11509
11510 /* Find interesting text in this line. */
11511 s = skipwhite(ml_get(lnum));
11512 /* skip C comment-start */
11513 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011516 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011517 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011518 {
11519 s = skipwhite(ml_get(lnum + 1));
11520 if (*s == '*')
11521 s = skipwhite(s + 1);
11522 }
11523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 txt = _("+-%s%3ld lines: ");
11525 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011526 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 + 20 /* for %3ld */
11528 + STRLEN(s))); /* concatenated */
11529 if (r != NULL)
11530 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11532 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11533 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 len = (int)STRLEN(r);
11535 STRCAT(r, s);
11536 /* remove 'foldmarker' and 'commentstring' */
11537 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 }
11540 }
11541#endif
11542}
11543
11544/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011545 * "foldtextresult(lnum)" function
11546 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011551{
11552#ifdef FEAT_FOLDING
11553 linenr_T lnum;
11554 char_u *text;
11555 char_u buf[51];
11556 foldinfo_T foldinfo;
11557 int fold_count;
11558#endif
11559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->v_type = VAR_STRING;
11561 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011562#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 /* treat illegal types and illegal string values for {lnum} the same */
11565 if (lnum < 0)
11566 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011567 fold_count = foldedCount(curwin, lnum, &foldinfo);
11568 if (fold_count > 0)
11569 {
11570 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11571 &foldinfo, buf);
11572 if (text == buf)
11573 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011575 }
11576#endif
11577}
11578
11579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 * "foreground()" function
11581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011584 typval_T *argvars UNUSED;
11585 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587#ifdef FEAT_GUI
11588 if (gui.in_use)
11589 gui_mch_set_foreground();
11590#else
11591# ifdef WIN32
11592 win32_set_foreground();
11593# endif
11594#endif
11595}
11596
11597/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011598 * "function()" function
11599 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 typval_T *argvars;
11603 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604{
11605 char_u *s;
11606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011608 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011609 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011610 /* Don't check an autoload name for existence here. */
11611 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011612 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011613 else
11614 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011615 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011616 {
11617 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011618 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011619
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011620 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11621 * also be called from another script. Using trans_function_name()
11622 * would also work, but some plugins depend on the name being
11623 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011624 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011625 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011626 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011627 if (rettv->vval.v_string != NULL)
11628 {
11629 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011630 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011631 }
11632 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011633 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011634 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011636 }
11637}
11638
11639/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011640 * "garbagecollect()" function
11641 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011642 static void
11643f_garbagecollect(argvars, rettv)
11644 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011645 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011646{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011647 /* This is postponed until we are back at the toplevel, because we may be
11648 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11649 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011650
11651 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11652 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011653}
11654
11655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 * "get()" function
11657 */
11658 static void
11659f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 typval_T *argvars;
11661 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662{
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 listitem_T *li;
11664 list_T *l;
11665 dictitem_T *di;
11666 dict_T *d;
11667 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668
Bram Moolenaare9a41262005-01-15 22:18:47 +000011669 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 int error = FALSE;
11674
11675 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11676 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 else if (argvars[0].v_type == VAR_DICT)
11681 {
11682 if ((d = argvars[0].vval.v_dict) != NULL)
11683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011684 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011685 if (di != NULL)
11686 tv = &di->di_tv;
11687 }
11688 }
11689 else
11690 EMSG2(_(e_listdictarg), "get()");
11691
11692 if (tv == NULL)
11693 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011694 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011695 copy_tv(&argvars[2], rettv);
11696 }
11697 else
11698 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699}
11700
Bram Moolenaar342337a2005-07-21 21:11:17 +000011701static 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 +000011702
11703/*
11704 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011705 * Return a range (from start to end) of lines in rettv from the specified
11706 * buffer.
11707 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011708 */
11709 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011710get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011711 buf_T *buf;
11712 linenr_T start;
11713 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011714 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011715 typval_T *rettv;
11716{
11717 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011718
Bram Moolenaar959a1432013-12-14 12:17:38 +010011719 rettv->v_type = VAR_STRING;
11720 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011721 if (retlist && rettv_list_alloc(rettv) == FAIL)
11722 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011723
11724 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11725 return;
11726
11727 if (!retlist)
11728 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011729 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11730 p = ml_get_buf(buf, start, FALSE);
11731 else
11732 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011733 rettv->vval.v_string = vim_strsave(p);
11734 }
11735 else
11736 {
11737 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011738 return;
11739
11740 if (start < 1)
11741 start = 1;
11742 if (end > buf->b_ml.ml_line_count)
11743 end = buf->b_ml.ml_line_count;
11744 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011745 if (list_append_string(rettv->vval.v_list,
11746 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011747 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011748 }
11749}
11750
11751/*
11752 * "getbufline()" function
11753 */
11754 static void
11755f_getbufline(argvars, rettv)
11756 typval_T *argvars;
11757 typval_T *rettv;
11758{
11759 linenr_T lnum;
11760 linenr_T end;
11761 buf_T *buf;
11762
11763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11764 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011765 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011766 --emsg_off;
11767
Bram Moolenaar661b1822005-07-28 22:36:45 +000011768 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011769 if (argvars[2].v_type == VAR_UNKNOWN)
11770 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011771 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011772 end = get_tv_lnum_buf(&argvars[2], buf);
11773
Bram Moolenaar342337a2005-07-21 21:11:17 +000011774 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011775}
11776
Bram Moolenaar0d660222005-01-07 21:51:51 +000011777/*
11778 * "getbufvar()" function
11779 */
11780 static void
11781f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011782 typval_T *argvars;
11783 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011784{
11785 buf_T *buf;
11786 buf_T *save_curbuf;
11787 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011788 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011789 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011791 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11792 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011793 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011794 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011796 rettv->v_type = VAR_STRING;
11797 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011798
11799 if (buf != NULL && varname != NULL)
11800 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011801 /* set curbuf to be our buf, temporarily */
11802 save_curbuf = curbuf;
11803 curbuf = buf;
11804
Bram Moolenaar0d660222005-01-07 21:51:51 +000011805 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011806 {
11807 if (get_option_tv(&varname, rettv, TRUE) == OK)
11808 done = TRUE;
11809 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011810 else if (STRCMP(varname, "changedtick") == 0)
11811 {
11812 rettv->v_type = VAR_NUMBER;
11813 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011814 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011815 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816 else
11817 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011818 /* Look up the variable. */
11819 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11820 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11821 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011822 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011823 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011824 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011825 done = TRUE;
11826 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011827 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011828
11829 /* restore previous notion of curbuf */
11830 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011831 }
11832
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011833 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11834 /* use the default value */
11835 copy_tv(&argvars[2], rettv);
11836
Bram Moolenaar0d660222005-01-07 21:51:51 +000011837 --emsg_off;
11838}
11839
11840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 * "getchar()" function
11842 */
11843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011845 typval_T *argvars;
11846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847{
11848 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011851 /* Position the cursor. Needed after a message that ends in a space. */
11852 windgoto(msg_row, msg_col);
11853
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 ++no_mapping;
11855 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011856 for (;;)
11857 {
11858 if (argvars[0].v_type == VAR_UNKNOWN)
11859 /* getchar(): blocking wait. */
11860 n = safe_vgetc();
11861 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11862 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011863 n = vpeekc_any();
11864 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011865 /* illegal argument or getchar(0) and no char avail: return zero */
11866 n = 0;
11867 else
11868 /* getchar(0) and char avail: return char */
11869 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011870
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011871 if (n == K_IGNORE)
11872 continue;
11873 break;
11874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 --no_mapping;
11876 --allow_keys;
11877
Bram Moolenaar219b8702006-11-01 14:32:36 +000011878 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11879 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11880 vimvars[VV_MOUSE_COL].vv_nr = 0;
11881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 if (IS_SPECIAL(n) || mod_mask != 0)
11884 {
11885 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11886 int i = 0;
11887
11888 /* Turn a special key into three bytes, plus modifier. */
11889 if (mod_mask != 0)
11890 {
11891 temp[i++] = K_SPECIAL;
11892 temp[i++] = KS_MODIFIER;
11893 temp[i++] = mod_mask;
11894 }
11895 if (IS_SPECIAL(n))
11896 {
11897 temp[i++] = K_SPECIAL;
11898 temp[i++] = K_SECOND(n);
11899 temp[i++] = K_THIRD(n);
11900 }
11901#ifdef FEAT_MBYTE
11902 else if (has_mbyte)
11903 i += (*mb_char2bytes)(n, temp + i);
11904#endif
11905 else
11906 temp[i++] = n;
11907 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011908 rettv->v_type = VAR_STRING;
11909 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011910
11911#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011912 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011913 {
11914 int row = mouse_row;
11915 int col = mouse_col;
11916 win_T *win;
11917 linenr_T lnum;
11918# ifdef FEAT_WINDOWS
11919 win_T *wp;
11920# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011921 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011922
11923 if (row >= 0 && col >= 0)
11924 {
11925 /* Find the window at the mouse coordinates and compute the
11926 * text position. */
11927 win = mouse_find_win(&row, &col);
11928 (void)mouse_comp_pos(win, &row, &col, &lnum);
11929# ifdef FEAT_WINDOWS
11930 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011931 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011932# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011933 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011934 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11935 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11936 }
11937 }
11938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 }
11940}
11941
11942/*
11943 * "getcharmod()" function
11944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011947 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951}
11952
11953/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011954 * "getcharsearch()" function
11955 */
11956 static void
11957f_getcharsearch(argvars, rettv)
11958 typval_T *argvars UNUSED;
11959 typval_T *rettv;
11960{
11961 if (rettv_dict_alloc(rettv) != FAIL)
11962 {
11963 dict_T *dict = rettv->vval.v_dict;
11964
11965 dict_add_nr_str(dict, "char", 0L, last_csearch());
11966 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11967 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11968 }
11969}
11970
11971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 * "getcmdline()" function
11973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011976 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 rettv->v_type = VAR_STRING;
11980 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981}
11982
11983/*
11984 * "getcmdpos()" function
11985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011988 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992}
11993
11994/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011995 * "getcmdtype()" function
11996 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011997 static void
11998f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011999 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012000 typval_T *rettv;
12001{
12002 rettv->v_type = VAR_STRING;
12003 rettv->vval.v_string = alloc(2);
12004 if (rettv->vval.v_string != NULL)
12005 {
12006 rettv->vval.v_string[0] = get_cmdline_type();
12007 rettv->vval.v_string[1] = NUL;
12008 }
12009}
12010
12011/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012012 * "getcmdwintype()" function
12013 */
12014 static void
12015f_getcmdwintype(argvars, rettv)
12016 typval_T *argvars UNUSED;
12017 typval_T *rettv;
12018{
12019 rettv->v_type = VAR_STRING;
12020 rettv->vval.v_string = NULL;
12021#ifdef FEAT_CMDWIN
12022 rettv->vval.v_string = alloc(2);
12023 if (rettv->vval.v_string != NULL)
12024 {
12025 rettv->vval.v_string[0] = cmdwin_type;
12026 rettv->vval.v_string[1] = NUL;
12027 }
12028#endif
12029}
12030
12031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 * "getcwd()" function
12033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012036 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038{
Bram Moolenaard9462e32011-04-11 21:35:11 +020012039 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012042 rettv->vval.v_string = NULL;
12043 cwd = alloc(MAXPATHL);
12044 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012046 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12047 {
12048 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020012050 if (rettv->vval.v_string != NULL)
12051 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020012053 }
12054 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 }
12056}
12057
12058/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012059 * "getfontname()" function
12060 */
12061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->v_type = VAR_STRING;
12067 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012068#ifdef FEAT_GUI
12069 if (gui.in_use)
12070 {
12071 GuiFont font;
12072 char_u *name = NULL;
12073
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012074 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012075 {
12076 /* Get the "Normal" font. Either the name saved by
12077 * hl_set_font_name() or from the font ID. */
12078 font = gui.norm_font;
12079 name = hl_get_font_name();
12080 }
12081 else
12082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012084 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12085 return;
12086 font = gui_mch_get_font(name, FALSE);
12087 if (font == NOFONT)
12088 return; /* Invalid font name, return empty string. */
12089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012091 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012092 gui_mch_free_font(font);
12093 }
12094#endif
12095}
12096
12097/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012098 * "getfperm({fname})" function
12099 */
12100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 typval_T *argvars;
12103 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012104{
12105 char_u *fname;
12106 struct stat st;
12107 char_u *perm = NULL;
12108 char_u flags[] = "rwx";
12109 int i;
12110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012114 if (mch_stat((char *)fname, &st) >= 0)
12115 {
12116 perm = vim_strsave((char_u *)"---------");
12117 if (perm != NULL)
12118 {
12119 for (i = 0; i < 9; i++)
12120 {
12121 if (st.st_mode & (1 << (8 - i)))
12122 perm[i] = flags[i % 3];
12123 }
12124 }
12125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127}
12128
12129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 * "getfsize({fname})" function
12131 */
12132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 typval_T *argvars;
12135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136{
12137 char_u *fname;
12138 struct stat st;
12139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143
12144 if (mch_stat((char *)fname, &st) >= 0)
12145 {
12146 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012151
12152 /* non-perfect check for overflow */
12153 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12154 rettv->vval.v_number = -2;
12155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 }
12157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159}
12160
12161/*
12162 * "getftime({fname})" function
12163 */
12164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012166 typval_T *argvars;
12167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168{
12169 char_u *fname;
12170 struct stat st;
12171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173
12174 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178}
12179
12180/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012181 * "getftype({fname})" function
12182 */
12183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012185 typval_T *argvars;
12186 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012187{
12188 char_u *fname;
12189 struct stat st;
12190 char_u *type = NULL;
12191 char *t;
12192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012196 if (mch_lstat((char *)fname, &st) >= 0)
12197 {
12198#ifdef S_ISREG
12199 if (S_ISREG(st.st_mode))
12200 t = "file";
12201 else if (S_ISDIR(st.st_mode))
12202 t = "dir";
12203# ifdef S_ISLNK
12204 else if (S_ISLNK(st.st_mode))
12205 t = "link";
12206# endif
12207# ifdef S_ISBLK
12208 else if (S_ISBLK(st.st_mode))
12209 t = "bdev";
12210# endif
12211# ifdef S_ISCHR
12212 else if (S_ISCHR(st.st_mode))
12213 t = "cdev";
12214# endif
12215# ifdef S_ISFIFO
12216 else if (S_ISFIFO(st.st_mode))
12217 t = "fifo";
12218# endif
12219# ifdef S_ISSOCK
12220 else if (S_ISSOCK(st.st_mode))
12221 t = "fifo";
12222# endif
12223 else
12224 t = "other";
12225#else
12226# ifdef S_IFMT
12227 switch (st.st_mode & S_IFMT)
12228 {
12229 case S_IFREG: t = "file"; break;
12230 case S_IFDIR: t = "dir"; break;
12231# ifdef S_IFLNK
12232 case S_IFLNK: t = "link"; break;
12233# endif
12234# ifdef S_IFBLK
12235 case S_IFBLK: t = "bdev"; break;
12236# endif
12237# ifdef S_IFCHR
12238 case S_IFCHR: t = "cdev"; break;
12239# endif
12240# ifdef S_IFIFO
12241 case S_IFIFO: t = "fifo"; break;
12242# endif
12243# ifdef S_IFSOCK
12244 case S_IFSOCK: t = "socket"; break;
12245# endif
12246 default: t = "other";
12247 }
12248# else
12249 if (mch_isdir(fname))
12250 t = "dir";
12251 else
12252 t = "file";
12253# endif
12254#endif
12255 type = vim_strsave((char_u *)t);
12256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012258}
12259
12260/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012261 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012262 */
12263 static void
12264f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012265 typval_T *argvars;
12266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012267{
12268 linenr_T lnum;
12269 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012270 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012271
12272 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012273 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012274 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012275 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012276 retlist = FALSE;
12277 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012279 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012281 retlist = TRUE;
12282 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012283
Bram Moolenaar342337a2005-07-21 21:11:17 +000012284 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012285}
12286
12287/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012288 * "getmatches()" function
12289 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012290 static void
12291f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012292 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012293 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012294{
12295#ifdef FEAT_SEARCH_EXTRA
12296 dict_T *dict;
12297 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012298 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012299
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012300 if (rettv_list_alloc(rettv) == OK)
12301 {
12302 while (cur != NULL)
12303 {
12304 dict = dict_alloc();
12305 if (dict == NULL)
12306 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012307 if (cur->match.regprog == NULL)
12308 {
12309 /* match added with matchaddpos() */
12310 for (i = 0; i < MAXPOSMATCH; ++i)
12311 {
12312 llpos_T *llpos;
12313 char buf[6];
12314 list_T *l;
12315
12316 llpos = &cur->pos.pos[i];
12317 if (llpos->lnum == 0)
12318 break;
12319 l = list_alloc();
12320 if (l == NULL)
12321 break;
12322 list_append_number(l, (varnumber_T)llpos->lnum);
12323 if (llpos->col > 0)
12324 {
12325 list_append_number(l, (varnumber_T)llpos->col);
12326 list_append_number(l, (varnumber_T)llpos->len);
12327 }
12328 sprintf(buf, "pos%d", i + 1);
12329 dict_add_list(dict, buf, l);
12330 }
12331 }
12332 else
12333 {
12334 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12335 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012336 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012337 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12338 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012339# ifdef FEAT_CONCEAL
12340 if (cur->conceal_char)
12341 {
12342 char_u buf[MB_MAXBYTES + 1];
12343
12344 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12345 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12346 }
12347# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012348 list_append_dict(rettv->vval.v_list, dict);
12349 cur = cur->next;
12350 }
12351 }
12352#endif
12353}
12354
12355/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012356 * "getpid()" function
12357 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012358 static void
12359f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012360 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012361 typval_T *rettv;
12362{
12363 rettv->vval.v_number = mch_get_pid();
12364}
12365
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012366static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12367
12368/*
12369 * "getcurpos()" function
12370 */
12371 static void
12372f_getcurpos(argvars, rettv)
12373 typval_T *argvars;
12374 typval_T *rettv;
12375{
12376 getpos_both(argvars, rettv, TRUE);
12377}
12378
Bram Moolenaar18081e32008-02-20 19:11:07 +000012379/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012380 * "getpos(string)" function
12381 */
12382 static void
12383f_getpos(argvars, rettv)
12384 typval_T *argvars;
12385 typval_T *rettv;
12386{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012387 getpos_both(argvars, rettv, FALSE);
12388}
12389
12390 static void
12391getpos_both(argvars, rettv, getcurpos)
12392 typval_T *argvars;
12393 typval_T *rettv;
12394 int getcurpos;
12395{
Bram Moolenaara5525202006-03-02 22:52:09 +000012396 pos_T *fp;
12397 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012398 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012399
12400 if (rettv_list_alloc(rettv) == OK)
12401 {
12402 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012403 if (getcurpos)
12404 fp = &curwin->w_cursor;
12405 else
12406 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012407 if (fnum != -1)
12408 list_append_number(l, (varnumber_T)fnum);
12409 else
12410 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012411 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12412 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012413 list_append_number(l, (fp != NULL)
12414 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012415 : (varnumber_T)0);
12416 list_append_number(l,
12417#ifdef FEAT_VIRTUALEDIT
12418 (fp != NULL) ? (varnumber_T)fp->coladd :
12419#endif
12420 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012421 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012422 list_append_number(l, curwin->w_curswant == MAXCOL ?
12423 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012424 }
12425 else
12426 rettv->vval.v_number = FALSE;
12427}
12428
12429/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012430 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012431 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012432 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012433f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012434 typval_T *argvars UNUSED;
12435 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012436{
12437#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012438 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012439#endif
12440
Bram Moolenaar2641f772005-03-25 21:58:17 +000012441#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012442 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012443 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012444 wp = NULL;
12445 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12446 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012447 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012448 if (wp == NULL)
12449 return;
12450 }
12451
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012452 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012453 }
12454#endif
12455}
12456
12457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 * "getreg()" function
12459 */
12460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012462 typval_T *argvars;
12463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464{
12465 char_u *strregname;
12466 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012467 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012468 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012469 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012471 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012473 strregname = get_tv_string_chk(&argvars[0]);
12474 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012477 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012478 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12479 return_list = get_tv_number_chk(&argvars[2], &error);
12480 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012483 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012484
12485 if (error)
12486 return;
12487
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 regname = (strregname == NULL ? '"' : *strregname);
12489 if (regname == 0)
12490 regname = '"';
12491
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012492 if (return_list)
12493 {
12494 rettv->v_type = VAR_LIST;
12495 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12496 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012497 if (rettv->vval.v_list != NULL)
12498 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012499 }
12500 else
12501 {
12502 rettv->v_type = VAR_STRING;
12503 rettv->vval.v_string = get_reg_contents(regname,
12504 arg2 ? GREG_EXPR_SRC : 0);
12505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506}
12507
12508/*
12509 * "getregtype()" function
12510 */
12511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012513 typval_T *argvars;
12514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515{
12516 char_u *strregname;
12517 int regname;
12518 char_u buf[NUMBUFLEN + 2];
12519 long reglen = 0;
12520
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012521 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 {
12523 strregname = get_tv_string_chk(&argvars[0]);
12524 if (strregname == NULL) /* type error; errmsg already given */
12525 {
12526 rettv->v_type = VAR_STRING;
12527 rettv->vval.v_string = NULL;
12528 return;
12529 }
12530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 else
12532 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012533 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534
12535 regname = (strregname == NULL ? '"' : *strregname);
12536 if (regname == 0)
12537 regname = '"';
12538
12539 buf[0] = NUL;
12540 buf[1] = NUL;
12541 switch (get_reg_type(regname, &reglen))
12542 {
12543 case MLINE: buf[0] = 'V'; break;
12544 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 case MBLOCK:
12546 buf[0] = Ctrl_V;
12547 sprintf((char *)buf + 1, "%ld", reglen + 1);
12548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012550 rettv->v_type = VAR_STRING;
12551 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552}
12553
12554/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012555 * "gettabvar()" function
12556 */
12557 static void
12558f_gettabvar(argvars, rettv)
12559 typval_T *argvars;
12560 typval_T *rettv;
12561{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012562 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012563 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012564 dictitem_T *v;
12565 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012566 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012567
12568 rettv->v_type = VAR_STRING;
12569 rettv->vval.v_string = NULL;
12570
12571 varname = get_tv_string_chk(&argvars[1]);
12572 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12573 if (tp != NULL && varname != NULL)
12574 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012575 /* Set tp to be our tabpage, temporarily. Also set the window to the
12576 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012577 if (switch_win(&oldcurwin, &oldtabpage,
12578 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012579 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012580 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012581 /* look up the variable */
12582 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12583 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12584 if (v != NULL)
12585 {
12586 copy_tv(&v->di_tv, rettv);
12587 done = TRUE;
12588 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012589 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012590
12591 /* restore previous notion of curwin */
12592 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012593 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012594
12595 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012596 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012597}
12598
12599/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012600 * "gettabwinvar()" function
12601 */
12602 static void
12603f_gettabwinvar(argvars, rettv)
12604 typval_T *argvars;
12605 typval_T *rettv;
12606{
12607 getwinvar(argvars, rettv, 1);
12608}
12609
12610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 * "getwinposx()" function
12612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012615 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619#ifdef FEAT_GUI
12620 if (gui.in_use)
12621 {
12622 int x, y;
12623
12624 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012625 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 }
12627#endif
12628}
12629
12630/*
12631 * "getwinposy()" function
12632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639#ifdef FEAT_GUI
12640 if (gui.in_use)
12641 {
12642 int x, y;
12643
12644 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 }
12647#endif
12648}
12649
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012650/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012651 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012652 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012653 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012654find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012655 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012656 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012657{
12658#ifdef FEAT_WINDOWS
12659 win_T *wp;
12660#endif
12661 int nr;
12662
12663 nr = get_tv_number_chk(vp, NULL);
12664
12665#ifdef FEAT_WINDOWS
12666 if (nr < 0)
12667 return NULL;
12668 if (nr == 0)
12669 return curwin;
12670
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012671 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12672 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012673 if (--nr <= 0)
12674 break;
12675 return wp;
12676#else
12677 if (nr == 0 || nr == 1)
12678 return curwin;
12679 return NULL;
12680#endif
12681}
12682
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683/*
12684 * "getwinvar()" function
12685 */
12686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *argvars;
12689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012691 getwinvar(argvars, rettv, 0);
12692}
12693
12694/*
12695 * getwinvar() and gettabwinvar()
12696 */
12697 static void
12698getwinvar(argvars, rettv, off)
12699 typval_T *argvars;
12700 typval_T *rettv;
12701 int off; /* 1 for gettabwinvar() */
12702{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012703 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012706 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012707 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012708#ifdef FEAT_WINDOWS
12709 win_T *oldcurwin;
12710 tabpage_T *oldtabpage;
12711 int need_switch_win;
12712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012714#ifdef FEAT_WINDOWS
12715 if (off == 1)
12716 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12717 else
12718 tp = curtab;
12719#endif
12720 win = find_win_by_nr(&argvars[off], tp);
12721 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012724 rettv->v_type = VAR_STRING;
12725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726
12727 if (win != NULL && varname != NULL)
12728 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012729#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012730 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012731 * otherwise the window is not valid. Only do this when needed,
12732 * autocommands get blocked. */
12733 need_switch_win = !(tp == curtab && win == curwin);
12734 if (!need_switch_win
12735 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12736#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012737 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012738 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012739 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012740 if (get_option_tv(&varname, rettv, 1) == OK)
12741 done = TRUE;
12742 }
12743 else
12744 {
12745 /* Look up the variable. */
12746 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12747 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12748 varname, FALSE);
12749 if (v != NULL)
12750 {
12751 copy_tv(&v->di_tv, rettv);
12752 done = TRUE;
12753 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012756
Bram Moolenaarba117c22015-09-29 16:53:22 +020012757#ifdef FEAT_WINDOWS
12758 if (need_switch_win)
12759 /* restore previous notion of curwin */
12760 restore_win(oldcurwin, oldtabpage, TRUE);
12761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 }
12763
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012764 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12765 /* use the default return value */
12766 copy_tv(&argvars[off + 2], rettv);
12767
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 --emsg_off;
12769}
12770
12771/*
12772 * "glob()" function
12773 */
12774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012776 typval_T *argvars;
12777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012779 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012781 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012783 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012784 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012786 if (argvars[1].v_type != VAR_UNKNOWN)
12787 {
12788 if (get_tv_number_chk(&argvars[1], &error))
12789 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012790 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012791 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012792 if (get_tv_number_chk(&argvars[2], &error))
12793 {
12794 rettv->v_type = VAR_LIST;
12795 rettv->vval.v_list = NULL;
12796 }
12797 if (argvars[3].v_type != VAR_UNKNOWN
12798 && get_tv_number_chk(&argvars[3], &error))
12799 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012800 }
12801 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012802 if (!error)
12803 {
12804 ExpandInit(&xpc);
12805 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012806 if (p_wic)
12807 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012808 if (rettv->v_type == VAR_STRING)
12809 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012810 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012811 else if (rettv_list_alloc(rettv) != FAIL)
12812 {
12813 int i;
12814
12815 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12816 NULL, options, WILD_ALL_KEEP);
12817 for (i = 0; i < xpc.xp_numfiles; i++)
12818 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12819
12820 ExpandCleanup(&xpc);
12821 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012822 }
12823 else
12824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825}
12826
12827/*
12828 * "globpath()" function
12829 */
12830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *argvars;
12833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012835 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012838 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012839 garray_T ga;
12840 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012842 /* When the optional second argument is non-zero, don't remove matches
12843 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012845 if (argvars[2].v_type != VAR_UNKNOWN)
12846 {
12847 if (get_tv_number_chk(&argvars[2], &error))
12848 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012849 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012850 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012851 if (get_tv_number_chk(&argvars[3], &error))
12852 {
12853 rettv->v_type = VAR_LIST;
12854 rettv->vval.v_list = NULL;
12855 }
12856 if (argvars[4].v_type != VAR_UNKNOWN
12857 && get_tv_number_chk(&argvars[4], &error))
12858 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012859 }
12860 }
12861 if (file != NULL && !error)
12862 {
12863 ga_init2(&ga, (int)sizeof(char_u *), 10);
12864 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12865 if (rettv->v_type == VAR_STRING)
12866 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12867 else if (rettv_list_alloc(rettv) != FAIL)
12868 for (i = 0; i < ga.ga_len; ++i)
12869 list_append_string(rettv->vval.v_list,
12870 ((char_u **)(ga.ga_data))[i], -1);
12871 ga_clear_strings(&ga);
12872 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012874 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875}
12876
12877/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012878 * "glob2regpat()" function
12879 */
12880 static void
12881f_glob2regpat(argvars, rettv)
12882 typval_T *argvars;
12883 typval_T *rettv;
12884{
12885 char_u *pat = get_tv_string_chk(&argvars[0]);
12886
12887 rettv->v_type = VAR_STRING;
12888 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12889}
12890
12891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 * "has()" function
12893 */
12894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 typval_T *argvars;
12897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898{
12899 int i;
12900 char_u *name;
12901 int n = FALSE;
12902 static char *(has_list[]) =
12903 {
12904#ifdef AMIGA
12905 "amiga",
12906# ifdef FEAT_ARP
12907 "arp",
12908# endif
12909#endif
12910#ifdef __BEOS__
12911 "beos",
12912#endif
12913#ifdef MSDOS
12914# ifdef DJGPP
12915 "dos32",
12916# else
12917 "dos16",
12918# endif
12919#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012920#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 "mac",
12922#endif
12923#if defined(MACOS_X_UNIX)
12924 "macunix",
12925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926#ifdef __QNX__
12927 "qnx",
12928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929#ifdef UNIX
12930 "unix",
12931#endif
12932#ifdef VMS
12933 "vms",
12934#endif
12935#ifdef WIN16
12936 "win16",
12937#endif
12938#ifdef WIN32
12939 "win32",
12940#endif
12941#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12942 "win32unix",
12943#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012944#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 "win64",
12946#endif
12947#ifdef EBCDIC
12948 "ebcdic",
12949#endif
12950#ifndef CASE_INSENSITIVE_FILENAME
12951 "fname_case",
12952#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012953#ifdef HAVE_ACL
12954 "acl",
12955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956#ifdef FEAT_ARABIC
12957 "arabic",
12958#endif
12959#ifdef FEAT_AUTOCMD
12960 "autocmd",
12961#endif
12962#ifdef FEAT_BEVAL
12963 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012964# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12965 "balloon_multiline",
12966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967#endif
12968#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12969 "builtin_terms",
12970# ifdef ALL_BUILTIN_TCAPS
12971 "all_builtin_terms",
12972# endif
12973#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012974#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12975 || defined(FEAT_GUI_W32) \
12976 || defined(FEAT_GUI_MOTIF))
12977 "browsefilter",
12978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979#ifdef FEAT_BYTEOFF
12980 "byte_offset",
12981#endif
12982#ifdef FEAT_CINDENT
12983 "cindent",
12984#endif
12985#ifdef FEAT_CLIENTSERVER
12986 "clientserver",
12987#endif
12988#ifdef FEAT_CLIPBOARD
12989 "clipboard",
12990#endif
12991#ifdef FEAT_CMDL_COMPL
12992 "cmdline_compl",
12993#endif
12994#ifdef FEAT_CMDHIST
12995 "cmdline_hist",
12996#endif
12997#ifdef FEAT_COMMENTS
12998 "comments",
12999#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013000#ifdef FEAT_CONCEAL
13001 "conceal",
13002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003#ifdef FEAT_CRYPT
13004 "cryptv",
13005#endif
13006#ifdef FEAT_CSCOPE
13007 "cscope",
13008#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013009#ifdef FEAT_CURSORBIND
13010 "cursorbind",
13011#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013012#ifdef CURSOR_SHAPE
13013 "cursorshape",
13014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015#ifdef DEBUG
13016 "debug",
13017#endif
13018#ifdef FEAT_CON_DIALOG
13019 "dialog_con",
13020#endif
13021#ifdef FEAT_GUI_DIALOG
13022 "dialog_gui",
13023#endif
13024#ifdef FEAT_DIFF
13025 "diff",
13026#endif
13027#ifdef FEAT_DIGRAPHS
13028 "digraphs",
13029#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013030#ifdef FEAT_DIRECTX
13031 "directx",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_DND
13034 "dnd",
13035#endif
13036#ifdef FEAT_EMACS_TAGS
13037 "emacs_tags",
13038#endif
13039 "eval", /* always present, of course! */
13040#ifdef FEAT_EX_EXTRA
13041 "ex_extra",
13042#endif
13043#ifdef FEAT_SEARCH_EXTRA
13044 "extra_search",
13045#endif
13046#ifdef FEAT_FKMAP
13047 "farsi",
13048#endif
13049#ifdef FEAT_SEARCHPATH
13050 "file_in_path",
13051#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013052#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013053 "filterpipe",
13054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef FEAT_FIND_ID
13056 "find_in_path",
13057#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013058#ifdef FEAT_FLOAT
13059 "float",
13060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061#ifdef FEAT_FOLDING
13062 "folding",
13063#endif
13064#ifdef FEAT_FOOTER
13065 "footer",
13066#endif
13067#if !defined(USE_SYSTEM) && defined(UNIX)
13068 "fork",
13069#endif
13070#ifdef FEAT_GETTEXT
13071 "gettext",
13072#endif
13073#ifdef FEAT_GUI
13074 "gui",
13075#endif
13076#ifdef FEAT_GUI_ATHENA
13077# ifdef FEAT_GUI_NEXTAW
13078 "gui_neXtaw",
13079# else
13080 "gui_athena",
13081# endif
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#ifdef FEAT_GUI_GTK
13084 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013087#ifdef FEAT_GUI_GNOME
13088 "gui_gnome",
13089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090#ifdef FEAT_GUI_MAC
13091 "gui_mac",
13092#endif
13093#ifdef FEAT_GUI_MOTIF
13094 "gui_motif",
13095#endif
13096#ifdef FEAT_GUI_PHOTON
13097 "gui_photon",
13098#endif
13099#ifdef FEAT_GUI_W16
13100 "gui_win16",
13101#endif
13102#ifdef FEAT_GUI_W32
13103 "gui_win32",
13104#endif
13105#ifdef FEAT_HANGULIN
13106 "hangul_input",
13107#endif
13108#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13109 "iconv",
13110#endif
13111#ifdef FEAT_INS_EXPAND
13112 "insert_expand",
13113#endif
13114#ifdef FEAT_JUMPLIST
13115 "jumplist",
13116#endif
13117#ifdef FEAT_KEYMAP
13118 "keymap",
13119#endif
13120#ifdef FEAT_LANGMAP
13121 "langmap",
13122#endif
13123#ifdef FEAT_LIBCALL
13124 "libcall",
13125#endif
13126#ifdef FEAT_LINEBREAK
13127 "linebreak",
13128#endif
13129#ifdef FEAT_LISP
13130 "lispindent",
13131#endif
13132#ifdef FEAT_LISTCMDS
13133 "listcmds",
13134#endif
13135#ifdef FEAT_LOCALMAP
13136 "localmap",
13137#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013138#ifdef FEAT_LUA
13139# ifndef DYNAMIC_LUA
13140 "lua",
13141# endif
13142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143#ifdef FEAT_MENU
13144 "menu",
13145#endif
13146#ifdef FEAT_SESSION
13147 "mksession",
13148#endif
13149#ifdef FEAT_MODIFY_FNAME
13150 "modify_fname",
13151#endif
13152#ifdef FEAT_MOUSE
13153 "mouse",
13154#endif
13155#ifdef FEAT_MOUSESHAPE
13156 "mouseshape",
13157#endif
13158#if defined(UNIX) || defined(VMS)
13159# ifdef FEAT_MOUSE_DEC
13160 "mouse_dec",
13161# endif
13162# ifdef FEAT_MOUSE_GPM
13163 "mouse_gpm",
13164# endif
13165# ifdef FEAT_MOUSE_JSB
13166 "mouse_jsbterm",
13167# endif
13168# ifdef FEAT_MOUSE_NET
13169 "mouse_netterm",
13170# endif
13171# ifdef FEAT_MOUSE_PTERM
13172 "mouse_pterm",
13173# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013174# ifdef FEAT_MOUSE_SGR
13175 "mouse_sgr",
13176# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013177# ifdef FEAT_SYSMOUSE
13178 "mouse_sysmouse",
13179# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013180# ifdef FEAT_MOUSE_URXVT
13181 "mouse_urxvt",
13182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183# ifdef FEAT_MOUSE_XTERM
13184 "mouse_xterm",
13185# endif
13186#endif
13187#ifdef FEAT_MBYTE
13188 "multi_byte",
13189#endif
13190#ifdef FEAT_MBYTE_IME
13191 "multi_byte_ime",
13192#endif
13193#ifdef FEAT_MULTI_LANG
13194 "multi_lang",
13195#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013196#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013197#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013198 "mzscheme",
13199#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_OLE
13202 "ole",
13203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204#ifdef FEAT_PATH_EXTRA
13205 "path_extra",
13206#endif
13207#ifdef FEAT_PERL
13208#ifndef DYNAMIC_PERL
13209 "perl",
13210#endif
13211#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013212#ifdef FEAT_PERSISTENT_UNDO
13213 "persistent_undo",
13214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215#ifdef FEAT_PYTHON
13216#ifndef DYNAMIC_PYTHON
13217 "python",
13218#endif
13219#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013220#ifdef FEAT_PYTHON3
13221#ifndef DYNAMIC_PYTHON3
13222 "python3",
13223#endif
13224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225#ifdef FEAT_POSTSCRIPT
13226 "postscript",
13227#endif
13228#ifdef FEAT_PRINTER
13229 "printer",
13230#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013231#ifdef FEAT_PROFILE
13232 "profile",
13233#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013234#ifdef FEAT_RELTIME
13235 "reltime",
13236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237#ifdef FEAT_QUICKFIX
13238 "quickfix",
13239#endif
13240#ifdef FEAT_RIGHTLEFT
13241 "rightleft",
13242#endif
13243#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13244 "ruby",
13245#endif
13246#ifdef FEAT_SCROLLBIND
13247 "scrollbind",
13248#endif
13249#ifdef FEAT_CMDL_INFO
13250 "showcmd",
13251 "cmdline_info",
13252#endif
13253#ifdef FEAT_SIGNS
13254 "signs",
13255#endif
13256#ifdef FEAT_SMARTINDENT
13257 "smartindent",
13258#endif
13259#ifdef FEAT_SNIFF
13260 "sniff",
13261#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013262#ifdef STARTUPTIME
13263 "startuptime",
13264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#ifdef FEAT_STL_OPT
13266 "statusline",
13267#endif
13268#ifdef FEAT_SUN_WORKSHOP
13269 "sun_workshop",
13270#endif
13271#ifdef FEAT_NETBEANS_INTG
13272 "netbeans_intg",
13273#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013274#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013275 "spell",
13276#endif
13277#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 "syntax",
13279#endif
13280#if defined(USE_SYSTEM) || !defined(UNIX)
13281 "system",
13282#endif
13283#ifdef FEAT_TAG_BINS
13284 "tag_binary",
13285#endif
13286#ifdef FEAT_TAG_OLDSTATIC
13287 "tag_old_static",
13288#endif
13289#ifdef FEAT_TAG_ANYWHITE
13290 "tag_any_white",
13291#endif
13292#ifdef FEAT_TCL
13293# ifndef DYNAMIC_TCL
13294 "tcl",
13295# endif
13296#endif
13297#ifdef TERMINFO
13298 "terminfo",
13299#endif
13300#ifdef FEAT_TERMRESPONSE
13301 "termresponse",
13302#endif
13303#ifdef FEAT_TEXTOBJ
13304 "textobjects",
13305#endif
13306#ifdef HAVE_TGETENT
13307 "tgetent",
13308#endif
13309#ifdef FEAT_TITLE
13310 "title",
13311#endif
13312#ifdef FEAT_TOOLBAR
13313 "toolbar",
13314#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013315#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13316 "unnamedplus",
13317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318#ifdef FEAT_USR_CMDS
13319 "user-commands", /* was accidentally included in 5.4 */
13320 "user_commands",
13321#endif
13322#ifdef FEAT_VIMINFO
13323 "viminfo",
13324#endif
13325#ifdef FEAT_VERTSPLIT
13326 "vertsplit",
13327#endif
13328#ifdef FEAT_VIRTUALEDIT
13329 "virtualedit",
13330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332#ifdef FEAT_VISUALEXTRA
13333 "visualextra",
13334#endif
13335#ifdef FEAT_VREPLACE
13336 "vreplace",
13337#endif
13338#ifdef FEAT_WILDIGN
13339 "wildignore",
13340#endif
13341#ifdef FEAT_WILDMENU
13342 "wildmenu",
13343#endif
13344#ifdef FEAT_WINDOWS
13345 "windows",
13346#endif
13347#ifdef FEAT_WAK
13348 "winaltkeys",
13349#endif
13350#ifdef FEAT_WRITEBACKUP
13351 "writebackup",
13352#endif
13353#ifdef FEAT_XIM
13354 "xim",
13355#endif
13356#ifdef FEAT_XFONTSET
13357 "xfontset",
13358#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013359#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013360 "xpm",
13361 "xpm_w32", /* for backward compatibility */
13362#else
13363# if defined(HAVE_XPM)
13364 "xpm",
13365# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367#ifdef USE_XSMP
13368 "xsmp",
13369#endif
13370#ifdef USE_XSMP_INTERACT
13371 "xsmp_interact",
13372#endif
13373#ifdef FEAT_XCLIPBOARD
13374 "xterm_clipboard",
13375#endif
13376#ifdef FEAT_XTERM_SAVE
13377 "xterm_save",
13378#endif
13379#if defined(UNIX) && defined(FEAT_X11)
13380 "X11",
13381#endif
13382 NULL
13383 };
13384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 for (i = 0; has_list[i] != NULL; ++i)
13387 if (STRICMP(name, has_list[i]) == 0)
13388 {
13389 n = TRUE;
13390 break;
13391 }
13392
13393 if (n == FALSE)
13394 {
13395 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013396 {
13397 if (name[5] == '-'
13398 && STRLEN(name) > 11
13399 && vim_isdigit(name[6])
13400 && vim_isdigit(name[8])
13401 && vim_isdigit(name[10]))
13402 {
13403 int major = atoi((char *)name + 6);
13404 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013405
13406 /* Expect "patch-9.9.01234". */
13407 n = (major < VIM_VERSION_MAJOR
13408 || (major == VIM_VERSION_MAJOR
13409 && (minor < VIM_VERSION_MINOR
13410 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013411 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013412 }
13413 else
13414 n = has_patch(atoi((char *)name + 5));
13415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 else if (STRICMP(name, "vim_starting") == 0)
13417 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013418#ifdef FEAT_MBYTE
13419 else if (STRICMP(name, "multi_byte_encoding") == 0)
13420 n = has_mbyte;
13421#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013422#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13423 else if (STRICMP(name, "balloon_multiline") == 0)
13424 n = multiline_balloon_available();
13425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426#ifdef DYNAMIC_TCL
13427 else if (STRICMP(name, "tcl") == 0)
13428 n = tcl_enabled(FALSE);
13429#endif
13430#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13431 else if (STRICMP(name, "iconv") == 0)
13432 n = iconv_enabled(FALSE);
13433#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013434#ifdef DYNAMIC_LUA
13435 else if (STRICMP(name, "lua") == 0)
13436 n = lua_enabled(FALSE);
13437#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013438#ifdef DYNAMIC_MZSCHEME
13439 else if (STRICMP(name, "mzscheme") == 0)
13440 n = mzscheme_enabled(FALSE);
13441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#ifdef DYNAMIC_RUBY
13443 else if (STRICMP(name, "ruby") == 0)
13444 n = ruby_enabled(FALSE);
13445#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013446#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447#ifdef DYNAMIC_PYTHON
13448 else if (STRICMP(name, "python") == 0)
13449 n = python_enabled(FALSE);
13450#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013451#endif
13452#ifdef FEAT_PYTHON3
13453#ifdef DYNAMIC_PYTHON3
13454 else if (STRICMP(name, "python3") == 0)
13455 n = python3_enabled(FALSE);
13456#endif
13457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458#ifdef DYNAMIC_PERL
13459 else if (STRICMP(name, "perl") == 0)
13460 n = perl_enabled(FALSE);
13461#endif
13462#ifdef FEAT_GUI
13463 else if (STRICMP(name, "gui_running") == 0)
13464 n = (gui.in_use || gui.starting);
13465# ifdef FEAT_GUI_W32
13466 else if (STRICMP(name, "gui_win32s") == 0)
13467 n = gui_is_win32s();
13468# endif
13469# ifdef FEAT_BROWSE
13470 else if (STRICMP(name, "browse") == 0)
13471 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13472# endif
13473#endif
13474#ifdef FEAT_SYN_HL
13475 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013476 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477#endif
13478#if defined(WIN3264)
13479 else if (STRICMP(name, "win95") == 0)
13480 n = mch_windows95();
13481#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013482#ifdef FEAT_NETBEANS_INTG
13483 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013484 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 }
13487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489}
13490
13491/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013492 * "has_key()" function
13493 */
13494 static void
13495f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013496 typval_T *argvars;
13497 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013498{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013499 if (argvars[0].v_type != VAR_DICT)
13500 {
13501 EMSG(_(e_dictreq));
13502 return;
13503 }
13504 if (argvars[0].vval.v_dict == NULL)
13505 return;
13506
13507 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013508 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013509}
13510
13511/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013512 * "haslocaldir()" function
13513 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013514 static void
13515f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013516 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013517 typval_T *rettv;
13518{
13519 rettv->vval.v_number = (curwin->w_localdir != NULL);
13520}
13521
13522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 * "hasmapto()" function
13524 */
13525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013527 typval_T *argvars;
13528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529{
13530 char_u *name;
13531 char_u *mode;
13532 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013533 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013536 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 mode = (char_u *)"nvo";
13538 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013541 if (argvars[2].v_type != VAR_UNKNOWN)
13542 abbr = get_tv_number(&argvars[2]);
13543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
Bram Moolenaar2c932302006-03-18 21:42:09 +000013545 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549}
13550
13551/*
13552 * "histadd()" function
13553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558{
13559#ifdef FEAT_CMDHIST
13560 int histype;
13561 char_u *str;
13562 char_u buf[NUMBUFLEN];
13563#endif
13564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 if (check_restricted() || check_secure())
13567 return;
13568#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13570 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 if (histype >= 0)
13572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 if (*str != NUL)
13575 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013576 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 return;
13580 }
13581 }
13582#endif
13583}
13584
13585/*
13586 * "histdel()" function
13587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013590 typval_T *argvars UNUSED;
13591 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592{
13593#ifdef FEAT_CMDHIST
13594 int n;
13595 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013596 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13599 if (str == NULL)
13600 n = 0;
13601 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013604 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 else
13609 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611 get_tv_string_buf(&argvars[1], buf));
13612 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613#endif
13614}
13615
13616/*
13617 * "histget()" function
13618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623{
13624#ifdef FEAT_CMDHIST
13625 int type;
13626 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013629 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13630 if (str == NULL)
13631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013633 {
13634 type = get_histtype(str);
13635 if (argvars[1].v_type == VAR_UNKNOWN)
13636 idx = get_history_idx(type);
13637 else
13638 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13639 /* -1 on type error */
13640 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646}
13647
13648/*
13649 * "histnr()" function
13650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655{
13656 int i;
13657
13658#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 char_u *history = get_tv_string_chk(&argvars[0]);
13660
13661 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 if (i >= HIST_CMD && i < HIST_COUNT)
13663 i = get_history_idx(i);
13664 else
13665#endif
13666 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668}
13669
13670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 * "highlightID(name)" function
13672 */
13673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013675 typval_T *argvars;
13676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679}
13680
13681/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682 * "highlight_exists()" function
13683 */
13684 static void
13685f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013686 typval_T *argvars;
13687 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688{
13689 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13690}
13691
13692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 * "hostname()" function
13694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013696f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
13700 char_u hostname[256];
13701
13702 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 rettv->v_type = VAR_STRING;
13704 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705}
13706
13707/*
13708 * iconv() function
13709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714{
13715#ifdef FEAT_MBYTE
13716 char_u buf1[NUMBUFLEN];
13717 char_u buf2[NUMBUFLEN];
13718 char_u *from, *to, *str;
13719 vimconv_T vimconv;
13720#endif
13721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->v_type = VAR_STRING;
13723 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724
13725#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 str = get_tv_string(&argvars[0]);
13727 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13728 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 vimconv.vc_type = CONV_NONE;
13730 convert_setup(&vimconv, from, to);
13731
13732 /* If the encodings are equal, no conversion needed. */
13733 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013736 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
13738 convert_setup(&vimconv, NULL, NULL);
13739 vim_free(from);
13740 vim_free(to);
13741#endif
13742}
13743
13744/*
13745 * "indent()" function
13746 */
13747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013749 typval_T *argvars;
13750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751{
13752 linenr_T lnum;
13753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759}
13760
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013761/*
13762 * "index()" function
13763 */
13764 static void
13765f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *argvars;
13767 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013768{
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 list_T *l;
13770 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013771 long idx = 0;
13772 int ic = FALSE;
13773
13774 rettv->vval.v_number = -1;
13775 if (argvars[0].v_type != VAR_LIST)
13776 {
13777 EMSG(_(e_listreq));
13778 return;
13779 }
13780 l = argvars[0].vval.v_list;
13781 if (l != NULL)
13782 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013783 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013784 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 int error = FALSE;
13787
Bram Moolenaar758711c2005-02-02 23:11:38 +000013788 /* Start at specified item. Use the cached index that list_find()
13789 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013790 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013791 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013792 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 ic = get_tv_number_chk(&argvars[3], &error);
13794 if (error)
13795 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013796 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013797
Bram Moolenaar758711c2005-02-02 23:11:38 +000013798 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013799 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013800 {
13801 rettv->vval.v_number = idx;
13802 break;
13803 }
13804 }
13805}
13806
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807static int inputsecret_flag = 0;
13808
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013809static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13810
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013812 * This function is used by f_input() and f_inputdialog() functions. The third
13813 * argument to f_input() specifies the type of completion to use at the
13814 * prompt. The third argument to f_inputdialog() specifies the value to return
13815 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 */
13817 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013818get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 typval_T *argvars;
13820 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013821 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 char_u *p = NULL;
13825 int c;
13826 char_u buf[NUMBUFLEN];
13827 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013829 int xp_type = EXPAND_NOTHING;
13830 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834
13835#ifdef NO_CONSOLE_INPUT
13836 /* While starting up, there is no place to enter text. */
13837 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839#endif
13840
13841 cmd_silent = FALSE; /* Want to see the prompt. */
13842 if (prompt != NULL)
13843 {
13844 /* Only the part of the message after the last NL is considered as
13845 * prompt for the command line */
13846 p = vim_strrchr(prompt, '\n');
13847 if (p == NULL)
13848 p = prompt;
13849 else
13850 {
13851 ++p;
13852 c = *p;
13853 *p = NUL;
13854 msg_start();
13855 msg_clr_eos();
13856 msg_puts_attr(prompt, echo_attr);
13857 msg_didout = FALSE;
13858 msg_starthere();
13859 *p = c;
13860 }
13861 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 if (argvars[1].v_type != VAR_UNKNOWN)
13864 {
13865 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13866 if (defstr != NULL)
13867 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013869 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013870 {
13871 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013872 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013873 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013874
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013875 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013876 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013877
Bram Moolenaar4463f292005-09-25 22:20:24 +000013878 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13879 if (xp_name == NULL)
13880 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013881
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013882 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013883
Bram Moolenaar4463f292005-09-25 22:20:24 +000013884 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13885 &xp_arg) == FAIL)
13886 return;
13887 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013888 }
13889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013891 {
13892# ifdef FEAT_EX_EXTRA
13893 int save_ex_normal_busy = ex_normal_busy;
13894 ex_normal_busy = 0;
13895# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013897 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13898 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013899# ifdef FEAT_EX_EXTRA
13900 ex_normal_busy = save_ex_normal_busy;
13901# endif
13902 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013903 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013904 && argvars[1].v_type != VAR_UNKNOWN
13905 && argvars[2].v_type != VAR_UNKNOWN)
13906 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13907 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013908
13909 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911 /* since the user typed this, no need to wait for return */
13912 need_wait_return = FALSE;
13913 msg_didout = FALSE;
13914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 cmd_silent = cmd_silent_save;
13916}
13917
13918/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013919 * "input()" function
13920 * Also handles inputsecret() when inputsecret is set.
13921 */
13922 static void
13923f_input(argvars, rettv)
13924 typval_T *argvars;
13925 typval_T *rettv;
13926{
13927 get_user_input(argvars, rettv, FALSE);
13928}
13929
13930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 * "inputdialog()" function
13932 */
13933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013935 typval_T *argvars;
13936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937{
13938#if defined(FEAT_GUI_TEXTDIALOG)
13939 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13940 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13941 {
13942 char_u *message;
13943 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 message = get_tv_string_chk(&argvars[0]);
13947 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013948 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013949 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 else
13951 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 if (message != NULL && defstr != NULL
13953 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013954 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 else
13957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 if (message != NULL && defstr != NULL
13959 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013960 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 rettv->vval.v_string = vim_strsave(
13962 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 }
13968 else
13969#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013970 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971}
13972
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013973/*
13974 * "inputlist()" function
13975 */
13976 static void
13977f_inputlist(argvars, rettv)
13978 typval_T *argvars;
13979 typval_T *rettv;
13980{
13981 listitem_T *li;
13982 int selected;
13983 int mouse_used;
13984
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013985#ifdef NO_CONSOLE_INPUT
13986 /* While starting up, there is no place to enter text. */
13987 if (no_console_input())
13988 return;
13989#endif
13990 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13991 {
13992 EMSG2(_(e_listarg), "inputlist()");
13993 return;
13994 }
13995
13996 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013997 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013998 lines_left = Rows; /* avoid more prompt */
13999 msg_scroll = TRUE;
14000 msg_clr_eos();
14001
14002 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14003 {
14004 msg_puts(get_tv_string(&li->li_tv));
14005 msg_putchar('\n');
14006 }
14007
14008 /* Ask for choice. */
14009 selected = prompt_for_number(&mouse_used);
14010 if (mouse_used)
14011 selected -= lines_left;
14012
14013 rettv->vval.v_number = selected;
14014}
14015
14016
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14018
14019/*
14020 * "inputrestore()" function
14021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014024 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026{
14027 if (ga_userinput.ga_len > 0)
14028 {
14029 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14031 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014032 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 }
14034 else if (p_verbose > 1)
14035 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014036 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 }
14039}
14040
14041/*
14042 * "inputsave()" function
14043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014049 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 if (ga_grow(&ga_userinput, 1) == OK)
14051 {
14052 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14053 + ga_userinput.ga_len);
14054 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014055 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 }
14057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059}
14060
14061/*
14062 * "inputsecret()" function
14063 */
14064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 typval_T *argvars;
14067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068{
14069 ++cmdline_star;
14070 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 --cmdline_star;
14073 --inputsecret_flag;
14074}
14075
14076/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014077 * "insert()" function
14078 */
14079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014081 typval_T *argvars;
14082 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083{
14084 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014085 listitem_T *item;
14086 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014088
14089 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014091 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014092 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014093 {
14094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014095 before = get_tv_number_chk(&argvars[2], &error);
14096 if (error)
14097 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014098
Bram Moolenaar758711c2005-02-02 23:11:38 +000014099 if (before == l->lv_len)
14100 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014101 else
14102 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014103 item = list_find(l, before);
14104 if (item == NULL)
14105 {
14106 EMSGN(_(e_listidx), before);
14107 l = NULL;
14108 }
14109 }
14110 if (l != NULL)
14111 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014112 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014113 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014114 }
14115 }
14116}
14117
14118/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014119 * "invert(expr)" function
14120 */
14121 static void
14122f_invert(argvars, rettv)
14123 typval_T *argvars;
14124 typval_T *rettv;
14125{
14126 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14127}
14128
14129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 * "isdirectory()" function
14131 */
14132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138}
14139
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014140/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014141 * "islocked()" function
14142 */
14143 static void
14144f_islocked(argvars, rettv)
14145 typval_T *argvars;
14146 typval_T *rettv;
14147{
14148 lval_T lv;
14149 char_u *end;
14150 dictitem_T *di;
14151
14152 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014153 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14154 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014155 if (end != NULL && lv.ll_name != NULL)
14156 {
14157 if (*end != NUL)
14158 EMSG(_(e_trailing));
14159 else
14160 {
14161 if (lv.ll_tv == NULL)
14162 {
14163 if (check_changedtick(lv.ll_name))
14164 rettv->vval.v_number = 1; /* always locked */
14165 else
14166 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014167 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014168 if (di != NULL)
14169 {
14170 /* Consider a variable locked when:
14171 * 1. the variable itself is locked
14172 * 2. the value of the variable is locked.
14173 * 3. the List or Dict value is locked.
14174 */
14175 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14176 || tv_islocked(&di->di_tv));
14177 }
14178 }
14179 }
14180 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014181 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014182 else if (lv.ll_newkey != NULL)
14183 EMSG2(_(e_dictkey), lv.ll_newkey);
14184 else if (lv.ll_list != NULL)
14185 /* List item. */
14186 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14187 else
14188 /* Dictionary item. */
14189 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14190 }
14191 }
14192
14193 clear_lval(&lv);
14194}
14195
Bram Moolenaar33570922005-01-25 22:26:29 +000014196static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014197
14198/*
14199 * Turn a dict into a list:
14200 * "what" == 0: list of keys
14201 * "what" == 1: list of values
14202 * "what" == 2: list of items
14203 */
14204 static void
14205dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *argvars;
14207 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014208 int what;
14209{
Bram Moolenaar33570922005-01-25 22:26:29 +000014210 list_T *l2;
14211 dictitem_T *di;
14212 hashitem_T *hi;
14213 listitem_T *li;
14214 listitem_T *li2;
14215 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014216 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014217
Bram Moolenaar8c711452005-01-14 21:53:12 +000014218 if (argvars[0].v_type != VAR_DICT)
14219 {
14220 EMSG(_(e_dictreq));
14221 return;
14222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014223 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014224 return;
14225
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014226 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014227 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014229 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014230 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014231 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014232 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014233 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014234 --todo;
14235 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014237 li = listitem_alloc();
14238 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014239 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014240 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014241
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014242 if (what == 0)
14243 {
14244 /* keys() */
14245 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014246 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014247 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14248 }
14249 else if (what == 1)
14250 {
14251 /* values() */
14252 copy_tv(&di->di_tv, &li->li_tv);
14253 }
14254 else
14255 {
14256 /* items() */
14257 l2 = list_alloc();
14258 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014259 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014260 li->li_tv.vval.v_list = l2;
14261 if (l2 == NULL)
14262 break;
14263 ++l2->lv_refcount;
14264
14265 li2 = listitem_alloc();
14266 if (li2 == NULL)
14267 break;
14268 list_append(l2, li2);
14269 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014270 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014271 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14272
14273 li2 = listitem_alloc();
14274 if (li2 == NULL)
14275 break;
14276 list_append(l2, li2);
14277 copy_tv(&di->di_tv, &li2->li_tv);
14278 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014279 }
14280 }
14281}
14282
14283/*
14284 * "items(dict)" function
14285 */
14286 static void
14287f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014288 typval_T *argvars;
14289 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014290{
14291 dict_list(argvars, rettv, 2);
14292}
14293
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014295 * "join()" function
14296 */
14297 static void
14298f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014299 typval_T *argvars;
14300 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014301{
14302 garray_T ga;
14303 char_u *sep;
14304
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014305 if (argvars[0].v_type != VAR_LIST)
14306 {
14307 EMSG(_(e_listreq));
14308 return;
14309 }
14310 if (argvars[0].vval.v_list == NULL)
14311 return;
14312 if (argvars[1].v_type == VAR_UNKNOWN)
14313 sep = (char_u *)" ";
14314 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014316
14317 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318
14319 if (sep != NULL)
14320 {
14321 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014322 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 ga_append(&ga, NUL);
14324 rettv->vval.v_string = (char_u *)ga.ga_data;
14325 }
14326 else
14327 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014328}
14329
14330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014331 * "keys()" function
14332 */
14333 static void
14334f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 typval_T *argvars;
14336 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014337{
14338 dict_list(argvars, rettv, 0);
14339}
14340
14341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 * "last_buffer_nr()" function.
14343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
14349 int n = 0;
14350 buf_T *buf;
14351
14352 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14353 if (n < buf->b_fnum)
14354 n = buf->b_fnum;
14355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014357}
14358
14359/*
14360 * "len()" function
14361 */
14362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 typval_T *argvars;
14365 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014366{
14367 switch (argvars[0].v_type)
14368 {
14369 case VAR_STRING:
14370 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 rettv->vval.v_number = (varnumber_T)STRLEN(
14372 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014373 break;
14374 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014376 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014377 case VAR_DICT:
14378 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14379 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014380 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014381 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014382 break;
14383 }
14384}
14385
Bram Moolenaar33570922005-01-25 22:26:29 +000014386static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014387
14388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014390 typval_T *argvars;
14391 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014392 int type;
14393{
14394#ifdef FEAT_LIBCALL
14395 char_u *string_in;
14396 char_u **string_result;
14397 int nr_result;
14398#endif
14399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014401 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014403
14404 if (check_restricted() || check_secure())
14405 return;
14406
14407#ifdef FEAT_LIBCALL
14408 /* The first two args must be strings, otherwise its meaningless */
14409 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14410 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014411 string_in = NULL;
14412 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014413 string_in = argvars[2].vval.v_string;
14414 if (type == VAR_NUMBER)
14415 string_result = NULL;
14416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014418 if (mch_libcall(argvars[0].vval.v_string,
14419 argvars[1].vval.v_string,
14420 string_in,
14421 argvars[2].vval.v_number,
14422 string_result,
14423 &nr_result) == OK
14424 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 }
14427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428}
14429
14430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 * "libcall()" function
14432 */
14433 static void
14434f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 typval_T *argvars;
14436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437{
14438 libcall_common(argvars, rettv, VAR_STRING);
14439}
14440
14441/*
14442 * "libcallnr()" function
14443 */
14444 static void
14445f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014446 typval_T *argvars;
14447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448{
14449 libcall_common(argvars, rettv, VAR_NUMBER);
14450}
14451
14452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 * "line(string)" function
14454 */
14455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 typval_T *argvars;
14458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459{
14460 linenr_T lnum = 0;
14461 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014462 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014464 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 if (fp != NULL)
14466 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468}
14469
14470/*
14471 * "line2byte(lnum)" function
14472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477{
14478#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480#else
14481 linenr_T lnum;
14482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14488 if (rettv->vval.v_number >= 0)
14489 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490#endif
14491}
14492
14493/*
14494 * "lispindent(lnum)" function
14495 */
14496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014498 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500{
14501#ifdef FEAT_LISP
14502 pos_T pos;
14503 linenr_T lnum;
14504
14505 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14508 {
14509 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014510 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 curwin->w_cursor = pos;
14512 }
14513 else
14514#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516}
14517
14518/*
14519 * "localtime()" function
14520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014523 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527}
14528
Bram Moolenaar33570922005-01-25 22:26:29 +000014529static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530
14531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014533 typval_T *argvars;
14534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 int exact;
14536{
14537 char_u *keys;
14538 char_u *which;
14539 char_u buf[NUMBUFLEN];
14540 char_u *keys_buf = NULL;
14541 char_u *rhs;
14542 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014543 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014544 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014545 mapblock_T *mp;
14546 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547
14548 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014549 rettv->v_type = VAR_STRING;
14550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 if (*keys == NUL)
14554 return;
14555
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014560 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014561 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014562 if (argvars[3].v_type != VAR_UNKNOWN)
14563 get_dict = get_tv_number(&argvars[3]);
14564 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 else
14567 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 if (which == NULL)
14569 return;
14570
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571 mode = get_map_mode(&which, 0);
14572
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014573 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014574 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014576
14577 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014579 /* Return a string. */
14580 if (rhs != NULL)
14581 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582
Bram Moolenaarbd743252010-10-20 21:23:33 +020014583 }
14584 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14585 {
14586 /* Return a dictionary. */
14587 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14588 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14589 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590
Bram Moolenaarbd743252010-10-20 21:23:33 +020014591 dict_add_nr_str(dict, "lhs", 0L, lhs);
14592 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14593 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14594 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14595 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14596 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14597 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014598 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014599 dict_add_nr_str(dict, "mode", 0L, mapmode);
14600
14601 vim_free(lhs);
14602 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 }
14604}
14605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014606#ifdef FEAT_FLOAT
14607/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014608 * "log()" function
14609 */
14610 static void
14611f_log(argvars, rettv)
14612 typval_T *argvars;
14613 typval_T *rettv;
14614{
14615 float_T f;
14616
14617 rettv->v_type = VAR_FLOAT;
14618 if (get_float_arg(argvars, &f) == OK)
14619 rettv->vval.v_float = log(f);
14620 else
14621 rettv->vval.v_float = 0.0;
14622}
14623
14624/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014625 * "log10()" function
14626 */
14627 static void
14628f_log10(argvars, rettv)
14629 typval_T *argvars;
14630 typval_T *rettv;
14631{
14632 float_T f;
14633
14634 rettv->v_type = VAR_FLOAT;
14635 if (get_float_arg(argvars, &f) == OK)
14636 rettv->vval.v_float = log10(f);
14637 else
14638 rettv->vval.v_float = 0.0;
14639}
14640#endif
14641
Bram Moolenaar1dced572012-04-05 16:54:08 +020014642#ifdef FEAT_LUA
14643/*
14644 * "luaeval()" function
14645 */
14646 static void
14647f_luaeval(argvars, rettv)
14648 typval_T *argvars;
14649 typval_T *rettv;
14650{
14651 char_u *str;
14652 char_u buf[NUMBUFLEN];
14653
14654 str = get_tv_string_buf(&argvars[0], buf);
14655 do_luaeval(str, argvars + 1, rettv);
14656}
14657#endif
14658
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014660 * "map()" function
14661 */
14662 static void
14663f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014664 typval_T *argvars;
14665 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014666{
14667 filter_map(argvars, rettv, TRUE);
14668}
14669
14670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 */
14673 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014674f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014675 typval_T *argvars;
14676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
14681/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 */
14684 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 typval_T *argvars;
14687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690}
14691
Bram Moolenaar33570922005-01-25 22:26:29 +000014692static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693
14694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014696 typval_T *argvars;
14697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 int type;
14699{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014700 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014701 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014702 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 char_u *pat;
14704 regmatch_T regmatch;
14705 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014706 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 char_u *save_cpo;
14708 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014709 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014710 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014711 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014712 list_T *l = NULL;
14713 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014714 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014715 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716
14717 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14718 save_cpo = p_cpo;
14719 p_cpo = (char_u *)"";
14720
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 rettv->vval.v_number = -1;
14722 if (type == 3)
14723 {
14724 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014725 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 }
14728 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014730 rettv->v_type = VAR_STRING;
14731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014734 if (argvars[0].v_type == VAR_LIST)
14735 {
14736 if ((l = argvars[0].vval.v_list) == NULL)
14737 goto theend;
14738 li = l->lv_first;
14739 }
14740 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014741 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014742 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014743 len = (long)STRLEN(str);
14744 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14747 if (pat == NULL)
14748 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014750 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 int error = FALSE;
14753
14754 start = get_tv_number_chk(&argvars[2], &error);
14755 if (error)
14756 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014757 if (l != NULL)
14758 {
14759 li = list_find(l, start);
14760 if (li == NULL)
14761 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014762 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014763 }
14764 else
14765 {
14766 if (start < 0)
14767 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014768 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014769 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014770 /* When "count" argument is there ignore matches before "start",
14771 * otherwise skip part of the string. Differs when pattern is "^"
14772 * or "\<". */
14773 if (argvars[3].v_type != VAR_UNKNOWN)
14774 startcol = start;
14775 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014776 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014777 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014778 len -= start;
14779 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014780 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014781
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014782 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014783 nth = get_tv_number_chk(&argvars[3], &error);
14784 if (error)
14785 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786 }
14787
14788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14789 if (regmatch.regprog != NULL)
14790 {
14791 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014792
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014793 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014794 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014795 if (l != NULL)
14796 {
14797 if (li == NULL)
14798 {
14799 match = FALSE;
14800 break;
14801 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014802 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014803 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014804 if (str == NULL)
14805 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014806 }
14807
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014808 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014809
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014810 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014811 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014812 if (l == NULL && !match)
14813 break;
14814
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014815 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014816 if (l != NULL)
14817 {
14818 li = li->li_next;
14819 ++idx;
14820 }
14821 else
14822 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014823#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014824 startcol = (colnr_T)(regmatch.startp[0]
14825 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014826#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014827 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014828#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014829 if (startcol > (colnr_T)len
14830 || str + startcol <= regmatch.startp[0])
14831 {
14832 match = FALSE;
14833 break;
14834 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014835 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014836 }
14837
14838 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014841 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014842 int i;
14843
14844 /* return list with matched string and submatches */
14845 for (i = 0; i < NSUBEXP; ++i)
14846 {
14847 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014848 {
14849 if (list_append_string(rettv->vval.v_list,
14850 (char_u *)"", 0) == FAIL)
14851 break;
14852 }
14853 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014854 regmatch.startp[i],
14855 (int)(regmatch.endp[i] - regmatch.startp[i]))
14856 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858 }
14859 }
14860 else if (type == 2)
14861 {
14862 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014863 if (l != NULL)
14864 copy_tv(&li->li_tv, rettv);
14865 else
14866 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014868 }
14869 else if (l != NULL)
14870 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 else
14872 {
14873 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 (varnumber_T)(regmatch.startp[0] - str);
14876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014877 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014879 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 }
14881 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014882 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 }
14884
14885theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014886 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 p_cpo = save_cpo;
14888}
14889
14890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014891 * "match()" function
14892 */
14893 static void
14894f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014895 typval_T *argvars;
14896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897{
14898 find_some_match(argvars, rettv, 1);
14899}
14900
14901/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014902 * "matchadd()" function
14903 */
14904 static void
14905f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014906 typval_T *argvars UNUSED;
14907 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014908{
14909#ifdef FEAT_SEARCH_EXTRA
14910 char_u buf[NUMBUFLEN];
14911 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14912 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14913 int prio = 10; /* default priority */
14914 int id = -1;
14915 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014916 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014917
14918 rettv->vval.v_number = -1;
14919
14920 if (grp == NULL || pat == NULL)
14921 return;
14922 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014923 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014924 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014925 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014926 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014927 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014928 if (argvars[4].v_type != VAR_UNKNOWN)
14929 {
14930 if (argvars[4].v_type != VAR_DICT)
14931 {
14932 EMSG(_(e_dictreq));
14933 return;
14934 }
14935 if (dict_find(argvars[4].vval.v_dict,
14936 (char_u *)"conceal", -1) != NULL)
14937 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14938 (char_u *)"conceal", FALSE);
14939 }
14940 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014941 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014942 if (error == TRUE)
14943 return;
14944 if (id >= 1 && id <= 3)
14945 {
14946 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14947 return;
14948 }
14949
Bram Moolenaar6561d522015-07-21 15:48:27 +020014950 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14951 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014952#endif
14953}
14954
14955/*
14956 * "matchaddpos()" function
14957 */
14958 static void
14959f_matchaddpos(argvars, rettv)
14960 typval_T *argvars UNUSED;
14961 typval_T *rettv UNUSED;
14962{
14963#ifdef FEAT_SEARCH_EXTRA
14964 char_u buf[NUMBUFLEN];
14965 char_u *group;
14966 int prio = 10;
14967 int id = -1;
14968 int error = FALSE;
14969 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014970 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014971
14972 rettv->vval.v_number = -1;
14973
14974 group = get_tv_string_buf_chk(&argvars[0], buf);
14975 if (group == NULL)
14976 return;
14977
14978 if (argvars[1].v_type != VAR_LIST)
14979 {
14980 EMSG2(_(e_listarg), "matchaddpos()");
14981 return;
14982 }
14983 l = argvars[1].vval.v_list;
14984 if (l == NULL)
14985 return;
14986
14987 if (argvars[2].v_type != VAR_UNKNOWN)
14988 {
14989 prio = get_tv_number_chk(&argvars[2], &error);
14990 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014991 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014992 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014993 if (argvars[4].v_type != VAR_UNKNOWN)
14994 {
14995 if (argvars[4].v_type != VAR_DICT)
14996 {
14997 EMSG(_(e_dictreq));
14998 return;
14999 }
15000 if (dict_find(argvars[4].vval.v_dict,
15001 (char_u *)"conceal", -1) != NULL)
15002 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15003 (char_u *)"conceal", FALSE);
15004 }
15005 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015006 }
15007 if (error == TRUE)
15008 return;
15009
15010 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15011 if (id == 1 || id == 2)
15012 {
15013 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15014 return;
15015 }
15016
Bram Moolenaar6561d522015-07-21 15:48:27 +020015017 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15018 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015019#endif
15020}
15021
15022/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015023 * "matcharg()" function
15024 */
15025 static void
15026f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015027 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015028 typval_T *rettv;
15029{
15030 if (rettv_list_alloc(rettv) == OK)
15031 {
15032#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015033 int id = get_tv_number(&argvars[0]);
15034 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015035
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015036 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015037 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015038 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15039 {
15040 list_append_string(rettv->vval.v_list,
15041 syn_id2name(m->hlg_id), -1);
15042 list_append_string(rettv->vval.v_list, m->pattern, -1);
15043 }
15044 else
15045 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015046 list_append_string(rettv->vval.v_list, NULL, -1);
15047 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015048 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015049 }
15050#endif
15051 }
15052}
15053
15054/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015055 * "matchdelete()" function
15056 */
15057 static void
15058f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015059 typval_T *argvars UNUSED;
15060 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015061{
15062#ifdef FEAT_SEARCH_EXTRA
15063 rettv->vval.v_number = match_delete(curwin,
15064 (int)get_tv_number(&argvars[0]), TRUE);
15065#endif
15066}
15067
15068/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069 * "matchend()" function
15070 */
15071 static void
15072f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015073 typval_T *argvars;
15074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075{
15076 find_some_match(argvars, rettv, 0);
15077}
15078
15079/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015080 * "matchlist()" function
15081 */
15082 static void
15083f_matchlist(argvars, rettv)
15084 typval_T *argvars;
15085 typval_T *rettv;
15086{
15087 find_some_match(argvars, rettv, 3);
15088}
15089
15090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 * "matchstr()" function
15092 */
15093 static void
15094f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 typval_T *argvars;
15096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097{
15098 find_some_match(argvars, rettv, 2);
15099}
15100
Bram Moolenaar33570922005-01-25 22:26:29 +000015101static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015102
15103 static void
15104max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 typval_T *argvars;
15106 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015107 int domax;
15108{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015109 long n = 0;
15110 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015111 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015112
15113 if (argvars[0].v_type == VAR_LIST)
15114 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 list_T *l;
15116 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015117
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015118 l = argvars[0].vval.v_list;
15119 if (l != NULL)
15120 {
15121 li = l->lv_first;
15122 if (li != NULL)
15123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015124 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015125 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015126 {
15127 li = li->li_next;
15128 if (li == NULL)
15129 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015130 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015131 if (domax ? i > n : i < n)
15132 n = i;
15133 }
15134 }
15135 }
15136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015137 else if (argvars[0].v_type == VAR_DICT)
15138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015140 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015142 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015143
15144 d = argvars[0].vval.v_dict;
15145 if (d != NULL)
15146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015147 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015150 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015152 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015153 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015154 if (first)
15155 {
15156 n = i;
15157 first = FALSE;
15158 }
15159 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015160 n = i;
15161 }
15162 }
15163 }
15164 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015165 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015166 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015168}
15169
15170/*
15171 * "max()" function
15172 */
15173 static void
15174f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015175 typval_T *argvars;
15176 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015177{
15178 max_min(argvars, rettv, TRUE);
15179}
15180
15181/*
15182 * "min()" function
15183 */
15184 static void
15185f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015186 typval_T *argvars;
15187 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015188{
15189 max_min(argvars, rettv, FALSE);
15190}
15191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015192static int mkdir_recurse __ARGS((char_u *dir, int prot));
15193
15194/*
15195 * Create the directory in which "dir" is located, and higher levels when
15196 * needed.
15197 */
15198 static int
15199mkdir_recurse(dir, prot)
15200 char_u *dir;
15201 int prot;
15202{
15203 char_u *p;
15204 char_u *updir;
15205 int r = FAIL;
15206
15207 /* Get end of directory name in "dir".
15208 * We're done when it's "/" or "c:/". */
15209 p = gettail_sep(dir);
15210 if (p <= get_past_head(dir))
15211 return OK;
15212
15213 /* If the directory exists we're done. Otherwise: create it.*/
15214 updir = vim_strnsave(dir, (int)(p - dir));
15215 if (updir == NULL)
15216 return FAIL;
15217 if (mch_isdir(updir))
15218 r = OK;
15219 else if (mkdir_recurse(updir, prot) == OK)
15220 r = vim_mkdir_emsg(updir, prot);
15221 vim_free(updir);
15222 return r;
15223}
15224
15225#ifdef vim_mkdir
15226/*
15227 * "mkdir()" function
15228 */
15229 static void
15230f_mkdir(argvars, rettv)
15231 typval_T *argvars;
15232 typval_T *rettv;
15233{
15234 char_u *dir;
15235 char_u buf[NUMBUFLEN];
15236 int prot = 0755;
15237
15238 rettv->vval.v_number = FAIL;
15239 if (check_restricted() || check_secure())
15240 return;
15241
15242 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015243 if (*dir == NUL)
15244 rettv->vval.v_number = FAIL;
15245 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015246 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015247 if (*gettail(dir) == NUL)
15248 /* remove trailing slashes */
15249 *gettail_sep(dir) = NUL;
15250
15251 if (argvars[1].v_type != VAR_UNKNOWN)
15252 {
15253 if (argvars[2].v_type != VAR_UNKNOWN)
15254 prot = get_tv_number_chk(&argvars[2], NULL);
15255 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15256 mkdir_recurse(dir, prot);
15257 }
15258 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015259 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015260}
15261#endif
15262
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 * "mode()" function
15265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *argvars;
15269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015271 char_u buf[3];
15272
15273 buf[1] = NUL;
15274 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 if (VIsual_active)
15277 {
15278 if (VIsual_select)
15279 buf[0] = VIsual_mode + 's' - 'v';
15280 else
15281 buf[0] = VIsual_mode;
15282 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015283 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015284 || State == CONFIRM)
15285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015287 if (State == ASKMORE)
15288 buf[1] = 'm';
15289 else if (State == CONFIRM)
15290 buf[1] = '?';
15291 }
15292 else if (State == EXTERNCMD)
15293 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 else if (State & INSERT)
15295 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015296#ifdef FEAT_VREPLACE
15297 if (State & VREPLACE_FLAG)
15298 {
15299 buf[0] = 'R';
15300 buf[1] = 'v';
15301 }
15302 else
15303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 if (State & REPLACE_FLAG)
15305 buf[0] = 'R';
15306 else
15307 buf[0] = 'i';
15308 }
15309 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015312 if (exmode_active)
15313 buf[1] = 'v';
15314 }
15315 else if (exmode_active)
15316 {
15317 buf[0] = 'c';
15318 buf[1] = 'e';
15319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015323 if (finish_op)
15324 buf[1] = 'o';
15325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015327 /* Clear out the minor mode when the argument is not a non-zero number or
15328 * non-empty string. */
15329 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015330 buf[1] = NUL;
15331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 rettv->vval.v_string = vim_strsave(buf);
15333 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334}
15335
Bram Moolenaar429fa852013-04-15 12:27:36 +020015336#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015337/*
15338 * "mzeval()" function
15339 */
15340 static void
15341f_mzeval(argvars, rettv)
15342 typval_T *argvars;
15343 typval_T *rettv;
15344{
15345 char_u *str;
15346 char_u buf[NUMBUFLEN];
15347
15348 str = get_tv_string_buf(&argvars[0], buf);
15349 do_mzeval(str, rettv);
15350}
Bram Moolenaar75676462013-01-30 14:55:42 +010015351
15352 void
15353mzscheme_call_vim(name, args, rettv)
15354 char_u *name;
15355 typval_T *args;
15356 typval_T *rettv;
15357{
15358 typval_T argvars[3];
15359
15360 argvars[0].v_type = VAR_STRING;
15361 argvars[0].vval.v_string = name;
15362 copy_tv(args, &argvars[1]);
15363 argvars[2].v_type = VAR_UNKNOWN;
15364 f_call(argvars, rettv);
15365 clear_tv(&argvars[1]);
15366}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015367#endif
15368
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015370 * "nextnonblank()" function
15371 */
15372 static void
15373f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015374 typval_T *argvars;
15375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015376{
15377 linenr_T lnum;
15378
15379 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015381 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015382 {
15383 lnum = 0;
15384 break;
15385 }
15386 if (*skipwhite(ml_get(lnum)) != NUL)
15387 break;
15388 }
15389 rettv->vval.v_number = lnum;
15390}
15391
15392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 * "nr2char()" function
15394 */
15395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015397 typval_T *argvars;
15398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399{
15400 char_u buf[NUMBUFLEN];
15401
15402#ifdef FEAT_MBYTE
15403 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015404 {
15405 int utf8 = 0;
15406
15407 if (argvars[1].v_type != VAR_UNKNOWN)
15408 utf8 = get_tv_number_chk(&argvars[1], NULL);
15409 if (utf8)
15410 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15411 else
15412 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 else
15415#endif
15416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 buf[1] = NUL;
15419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->v_type = VAR_STRING;
15421 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015422}
15423
15424/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015425 * "or(expr, expr)" function
15426 */
15427 static void
15428f_or(argvars, rettv)
15429 typval_T *argvars;
15430 typval_T *rettv;
15431{
15432 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15433 | get_tv_number_chk(&argvars[1], NULL);
15434}
15435
15436/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015437 * "pathshorten()" function
15438 */
15439 static void
15440f_pathshorten(argvars, rettv)
15441 typval_T *argvars;
15442 typval_T *rettv;
15443{
15444 char_u *p;
15445
15446 rettv->v_type = VAR_STRING;
15447 p = get_tv_string_chk(&argvars[0]);
15448 if (p == NULL)
15449 rettv->vval.v_string = NULL;
15450 else
15451 {
15452 p = vim_strsave(p);
15453 rettv->vval.v_string = p;
15454 if (p != NULL)
15455 shorten_dir(p);
15456 }
15457}
15458
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015459#ifdef FEAT_FLOAT
15460/*
15461 * "pow()" function
15462 */
15463 static void
15464f_pow(argvars, rettv)
15465 typval_T *argvars;
15466 typval_T *rettv;
15467{
15468 float_T fx, fy;
15469
15470 rettv->v_type = VAR_FLOAT;
15471 if (get_float_arg(argvars, &fx) == OK
15472 && get_float_arg(&argvars[1], &fy) == OK)
15473 rettv->vval.v_float = pow(fx, fy);
15474 else
15475 rettv->vval.v_float = 0.0;
15476}
15477#endif
15478
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015480 * "prevnonblank()" function
15481 */
15482 static void
15483f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 typval_T *argvars;
15485 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015486{
15487 linenr_T lnum;
15488
15489 lnum = get_tv_lnum(argvars);
15490 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15491 lnum = 0;
15492 else
15493 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15494 --lnum;
15495 rettv->vval.v_number = lnum;
15496}
15497
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015498#ifdef HAVE_STDARG_H
15499/* This dummy va_list is here because:
15500 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15501 * - locally in the function results in a "used before set" warning
15502 * - using va_start() to initialize it gives "function with fixed args" error */
15503static va_list ap;
15504#endif
15505
Bram Moolenaar8c711452005-01-14 21:53:12 +000015506/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015507 * "printf()" function
15508 */
15509 static void
15510f_printf(argvars, rettv)
15511 typval_T *argvars;
15512 typval_T *rettv;
15513{
15514 rettv->v_type = VAR_STRING;
15515 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015516#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015517 {
15518 char_u buf[NUMBUFLEN];
15519 int len;
15520 char_u *s;
15521 int saved_did_emsg = did_emsg;
15522 char *fmt;
15523
15524 /* Get the required length, allocate the buffer and do it for real. */
15525 did_emsg = FALSE;
15526 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015527 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015528 if (!did_emsg)
15529 {
15530 s = alloc(len + 1);
15531 if (s != NULL)
15532 {
15533 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015534 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015535 }
15536 }
15537 did_emsg |= saved_did_emsg;
15538 }
15539#endif
15540}
15541
15542/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015543 * "pumvisible()" function
15544 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015545 static void
15546f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015547 typval_T *argvars UNUSED;
15548 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015549{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015550#ifdef FEAT_INS_EXPAND
15551 if (pum_visible())
15552 rettv->vval.v_number = 1;
15553#endif
15554}
15555
Bram Moolenaardb913952012-06-29 12:54:53 +020015556#ifdef FEAT_PYTHON3
15557/*
15558 * "py3eval()" function
15559 */
15560 static void
15561f_py3eval(argvars, rettv)
15562 typval_T *argvars;
15563 typval_T *rettv;
15564{
15565 char_u *str;
15566 char_u buf[NUMBUFLEN];
15567
15568 str = get_tv_string_buf(&argvars[0], buf);
15569 do_py3eval(str, rettv);
15570}
15571#endif
15572
15573#ifdef FEAT_PYTHON
15574/*
15575 * "pyeval()" function
15576 */
15577 static void
15578f_pyeval(argvars, rettv)
15579 typval_T *argvars;
15580 typval_T *rettv;
15581{
15582 char_u *str;
15583 char_u buf[NUMBUFLEN];
15584
15585 str = get_tv_string_buf(&argvars[0], buf);
15586 do_pyeval(str, rettv);
15587}
15588#endif
15589
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015590/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015591 * "range()" function
15592 */
15593 static void
15594f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 typval_T *argvars;
15596 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015597{
15598 long start;
15599 long end;
15600 long stride = 1;
15601 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015602 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015604 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015605 if (argvars[1].v_type == VAR_UNKNOWN)
15606 {
15607 end = start - 1;
15608 start = 0;
15609 }
15610 else
15611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015614 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015615 }
15616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015617 if (error)
15618 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015619 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015620 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015621 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015622 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015623 else
15624 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015625 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015626 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015627 if (list_append_number(rettv->vval.v_list,
15628 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015629 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015630 }
15631}
15632
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015633/*
15634 * "readfile()" function
15635 */
15636 static void
15637f_readfile(argvars, rettv)
15638 typval_T *argvars;
15639 typval_T *rettv;
15640{
15641 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015642 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015643 char_u *fname;
15644 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015645 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15646 int io_size = sizeof(buf);
15647 int readlen; /* size of last fread() */
15648 char_u *prev = NULL; /* previously read bytes, if any */
15649 long prevlen = 0; /* length of data in prev */
15650 long prevsize = 0; /* size of prev buffer */
15651 long maxline = MAXLNUM;
15652 long cnt = 0;
15653 char_u *p; /* position in buf */
15654 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015655
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015656 if (argvars[1].v_type != VAR_UNKNOWN)
15657 {
15658 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15659 binary = TRUE;
15660 if (argvars[2].v_type != VAR_UNKNOWN)
15661 maxline = get_tv_number(&argvars[2]);
15662 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015663
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015664 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015665 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015666
15667 /* Always open the file in binary mode, library functions have a mind of
15668 * their own about CR-LF conversion. */
15669 fname = get_tv_string(&argvars[0]);
15670 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15671 {
15672 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15673 return;
15674 }
15675
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015676 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015677 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015678 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015679
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015680 /* This for loop processes what was read, but is also entered at end
15681 * of file so that either:
15682 * - an incomplete line gets written
15683 * - a "binary" file gets an empty line at the end if it ends in a
15684 * newline. */
15685 for (p = buf, start = buf;
15686 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15687 ++p)
15688 {
15689 if (*p == '\n' || readlen <= 0)
15690 {
15691 listitem_T *li;
15692 char_u *s = NULL;
15693 long_u len = p - start;
15694
15695 /* Finished a line. Remove CRs before NL. */
15696 if (readlen > 0 && !binary)
15697 {
15698 while (len > 0 && start[len - 1] == '\r')
15699 --len;
15700 /* removal may cross back to the "prev" string */
15701 if (len == 0)
15702 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15703 --prevlen;
15704 }
15705 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015706 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707 else
15708 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015709 /* Change "prev" buffer to be the right size. This way
15710 * the bytes are only copied once, and very long lines are
15711 * allocated only once. */
15712 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015713 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015714 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015715 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015716 prev = NULL; /* the list will own the string */
15717 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015718 }
15719 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015720 if (s == NULL)
15721 {
15722 do_outofmem_msg((long_u) prevlen + len + 1);
15723 failed = TRUE;
15724 break;
15725 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015727 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015728 {
15729 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015730 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731 break;
15732 }
15733 li->li_tv.v_type = VAR_STRING;
15734 li->li_tv.v_lock = 0;
15735 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015736 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015737
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015738 start = p + 1; /* step over newline */
15739 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015740 break;
15741 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015742 else if (*p == NUL)
15743 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015744#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015745 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15746 * when finding the BF and check the previous two bytes. */
15747 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015748 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015749 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15750 * + 1, these may be in the "prev" string. */
15751 char_u back1 = p >= buf + 1 ? p[-1]
15752 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15753 char_u back2 = p >= buf + 2 ? p[-2]
15754 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15755 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015756
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015757 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015758 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015759 char_u *dest = p - 2;
15760
15761 /* Usually a BOM is at the beginning of a file, and so at
15762 * the beginning of a line; then we can just step over it.
15763 */
15764 if (start == dest)
15765 start = p + 1;
15766 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015767 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015768 /* have to shuffle buf to close gap */
15769 int adjust_prevlen = 0;
15770
15771 if (dest < buf)
15772 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015773 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015774 dest = buf;
15775 }
15776 if (readlen > p - buf + 1)
15777 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15778 readlen -= 3 - adjust_prevlen;
15779 prevlen -= adjust_prevlen;
15780 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015781 }
15782 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015784#endif
15785 } /* for */
15786
15787 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15788 break;
15789 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015790 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015791 /* There's part of a line in buf, store it in "prev". */
15792 if (p - start + prevlen >= prevsize)
15793 {
15794 /* need bigger "prev" buffer */
15795 char_u *newprev;
15796
15797 /* A common use case is ordinary text files and "prev" gets a
15798 * fragment of a line, so the first allocation is made
15799 * small, to avoid repeatedly 'allocing' large and
15800 * 'reallocing' small. */
15801 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015802 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015803 else
15804 {
15805 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015806 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015807 prevsize = grow50pc > growmin ? grow50pc : growmin;
15808 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015809 newprev = prev == NULL ? alloc(prevsize)
15810 : vim_realloc(prev, prevsize);
15811 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015812 {
15813 do_outofmem_msg((long_u)prevsize);
15814 failed = TRUE;
15815 break;
15816 }
15817 prev = newprev;
15818 }
15819 /* Add the line part to end of "prev". */
15820 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015821 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015822 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015823 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015824
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015825 /*
15826 * For a negative line count use only the lines at the end of the file,
15827 * free the rest.
15828 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015829 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015830 while (cnt > -maxline)
15831 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015832 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015833 --cnt;
15834 }
15835
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015836 if (failed)
15837 {
15838 list_free(rettv->vval.v_list, TRUE);
15839 /* readfile doc says an empty list is returned on error */
15840 rettv->vval.v_list = list_alloc();
15841 }
15842
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015843 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015844 fclose(fd);
15845}
15846
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015847#if defined(FEAT_RELTIME)
15848static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15849
15850/*
15851 * Convert a List to proftime_T.
15852 * Return FAIL when there is something wrong.
15853 */
15854 static int
15855list2proftime(arg, tm)
15856 typval_T *arg;
15857 proftime_T *tm;
15858{
15859 long n1, n2;
15860 int error = FALSE;
15861
15862 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15863 || arg->vval.v_list->lv_len != 2)
15864 return FAIL;
15865 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15866 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15867# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015868 tm->HighPart = n1;
15869 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015870# else
15871 tm->tv_sec = n1;
15872 tm->tv_usec = n2;
15873# endif
15874 return error ? FAIL : OK;
15875}
15876#endif /* FEAT_RELTIME */
15877
15878/*
15879 * "reltime()" function
15880 */
15881 static void
15882f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015883 typval_T *argvars UNUSED;
15884 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015885{
15886#ifdef FEAT_RELTIME
15887 proftime_T res;
15888 proftime_T start;
15889
15890 if (argvars[0].v_type == VAR_UNKNOWN)
15891 {
15892 /* No arguments: get current time. */
15893 profile_start(&res);
15894 }
15895 else if (argvars[1].v_type == VAR_UNKNOWN)
15896 {
15897 if (list2proftime(&argvars[0], &res) == FAIL)
15898 return;
15899 profile_end(&res);
15900 }
15901 else
15902 {
15903 /* Two arguments: compute the difference. */
15904 if (list2proftime(&argvars[0], &start) == FAIL
15905 || list2proftime(&argvars[1], &res) == FAIL)
15906 return;
15907 profile_sub(&res, &start);
15908 }
15909
15910 if (rettv_list_alloc(rettv) == OK)
15911 {
15912 long n1, n2;
15913
15914# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015915 n1 = res.HighPart;
15916 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015917# else
15918 n1 = res.tv_sec;
15919 n2 = res.tv_usec;
15920# endif
15921 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15922 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15923 }
15924#endif
15925}
15926
15927/*
15928 * "reltimestr()" function
15929 */
15930 static void
15931f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015932 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015933 typval_T *rettv;
15934{
15935#ifdef FEAT_RELTIME
15936 proftime_T tm;
15937#endif
15938
15939 rettv->v_type = VAR_STRING;
15940 rettv->vval.v_string = NULL;
15941#ifdef FEAT_RELTIME
15942 if (list2proftime(&argvars[0], &tm) == OK)
15943 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15944#endif
15945}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015946
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15948static void make_connection __ARGS((void));
15949static int check_connection __ARGS((void));
15950
15951 static void
15952make_connection()
15953{
15954 if (X_DISPLAY == NULL
15955# ifdef FEAT_GUI
15956 && !gui.in_use
15957# endif
15958 )
15959 {
15960 x_force_connect = TRUE;
15961 setup_term_clip();
15962 x_force_connect = FALSE;
15963 }
15964}
15965
15966 static int
15967check_connection()
15968{
15969 make_connection();
15970 if (X_DISPLAY == NULL)
15971 {
15972 EMSG(_("E240: No connection to Vim server"));
15973 return FAIL;
15974 }
15975 return OK;
15976}
15977#endif
15978
15979#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015980static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015981
15982 static void
15983remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 typval_T *argvars;
15985 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015986 int expr;
15987{
15988 char_u *server_name;
15989 char_u *keys;
15990 char_u *r = NULL;
15991 char_u buf[NUMBUFLEN];
15992# ifdef WIN32
15993 HWND w;
15994# else
15995 Window w;
15996# endif
15997
15998 if (check_restricted() || check_secure())
15999 return;
16000
16001# ifdef FEAT_X11
16002 if (check_connection() == FAIL)
16003 return;
16004# endif
16005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 server_name = get_tv_string_chk(&argvars[0]);
16007 if (server_name == NULL)
16008 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016009 keys = get_tv_string_buf(&argvars[1], buf);
16010# ifdef WIN32
16011 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16012# else
16013 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16014 < 0)
16015# endif
16016 {
16017 if (r != NULL)
16018 EMSG(r); /* sending worked but evaluation failed */
16019 else
16020 EMSG2(_("E241: Unable to send to %s"), server_name);
16021 return;
16022 }
16023
16024 rettv->vval.v_string = r;
16025
16026 if (argvars[2].v_type != VAR_UNKNOWN)
16027 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016028 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016029 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016032 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016033 v.di_tv.v_type = VAR_STRING;
16034 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016035 idvar = get_tv_string_chk(&argvars[2]);
16036 if (idvar != NULL)
16037 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016038 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016039 }
16040}
16041#endif
16042
16043/*
16044 * "remote_expr()" function
16045 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016046 static void
16047f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016048 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016050{
16051 rettv->v_type = VAR_STRING;
16052 rettv->vval.v_string = NULL;
16053#ifdef FEAT_CLIENTSERVER
16054 remote_common(argvars, rettv, TRUE);
16055#endif
16056}
16057
16058/*
16059 * "remote_foreground()" function
16060 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016061 static void
16062f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016063 typval_T *argvars UNUSED;
16064 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016066#ifdef FEAT_CLIENTSERVER
16067# ifdef WIN32
16068 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016069 {
16070 char_u *server_name = get_tv_string_chk(&argvars[0]);
16071
16072 if (server_name != NULL)
16073 serverForeground(server_name);
16074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016075# else
16076 /* Send a foreground() expression to the server. */
16077 argvars[1].v_type = VAR_STRING;
16078 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16079 argvars[2].v_type = VAR_UNKNOWN;
16080 remote_common(argvars, rettv, TRUE);
16081 vim_free(argvars[1].vval.v_string);
16082# endif
16083#endif
16084}
16085
Bram Moolenaar0d660222005-01-07 21:51:51 +000016086 static void
16087f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016088 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090{
16091#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016092 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093 char_u *s = NULL;
16094# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016095 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098
16099 if (check_restricted() || check_secure())
16100 {
16101 rettv->vval.v_number = -1;
16102 return;
16103 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016104 serverid = get_tv_string_chk(&argvars[0]);
16105 if (serverid == NULL)
16106 {
16107 rettv->vval.v_number = -1;
16108 return; /* type error; errmsg already given */
16109 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016111 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016112 if (n == 0)
16113 rettv->vval.v_number = -1;
16114 else
16115 {
16116 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16117 rettv->vval.v_number = (s != NULL);
16118 }
16119# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120 if (check_connection() == FAIL)
16121 return;
16122
16123 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016124 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125# endif
16126
16127 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016129 char_u *retvar;
16130
Bram Moolenaar33570922005-01-25 22:26:29 +000016131 v.di_tv.v_type = VAR_STRING;
16132 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016133 retvar = get_tv_string_chk(&argvars[1]);
16134 if (retvar != NULL)
16135 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016136 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016137 }
16138#else
16139 rettv->vval.v_number = -1;
16140#endif
16141}
16142
Bram Moolenaar0d660222005-01-07 21:51:51 +000016143 static void
16144f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016147{
16148 char_u *r = NULL;
16149
16150#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 char_u *serverid = get_tv_string_chk(&argvars[0]);
16152
16153 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154 {
16155# ifdef WIN32
16156 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016157 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016159 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 if (n != 0)
16161 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16162 if (r == NULL)
16163# else
16164 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016165 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166# endif
16167 EMSG(_("E277: Unable to read a server reply"));
16168 }
16169#endif
16170 rettv->v_type = VAR_STRING;
16171 rettv->vval.v_string = r;
16172}
16173
16174/*
16175 * "remote_send()" function
16176 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 static void
16178f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016179 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016180 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181{
16182 rettv->v_type = VAR_STRING;
16183 rettv->vval.v_string = NULL;
16184#ifdef FEAT_CLIENTSERVER
16185 remote_common(argvars, rettv, FALSE);
16186#endif
16187}
16188
16189/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016190 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016191 */
16192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 typval_T *argvars;
16195 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016196{
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 list_T *l;
16198 listitem_T *item, *item2;
16199 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016200 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016201 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016202 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016203 dict_T *d;
16204 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016205 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016206
Bram Moolenaar8c711452005-01-14 21:53:12 +000016207 if (argvars[0].v_type == VAR_DICT)
16208 {
16209 if (argvars[2].v_type != VAR_UNKNOWN)
16210 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016211 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016212 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016214 key = get_tv_string_chk(&argvars[1]);
16215 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 di = dict_find(d, key, -1);
16218 if (di == NULL)
16219 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016220 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16221 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016222 {
16223 *rettv = di->di_tv;
16224 init_tv(&di->di_tv);
16225 dictitem_remove(d, di);
16226 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016228 }
16229 }
16230 else if (argvars[0].v_type != VAR_LIST)
16231 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016232 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016233 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016235 int error = FALSE;
16236
16237 idx = get_tv_number_chk(&argvars[1], &error);
16238 if (error)
16239 ; /* type error: do nothing, errmsg already given */
16240 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016241 EMSGN(_(e_listidx), idx);
16242 else
16243 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016244 if (argvars[2].v_type == VAR_UNKNOWN)
16245 {
16246 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016247 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016248 *rettv = item->li_tv;
16249 vim_free(item);
16250 }
16251 else
16252 {
16253 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 end = get_tv_number_chk(&argvars[2], &error);
16255 if (error)
16256 ; /* type error: do nothing */
16257 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016258 EMSGN(_(e_listidx), end);
16259 else
16260 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016261 int cnt = 0;
16262
16263 for (li = item; li != NULL; li = li->li_next)
16264 {
16265 ++cnt;
16266 if (li == item2)
16267 break;
16268 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016269 if (li == NULL) /* didn't find "item2" after "item" */
16270 EMSG(_(e_invrange));
16271 else
16272 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016273 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016274 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016275 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016276 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016277 l->lv_first = item;
16278 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016279 item->li_prev = NULL;
16280 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016281 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016282 }
16283 }
16284 }
16285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016286 }
16287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288}
16289
16290/*
16291 * "rename({from}, {to})" function
16292 */
16293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016294f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016295 typval_T *argvars;
16296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297{
16298 char_u buf[NUMBUFLEN];
16299
16300 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016303 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16304 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305}
16306
16307/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016308 * "repeat()" function
16309 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016310 static void
16311f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016312 typval_T *argvars;
16313 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016314{
16315 char_u *p;
16316 int n;
16317 int slen;
16318 int len;
16319 char_u *r;
16320 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016321
16322 n = get_tv_number(&argvars[1]);
16323 if (argvars[0].v_type == VAR_LIST)
16324 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016325 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016326 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016327 if (list_extend(rettv->vval.v_list,
16328 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016329 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016330 }
16331 else
16332 {
16333 p = get_tv_string(&argvars[0]);
16334 rettv->v_type = VAR_STRING;
16335 rettv->vval.v_string = NULL;
16336
16337 slen = (int)STRLEN(p);
16338 len = slen * n;
16339 if (len <= 0)
16340 return;
16341
16342 r = alloc(len + 1);
16343 if (r != NULL)
16344 {
16345 for (i = 0; i < n; i++)
16346 mch_memmove(r + i * slen, p, (size_t)slen);
16347 r[len] = NUL;
16348 }
16349
16350 rettv->vval.v_string = r;
16351 }
16352}
16353
16354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355 * "resolve()" function
16356 */
16357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016359 typval_T *argvars;
16360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361{
16362 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016363#ifdef HAVE_READLINK
16364 char_u *buf = NULL;
16365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368#ifdef FEAT_SHORTCUT
16369 {
16370 char_u *v = NULL;
16371
16372 v = mch_resolve_shortcut(p);
16373 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016374 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016376 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 }
16378#else
16379# ifdef HAVE_READLINK
16380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 char_u *cpy;
16382 int len;
16383 char_u *remain = NULL;
16384 char_u *q;
16385 int is_relative_to_current = FALSE;
16386 int has_trailing_pathsep = FALSE;
16387 int limit = 100;
16388
16389 p = vim_strsave(p);
16390
16391 if (p[0] == '.' && (vim_ispathsep(p[1])
16392 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16393 is_relative_to_current = TRUE;
16394
16395 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016396 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016399 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401
16402 q = getnextcomp(p);
16403 if (*q != NUL)
16404 {
16405 /* Separate the first path component in "p", and keep the
16406 * remainder (beginning with the path separator). */
16407 remain = vim_strsave(q - 1);
16408 q[-1] = NUL;
16409 }
16410
Bram Moolenaard9462e32011-04-11 21:35:11 +020016411 buf = alloc(MAXPATHL + 1);
16412 if (buf == NULL)
16413 goto fail;
16414
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 for (;;)
16416 {
16417 for (;;)
16418 {
16419 len = readlink((char *)p, (char *)buf, MAXPATHL);
16420 if (len <= 0)
16421 break;
16422 buf[len] = NUL;
16423
16424 if (limit-- == 0)
16425 {
16426 vim_free(p);
16427 vim_free(remain);
16428 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 goto fail;
16431 }
16432
16433 /* Ensure that the result will have a trailing path separator
16434 * if the argument has one. */
16435 if (remain == NULL && has_trailing_pathsep)
16436 add_pathsep(buf);
16437
16438 /* Separate the first path component in the link value and
16439 * concatenate the remainders. */
16440 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16441 if (*q != NUL)
16442 {
16443 if (remain == NULL)
16444 remain = vim_strsave(q - 1);
16445 else
16446 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016447 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 if (cpy != NULL)
16449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 vim_free(remain);
16451 remain = cpy;
16452 }
16453 }
16454 q[-1] = NUL;
16455 }
16456
16457 q = gettail(p);
16458 if (q > p && *q == NUL)
16459 {
16460 /* Ignore trailing path separator. */
16461 q[-1] = NUL;
16462 q = gettail(p);
16463 }
16464 if (q > p && !mch_isFullName(buf))
16465 {
16466 /* symlink is relative to directory of argument */
16467 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16468 if (cpy != NULL)
16469 {
16470 STRCPY(cpy, p);
16471 STRCPY(gettail(cpy), buf);
16472 vim_free(p);
16473 p = cpy;
16474 }
16475 }
16476 else
16477 {
16478 vim_free(p);
16479 p = vim_strsave(buf);
16480 }
16481 }
16482
16483 if (remain == NULL)
16484 break;
16485
16486 /* Append the first path component of "remain" to "p". */
16487 q = getnextcomp(remain + 1);
16488 len = q - remain - (*q != NUL);
16489 cpy = vim_strnsave(p, STRLEN(p) + len);
16490 if (cpy != NULL)
16491 {
16492 STRNCAT(cpy, remain, len);
16493 vim_free(p);
16494 p = cpy;
16495 }
16496 /* Shorten "remain". */
16497 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016498 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 else
16500 {
16501 vim_free(remain);
16502 remain = NULL;
16503 }
16504 }
16505
16506 /* If the result is a relative path name, make it explicitly relative to
16507 * the current directory if and only if the argument had this form. */
16508 if (!vim_ispathsep(*p))
16509 {
16510 if (is_relative_to_current
16511 && *p != NUL
16512 && !(p[0] == '.'
16513 && (p[1] == NUL
16514 || vim_ispathsep(p[1])
16515 || (p[1] == '.'
16516 && (p[2] == NUL
16517 || vim_ispathsep(p[2]))))))
16518 {
16519 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016520 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 if (cpy != NULL)
16522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 vim_free(p);
16524 p = cpy;
16525 }
16526 }
16527 else if (!is_relative_to_current)
16528 {
16529 /* Strip leading "./". */
16530 q = p;
16531 while (q[0] == '.' && vim_ispathsep(q[1]))
16532 q += 2;
16533 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016534 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 }
16536 }
16537
16538 /* Ensure that the result will have no trailing path separator
16539 * if the argument had none. But keep "/" or "//". */
16540 if (!has_trailing_pathsep)
16541 {
16542 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016543 if (after_pathsep(p, q))
16544 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 }
16546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 }
16549# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016550 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551# endif
16552#endif
16553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555
16556#ifdef HAVE_READLINK
16557fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016558 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561}
16562
16563/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 */
16566 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016567f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016568 typval_T *argvars;
16569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570{
Bram Moolenaar33570922005-01-25 22:26:29 +000016571 list_T *l;
16572 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574 if (argvars[0].v_type != VAR_LIST)
16575 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016576 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016577 && !tv_check_lock(l->lv_lock,
16578 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579 {
16580 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016581 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016582 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016583 while (li != NULL)
16584 {
16585 ni = li->li_prev;
16586 list_append(l, li);
16587 li = ni;
16588 }
16589 rettv->vval.v_list = l;
16590 rettv->v_type = VAR_LIST;
16591 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016592 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594}
16595
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016596#define SP_NOMOVE 0x01 /* don't move cursor */
16597#define SP_REPEAT 0x02 /* repeat to find outer pair */
16598#define SP_RETCOUNT 0x04 /* return matchcount */
16599#define SP_SETPCMARK 0x08 /* set previous context mark */
16600#define SP_START 0x10 /* accept match at start position */
16601#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16602#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016603#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016604
Bram Moolenaar33570922005-01-25 22:26:29 +000016605static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016606
16607/*
16608 * Get flags for a search function.
16609 * Possibly sets "p_ws".
16610 * Returns BACKWARD, FORWARD or zero (for an error).
16611 */
16612 static int
16613get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016615 int *flagsp;
16616{
16617 int dir = FORWARD;
16618 char_u *flags;
16619 char_u nbuf[NUMBUFLEN];
16620 int mask;
16621
16622 if (varp->v_type != VAR_UNKNOWN)
16623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016624 flags = get_tv_string_buf_chk(varp, nbuf);
16625 if (flags == NULL)
16626 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627 while (*flags != NUL)
16628 {
16629 switch (*flags)
16630 {
16631 case 'b': dir = BACKWARD; break;
16632 case 'w': p_ws = TRUE; break;
16633 case 'W': p_ws = FALSE; break;
16634 default: mask = 0;
16635 if (flagsp != NULL)
16636 switch (*flags)
16637 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016638 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016639 case 'e': mask = SP_END; break;
16640 case 'm': mask = SP_RETCOUNT; break;
16641 case 'n': mask = SP_NOMOVE; break;
16642 case 'p': mask = SP_SUBPAT; break;
16643 case 'r': mask = SP_REPEAT; break;
16644 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016645 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 }
16647 if (mask == 0)
16648 {
16649 EMSG2(_(e_invarg2), flags);
16650 dir = 0;
16651 }
16652 else
16653 *flagsp |= mask;
16654 }
16655 if (dir == 0)
16656 break;
16657 ++flags;
16658 }
16659 }
16660 return dir;
16661}
16662
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016664 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016666 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016667search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016668 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016669 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016670 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016672 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 char_u *pat;
16674 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016675 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 int save_p_ws = p_ws;
16677 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016678 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016679 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016680 proftime_T tm;
16681#ifdef FEAT_RELTIME
16682 long time_limit = 0;
16683#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016684 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016685 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016687 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016688 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016689 if (dir == 0)
16690 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016691 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016692 if (flags & SP_START)
16693 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016694 if (flags & SP_END)
16695 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016696 if (flags & SP_COLUMN)
16697 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016698
Bram Moolenaar76929292008-01-06 19:07:36 +000016699 /* Optional arguments: line number to stop searching and timeout. */
16700 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016701 {
16702 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16703 if (lnum_stop < 0)
16704 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016705#ifdef FEAT_RELTIME
16706 if (argvars[3].v_type != VAR_UNKNOWN)
16707 {
16708 time_limit = get_tv_number_chk(&argvars[3], NULL);
16709 if (time_limit < 0)
16710 goto theend;
16711 }
16712#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016713 }
16714
Bram Moolenaar76929292008-01-06 19:07:36 +000016715#ifdef FEAT_RELTIME
16716 /* Set the time limit, if there is one. */
16717 profile_setlimit(time_limit, &tm);
16718#endif
16719
Bram Moolenaar231334e2005-07-25 20:46:57 +000016720 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016721 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016722 * Check to make sure only those flags are set.
16723 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16724 * flags cannot be set. Check for that condition also.
16725 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016726 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016727 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016729 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016730 goto theend;
16731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016733 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016734 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016735 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016736 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016738 if (flags & SP_SUBPAT)
16739 retval = subpatnum;
16740 else
16741 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016742 if (flags & SP_SETPCMARK)
16743 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016745 if (match_pos != NULL)
16746 {
16747 /* Store the match cursor position */
16748 match_pos->lnum = pos.lnum;
16749 match_pos->col = pos.col + 1;
16750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 /* "/$" will put the cursor after the end of the line, may need to
16752 * correct that here */
16753 check_cursor();
16754 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016755
16756 /* If 'n' flag is used: restore cursor position. */
16757 if (flags & SP_NOMOVE)
16758 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016759 else
16760 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016761theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016763
16764 return retval;
16765}
16766
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016767#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016768
16769/*
16770 * round() is not in C90, use ceil() or floor() instead.
16771 */
16772 float_T
16773vim_round(f)
16774 float_T f;
16775{
16776 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16777}
16778
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016779/*
16780 * "round({float})" function
16781 */
16782 static void
16783f_round(argvars, rettv)
16784 typval_T *argvars;
16785 typval_T *rettv;
16786{
16787 float_T f;
16788
16789 rettv->v_type = VAR_FLOAT;
16790 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016791 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016792 else
16793 rettv->vval.v_float = 0.0;
16794}
16795#endif
16796
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016797/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016798 * "screenattr()" function
16799 */
16800 static void
16801f_screenattr(argvars, rettv)
16802 typval_T *argvars UNUSED;
16803 typval_T *rettv;
16804{
16805 int row;
16806 int col;
16807 int c;
16808
16809 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16810 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16811 if (row < 0 || row >= screen_Rows
16812 || col < 0 || col >= screen_Columns)
16813 c = -1;
16814 else
16815 c = ScreenAttrs[LineOffset[row] + col];
16816 rettv->vval.v_number = c;
16817}
16818
16819/*
16820 * "screenchar()" function
16821 */
16822 static void
16823f_screenchar(argvars, rettv)
16824 typval_T *argvars UNUSED;
16825 typval_T *rettv;
16826{
16827 int row;
16828 int col;
16829 int off;
16830 int c;
16831
16832 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16833 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16834 if (row < 0 || row >= screen_Rows
16835 || col < 0 || col >= screen_Columns)
16836 c = -1;
16837 else
16838 {
16839 off = LineOffset[row] + col;
16840#ifdef FEAT_MBYTE
16841 if (enc_utf8 && ScreenLinesUC[off] != 0)
16842 c = ScreenLinesUC[off];
16843 else
16844#endif
16845 c = ScreenLines[off];
16846 }
16847 rettv->vval.v_number = c;
16848}
16849
16850/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016851 * "screencol()" function
16852 *
16853 * First column is 1 to be consistent with virtcol().
16854 */
16855 static void
16856f_screencol(argvars, rettv)
16857 typval_T *argvars UNUSED;
16858 typval_T *rettv;
16859{
16860 rettv->vval.v_number = screen_screencol() + 1;
16861}
16862
16863/*
16864 * "screenrow()" function
16865 */
16866 static void
16867f_screenrow(argvars, rettv)
16868 typval_T *argvars UNUSED;
16869 typval_T *rettv;
16870{
16871 rettv->vval.v_number = screen_screenrow() + 1;
16872}
16873
16874/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016875 * "search()" function
16876 */
16877 static void
16878f_search(argvars, rettv)
16879 typval_T *argvars;
16880 typval_T *rettv;
16881{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016882 int flags = 0;
16883
16884 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885}
16886
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016888 * "searchdecl()" function
16889 */
16890 static void
16891f_searchdecl(argvars, rettv)
16892 typval_T *argvars;
16893 typval_T *rettv;
16894{
16895 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016896 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016897 int error = FALSE;
16898 char_u *name;
16899
16900 rettv->vval.v_number = 1; /* default: FAIL */
16901
16902 name = get_tv_string_chk(&argvars[0]);
16903 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016904 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016905 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016906 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16907 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16908 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016909 if (!error && name != NULL)
16910 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016911 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016912}
16913
16914/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016915 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016917 static int
16918searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016920 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921{
16922 char_u *spat, *mpat, *epat;
16923 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 int dir;
16926 int flags = 0;
16927 char_u nbuf1[NUMBUFLEN];
16928 char_u nbuf2[NUMBUFLEN];
16929 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016930 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016931 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016932 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935 spat = get_tv_string_chk(&argvars[0]);
16936 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16937 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16938 if (spat == NULL || mpat == NULL || epat == NULL)
16939 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 /* Handle the optional fourth argument: flags */
16942 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016943 if (dir == 0)
16944 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016945
16946 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016947 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16948 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016949 if ((flags & (SP_END | SP_SUBPAT)) != 0
16950 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016951 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016952 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016953 goto theend;
16954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016956 /* Using 'r' implies 'W', otherwise it doesn't work. */
16957 if (flags & SP_REPEAT)
16958 p_ws = FALSE;
16959
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016960 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016961 if (argvars[3].v_type == VAR_UNKNOWN
16962 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 skip = (char_u *)"";
16964 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016966 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016967 if (argvars[5].v_type != VAR_UNKNOWN)
16968 {
16969 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16970 if (lnum_stop < 0)
16971 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016972#ifdef FEAT_RELTIME
16973 if (argvars[6].v_type != VAR_UNKNOWN)
16974 {
16975 time_limit = get_tv_number_chk(&argvars[6], NULL);
16976 if (time_limit < 0)
16977 goto theend;
16978 }
16979#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016980 }
16981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016982 if (skip == NULL)
16983 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016985 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016986 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016987
16988theend:
16989 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016990
16991 return retval;
16992}
16993
16994/*
16995 * "searchpair()" function
16996 */
16997 static void
16998f_searchpair(argvars, rettv)
16999 typval_T *argvars;
17000 typval_T *rettv;
17001{
17002 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17003}
17004
17005/*
17006 * "searchpairpos()" function
17007 */
17008 static void
17009f_searchpairpos(argvars, rettv)
17010 typval_T *argvars;
17011 typval_T *rettv;
17012{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017013 pos_T match_pos;
17014 int lnum = 0;
17015 int col = 0;
17016
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017017 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017018 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017019
17020 if (searchpair_cmn(argvars, &match_pos) > 0)
17021 {
17022 lnum = match_pos.lnum;
17023 col = match_pos.col;
17024 }
17025
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017026 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17027 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017028}
17029
17030/*
17031 * Search for a start/middle/end thing.
17032 * Used by searchpair(), see its documentation for the details.
17033 * Returns 0 or -1 for no match,
17034 */
17035 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017036do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17037 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017038 char_u *spat; /* start pattern */
17039 char_u *mpat; /* middle pattern */
17040 char_u *epat; /* end pattern */
17041 int dir; /* BACKWARD or FORWARD */
17042 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017043 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017044 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017045 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017046 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017047{
17048 char_u *save_cpo;
17049 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17050 long retval = 0;
17051 pos_T pos;
17052 pos_T firstpos;
17053 pos_T foundpos;
17054 pos_T save_cursor;
17055 pos_T save_pos;
17056 int n;
17057 int r;
17058 int nest = 1;
17059 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017060 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017061 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017062
17063 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17064 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017065 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017066
Bram Moolenaar76929292008-01-06 19:07:36 +000017067#ifdef FEAT_RELTIME
17068 /* Set the time limit, if there is one. */
17069 profile_setlimit(time_limit, &tm);
17070#endif
17071
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017072 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17073 * start/middle/end (pat3, for the top pair). */
17074 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17075 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17076 if (pat2 == NULL || pat3 == NULL)
17077 goto theend;
17078 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17079 if (*mpat == NUL)
17080 STRCPY(pat3, pat2);
17081 else
17082 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17083 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017084 if (flags & SP_START)
17085 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017086
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 save_cursor = curwin->w_cursor;
17088 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017089 clearpos(&firstpos);
17090 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 pat = pat3;
17092 for (;;)
17093 {
17094 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017095 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17097 /* didn't find it or found the first match again: FAIL */
17098 break;
17099
17100 if (firstpos.lnum == 0)
17101 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017102 if (equalpos(pos, foundpos))
17103 {
17104 /* Found the same position again. Can happen with a pattern that
17105 * has "\zs" at the end and searching backwards. Advance one
17106 * character and try again. */
17107 if (dir == BACKWARD)
17108 decl(&pos);
17109 else
17110 incl(&pos);
17111 }
17112 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017114 /* clear the start flag to avoid getting stuck here */
17115 options &= ~SEARCH_START;
17116
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 /* If the skip pattern matches, ignore this match. */
17118 if (*skip != NUL)
17119 {
17120 save_pos = curwin->w_cursor;
17121 curwin->w_cursor = pos;
17122 r = eval_to_bool(skip, &err, NULL, FALSE);
17123 curwin->w_cursor = save_pos;
17124 if (err)
17125 {
17126 /* Evaluating {skip} caused an error, break here. */
17127 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017128 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129 break;
17130 }
17131 if (r)
17132 continue;
17133 }
17134
17135 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17136 {
17137 /* Found end when searching backwards or start when searching
17138 * forward: nested pair. */
17139 ++nest;
17140 pat = pat2; /* nested, don't search for middle */
17141 }
17142 else
17143 {
17144 /* Found end when searching forward or start when searching
17145 * backward: end of (nested) pair; or found middle in outer pair. */
17146 if (--nest == 1)
17147 pat = pat3; /* outer level, search for middle */
17148 }
17149
17150 if (nest == 0)
17151 {
17152 /* Found the match: return matchcount or line number. */
17153 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017154 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017156 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017157 if (flags & SP_SETPCMARK)
17158 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 curwin->w_cursor = pos;
17160 if (!(flags & SP_REPEAT))
17161 break;
17162 nest = 1; /* search for next unmatched */
17163 }
17164 }
17165
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017166 if (match_pos != NULL)
17167 {
17168 /* Store the match cursor position */
17169 match_pos->lnum = curwin->w_cursor.lnum;
17170 match_pos->col = curwin->w_cursor.col + 1;
17171 }
17172
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017174 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 curwin->w_cursor = save_cursor;
17176
17177theend:
17178 vim_free(pat2);
17179 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017180 if (p_cpo == empty_option)
17181 p_cpo = save_cpo;
17182 else
17183 /* Darn, evaluating the {skip} expression changed the value. */
17184 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017185
17186 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187}
17188
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017189/*
17190 * "searchpos()" function
17191 */
17192 static void
17193f_searchpos(argvars, rettv)
17194 typval_T *argvars;
17195 typval_T *rettv;
17196{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017197 pos_T match_pos;
17198 int lnum = 0;
17199 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017200 int n;
17201 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017202
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017203 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017204 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017205
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017206 n = search_cmn(argvars, &match_pos, &flags);
17207 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017208 {
17209 lnum = match_pos.lnum;
17210 col = match_pos.col;
17211 }
17212
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017213 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17214 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017215 if (flags & SP_SUBPAT)
17216 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017217}
17218
17219
Bram Moolenaar0d660222005-01-07 21:51:51 +000017220 static void
17221f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017222 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225#ifdef FEAT_CLIENTSERVER
17226 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017227 char_u *server = get_tv_string_chk(&argvars[0]);
17228 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017231 if (server == NULL || reply == NULL)
17232 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233 if (check_restricted() || check_secure())
17234 return;
17235# ifdef FEAT_X11
17236 if (check_connection() == FAIL)
17237 return;
17238# endif
17239
17240 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242 EMSG(_("E258: Unable to send to client"));
17243 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245 rettv->vval.v_number = 0;
17246#else
17247 rettv->vval.v_number = -1;
17248#endif
17249}
17250
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251 static void
17252f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017254 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017255{
17256 char_u *r = NULL;
17257
17258#ifdef FEAT_CLIENTSERVER
17259# ifdef WIN32
17260 r = serverGetVimNames();
17261# else
17262 make_connection();
17263 if (X_DISPLAY != NULL)
17264 r = serverGetVimNames(X_DISPLAY);
17265# endif
17266#endif
17267 rettv->v_type = VAR_STRING;
17268 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269}
17270
17271/*
17272 * "setbufvar()" function
17273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017277 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
17279 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017282 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 char_u nbuf[NUMBUFLEN];
17284
17285 if (check_restricted() || check_secure())
17286 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017287 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17288 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017289 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 varp = &argvars[2];
17291
17292 if (buf != NULL && varname != NULL && varp != NULL)
17293 {
17294 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296
17297 if (*varname == '&')
17298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 long numval;
17300 char_u *strval;
17301 int error = FALSE;
17302
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 numval = get_tv_number_chk(varp, &error);
17305 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017306 if (!error && strval != NULL)
17307 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 }
17309 else
17310 {
17311 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17312 if (bufvarname != NULL)
17313 {
17314 STRCPY(bufvarname, "b:");
17315 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017316 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 vim_free(bufvarname);
17318 }
17319 }
17320
17321 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324}
17325
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017326 static void
17327f_setcharsearch(argvars, rettv)
17328 typval_T *argvars;
17329 typval_T *rettv UNUSED;
17330{
17331 dict_T *d;
17332 dictitem_T *di;
17333 char_u *csearch;
17334
17335 if (argvars[0].v_type != VAR_DICT)
17336 {
17337 EMSG(_(e_dictreq));
17338 return;
17339 }
17340
17341 if ((d = argvars[0].vval.v_dict) != NULL)
17342 {
17343 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17344 if (csearch != NULL)
17345 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017346#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017347 if (enc_utf8)
17348 {
17349 int pcc[MAX_MCO];
17350 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017351
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017352 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17353 }
17354 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017355#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017356 set_last_csearch(PTR2CHAR(csearch),
17357 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017358 }
17359
17360 di = dict_find(d, (char_u *)"forward", -1);
17361 if (di != NULL)
17362 set_csearch_direction(get_tv_number(&di->di_tv)
17363 ? FORWARD : BACKWARD);
17364
17365 di = dict_find(d, (char_u *)"until", -1);
17366 if (di != NULL)
17367 set_csearch_until(!!get_tv_number(&di->di_tv));
17368 }
17369}
17370
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371/*
17372 * "setcmdpos()" function
17373 */
17374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 typval_T *argvars;
17377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 int pos = (int)get_tv_number(&argvars[0]) - 1;
17380
17381 if (pos >= 0)
17382 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383}
17384
17385/*
17386 * "setline()" function
17387 */
17388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 typval_T *argvars;
17391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392{
17393 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017394 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017395 list_T *l = NULL;
17396 listitem_T *li = NULL;
17397 long added = 0;
17398 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017400 lnum = get_tv_lnum(&argvars[0]);
17401 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017403 l = argvars[1].vval.v_list;
17404 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017406 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017407 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017408
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017409 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017410 for (;;)
17411 {
17412 if (l != NULL)
17413 {
17414 /* list argument, get next string */
17415 if (li == NULL)
17416 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017417 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017418 li = li->li_next;
17419 }
17420
17421 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017422 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017423 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017424
17425 /* When coming here from Insert mode, sync undo, so that this can be
17426 * undone separately from what was previously inserted. */
17427 if (u_sync_once == 2)
17428 {
17429 u_sync_once = 1; /* notify that u_sync() was called */
17430 u_sync(TRUE);
17431 }
17432
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017433 if (lnum <= curbuf->b_ml.ml_line_count)
17434 {
17435 /* existing line, replace it */
17436 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17437 {
17438 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017439 if (lnum == curwin->w_cursor.lnum)
17440 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017441 rettv->vval.v_number = 0; /* OK */
17442 }
17443 }
17444 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17445 {
17446 /* lnum is one past the last line, append the line */
17447 ++added;
17448 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17449 rettv->vval.v_number = 0; /* OK */
17450 }
17451
17452 if (l == NULL) /* only one string argument */
17453 break;
17454 ++lnum;
17455 }
17456
17457 if (added > 0)
17458 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459}
17460
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017461static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17462
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017464 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017465 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017466 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017467set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017468 win_T *wp UNUSED;
17469 typval_T *list_arg UNUSED;
17470 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017471 typval_T *rettv;
17472{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017473#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017474 char_u *act;
17475 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017476#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017477
Bram Moolenaar2641f772005-03-25 21:58:17 +000017478 rettv->vval.v_number = -1;
17479
17480#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017481 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017482 EMSG(_(e_listreq));
17483 else
17484 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017485 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017486
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017487 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017488 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017489 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017490 if (act == NULL)
17491 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017492 if (*act == 'a' || *act == 'r')
17493 action = *act;
17494 }
17495
Bram Moolenaar81484f42012-12-05 15:16:47 +010017496 if (l != NULL && set_errorlist(wp, l, action,
17497 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017498 rettv->vval.v_number = 0;
17499 }
17500#endif
17501}
17502
17503/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017504 * "setloclist()" function
17505 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017506 static void
17507f_setloclist(argvars, rettv)
17508 typval_T *argvars;
17509 typval_T *rettv;
17510{
17511 win_T *win;
17512
17513 rettv->vval.v_number = -1;
17514
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017515 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017516 if (win != NULL)
17517 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17518}
17519
17520/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017521 * "setmatches()" function
17522 */
17523 static void
17524f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017525 typval_T *argvars UNUSED;
17526 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017527{
17528#ifdef FEAT_SEARCH_EXTRA
17529 list_T *l;
17530 listitem_T *li;
17531 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017532 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017533
17534 rettv->vval.v_number = -1;
17535 if (argvars[0].v_type != VAR_LIST)
17536 {
17537 EMSG(_(e_listreq));
17538 return;
17539 }
17540 if ((l = argvars[0].vval.v_list) != NULL)
17541 {
17542
17543 /* To some extent make sure that we are dealing with a list from
17544 * "getmatches()". */
17545 li = l->lv_first;
17546 while (li != NULL)
17547 {
17548 if (li->li_tv.v_type != VAR_DICT
17549 || (d = li->li_tv.vval.v_dict) == NULL)
17550 {
17551 EMSG(_(e_invarg));
17552 return;
17553 }
17554 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017555 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17556 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017557 && dict_find(d, (char_u *)"priority", -1) != NULL
17558 && dict_find(d, (char_u *)"id", -1) != NULL))
17559 {
17560 EMSG(_(e_invarg));
17561 return;
17562 }
17563 li = li->li_next;
17564 }
17565
17566 clear_matches(curwin);
17567 li = l->lv_first;
17568 while (li != NULL)
17569 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017570 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017571 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017572 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017573 char_u *group;
17574 int priority;
17575 int id;
17576 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017577
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017578 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017579 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17580 {
17581 if (s == NULL)
17582 {
17583 s = list_alloc();
17584 if (s == NULL)
17585 return;
17586 }
17587
17588 /* match from matchaddpos() */
17589 for (i = 1; i < 9; i++)
17590 {
17591 sprintf((char *)buf, (char *)"pos%d", i);
17592 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17593 {
17594 if (di->di_tv.v_type != VAR_LIST)
17595 return;
17596
17597 list_append_tv(s, &di->di_tv);
17598 s->lv_refcount++;
17599 }
17600 else
17601 break;
17602 }
17603 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017604
17605 group = get_dict_string(d, (char_u *)"group", FALSE);
17606 priority = (int)get_dict_number(d, (char_u *)"priority");
17607 id = (int)get_dict_number(d, (char_u *)"id");
17608 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17609 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17610 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017611 if (i == 0)
17612 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017613 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017614 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017615 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017616 }
17617 else
17618 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017619 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017620 list_unref(s);
17621 s = NULL;
17622 }
17623
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017624 li = li->li_next;
17625 }
17626 rettv->vval.v_number = 0;
17627 }
17628#endif
17629}
17630
17631/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017632 * "setpos()" function
17633 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017634 static void
17635f_setpos(argvars, rettv)
17636 typval_T *argvars;
17637 typval_T *rettv;
17638{
17639 pos_T pos;
17640 int fnum;
17641 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017642 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017643
Bram Moolenaar08250432008-02-13 11:42:46 +000017644 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017645 name = get_tv_string_chk(argvars);
17646 if (name != NULL)
17647 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017648 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017649 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017650 if (--pos.col < 0)
17651 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017652 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017653 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017654 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017655 if (fnum == curbuf->b_fnum)
17656 {
17657 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017658 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017659 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017660 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017661 curwin->w_set_curswant = FALSE;
17662 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017663 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017664 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017665 }
17666 else
17667 EMSG(_(e_invarg));
17668 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017669 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17670 {
17671 /* set mark */
17672 if (setmark_pos(name[1], &pos, fnum) == OK)
17673 rettv->vval.v_number = 0;
17674 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017675 else
17676 EMSG(_(e_invarg));
17677 }
17678 }
17679}
17680
17681/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017682 * "setqflist()" function
17683 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017684 static void
17685f_setqflist(argvars, rettv)
17686 typval_T *argvars;
17687 typval_T *rettv;
17688{
17689 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17690}
17691
17692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 * "setreg()" function
17694 */
17695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 typval_T *argvars;
17698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699{
17700 int regname;
17701 char_u *strregname;
17702 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017703 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 int append;
17705 char_u yank_type;
17706 long block_len;
17707
17708 block_len = -1;
17709 yank_type = MAUTO;
17710 append = FALSE;
17711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017715 if (strregname == NULL)
17716 return; /* type error; errmsg already given */
17717 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 if (regname == 0 || regname == '@')
17719 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017723 stropt = get_tv_string_chk(&argvars[2]);
17724 if (stropt == NULL)
17725 return; /* type error */
17726 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 switch (*stropt)
17728 {
17729 case 'a': case 'A': /* append */
17730 append = TRUE;
17731 break;
17732 case 'v': case 'c': /* character-wise selection */
17733 yank_type = MCHAR;
17734 break;
17735 case 'V': case 'l': /* line-wise selection */
17736 yank_type = MLINE;
17737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 case 'b': case Ctrl_V: /* block-wise selection */
17739 yank_type = MBLOCK;
17740 if (VIM_ISDIGIT(stropt[1]))
17741 {
17742 ++stropt;
17743 block_len = getdigits(&stropt) - 1;
17744 --stropt;
17745 }
17746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 }
17748 }
17749
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017750 if (argvars[1].v_type == VAR_LIST)
17751 {
17752 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017753 char_u **allocval;
17754 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017755 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017756 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017757 int len = argvars[1].vval.v_list->lv_len;
17758 listitem_T *li;
17759
Bram Moolenaar7d647822014-04-05 21:28:56 +020017760 /* First half: use for pointers to result lines; second half: use for
17761 * pointers to allocated copies. */
17762 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017763 if (lstval == NULL)
17764 return;
17765 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017766 allocval = lstval + len + 2;
17767 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017768
17769 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17770 li = li->li_next)
17771 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017772 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017773 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017774 goto free_lstval;
17775 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017776 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017777 /* Need to make a copy, next get_tv_string_buf_chk() will
17778 * overwrite the string. */
17779 strval = vim_strsave(buf);
17780 if (strval == NULL)
17781 goto free_lstval;
17782 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017783 }
17784 *curval++ = strval;
17785 }
17786 *curval++ = NULL;
17787
17788 write_reg_contents_lst(regname, lstval, -1,
17789 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017790free_lstval:
17791 while (curallocval > allocval)
17792 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017793 vim_free(lstval);
17794 }
17795 else
17796 {
17797 strval = get_tv_string_chk(&argvars[1]);
17798 if (strval == NULL)
17799 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017800 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017803 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804}
17805
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017806/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017807 * "settabvar()" function
17808 */
17809 static void
17810f_settabvar(argvars, rettv)
17811 typval_T *argvars;
17812 typval_T *rettv;
17813{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017814#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017815 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017816 tabpage_T *tp;
17817#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017818 char_u *varname, *tabvarname;
17819 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017820
17821 rettv->vval.v_number = 0;
17822
17823 if (check_restricted() || check_secure())
17824 return;
17825
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017826#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017827 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017828#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017829 varname = get_tv_string_chk(&argvars[1]);
17830 varp = &argvars[2];
17831
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017832 if (varname != NULL && varp != NULL
17833#ifdef FEAT_WINDOWS
17834 && tp != NULL
17835#endif
17836 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017837 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017838#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017839 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017840 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017841#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017842
17843 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17844 if (tabvarname != NULL)
17845 {
17846 STRCPY(tabvarname, "t:");
17847 STRCPY(tabvarname + 2, varname);
17848 set_var(tabvarname, varp, TRUE);
17849 vim_free(tabvarname);
17850 }
17851
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017852#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017853 /* Restore current tabpage */
17854 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017855 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017856#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017857 }
17858}
17859
17860/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017861 * "settabwinvar()" function
17862 */
17863 static void
17864f_settabwinvar(argvars, rettv)
17865 typval_T *argvars;
17866 typval_T *rettv;
17867{
17868 setwinvar(argvars, rettv, 1);
17869}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870
17871/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017872 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017875f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017876 typval_T *argvars;
17877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017879 setwinvar(argvars, rettv, 0);
17880}
17881
17882/*
17883 * "setwinvar()" and "settabwinvar()" functions
17884 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017885
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017886 static void
17887setwinvar(argvars, rettv, off)
17888 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017889 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017890 int off;
17891{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 win_T *win;
17893#ifdef FEAT_WINDOWS
17894 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017895 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017896 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897#endif
17898 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017901 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902
17903 if (check_restricted() || check_secure())
17904 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017905
17906#ifdef FEAT_WINDOWS
17907 if (off == 1)
17908 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17909 else
17910 tp = curtab;
17911#endif
17912 win = find_win_by_nr(&argvars[off], tp);
17913 varname = get_tv_string_chk(&argvars[off + 1]);
17914 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915
17916 if (win != NULL && varname != NULL && varp != NULL)
17917 {
17918#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017919 need_switch_win = !(tp == curtab && win == curwin);
17920 if (!need_switch_win
17921 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017924 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017926 long numval;
17927 char_u *strval;
17928 int error = FALSE;
17929
17930 ++varname;
17931 numval = get_tv_number_chk(varp, &error);
17932 strval = get_tv_string_buf_chk(varp, nbuf);
17933 if (!error && strval != NULL)
17934 set_option_value(varname, numval, strval, OPT_LOCAL);
17935 }
17936 else
17937 {
17938 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17939 if (winvarname != NULL)
17940 {
17941 STRCPY(winvarname, "w:");
17942 STRCPY(winvarname + 2, varname);
17943 set_var(winvarname, varp, TRUE);
17944 vim_free(winvarname);
17945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 }
17947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017949 if (need_switch_win)
17950 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951#endif
17952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953}
17954
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017955#ifdef FEAT_CRYPT
17956/*
17957 * "sha256({string})" function
17958 */
17959 static void
17960f_sha256(argvars, rettv)
17961 typval_T *argvars;
17962 typval_T *rettv;
17963{
17964 char_u *p;
17965
17966 p = get_tv_string(&argvars[0]);
17967 rettv->vval.v_string = vim_strsave(
17968 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17969 rettv->v_type = VAR_STRING;
17970}
17971#endif /* FEAT_CRYPT */
17972
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017974 * "shellescape({string})" function
17975 */
17976 static void
17977f_shellescape(argvars, rettv)
17978 typval_T *argvars;
17979 typval_T *rettv;
17980{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017981 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017982 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017983 rettv->v_type = VAR_STRING;
17984}
17985
17986/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017987 * shiftwidth() function
17988 */
17989 static void
17990f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017991 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017992 typval_T *rettv;
17993{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017994 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017995}
17996
17997/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017998 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 */
18000 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018001f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018002 typval_T *argvars;
18003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006
Bram Moolenaar0d660222005-01-07 21:51:51 +000018007 p = get_tv_string(&argvars[0]);
18008 rettv->vval.v_string = vim_strsave(p);
18009 simplify_filename(rettv->vval.v_string); /* simplify in place */
18010 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011}
18012
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018013#ifdef FEAT_FLOAT
18014/*
18015 * "sin()" function
18016 */
18017 static void
18018f_sin(argvars, rettv)
18019 typval_T *argvars;
18020 typval_T *rettv;
18021{
18022 float_T f;
18023
18024 rettv->v_type = VAR_FLOAT;
18025 if (get_float_arg(argvars, &f) == OK)
18026 rettv->vval.v_float = sin(f);
18027 else
18028 rettv->vval.v_float = 0.0;
18029}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018030
18031/*
18032 * "sinh()" function
18033 */
18034 static void
18035f_sinh(argvars, rettv)
18036 typval_T *argvars;
18037 typval_T *rettv;
18038{
18039 float_T f;
18040
18041 rettv->v_type = VAR_FLOAT;
18042 if (get_float_arg(argvars, &f) == OK)
18043 rettv->vval.v_float = sinh(f);
18044 else
18045 rettv->vval.v_float = 0.0;
18046}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018047#endif
18048
Bram Moolenaar0d660222005-01-07 21:51:51 +000018049static int
18050#ifdef __BORLANDC__
18051 _RTLENTRYF
18052#endif
18053 item_compare __ARGS((const void *s1, const void *s2));
18054static int
18055#ifdef __BORLANDC__
18056 _RTLENTRYF
18057#endif
18058 item_compare2 __ARGS((const void *s1, const void *s2));
18059
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018060/* struct used in the array that's given to qsort() */
18061typedef struct
18062{
18063 listitem_T *item;
18064 int idx;
18065} sortItem_T;
18066
Bram Moolenaar0d660222005-01-07 21:51:51 +000018067static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018068static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018069static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018070static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018071static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018072static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018073static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018074static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018075#define ITEM_COMPARE_FAIL 999
18076
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018078 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018080 static int
18081#ifdef __BORLANDC__
18082_RTLENTRYF
18083#endif
18084item_compare(s1, s2)
18085 const void *s1;
18086 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018088 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018089 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018090 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018091 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018092 int res;
18093 char_u numbuf1[NUMBUFLEN];
18094 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018096 si1 = (sortItem_T *)s1;
18097 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018098 tv1 = &si1->item->li_tv;
18099 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018100
18101 if (item_compare_numbers)
18102 {
18103 long v1 = get_tv_number(tv1);
18104 long v2 = get_tv_number(tv2);
18105
18106 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18107 }
18108
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018109 /* tv2string() puts quotes around a string and allocates memory. Don't do
18110 * that for string variables. Use a single quote when comparing with a
18111 * non-string to do what the docs promise. */
18112 if (tv1->v_type == VAR_STRING)
18113 {
18114 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18115 p1 = (char_u *)"'";
18116 else
18117 p1 = tv1->vval.v_string;
18118 }
18119 else
18120 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18121 if (tv2->v_type == VAR_STRING)
18122 {
18123 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18124 p2 = (char_u *)"'";
18125 else
18126 p2 = tv2->vval.v_string;
18127 }
18128 else
18129 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018130 if (p1 == NULL)
18131 p1 = (char_u *)"";
18132 if (p2 == NULL)
18133 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018134 if (!item_compare_numeric)
18135 {
18136 if (item_compare_ic)
18137 res = STRICMP(p1, p2);
18138 else
18139 res = STRCMP(p1, p2);
18140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018142 {
18143 double n1, n2;
18144 n1 = strtod((char *)p1, (char **)&p1);
18145 n2 = strtod((char *)p2, (char **)&p2);
18146 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18147 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018148
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018149 /* When the result would be zero, compare the item indexes. Makes the
18150 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018151 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018152 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018153
Bram Moolenaar0d660222005-01-07 21:51:51 +000018154 vim_free(tofree1);
18155 vim_free(tofree2);
18156 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157}
18158
18159 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160#ifdef __BORLANDC__
18161_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163item_compare2(s1, s2)
18164 const void *s1;
18165 const void *s2;
18166{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018167 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018168 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018169 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018170 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018171 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018173 /* shortcut after failure in previous call; compare all items equal */
18174 if (item_compare_func_err)
18175 return 0;
18176
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018177 si1 = (sortItem_T *)s1;
18178 si2 = (sortItem_T *)s2;
18179
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018180 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018181 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018182 copy_tv(&si1->item->li_tv, &argv[0]);
18183 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018184
18185 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018186 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018187 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18188 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018189 clear_tv(&argv[0]);
18190 clear_tv(&argv[1]);
18191
18192 if (res == FAIL)
18193 res = ITEM_COMPARE_FAIL;
18194 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018195 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18196 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018197 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018198 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018199
18200 /* When the result would be zero, compare the pointers themselves. Makes
18201 * the sort stable. */
18202 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018203 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018204
Bram Moolenaar0d660222005-01-07 21:51:51 +000018205 return res;
18206}
18207
18208/*
18209 * "sort({list})" function
18210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018212do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018213 typval_T *argvars;
18214 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018215 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216{
Bram Moolenaar33570922005-01-25 22:26:29 +000018217 list_T *l;
18218 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018219 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018220 long len;
18221 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222
Bram Moolenaar0d660222005-01-07 21:51:51 +000018223 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018224 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 else
18226 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018227 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018228 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018229 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18230 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018231 return;
18232 rettv->vval.v_list = l;
18233 rettv->v_type = VAR_LIST;
18234 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235
Bram Moolenaar0d660222005-01-07 21:51:51 +000018236 len = list_len(l);
18237 if (len <= 1)
18238 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018241 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018242 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018244 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 if (argvars[1].v_type != VAR_UNKNOWN)
18246 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018247 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018249 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018250 else
18251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018252 int error = FALSE;
18253
18254 i = get_tv_number_chk(&argvars[1], &error);
18255 if (error)
18256 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018257 if (i == 1)
18258 item_compare_ic = TRUE;
18259 else
18260 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018261 if (item_compare_func != NULL)
18262 {
18263 if (STRCMP(item_compare_func, "n") == 0)
18264 {
18265 item_compare_func = NULL;
18266 item_compare_numeric = TRUE;
18267 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018268 else if (STRCMP(item_compare_func, "N") == 0)
18269 {
18270 item_compare_func = NULL;
18271 item_compare_numbers = TRUE;
18272 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018273 else if (STRCMP(item_compare_func, "i") == 0)
18274 {
18275 item_compare_func = NULL;
18276 item_compare_ic = TRUE;
18277 }
18278 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018280
18281 if (argvars[2].v_type != VAR_UNKNOWN)
18282 {
18283 /* optional third argument: {dict} */
18284 if (argvars[2].v_type != VAR_DICT)
18285 {
18286 EMSG(_(e_dictreq));
18287 return;
18288 }
18289 item_compare_selfdict = argvars[2].vval.v_dict;
18290 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292
Bram Moolenaar0d660222005-01-07 21:51:51 +000018293 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018294 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 if (ptrs == NULL)
18296 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297
Bram Moolenaar327aa022014-03-25 18:24:23 +010018298 i = 0;
18299 if (sort)
18300 {
18301 /* sort(): ptrs will be the list to sort */
18302 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018303 {
18304 ptrs[i].item = li;
18305 ptrs[i].idx = i;
18306 ++i;
18307 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018308
18309 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018310 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018311 /* test the compare function */
18312 if (item_compare_func != NULL
18313 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018314 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018315 EMSG(_("E702: Sort compare function failed"));
18316 else
18317 {
18318 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018319 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018320 item_compare_func == NULL ? item_compare : item_compare2);
18321
18322 if (!item_compare_func_err)
18323 {
18324 /* Clear the List and append the items in sorted order. */
18325 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18326 l->lv_len = 0;
18327 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018328 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018329 }
18330 }
18331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018334 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18335
18336 /* f_uniq(): ptrs will be a stack of items to remove */
18337 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018338 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018339 item_compare_func_ptr = item_compare_func
18340 ? item_compare2 : item_compare;
18341
18342 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18343 li = li->li_next)
18344 {
18345 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18346 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018347 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018348 if (item_compare_func_err)
18349 {
18350 EMSG(_("E882: Uniq compare function failed"));
18351 break;
18352 }
18353 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018355 if (!item_compare_func_err)
18356 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018357 while (--i >= 0)
18358 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018359 li = ptrs[i].item->li_next;
18360 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018361 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018362 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018363 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018364 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018365 list_fix_watch(l, li);
18366 listitem_free(li);
18367 l->lv_len--;
18368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018369 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018370 }
18371
18372 vim_free(ptrs);
18373 }
18374}
18375
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018376/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018377 * "sort({list})" function
18378 */
18379 static void
18380f_sort(argvars, rettv)
18381 typval_T *argvars;
18382 typval_T *rettv;
18383{
18384 do_sort_uniq(argvars, rettv, TRUE);
18385}
18386
18387/*
18388 * "uniq({list})" function
18389 */
18390 static void
18391f_uniq(argvars, rettv)
18392 typval_T *argvars;
18393 typval_T *rettv;
18394{
18395 do_sort_uniq(argvars, rettv, FALSE);
18396}
18397
18398/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018399 * "soundfold({word})" function
18400 */
18401 static void
18402f_soundfold(argvars, rettv)
18403 typval_T *argvars;
18404 typval_T *rettv;
18405{
18406 char_u *s;
18407
18408 rettv->v_type = VAR_STRING;
18409 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018410#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018411 rettv->vval.v_string = eval_soundfold(s);
18412#else
18413 rettv->vval.v_string = vim_strsave(s);
18414#endif
18415}
18416
18417/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018418 * "spellbadword()" function
18419 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018420 static void
18421f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018422 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018423 typval_T *rettv;
18424{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018425 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018426 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018427 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018428
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018429 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018430 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018431
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018432#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018433 if (argvars[0].v_type == VAR_UNKNOWN)
18434 {
18435 /* Find the start and length of the badly spelled word. */
18436 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18437 if (len != 0)
18438 word = ml_get_cursor();
18439 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018440 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018441 {
18442 char_u *str = get_tv_string_chk(&argvars[0]);
18443 int capcol = -1;
18444
18445 if (str != NULL)
18446 {
18447 /* Check the argument for spelling. */
18448 while (*str != NUL)
18449 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018450 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018451 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018452 {
18453 word = str;
18454 break;
18455 }
18456 str += len;
18457 }
18458 }
18459 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018460#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018461
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018462 list_append_string(rettv->vval.v_list, word, len);
18463 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018464 attr == HLF_SPB ? "bad" :
18465 attr == HLF_SPR ? "rare" :
18466 attr == HLF_SPL ? "local" :
18467 attr == HLF_SPC ? "caps" :
18468 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018469}
18470
18471/*
18472 * "spellsuggest()" function
18473 */
18474 static void
18475f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018476 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018477 typval_T *rettv;
18478{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018479#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018480 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018481 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018482 int maxcount;
18483 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018484 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018485 listitem_T *li;
18486 int need_capital = FALSE;
18487#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018488
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018489 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018490 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018491
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018492#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018493 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018494 {
18495 str = get_tv_string(&argvars[0]);
18496 if (argvars[1].v_type != VAR_UNKNOWN)
18497 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018498 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018499 if (maxcount <= 0)
18500 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018501 if (argvars[2].v_type != VAR_UNKNOWN)
18502 {
18503 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18504 if (typeerr)
18505 return;
18506 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018507 }
18508 else
18509 maxcount = 25;
18510
Bram Moolenaar4770d092006-01-12 23:22:24 +000018511 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018512
18513 for (i = 0; i < ga.ga_len; ++i)
18514 {
18515 str = ((char_u **)ga.ga_data)[i];
18516
18517 li = listitem_alloc();
18518 if (li == NULL)
18519 vim_free(str);
18520 else
18521 {
18522 li->li_tv.v_type = VAR_STRING;
18523 li->li_tv.v_lock = 0;
18524 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018525 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018526 }
18527 }
18528 ga_clear(&ga);
18529 }
18530#endif
18531}
18532
Bram Moolenaar0d660222005-01-07 21:51:51 +000018533 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018534f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018535 typval_T *argvars;
18536 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537{
18538 char_u *str;
18539 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018540 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541 regmatch_T regmatch;
18542 char_u patbuf[NUMBUFLEN];
18543 char_u *save_cpo;
18544 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018546 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018547 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018548
18549 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18550 save_cpo = p_cpo;
18551 p_cpo = (char_u *)"";
18552
18553 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018554 if (argvars[1].v_type != VAR_UNKNOWN)
18555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018556 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18557 if (pat == NULL)
18558 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018560 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018561 }
18562 if (pat == NULL || *pat == NUL)
18563 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018567 if (typeerr)
18568 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
Bram Moolenaar0d660222005-01-07 21:51:51 +000018570 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18571 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018573 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018574 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018575 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018576 if (*str == NUL)
18577 match = FALSE; /* empty item at the end */
18578 else
18579 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580 if (match)
18581 end = regmatch.startp[0];
18582 else
18583 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018584 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18585 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018586 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018587 if (list_append_string(rettv->vval.v_list, str,
18588 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018589 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018590 }
18591 if (!match)
18592 break;
18593 /* Advance to just after the match. */
18594 if (regmatch.endp[0] > str)
18595 col = 0;
18596 else
18597 {
18598 /* Don't get stuck at the same match. */
18599#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018600 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601#else
18602 col = 1;
18603#endif
18604 }
18605 str = regmatch.endp[0];
18606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607
Bram Moolenaar473de612013-06-08 18:19:48 +020018608 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612}
18613
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018614#ifdef FEAT_FLOAT
18615/*
18616 * "sqrt()" function
18617 */
18618 static void
18619f_sqrt(argvars, rettv)
18620 typval_T *argvars;
18621 typval_T *rettv;
18622{
18623 float_T f;
18624
18625 rettv->v_type = VAR_FLOAT;
18626 if (get_float_arg(argvars, &f) == OK)
18627 rettv->vval.v_float = sqrt(f);
18628 else
18629 rettv->vval.v_float = 0.0;
18630}
18631
18632/*
18633 * "str2float()" function
18634 */
18635 static void
18636f_str2float(argvars, rettv)
18637 typval_T *argvars;
18638 typval_T *rettv;
18639{
18640 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18641
18642 if (*p == '+')
18643 p = skipwhite(p + 1);
18644 (void)string2float(p, &rettv->vval.v_float);
18645 rettv->v_type = VAR_FLOAT;
18646}
18647#endif
18648
Bram Moolenaar2c932302006-03-18 21:42:09 +000018649/*
18650 * "str2nr()" function
18651 */
18652 static void
18653f_str2nr(argvars, rettv)
18654 typval_T *argvars;
18655 typval_T *rettv;
18656{
18657 int base = 10;
18658 char_u *p;
18659 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018660 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018661
18662 if (argvars[1].v_type != VAR_UNKNOWN)
18663 {
18664 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018665 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018666 {
18667 EMSG(_(e_invarg));
18668 return;
18669 }
18670 }
18671
18672 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018673 if (*p == '+')
18674 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018675 switch (base)
18676 {
18677 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18678 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18679 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18680 default: what = 0;
18681 }
18682 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018683 rettv->vval.v_number = n;
18684}
18685
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686#ifdef HAVE_STRFTIME
18687/*
18688 * "strftime({format}[, {time}])" function
18689 */
18690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018691f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018692 typval_T *argvars;
18693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694{
18695 char_u result_buf[256];
18696 struct tm *curtime;
18697 time_t seconds;
18698 char_u *p;
18699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018700 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018703 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 seconds = time(NULL);
18705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707 curtime = localtime(&seconds);
18708 /* MSVC returns NULL for an invalid value of seconds. */
18709 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 else
18712 {
18713# ifdef FEAT_MBYTE
18714 vimconv_T conv;
18715 char_u *enc;
18716
18717 conv.vc_type = CONV_NONE;
18718 enc = enc_locale();
18719 convert_setup(&conv, p_enc, enc);
18720 if (conv.vc_type != CONV_NONE)
18721 p = string_convert(&conv, p, NULL);
18722# endif
18723 if (p != NULL)
18724 (void)strftime((char *)result_buf, sizeof(result_buf),
18725 (char *)p, curtime);
18726 else
18727 result_buf[0] = NUL;
18728
18729# ifdef FEAT_MBYTE
18730 if (conv.vc_type != CONV_NONE)
18731 vim_free(p);
18732 convert_setup(&conv, enc, p_enc);
18733 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 else
18736# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018737 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
18739# ifdef FEAT_MBYTE
18740 /* Release conversion descriptors */
18741 convert_setup(&conv, NULL, NULL);
18742 vim_free(enc);
18743# endif
18744 }
18745}
18746#endif
18747
18748/*
18749 * "stridx()" function
18750 */
18751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 typval_T *argvars;
18754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755{
18756 char_u buf[NUMBUFLEN];
18757 char_u *needle;
18758 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018759 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018763 needle = get_tv_string_chk(&argvars[1]);
18764 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018766 if (needle == NULL || haystack == NULL)
18767 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 if (argvars[2].v_type != VAR_UNKNOWN)
18770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018771 int error = FALSE;
18772
18773 start_idx = get_tv_number_chk(&argvars[2], &error);
18774 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018776 if (start_idx >= 0)
18777 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018778 }
18779
18780 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18781 if (pos != NULL)
18782 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783}
18784
18785/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018786 * "string()" function
18787 */
18788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018789f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018790 typval_T *argvars;
18791 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018792{
18793 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018794 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018796 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018797 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018798 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018799 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
18803/*
18804 * "strlen()" function
18805 */
18806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 typval_T *argvars;
18809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 rettv->vval.v_number = (varnumber_T)(STRLEN(
18812 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813}
18814
18815/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018816 * "strchars()" function
18817 */
18818 static void
18819f_strchars(argvars, rettv)
18820 typval_T *argvars;
18821 typval_T *rettv;
18822{
18823 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018824 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018825#ifdef FEAT_MBYTE
18826 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018827 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018828#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018829
18830 if (argvars[1].v_type != VAR_UNKNOWN)
18831 skipcc = get_tv_number_chk(&argvars[1], NULL);
18832 if (skipcc < 0 || skipcc > 1)
18833 EMSG(_(e_invarg));
18834 else
18835 {
18836#ifdef FEAT_MBYTE
18837 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18838 while (*s != NUL)
18839 {
18840 func_mb_ptr2char_adv(&s);
18841 ++len;
18842 }
18843 rettv->vval.v_number = len;
18844#else
18845 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18846#endif
18847 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018848}
18849
18850/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018851 * "strdisplaywidth()" function
18852 */
18853 static void
18854f_strdisplaywidth(argvars, rettv)
18855 typval_T *argvars;
18856 typval_T *rettv;
18857{
18858 char_u *s = get_tv_string(&argvars[0]);
18859 int col = 0;
18860
18861 if (argvars[1].v_type != VAR_UNKNOWN)
18862 col = get_tv_number(&argvars[1]);
18863
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018864 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018865}
18866
18867/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018868 * "strwidth()" function
18869 */
18870 static void
18871f_strwidth(argvars, rettv)
18872 typval_T *argvars;
18873 typval_T *rettv;
18874{
18875 char_u *s = get_tv_string(&argvars[0]);
18876
18877 rettv->vval.v_number = (varnumber_T)(
18878#ifdef FEAT_MBYTE
18879 mb_string2cells(s, -1)
18880#else
18881 STRLEN(s)
18882#endif
18883 );
18884}
18885
18886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 * "strpart()" function
18888 */
18889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018890f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018891 typval_T *argvars;
18892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893{
18894 char_u *p;
18895 int n;
18896 int len;
18897 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018898 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018900 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 slen = (int)STRLEN(p);
18902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018903 n = get_tv_number_chk(&argvars[1], &error);
18904 if (error)
18905 len = 0;
18906 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 else
18909 len = slen - n; /* default len: all bytes that are available. */
18910
18911 /*
18912 * Only return the overlap between the specified part and the actual
18913 * string.
18914 */
18915 if (n < 0)
18916 {
18917 len += n;
18918 n = 0;
18919 }
18920 else if (n > slen)
18921 n = slen;
18922 if (len < 0)
18923 len = 0;
18924 else if (n + len > slen)
18925 len = slen - n;
18926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927 rettv->v_type = VAR_STRING;
18928 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929}
18930
18931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932 * "strridx()" function
18933 */
18934 static void
18935f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 typval_T *argvars;
18937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018938{
18939 char_u buf[NUMBUFLEN];
18940 char_u *needle;
18941 char_u *haystack;
18942 char_u *rest;
18943 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018944 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018946 needle = get_tv_string_chk(&argvars[1]);
18947 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018948
18949 rettv->vval.v_number = -1;
18950 if (needle == NULL || haystack == NULL)
18951 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018952
18953 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018954 if (argvars[2].v_type != VAR_UNKNOWN)
18955 {
18956 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018957 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018958 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018959 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018960 }
18961 else
18962 end_idx = haystack_len;
18963
Bram Moolenaar0d660222005-01-07 21:51:51 +000018964 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018965 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018966 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018967 lastmatch = haystack + end_idx;
18968 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018970 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971 for (rest = haystack; *rest != '\0'; ++rest)
18972 {
18973 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018974 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 break;
18976 lastmatch = rest;
18977 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979
18980 if (lastmatch == NULL)
18981 rettv->vval.v_number = -1;
18982 else
18983 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18984}
18985
18986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 * "strtrans()" function
18988 */
18989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018990f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018991 typval_T *argvars;
18992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018994 rettv->v_type = VAR_STRING;
18995 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996}
18997
18998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 * "submatch()" function
19000 */
19001 static void
19002f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 typval_T *argvars;
19004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005{
Bram Moolenaar41571762014-04-02 19:00:58 +020019006 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019007 int no;
19008 int retList = 0;
19009
19010 no = (int)get_tv_number_chk(&argvars[0], &error);
19011 if (error)
19012 return;
19013 error = FALSE;
19014 if (argvars[1].v_type != VAR_UNKNOWN)
19015 retList = get_tv_number_chk(&argvars[1], &error);
19016 if (error)
19017 return;
19018
19019 if (retList == 0)
19020 {
19021 rettv->v_type = VAR_STRING;
19022 rettv->vval.v_string = reg_submatch(no);
19023 }
19024 else
19025 {
19026 rettv->v_type = VAR_LIST;
19027 rettv->vval.v_list = reg_submatch_list(no);
19028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019029}
19030
19031/*
19032 * "substitute()" function
19033 */
19034 static void
19035f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019036 typval_T *argvars;
19037 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019038{
19039 char_u patbuf[NUMBUFLEN];
19040 char_u subbuf[NUMBUFLEN];
19041 char_u flagsbuf[NUMBUFLEN];
19042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019043 char_u *str = get_tv_string_chk(&argvars[0]);
19044 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19045 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19046 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19047
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019049 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19050 rettv->vval.v_string = NULL;
19051 else
19052 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053}
19054
19055/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019056 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062{
19063 int id = 0;
19064#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019065 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 long col;
19067 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019068 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019070 lnum = get_tv_lnum(argvars); /* -1 on type error */
19071 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19072 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019074 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019075 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019076 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077#endif
19078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080}
19081
19082/*
19083 * "synIDattr(id, what [, mode])" function
19084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019086f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089{
19090 char_u *p = NULL;
19091#ifdef FEAT_SYN_HL
19092 int id;
19093 char_u *what;
19094 char_u *mode;
19095 char_u modebuf[NUMBUFLEN];
19096 int modec;
19097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098 id = get_tv_number(&argvars[0]);
19099 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019100 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019104 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 modec = 0; /* replace invalid with current */
19106 }
19107 else
19108 {
19109#ifdef FEAT_GUI
19110 if (gui.in_use)
19111 modec = 'g';
19112 else
19113#endif
19114 if (t_colors > 1)
19115 modec = 'c';
19116 else
19117 modec = 't';
19118 }
19119
19120
19121 switch (TOLOWER_ASC(what[0]))
19122 {
19123 case 'b':
19124 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19125 p = highlight_color(id, what, modec);
19126 else /* bold */
19127 p = highlight_has_attr(id, HL_BOLD, modec);
19128 break;
19129
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019130 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 p = highlight_color(id, what, modec);
19132 break;
19133
19134 case 'i':
19135 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19136 p = highlight_has_attr(id, HL_INVERSE, modec);
19137 else /* italic */
19138 p = highlight_has_attr(id, HL_ITALIC, modec);
19139 break;
19140
19141 case 'n': /* name */
19142 p = get_highlight_name(NULL, id - 1);
19143 break;
19144
19145 case 'r': /* reverse */
19146 p = highlight_has_attr(id, HL_INVERSE, modec);
19147 break;
19148
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019149 case 's':
19150 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19151 p = highlight_color(id, what, modec);
19152 else /* standout */
19153 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 break;
19155
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019156 case 'u':
19157 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19158 /* underline */
19159 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19160 else
19161 /* undercurl */
19162 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 break;
19164 }
19165
19166 if (p != NULL)
19167 p = vim_strsave(p);
19168#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 rettv->v_type = VAR_STRING;
19170 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171}
19172
19173/*
19174 * "synIDtrans(id)" function
19175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180{
19181 int id;
19182
19183#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019184 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185
19186 if (id > 0)
19187 id = syn_get_final_id(id);
19188 else
19189#endif
19190 id = 0;
19191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019192 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193}
19194
19195/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019196 * "synconcealed(lnum, col)" function
19197 */
19198 static void
19199f_synconcealed(argvars, rettv)
19200 typval_T *argvars UNUSED;
19201 typval_T *rettv;
19202{
19203#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19204 long lnum;
19205 long col;
19206 int syntax_flags = 0;
19207 int cchar;
19208 int matchid = 0;
19209 char_u str[NUMBUFLEN];
19210#endif
19211
19212 rettv->v_type = VAR_LIST;
19213 rettv->vval.v_list = NULL;
19214
19215#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19216 lnum = get_tv_lnum(argvars); /* -1 on type error */
19217 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19218
19219 vim_memset(str, NUL, sizeof(str));
19220
19221 if (rettv_list_alloc(rettv) != FAIL)
19222 {
19223 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19224 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19225 && curwin->w_p_cole > 0)
19226 {
19227 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19228 syntax_flags = get_syntax_info(&matchid);
19229
19230 /* get the conceal character */
19231 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19232 {
19233 cchar = syn_get_sub_char();
19234 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19235 cchar = lcs_conceal;
19236 if (cchar != NUL)
19237 {
19238# ifdef FEAT_MBYTE
19239 if (has_mbyte)
19240 (*mb_char2bytes)(cchar, str);
19241 else
19242# endif
19243 str[0] = cchar;
19244 }
19245 }
19246 }
19247
19248 list_append_number(rettv->vval.v_list,
19249 (syntax_flags & HL_CONCEAL) != 0);
19250 /* -1 to auto-determine strlen */
19251 list_append_string(rettv->vval.v_list, str, -1);
19252 list_append_number(rettv->vval.v_list, matchid);
19253 }
19254#endif
19255}
19256
19257/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019258 * "synstack(lnum, col)" function
19259 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019260 static void
19261f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019262 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019263 typval_T *rettv;
19264{
19265#ifdef FEAT_SYN_HL
19266 long lnum;
19267 long col;
19268 int i;
19269 int id;
19270#endif
19271
19272 rettv->v_type = VAR_LIST;
19273 rettv->vval.v_list = NULL;
19274
19275#ifdef FEAT_SYN_HL
19276 lnum = get_tv_lnum(argvars); /* -1 on type error */
19277 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19278
19279 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019280 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019281 && rettv_list_alloc(rettv) != FAIL)
19282 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019283 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019284 for (i = 0; ; ++i)
19285 {
19286 id = syn_get_stack_item(i);
19287 if (id < 0)
19288 break;
19289 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19290 break;
19291 }
19292 }
19293#endif
19294}
19295
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019297get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019298 typval_T *argvars;
19299 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019300 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019302 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019304 char_u *infile = NULL;
19305 char_u buf[NUMBUFLEN];
19306 int err = FALSE;
19307 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019309 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019311 rettv->v_type = VAR_STRING;
19312 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019313 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019314 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019315
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019316 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019317 {
19318 /*
19319 * Write the string to a temp file, to be used for input of the shell
19320 * command.
19321 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019322 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019323 {
19324 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019325 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019326 }
19327
19328 fd = mch_fopen((char *)infile, WRITEBIN);
19329 if (fd == NULL)
19330 {
19331 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019332 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019333 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019334 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019335 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019336 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19337 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019338 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019339 else
19340 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019341 size_t len;
19342
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019343 p = get_tv_string_buf_chk(&argvars[1], buf);
19344 if (p == NULL)
19345 {
19346 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019347 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019348 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019349 len = STRLEN(p);
19350 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019351 err = TRUE;
19352 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019353 if (fclose(fd) != 0)
19354 err = TRUE;
19355 if (err)
19356 {
19357 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019358 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019359 }
19360 }
19361
Bram Moolenaar52a72462014-08-29 15:53:52 +020019362 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19363 * echoes typeahead, that messes up the display. */
19364 if (!msg_silent)
19365 flags += SHELL_COOKED;
19366
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019367 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019369 int len;
19370 listitem_T *li;
19371 char_u *s = NULL;
19372 char_u *start;
19373 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019374 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375
Bram Moolenaar52a72462014-08-29 15:53:52 +020019376 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019377 if (res == NULL)
19378 goto errret;
19379
19380 list = list_alloc();
19381 if (list == NULL)
19382 goto errret;
19383
19384 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019387 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019388 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019389 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019390
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019391 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019392 if (s == NULL)
19393 goto errret;
19394
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019395 for (p = s; start < end; ++p, ++start)
19396 *p = *start == NUL ? NL : *start;
19397 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019398
19399 li = listitem_alloc();
19400 if (li == NULL)
19401 {
19402 vim_free(s);
19403 goto errret;
19404 }
19405 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019406 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019407 li->li_tv.vval.v_string = s;
19408 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019410
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019411 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019412 rettv->v_type = VAR_LIST;
19413 rettv->vval.v_list = list;
19414 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019416 else
19417 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019418 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019419#ifdef USE_CR
19420 /* translate <CR> into <NL> */
19421 if (res != NULL)
19422 {
19423 char_u *s;
19424
19425 for (s = res; *s; ++s)
19426 {
19427 if (*s == CAR)
19428 *s = NL;
19429 }
19430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431#else
19432# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019433 /* translate <CR><NL> into <NL> */
19434 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019436 char_u *s, *d;
19437
19438 d = res;
19439 for (s = res; *s; ++s)
19440 {
19441 if (s[0] == CAR && s[1] == NL)
19442 ++s;
19443 *d++ = *s;
19444 }
19445 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447# endif
19448#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019449 rettv->vval.v_string = res;
19450 res = NULL;
19451 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019452
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019453errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019454 if (infile != NULL)
19455 {
19456 mch_remove(infile);
19457 vim_free(infile);
19458 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019459 if (res != NULL)
19460 vim_free(res);
19461 if (list != NULL)
19462 list_free(list, TRUE);
19463}
19464
19465/*
19466 * "system()" function
19467 */
19468 static void
19469f_system(argvars, rettv)
19470 typval_T *argvars;
19471 typval_T *rettv;
19472{
19473 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19474}
19475
19476/*
19477 * "systemlist()" function
19478 */
19479 static void
19480f_systemlist(argvars, rettv)
19481 typval_T *argvars;
19482 typval_T *rettv;
19483{
19484 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485}
19486
19487/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019488 * "tabpagebuflist()" function
19489 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019490 static void
19491f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019492 typval_T *argvars UNUSED;
19493 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019494{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019495#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019496 tabpage_T *tp;
19497 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019498
19499 if (argvars[0].v_type == VAR_UNKNOWN)
19500 wp = firstwin;
19501 else
19502 {
19503 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19504 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019505 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019506 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019507 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019508 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019509 for (; wp != NULL; wp = wp->w_next)
19510 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019511 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019512 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019513 }
19514#endif
19515}
19516
19517
19518/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019519 * "tabpagenr()" function
19520 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019521 static void
19522f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019523 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019524 typval_T *rettv;
19525{
19526 int nr = 1;
19527#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019528 char_u *arg;
19529
19530 if (argvars[0].v_type != VAR_UNKNOWN)
19531 {
19532 arg = get_tv_string_chk(&argvars[0]);
19533 nr = 0;
19534 if (arg != NULL)
19535 {
19536 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019537 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019538 else
19539 EMSG2(_(e_invexpr2), arg);
19540 }
19541 }
19542 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019543 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019544#endif
19545 rettv->vval.v_number = nr;
19546}
19547
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019548
19549#ifdef FEAT_WINDOWS
19550static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19551
19552/*
19553 * Common code for tabpagewinnr() and winnr().
19554 */
19555 static int
19556get_winnr(tp, argvar)
19557 tabpage_T *tp;
19558 typval_T *argvar;
19559{
19560 win_T *twin;
19561 int nr = 1;
19562 win_T *wp;
19563 char_u *arg;
19564
19565 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19566 if (argvar->v_type != VAR_UNKNOWN)
19567 {
19568 arg = get_tv_string_chk(argvar);
19569 if (arg == NULL)
19570 nr = 0; /* type error; errmsg already given */
19571 else if (STRCMP(arg, "$") == 0)
19572 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19573 else if (STRCMP(arg, "#") == 0)
19574 {
19575 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19576 if (twin == NULL)
19577 nr = 0;
19578 }
19579 else
19580 {
19581 EMSG2(_(e_invexpr2), arg);
19582 nr = 0;
19583 }
19584 }
19585
19586 if (nr > 0)
19587 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19588 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019589 {
19590 if (wp == NULL)
19591 {
19592 /* didn't find it in this tabpage */
19593 nr = 0;
19594 break;
19595 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019596 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019597 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019598 return nr;
19599}
19600#endif
19601
19602/*
19603 * "tabpagewinnr()" function
19604 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019605 static void
19606f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019607 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019608 typval_T *rettv;
19609{
19610 int nr = 1;
19611#ifdef FEAT_WINDOWS
19612 tabpage_T *tp;
19613
19614 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19615 if (tp == NULL)
19616 nr = 0;
19617 else
19618 nr = get_winnr(tp, &argvars[1]);
19619#endif
19620 rettv->vval.v_number = nr;
19621}
19622
19623
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019624/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019625 * "tagfiles()" function
19626 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019627 static void
19628f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019629 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019630 typval_T *rettv;
19631{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019632 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019633 tagname_T tn;
19634 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019635
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019636 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019637 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019638 fname = alloc(MAXPATHL);
19639 if (fname == NULL)
19640 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019641
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019642 for (first = TRUE; ; first = FALSE)
19643 if (get_tagfname(&tn, first, fname) == FAIL
19644 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019645 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019646 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019647 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019648}
19649
19650/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019651 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019652 */
19653 static void
19654f_taglist(argvars, rettv)
19655 typval_T *argvars;
19656 typval_T *rettv;
19657{
19658 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019659
19660 tag_pattern = get_tv_string(&argvars[0]);
19661
19662 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019663 if (*tag_pattern == NUL)
19664 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019666 if (rettv_list_alloc(rettv) == OK)
19667 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019668}
19669
19670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 * "tempname()" function
19672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019674f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019675 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677{
19678 static int x = 'A';
19679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019680 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019681 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682
19683 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19684 * names. Skip 'I' and 'O', they are used for shell redirection. */
19685 do
19686 {
19687 if (x == 'Z')
19688 x = '0';
19689 else if (x == '9')
19690 x = 'A';
19691 else
19692 {
19693#ifdef EBCDIC
19694 if (x == 'I')
19695 x = 'J';
19696 else if (x == 'R')
19697 x = 'S';
19698 else
19699#endif
19700 ++x;
19701 }
19702 } while (x == 'I' || x == 'O');
19703}
19704
19705/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019706 * "test(list)" function: Just checking the walls...
19707 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019708 static void
19709f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019710 typval_T *argvars UNUSED;
19711 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019712{
19713 /* Used for unit testing. Change the code below to your liking. */
19714#if 0
19715 listitem_T *li;
19716 list_T *l;
19717 char_u *bad, *good;
19718
19719 if (argvars[0].v_type != VAR_LIST)
19720 return;
19721 l = argvars[0].vval.v_list;
19722 if (l == NULL)
19723 return;
19724 li = l->lv_first;
19725 if (li == NULL)
19726 return;
19727 bad = get_tv_string(&li->li_tv);
19728 li = li->li_next;
19729 if (li == NULL)
19730 return;
19731 good = get_tv_string(&li->li_tv);
19732 rettv->vval.v_number = test_edit_score(bad, good);
19733#endif
19734}
19735
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019736#ifdef FEAT_FLOAT
19737/*
19738 * "tan()" function
19739 */
19740 static void
19741f_tan(argvars, rettv)
19742 typval_T *argvars;
19743 typval_T *rettv;
19744{
19745 float_T f;
19746
19747 rettv->v_type = VAR_FLOAT;
19748 if (get_float_arg(argvars, &f) == OK)
19749 rettv->vval.v_float = tan(f);
19750 else
19751 rettv->vval.v_float = 0.0;
19752}
19753
19754/*
19755 * "tanh()" function
19756 */
19757 static void
19758f_tanh(argvars, rettv)
19759 typval_T *argvars;
19760 typval_T *rettv;
19761{
19762 float_T f;
19763
19764 rettv->v_type = VAR_FLOAT;
19765 if (get_float_arg(argvars, &f) == OK)
19766 rettv->vval.v_float = tanh(f);
19767 else
19768 rettv->vval.v_float = 0.0;
19769}
19770#endif
19771
Bram Moolenaard52d9742005-08-21 22:20:28 +000019772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 * "tolower(string)" function
19774 */
19775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 typval_T *argvars;
19778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779{
19780 char_u *p;
19781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 p = vim_strsave(get_tv_string(&argvars[0]));
19783 rettv->v_type = VAR_STRING;
19784 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785
19786 if (p != NULL)
19787 while (*p != NUL)
19788 {
19789#ifdef FEAT_MBYTE
19790 int l;
19791
19792 if (enc_utf8)
19793 {
19794 int c, lc;
19795
19796 c = utf_ptr2char(p);
19797 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019798 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 /* TODO: reallocate string when byte count changes. */
19800 if (utf_char2len(lc) == l)
19801 utf_char2bytes(lc, p);
19802 p += l;
19803 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019804 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 p += l; /* skip multi-byte character */
19806 else
19807#endif
19808 {
19809 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19810 ++p;
19811 }
19812 }
19813}
19814
19815/*
19816 * "toupper(string)" function
19817 */
19818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 typval_T *argvars;
19821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019824 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825}
19826
19827/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019828 * "tr(string, fromstr, tostr)" function
19829 */
19830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 typval_T *argvars;
19833 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019834{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019835 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019836 char_u *fromstr;
19837 char_u *tostr;
19838 char_u *p;
19839#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019840 int inlen;
19841 int fromlen;
19842 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019843 int idx;
19844 char_u *cpstr;
19845 int cplen;
19846 int first = TRUE;
19847#endif
19848 char_u buf[NUMBUFLEN];
19849 char_u buf2[NUMBUFLEN];
19850 garray_T ga;
19851
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019852 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019853 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19854 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019855
19856 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 rettv->v_type = VAR_STRING;
19858 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019859 if (fromstr == NULL || tostr == NULL)
19860 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019861 ga_init2(&ga, (int)sizeof(char), 80);
19862
19863#ifdef FEAT_MBYTE
19864 if (!has_mbyte)
19865#endif
19866 /* not multi-byte: fromstr and tostr must be the same length */
19867 if (STRLEN(fromstr) != STRLEN(tostr))
19868 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019869#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019870error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019871#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019872 EMSG2(_(e_invarg2), fromstr);
19873 ga_clear(&ga);
19874 return;
19875 }
19876
19877 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019878 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019879 {
19880#ifdef FEAT_MBYTE
19881 if (has_mbyte)
19882 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019883 inlen = (*mb_ptr2len)(in_str);
19884 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019885 cplen = inlen;
19886 idx = 0;
19887 for (p = fromstr; *p != NUL; p += fromlen)
19888 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019889 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019890 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019891 {
19892 for (p = tostr; *p != NUL; p += tolen)
19893 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019894 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019895 if (idx-- == 0)
19896 {
19897 cplen = tolen;
19898 cpstr = p;
19899 break;
19900 }
19901 }
19902 if (*p == NUL) /* tostr is shorter than fromstr */
19903 goto error;
19904 break;
19905 }
19906 ++idx;
19907 }
19908
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019909 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019910 {
19911 /* Check that fromstr and tostr have the same number of
19912 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019913 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019914 first = FALSE;
19915 for (p = tostr; *p != NUL; p += tolen)
19916 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019917 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019918 --idx;
19919 }
19920 if (idx != 0)
19921 goto error;
19922 }
19923
Bram Moolenaarcde88542015-08-11 19:14:00 +020019924 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019925 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019926 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019927
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019928 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019929 }
19930 else
19931#endif
19932 {
19933 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019934 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019935 if (p != NULL)
19936 ga_append(&ga, tostr[p - fromstr]);
19937 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019938 ga_append(&ga, *in_str);
19939 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019940 }
19941 }
19942
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019943 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019944 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019945 ga_append(&ga, NUL);
19946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019947 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019948}
19949
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019950#ifdef FEAT_FLOAT
19951/*
19952 * "trunc({float})" function
19953 */
19954 static void
19955f_trunc(argvars, rettv)
19956 typval_T *argvars;
19957 typval_T *rettv;
19958{
19959 float_T f;
19960
19961 rettv->v_type = VAR_FLOAT;
19962 if (get_float_arg(argvars, &f) == OK)
19963 /* trunc() is not in C90, use floor() or ceil() instead. */
19964 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19965 else
19966 rettv->vval.v_float = 0.0;
19967}
19968#endif
19969
Bram Moolenaar8299df92004-07-10 09:47:34 +000019970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 * "type(expr)" function
19972 */
19973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019975 typval_T *argvars;
19976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019978 int n;
19979
19980 switch (argvars[0].v_type)
19981 {
19982 case VAR_NUMBER: n = 0; break;
19983 case VAR_STRING: n = 1; break;
19984 case VAR_FUNC: n = 2; break;
19985 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019986 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019987#ifdef FEAT_FLOAT
19988 case VAR_FLOAT: n = 5; break;
19989#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019990 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19991 }
19992 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993}
19994
19995/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019996 * "undofile(name)" function
19997 */
19998 static void
19999f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020000 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020001 typval_T *rettv;
20002{
20003 rettv->v_type = VAR_STRING;
20004#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020005 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020006 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020007
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020008 if (*fname == NUL)
20009 {
20010 /* If there is no file name there will be no undo file. */
20011 rettv->vval.v_string = NULL;
20012 }
20013 else
20014 {
20015 char_u *ffname = FullName_save(fname, FALSE);
20016
20017 if (ffname != NULL)
20018 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20019 vim_free(ffname);
20020 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020021 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020022#else
20023 rettv->vval.v_string = NULL;
20024#endif
20025}
20026
20027/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020028 * "undotree()" function
20029 */
20030 static void
20031f_undotree(argvars, rettv)
20032 typval_T *argvars UNUSED;
20033 typval_T *rettv;
20034{
20035 if (rettv_dict_alloc(rettv) == OK)
20036 {
20037 dict_T *dict = rettv->vval.v_dict;
20038 list_T *list;
20039
Bram Moolenaar730cde92010-06-27 05:18:54 +020020040 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020041 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020042 dict_add_nr_str(dict, "save_last",
20043 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020044 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20045 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020046 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020047
20048 list = list_alloc();
20049 if (list != NULL)
20050 {
20051 u_eval_tree(curbuf->b_u_oldhead, list);
20052 dict_add_list(dict, "entries", list);
20053 }
20054 }
20055}
20056
20057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020058 * "values(dict)" function
20059 */
20060 static void
20061f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 typval_T *argvars;
20063 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020064{
20065 dict_list(argvars, rettv, 1);
20066}
20067
20068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 * "virtcol(string)" function
20070 */
20071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 typval_T *argvars;
20074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075{
20076 colnr_T vcol = 0;
20077 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020078 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020080 fp = var2fpos(&argvars[0], FALSE, &fnum);
20081 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20082 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 {
20084 getvvcol(curwin, fp, NULL, NULL, &vcol);
20085 ++vcol;
20086 }
20087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089}
20090
20091/*
20092 * "visualmode()" function
20093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020096 typval_T *argvars UNUSED;
20097 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 char_u str[2];
20100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 str[0] = curbuf->b_visual_mode_eval;
20103 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105
20106 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020107 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109}
20110
20111/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020112 * "wildmenumode()" function
20113 */
20114 static void
20115f_wildmenumode(argvars, rettv)
20116 typval_T *argvars UNUSED;
20117 typval_T *rettv UNUSED;
20118{
20119#ifdef FEAT_WILDMENU
20120 if (wild_menu_showing)
20121 rettv->vval.v_number = 1;
20122#endif
20123}
20124
20125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 * "winbufnr(nr)" function
20127 */
20128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020130 typval_T *argvars;
20131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132{
20133 win_T *wp;
20134
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020135 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140}
20141
20142/*
20143 * "wincol()" function
20144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149{
20150 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020151 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152}
20153
20154/*
20155 * "winheight(nr)" function
20156 */
20157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020159 typval_T *argvars;
20160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161{
20162 win_T *wp;
20163
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020164 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020168 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169}
20170
20171/*
20172 * "winline()" function
20173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020176 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178{
20179 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181}
20182
20183/*
20184 * "winnr()" function
20185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020187f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020188 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190{
20191 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020192
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020194 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020196 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197}
20198
20199/*
20200 * "winrestcmd()" function
20201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020203f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020204 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206{
20207#ifdef FEAT_WINDOWS
20208 win_T *wp;
20209 int winnr = 1;
20210 garray_T ga;
20211 char_u buf[50];
20212
20213 ga_init2(&ga, (int)sizeof(char), 70);
20214 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20215 {
20216 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20217 ga_concat(&ga, buf);
20218# ifdef FEAT_VERTSPLIT
20219 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20220 ga_concat(&ga, buf);
20221# endif
20222 ++winnr;
20223 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020224 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020228 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020230 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231}
20232
20233/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020234 * "winrestview()" function
20235 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020236 static void
20237f_winrestview(argvars, rettv)
20238 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020239 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020240{
20241 dict_T *dict;
20242
20243 if (argvars[0].v_type != VAR_DICT
20244 || (dict = argvars[0].vval.v_dict) == NULL)
20245 EMSG(_(e_invarg));
20246 else
20247 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020248 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20249 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20250 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20251 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020252#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020253 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20254 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020255#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020256 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20257 {
20258 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20259 curwin->w_set_curswant = FALSE;
20260 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020261
Bram Moolenaar82c25852014-05-28 16:47:16 +020020262 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20263 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020264#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020265 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20266 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020267#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020268 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20269 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20270 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20271 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020272
20273 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020274 win_new_height(curwin, curwin->w_height);
20275# ifdef FEAT_VERTSPLIT
20276 win_new_width(curwin, W_WIDTH(curwin));
20277# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020278 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020279
Bram Moolenaarb851a962014-10-31 15:45:52 +010020280 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020281 curwin->w_topline = 1;
20282 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20283 curwin->w_topline = curbuf->b_ml.ml_line_count;
20284#ifdef FEAT_DIFF
20285 check_topfill(curwin, TRUE);
20286#endif
20287 }
20288}
20289
20290/*
20291 * "winsaveview()" function
20292 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020293 static void
20294f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020295 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020296 typval_T *rettv;
20297{
20298 dict_T *dict;
20299
Bram Moolenaara800b422010-06-27 01:15:55 +020020300 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020301 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020302 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020303
20304 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20305 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20306#ifdef FEAT_VIRTUALEDIT
20307 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20308#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020309 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020310 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20311
20312 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20313#ifdef FEAT_DIFF
20314 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20315#endif
20316 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20317 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20318}
20319
20320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 * "winwidth(nr)" function
20322 */
20323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020324f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 typval_T *argvars;
20326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327{
20328 win_T *wp;
20329
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020330 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 else
20334#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020335 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020337 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338#endif
20339}
20340
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020342 * "wordcount()" function
20343 */
20344 static void
20345f_wordcount(argvars, rettv)
20346 typval_T *argvars UNUSED;
20347 typval_T *rettv;
20348{
20349 if (rettv_dict_alloc(rettv) == FAIL)
20350 return;
20351 cursor_pos_info(rettv->vval.v_dict);
20352}
20353
20354/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020355 * Write list of strings to file
20356 */
20357 static int
20358write_list(fd, list, binary)
20359 FILE *fd;
20360 list_T *list;
20361 int binary;
20362{
20363 listitem_T *li;
20364 int c;
20365 int ret = OK;
20366 char_u *s;
20367
20368 for (li = list->lv_first; li != NULL; li = li->li_next)
20369 {
20370 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20371 {
20372 if (*s == '\n')
20373 c = putc(NUL, fd);
20374 else
20375 c = putc(*s, fd);
20376 if (c == EOF)
20377 {
20378 ret = FAIL;
20379 break;
20380 }
20381 }
20382 if (!binary || li->li_next != NULL)
20383 if (putc('\n', fd) == EOF)
20384 {
20385 ret = FAIL;
20386 break;
20387 }
20388 if (ret == FAIL)
20389 {
20390 EMSG(_(e_write));
20391 break;
20392 }
20393 }
20394 return ret;
20395}
20396
20397/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020398 * "writefile()" function
20399 */
20400 static void
20401f_writefile(argvars, rettv)
20402 typval_T *argvars;
20403 typval_T *rettv;
20404{
20405 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020406 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020407 char_u *fname;
20408 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020409 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020410
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020411 if (check_restricted() || check_secure())
20412 return;
20413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020414 if (argvars[0].v_type != VAR_LIST)
20415 {
20416 EMSG2(_(e_listarg), "writefile()");
20417 return;
20418 }
20419 if (argvars[0].vval.v_list == NULL)
20420 return;
20421
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020422 if (argvars[2].v_type != VAR_UNKNOWN)
20423 {
20424 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20425 binary = TRUE;
20426 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20427 append = TRUE;
20428 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020429
20430 /* Always open the file in binary mode, library functions have a mind of
20431 * their own about CR-LF conversion. */
20432 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020433 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20434 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020435 {
20436 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20437 ret = -1;
20438 }
20439 else
20440 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020441 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20442 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020443 fclose(fd);
20444 }
20445
20446 rettv->vval.v_number = ret;
20447}
20448
20449/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020450 * "xor(expr, expr)" function
20451 */
20452 static void
20453f_xor(argvars, rettv)
20454 typval_T *argvars;
20455 typval_T *rettv;
20456{
20457 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20458 ^ get_tv_number_chk(&argvars[1], NULL);
20459}
20460
20461
20462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020464 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 */
20466 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020467var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020468 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020469 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020470 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020472 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020474 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475
Bram Moolenaara5525202006-03-02 22:52:09 +000020476 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020477 if (varp->v_type == VAR_LIST)
20478 {
20479 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020480 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020481 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020482 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020483
20484 l = varp->vval.v_list;
20485 if (l == NULL)
20486 return NULL;
20487
20488 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020489 pos.lnum = list_find_nr(l, 0L, &error);
20490 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020491 return NULL; /* invalid line number */
20492
20493 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020494 pos.col = list_find_nr(l, 1L, &error);
20495 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020496 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020497 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020498
20499 /* We accept "$" for the column number: last column. */
20500 li = list_find(l, 1L);
20501 if (li != NULL && li->li_tv.v_type == VAR_STRING
20502 && li->li_tv.vval.v_string != NULL
20503 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20504 pos.col = len + 1;
20505
Bram Moolenaara5525202006-03-02 22:52:09 +000020506 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020507 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020508 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020509 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020510
Bram Moolenaara5525202006-03-02 22:52:09 +000020511#ifdef FEAT_VIRTUALEDIT
20512 /* Get the virtual offset. Defaults to zero. */
20513 pos.coladd = list_find_nr(l, 2L, &error);
20514 if (error)
20515 pos.coladd = 0;
20516#endif
20517
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020518 return &pos;
20519 }
20520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020521 name = get_tv_string_chk(varp);
20522 if (name == NULL)
20523 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020524 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020526 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20527 {
20528 if (VIsual_active)
20529 return &VIsual;
20530 return &curwin->w_cursor;
20531 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020532 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020534 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20536 return NULL;
20537 return pp;
20538 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020539
20540#ifdef FEAT_VIRTUALEDIT
20541 pos.coladd = 0;
20542#endif
20543
Bram Moolenaar477933c2007-07-17 14:32:23 +000020544 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020545 {
20546 pos.col = 0;
20547 if (name[1] == '0') /* "w0": first visible line */
20548 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020549 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020550 pos.lnum = curwin->w_topline;
20551 return &pos;
20552 }
20553 else if (name[1] == '$') /* "w$": last visible line */
20554 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020555 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020556 pos.lnum = curwin->w_botline - 1;
20557 return &pos;
20558 }
20559 }
20560 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020562 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 {
20564 pos.lnum = curbuf->b_ml.ml_line_count;
20565 pos.col = 0;
20566 }
20567 else
20568 {
20569 pos.lnum = curwin->w_cursor.lnum;
20570 pos.col = (colnr_T)STRLEN(ml_get_curline());
20571 }
20572 return &pos;
20573 }
20574 return NULL;
20575}
20576
20577/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020578 * Convert list in "arg" into a position and optional file number.
20579 * When "fnump" is NULL there is no file number, only 3 items.
20580 * Note that the column is passed on as-is, the caller may want to decrement
20581 * it to use 1 for the first column.
20582 * Return FAIL when conversion is not possible, doesn't check the position for
20583 * validity.
20584 */
20585 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020586list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020587 typval_T *arg;
20588 pos_T *posp;
20589 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020590 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020591{
20592 list_T *l = arg->vval.v_list;
20593 long i = 0;
20594 long n;
20595
Bram Moolenaar493c1782014-05-28 14:34:46 +020020596 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20597 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020598 if (arg->v_type != VAR_LIST
20599 || l == NULL
20600 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020601 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020602 return FAIL;
20603
20604 if (fnump != NULL)
20605 {
20606 n = list_find_nr(l, i++, NULL); /* fnum */
20607 if (n < 0)
20608 return FAIL;
20609 if (n == 0)
20610 n = curbuf->b_fnum; /* current buffer */
20611 *fnump = n;
20612 }
20613
20614 n = list_find_nr(l, i++, NULL); /* lnum */
20615 if (n < 0)
20616 return FAIL;
20617 posp->lnum = n;
20618
20619 n = list_find_nr(l, i++, NULL); /* col */
20620 if (n < 0)
20621 return FAIL;
20622 posp->col = n;
20623
20624#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020625 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020626 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020627 posp->coladd = 0;
20628 else
20629 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020630#endif
20631
Bram Moolenaar493c1782014-05-28 14:34:46 +020020632 if (curswantp != NULL)
20633 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20634
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020635 return OK;
20636}
20637
20638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 * Get the length of an environment variable name.
20640 * Advance "arg" to the first character after the name.
20641 * Return 0 for error.
20642 */
20643 static int
20644get_env_len(arg)
20645 char_u **arg;
20646{
20647 char_u *p;
20648 int len;
20649
20650 for (p = *arg; vim_isIDc(*p); ++p)
20651 ;
20652 if (p == *arg) /* no name found */
20653 return 0;
20654
20655 len = (int)(p - *arg);
20656 *arg = p;
20657 return len;
20658}
20659
20660/*
20661 * Get the length of the name of a function or internal variable.
20662 * "arg" is advanced to the first non-white character after the name.
20663 * Return 0 if something is wrong.
20664 */
20665 static int
20666get_id_len(arg)
20667 char_u **arg;
20668{
20669 char_u *p;
20670 int len;
20671
20672 /* Find the end of the name. */
20673 for (p = *arg; eval_isnamec(*p); ++p)
20674 ;
20675 if (p == *arg) /* no name found */
20676 return 0;
20677
20678 len = (int)(p - *arg);
20679 *arg = skipwhite(p);
20680
20681 return len;
20682}
20683
20684/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020685 * Get the length of the name of a variable or function.
20686 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020688 * Return -1 if curly braces expansion failed.
20689 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 * If the name contains 'magic' {}'s, expand them and return the
20691 * expanded name in an allocated string via 'alias' - caller must free.
20692 */
20693 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020694get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695 char_u **arg;
20696 char_u **alias;
20697 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020698 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699{
20700 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 char_u *p;
20702 char_u *expr_start;
20703 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704
20705 *alias = NULL; /* default to no alias */
20706
20707 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20708 && (*arg)[2] == (int)KE_SNR)
20709 {
20710 /* hard coded <SNR>, already translated */
20711 *arg += 3;
20712 return get_id_len(arg) + 3;
20713 }
20714 len = eval_fname_script(*arg);
20715 if (len > 0)
20716 {
20717 /* literal "<SID>", "s:" or "<SNR>" */
20718 *arg += len;
20719 }
20720
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020724 p = find_name_end(*arg, &expr_start, &expr_end,
20725 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 if (expr_start != NULL)
20727 {
20728 char_u *temp_string;
20729
20730 if (!evaluate)
20731 {
20732 len += (int)(p - *arg);
20733 *arg = skipwhite(p);
20734 return len;
20735 }
20736
20737 /*
20738 * Include any <SID> etc in the expanded string:
20739 * Thus the -len here.
20740 */
20741 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20742 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020743 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 *alias = temp_string;
20745 *arg = skipwhite(p);
20746 return (int)STRLEN(temp_string);
20747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748
20749 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020750 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 EMSG2(_(e_invexpr2), *arg);
20752
20753 return len;
20754}
20755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020756/*
20757 * Find the end of a variable or function name, taking care of magic braces.
20758 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20759 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020760 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020761 * Return a pointer to just after the name. Equal to "arg" if there is no
20762 * valid name.
20763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020765find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766 char_u *arg;
20767 char_u **expr_start;
20768 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020769 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771 int mb_nest = 0;
20772 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 char_u *p;
20774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020775 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020777 *expr_start = NULL;
20778 *expr_end = NULL;
20779 }
20780
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020781 /* Quick check for valid starting character. */
20782 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20783 return arg;
20784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020785 for (p = arg; *p != NUL
20786 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020787 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020788 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020790 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020791 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020792 if (*p == '\'')
20793 {
20794 /* skip over 'string' to avoid counting [ and ] inside it. */
20795 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20796 ;
20797 if (*p == NUL)
20798 break;
20799 }
20800 else if (*p == '"')
20801 {
20802 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20803 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20804 if (*p == '\\' && p[1] != NUL)
20805 ++p;
20806 if (*p == NUL)
20807 break;
20808 }
20809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020810 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 if (*p == '[')
20813 ++br_nest;
20814 else if (*p == ']')
20815 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020818 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020820 if (*p == '{')
20821 {
20822 mb_nest++;
20823 if (expr_start != NULL && *expr_start == NULL)
20824 *expr_start = p;
20825 }
20826 else if (*p == '}')
20827 {
20828 mb_nest--;
20829 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20830 *expr_end = p;
20831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 }
20834
20835 return p;
20836}
20837
20838/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020839 * Expands out the 'magic' {}'s in a variable/function name.
20840 * Note that this can call itself recursively, to deal with
20841 * constructs like foo{bar}{baz}{bam}
20842 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20843 * "in_start" ^
20844 * "expr_start" ^
20845 * "expr_end" ^
20846 * "in_end" ^
20847 *
20848 * Returns a new allocated string, which the caller must free.
20849 * Returns NULL for failure.
20850 */
20851 static char_u *
20852make_expanded_name(in_start, expr_start, expr_end, in_end)
20853 char_u *in_start;
20854 char_u *expr_start;
20855 char_u *expr_end;
20856 char_u *in_end;
20857{
20858 char_u c1;
20859 char_u *retval = NULL;
20860 char_u *temp_result;
20861 char_u *nextcmd = NULL;
20862
20863 if (expr_end == NULL || in_end == NULL)
20864 return NULL;
20865 *expr_start = NUL;
20866 *expr_end = NUL;
20867 c1 = *in_end;
20868 *in_end = NUL;
20869
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020870 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020871 if (temp_result != NULL && nextcmd == NULL)
20872 {
20873 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20874 + (in_end - expr_end) + 1));
20875 if (retval != NULL)
20876 {
20877 STRCPY(retval, in_start);
20878 STRCAT(retval, temp_result);
20879 STRCAT(retval, expr_end + 1);
20880 }
20881 }
20882 vim_free(temp_result);
20883
20884 *in_end = c1; /* put char back for error messages */
20885 *expr_start = '{';
20886 *expr_end = '}';
20887
20888 if (retval != NULL)
20889 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020890 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020891 if (expr_start != NULL)
20892 {
20893 /* Further expansion! */
20894 temp_result = make_expanded_name(retval, expr_start,
20895 expr_end, temp_result);
20896 vim_free(retval);
20897 retval = temp_result;
20898 }
20899 }
20900
20901 return retval;
20902}
20903
20904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020906 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 */
20908 static int
20909eval_isnamec(c)
20910 int c;
20911{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020912 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20913}
20914
20915/*
20916 * Return TRUE if character "c" can be used as the first character in a
20917 * variable or function name (excluding '{' and '}').
20918 */
20919 static int
20920eval_isnamec1(c)
20921 int c;
20922{
20923 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924}
20925
20926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 * Set number v: variable to "val".
20928 */
20929 void
20930set_vim_var_nr(idx, val)
20931 int idx;
20932 long val;
20933{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020934 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935}
20936
20937/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020938 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 */
20940 long
20941get_vim_var_nr(idx)
20942 int idx;
20943{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020944 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945}
20946
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020947/*
20948 * Get string v: variable value. Uses a static buffer, can only be used once.
20949 */
20950 char_u *
20951get_vim_var_str(idx)
20952 int idx;
20953{
20954 return get_tv_string(&vimvars[idx].vv_tv);
20955}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020956
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020958 * Get List v: variable value. Caller must take care of reference count when
20959 * needed.
20960 */
20961 list_T *
20962get_vim_var_list(idx)
20963 int idx;
20964{
20965 return vimvars[idx].vv_list;
20966}
20967
20968/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020969 * Set v:char to character "c".
20970 */
20971 void
20972set_vim_var_char(c)
20973 int c;
20974{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020975 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020976
20977#ifdef FEAT_MBYTE
20978 if (has_mbyte)
20979 buf[(*mb_char2bytes)(c, buf)] = NUL;
20980 else
20981#endif
20982 {
20983 buf[0] = c;
20984 buf[1] = NUL;
20985 }
20986 set_vim_var_string(VV_CHAR, buf, -1);
20987}
20988
20989/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020990 * Set v:count to "count" and v:count1 to "count1".
20991 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 */
20993 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020994set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 long count;
20996 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020997 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020999 if (set_prevcount)
21000 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021001 vimvars[VV_COUNT].vv_nr = count;
21002 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003}
21004
21005/*
21006 * Set string v: variable to a copy of "val".
21007 */
21008 void
21009set_vim_var_string(idx, val, len)
21010 int idx;
21011 char_u *val;
21012 int len; /* length of "val" to use or -1 (whole string) */
21013{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021014 /* Need to do this (at least) once, since we can't initialize a union.
21015 * Will always be invoked when "v:progname" is set. */
21016 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21017
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021020 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021022 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021024 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025}
21026
21027/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021028 * Set List v: variable to "val".
21029 */
21030 void
21031set_vim_var_list(idx, val)
21032 int idx;
21033 list_T *val;
21034{
21035 list_unref(vimvars[idx].vv_list);
21036 vimvars[idx].vv_list = val;
21037 if (val != NULL)
21038 ++val->lv_refcount;
21039}
21040
21041/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021042 * Set Dictionary v: variable to "val".
21043 */
21044 void
21045set_vim_var_dict(idx, val)
21046 int idx;
21047 dict_T *val;
21048{
21049 int todo;
21050 hashitem_T *hi;
21051
21052 dict_unref(vimvars[idx].vv_dict);
21053 vimvars[idx].vv_dict = val;
21054 if (val != NULL)
21055 {
21056 ++val->dv_refcount;
21057
21058 /* Set readonly */
21059 todo = (int)val->dv_hashtab.ht_used;
21060 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21061 {
21062 if (HASHITEM_EMPTY(hi))
21063 continue;
21064 --todo;
21065 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21066 }
21067 }
21068}
21069
21070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 * Set v:register if needed.
21072 */
21073 void
21074set_reg_var(c)
21075 int c;
21076{
21077 char_u regname;
21078
21079 if (c == 0 || c == ' ')
21080 regname = '"';
21081 else
21082 regname = c;
21083 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021084 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 set_vim_var_string(VV_REG, &regname, 1);
21086}
21087
21088/*
21089 * Get or set v:exception. If "oldval" == NULL, return the current value.
21090 * Otherwise, restore the value to "oldval" and return NULL.
21091 * Must always be called in pairs to save and restore v:exception! Does not
21092 * take care of memory allocations.
21093 */
21094 char_u *
21095v_exception(oldval)
21096 char_u *oldval;
21097{
21098 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021099 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100
Bram Moolenaare9a41262005-01-15 22:18:47 +000021101 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 return NULL;
21103}
21104
21105/*
21106 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21107 * Otherwise, restore the value to "oldval" and return NULL.
21108 * Must always be called in pairs to save and restore v:throwpoint! Does not
21109 * take care of memory allocations.
21110 */
21111 char_u *
21112v_throwpoint(oldval)
21113 char_u *oldval;
21114{
21115 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021116 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117
Bram Moolenaare9a41262005-01-15 22:18:47 +000021118 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 return NULL;
21120}
21121
21122#if defined(FEAT_AUTOCMD) || defined(PROTO)
21123/*
21124 * Set v:cmdarg.
21125 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21126 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21127 * Must always be called in pairs!
21128 */
21129 char_u *
21130set_cmdarg(eap, oldarg)
21131 exarg_T *eap;
21132 char_u *oldarg;
21133{
21134 char_u *oldval;
21135 char_u *newval;
21136 unsigned len;
21137
Bram Moolenaare9a41262005-01-15 22:18:47 +000021138 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021139 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021141 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021142 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021143 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 }
21145
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021146 if (eap->force_bin == FORCE_BIN)
21147 len = 6;
21148 else if (eap->force_bin == FORCE_NOBIN)
21149 len = 8;
21150 else
21151 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021152
21153 if (eap->read_edit)
21154 len += 7;
21155
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021156 if (eap->force_ff != 0)
21157 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21158# ifdef FEAT_MBYTE
21159 if (eap->force_enc != 0)
21160 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021161 if (eap->bad_char != 0)
21162 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021163# endif
21164
21165 newval = alloc(len + 1);
21166 if (newval == NULL)
21167 return NULL;
21168
21169 if (eap->force_bin == FORCE_BIN)
21170 sprintf((char *)newval, " ++bin");
21171 else if (eap->force_bin == FORCE_NOBIN)
21172 sprintf((char *)newval, " ++nobin");
21173 else
21174 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021175
21176 if (eap->read_edit)
21177 STRCAT(newval, " ++edit");
21178
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021179 if (eap->force_ff != 0)
21180 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21181 eap->cmd + eap->force_ff);
21182# ifdef FEAT_MBYTE
21183 if (eap->force_enc != 0)
21184 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21185 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021186 if (eap->bad_char == BAD_KEEP)
21187 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21188 else if (eap->bad_char == BAD_DROP)
21189 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21190 else if (eap->bad_char != 0)
21191 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021192# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021193 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021194 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195}
21196#endif
21197
21198/*
21199 * Get the value of internal variable "name".
21200 * Return OK or FAIL.
21201 */
21202 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021203get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 char_u *name;
21205 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021206 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021207 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021208 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021209 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210{
21211 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021212 typval_T *tv = NULL;
21213 typval_T atv;
21214 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216
21217 /* truncate the name, so that we can use strcmp() */
21218 cc = name[len];
21219 name[len] = NUL;
21220
21221 /*
21222 * Check for "b:changedtick".
21223 */
21224 if (STRCMP(name, "b:changedtick") == 0)
21225 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021226 atv.v_type = VAR_NUMBER;
21227 atv.vval.v_number = curbuf->b_changedtick;
21228 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 }
21230
21231 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 * Check for user-defined variables.
21233 */
21234 else
21235 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021236 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021238 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021239 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021240 if (dip != NULL)
21241 *dip = v;
21242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 }
21244
Bram Moolenaare9a41262005-01-15 22:18:47 +000021245 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021247 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021248 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 ret = FAIL;
21250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021251 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021252 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253
21254 name[len] = cc;
21255
21256 return ret;
21257}
21258
21259/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021260 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21261 * Also handle function call with Funcref variable: func(expr)
21262 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21263 */
21264 static int
21265handle_subscript(arg, rettv, evaluate, verbose)
21266 char_u **arg;
21267 typval_T *rettv;
21268 int evaluate; /* do more than finding the end */
21269 int verbose; /* give error messages */
21270{
21271 int ret = OK;
21272 dict_T *selfdict = NULL;
21273 char_u *s;
21274 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021275 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021276
21277 while (ret == OK
21278 && (**arg == '['
21279 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021280 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021281 && !vim_iswhite(*(*arg - 1)))
21282 {
21283 if (**arg == '(')
21284 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021285 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021286 if (evaluate)
21287 {
21288 functv = *rettv;
21289 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021290
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021291 /* Invoke the function. Recursive! */
21292 s = functv.vval.v_string;
21293 }
21294 else
21295 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021296 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021297 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21298 &len, evaluate, selfdict);
21299
21300 /* Clear the funcref afterwards, so that deleting it while
21301 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021302 if (evaluate)
21303 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304
21305 /* Stop the expression evaluation when immediately aborting on
21306 * error, or when an interrupt occurred or an exception was thrown
21307 * but not caught. */
21308 if (aborting())
21309 {
21310 if (ret == OK)
21311 clear_tv(rettv);
21312 ret = FAIL;
21313 }
21314 dict_unref(selfdict);
21315 selfdict = NULL;
21316 }
21317 else /* **arg == '[' || **arg == '.' */
21318 {
21319 dict_unref(selfdict);
21320 if (rettv->v_type == VAR_DICT)
21321 {
21322 selfdict = rettv->vval.v_dict;
21323 if (selfdict != NULL)
21324 ++selfdict->dv_refcount;
21325 }
21326 else
21327 selfdict = NULL;
21328 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21329 {
21330 clear_tv(rettv);
21331 ret = FAIL;
21332 }
21333 }
21334 }
21335 dict_unref(selfdict);
21336 return ret;
21337}
21338
21339/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021340 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021341 * value).
21342 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021343 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021344alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021345{
Bram Moolenaar33570922005-01-25 22:26:29 +000021346 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021347}
21348
21349/*
21350 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 * The string "s" must have been allocated, it is consumed.
21352 * Return NULL for out of memory, the variable otherwise.
21353 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021354 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021355alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 char_u *s;
21357{
Bram Moolenaar33570922005-01-25 22:26:29 +000021358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 rettv = alloc_tv();
21361 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021363 rettv->v_type = VAR_STRING;
21364 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 }
21366 else
21367 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021368 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369}
21370
21371/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021374 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021375free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021376 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377{
21378 if (varp != NULL)
21379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021380 switch (varp->v_type)
21381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021382 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021383 func_unref(varp->vval.v_string);
21384 /*FALLTHROUGH*/
21385 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021386 vim_free(varp->vval.v_string);
21387 break;
21388 case VAR_LIST:
21389 list_unref(varp->vval.v_list);
21390 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021391 case VAR_DICT:
21392 dict_unref(varp->vval.v_dict);
21393 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021394 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021395#ifdef FEAT_FLOAT
21396 case VAR_FLOAT:
21397#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021398 case VAR_UNKNOWN:
21399 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021400 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021401 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021402 break;
21403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 vim_free(varp);
21405 }
21406}
21407
21408/*
21409 * Free the memory for a variable value and set the value to NULL or 0.
21410 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021411 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021412clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021413 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414{
21415 if (varp != NULL)
21416 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021419 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021420 func_unref(varp->vval.v_string);
21421 /*FALLTHROUGH*/
21422 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021423 vim_free(varp->vval.v_string);
21424 varp->vval.v_string = NULL;
21425 break;
21426 case VAR_LIST:
21427 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021428 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021430 case VAR_DICT:
21431 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021432 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021433 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021435 varp->vval.v_number = 0;
21436 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021437#ifdef FEAT_FLOAT
21438 case VAR_FLOAT:
21439 varp->vval.v_float = 0.0;
21440 break;
21441#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021442 case VAR_UNKNOWN:
21443 break;
21444 default:
21445 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021447 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 }
21449}
21450
21451/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 * Set the value of a variable to NULL without freeing items.
21453 */
21454 static void
21455init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021457{
21458 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460}
21461
21462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 * Get the number value of a variable.
21464 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021465 * For incompatible types, return 0.
21466 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21467 * caller of incompatible types: it sets *denote to TRUE if "denote"
21468 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 */
21470 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021471get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021474 int error = FALSE;
21475
21476 return get_tv_number_chk(varp, &error); /* return 0L on error */
21477}
21478
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021479 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021480get_tv_number_chk(varp, denote)
21481 typval_T *varp;
21482 int *denote;
21483{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021484 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021486 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021488 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021489 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021490#ifdef FEAT_FLOAT
21491 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021492 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021493 break;
21494#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021496 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021497 break;
21498 case VAR_STRING:
21499 if (varp->vval.v_string != NULL)
21500 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021501 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021502 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021503 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021504 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021505 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021507 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021508 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021509 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021510 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021513 if (denote == NULL) /* useful for values that must be unsigned */
21514 n = -1;
21515 else
21516 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021517 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518}
21519
21520/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021521 * Get the lnum from the first argument.
21522 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021523 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 */
21525 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021526get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021527 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528{
Bram Moolenaar33570922005-01-25 22:26:29 +000021529 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 linenr_T lnum;
21531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021532 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 if (lnum == 0) /* no valid number, try using line() */
21534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 rettv.v_type = VAR_NUMBER;
21536 f_line(argvars, &rettv);
21537 lnum = rettv.vval.v_number;
21538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 }
21540 return lnum;
21541}
21542
21543/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021544 * Get the lnum from the first argument.
21545 * Also accepts "$", then "buf" is used.
21546 * Returns 0 on error.
21547 */
21548 static linenr_T
21549get_tv_lnum_buf(argvars, buf)
21550 typval_T *argvars;
21551 buf_T *buf;
21552{
21553 if (argvars[0].v_type == VAR_STRING
21554 && argvars[0].vval.v_string != NULL
21555 && argvars[0].vval.v_string[0] == '$'
21556 && buf != NULL)
21557 return buf->b_ml.ml_line_count;
21558 return get_tv_number_chk(&argvars[0], NULL);
21559}
21560
21561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 * Get the string value of a variable.
21563 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021564 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21565 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 * If the String variable has never been set, return an empty string.
21567 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021568 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21569 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 */
21571 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021572get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574{
21575 static char_u mybuf[NUMBUFLEN];
21576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021577 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578}
21579
21580 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021582 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021583 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021585 char_u *res = get_tv_string_buf_chk(varp, buf);
21586
21587 return res != NULL ? res : (char_u *)"";
21588}
21589
Bram Moolenaar7d647822014-04-05 21:28:56 +020021590/*
21591 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21592 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021593 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021594get_tv_string_chk(varp)
21595 typval_T *varp;
21596{
21597 static char_u mybuf[NUMBUFLEN];
21598
21599 return get_tv_string_buf_chk(varp, mybuf);
21600}
21601
21602 static char_u *
21603get_tv_string_buf_chk(varp, buf)
21604 typval_T *varp;
21605 char_u *buf;
21606{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021609 case VAR_NUMBER:
21610 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21611 return buf;
21612 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021614 break;
21615 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021617 break;
21618 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021621#ifdef FEAT_FLOAT
21622 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021623 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021624 break;
21625#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021626 case VAR_STRING:
21627 if (varp->vval.v_string != NULL)
21628 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021629 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021630 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021631 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021634 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635}
21636
21637/*
21638 * Find variable "name" in the list of variables.
21639 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021641 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021642 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021644 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021645find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021647 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021648 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021651 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652
Bram Moolenaara7043832005-01-21 11:56:39 +000021653 ht = find_var_ht(name, &varname);
21654 if (htp != NULL)
21655 *htp = ht;
21656 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021658 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659}
21660
21661/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021662 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021663 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021666find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021668 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021669 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021670 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021671{
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 hashitem_T *hi;
21673
21674 if (*varname == NUL)
21675 {
21676 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021677 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021678 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021679 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 case 'g': return &globvars_var;
21681 case 'v': return &vimvars_var;
21682 case 'b': return &curbuf->b_bufvar;
21683 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021684#ifdef FEAT_WINDOWS
21685 case 't': return &curtab->tp_winvar;
21686#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021687 case 'l': return current_funccal == NULL
21688 ? NULL : &current_funccal->l_vars_var;
21689 case 'a': return current_funccal == NULL
21690 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021691 }
21692 return NULL;
21693 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021694
21695 hi = hash_find(ht, varname);
21696 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021697 {
21698 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021699 * worked find the variable again. Don't auto-load a script if it was
21700 * loaded already, otherwise it would be loaded every time when
21701 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021702 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021703 {
21704 /* Note: script_autoload() may make "hi" invalid. It must either
21705 * be obtained again or not used. */
21706 if (!script_autoload(varname, FALSE) || aborting())
21707 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021708 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021709 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 if (HASHITEM_EMPTY(hi))
21711 return NULL;
21712 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021713 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021714}
21715
21716/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021717 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021718 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021719 * Set "varname" to the start of name without ':'.
21720 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021722find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 char_u *name;
21724 char_u **varname;
21725{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021726 hashitem_T *hi;
21727
Bram Moolenaar73627d02015-08-11 15:46:09 +020021728 if (name[0] == NUL)
21729 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 if (name[1] != ':')
21731 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021732 /* The name must not start with a colon or #. */
21733 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 return NULL;
21735 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021736
21737 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021738 hi = hash_find(&compat_hashtab, name);
21739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021740 return &compat_hashtab;
21741
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 return &globvarht; /* global variable */
21744 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 }
21746 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021747 if (*name == 'g') /* global variable */
21748 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021749 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21750 */
21751 if (vim_strchr(name + 2, ':') != NULL
21752 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021753 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021755 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021757 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021758#ifdef FEAT_WINDOWS
21759 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021760 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021761#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 if (*name == 'v') /* v: variable */
21763 return &vimvarht;
21764 if (*name == 'a' && current_funccal != NULL) /* function argument */
21765 return &current_funccal->l_avars.dv_hashtab;
21766 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21767 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 if (*name == 's' /* script variable */
21769 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21770 return &SCRIPT_VARS(current_SID);
21771 return NULL;
21772}
21773
21774/*
21775 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021776 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 * Returns NULL when it doesn't exist.
21778 */
21779 char_u *
21780get_var_value(name)
21781 char_u *name;
21782{
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021785 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 if (v == NULL)
21787 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789}
21790
21791/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 * sourcing this script and when executing functions defined in the script.
21794 */
21795 void
21796new_script_vars(id)
21797 scid_T id;
21798{
Bram Moolenaara7043832005-01-21 11:56:39 +000021799 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 hashtab_T *ht;
21801 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021802
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21804 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021805 /* Re-allocating ga_data means that an ht_array pointing to
21806 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021808 for (i = 1; i <= ga_scripts.ga_len; ++i)
21809 {
21810 ht = &SCRIPT_VARS(i);
21811 if (ht->ht_mask == HT_INIT_SIZE - 1)
21812 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021813 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021814 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021815 }
21816
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 while (ga_scripts.ga_len < id)
21818 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021819 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021820 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021821 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 }
21824 }
21825}
21826
21827/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021828 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21829 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 */
21831 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021832init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021833 dict_T *dict;
21834 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021835 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836{
Bram Moolenaar33570922005-01-25 22:26:29 +000021837 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021838 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021839 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021840 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021841 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 dict_var->di_tv.vval.v_dict = dict;
21843 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021844 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21846 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847}
21848
21849/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021850 * Unreference a dictionary initialized by init_var_dict().
21851 */
21852 void
21853unref_var_dict(dict)
21854 dict_T *dict;
21855{
21856 /* Now the dict needs to be freed if no one else is using it, go back to
21857 * normal reference counting. */
21858 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21859 dict_unref(dict);
21860}
21861
21862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 * Frees all allocated variables and the value they contain.
21865 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 */
21867 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021868vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021869 hashtab_T *ht;
21870{
21871 vars_clear_ext(ht, TRUE);
21872}
21873
21874/*
21875 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21876 */
21877 static void
21878vars_clear_ext(ht, free_val)
21879 hashtab_T *ht;
21880 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881{
Bram Moolenaara7043832005-01-21 11:56:39 +000021882 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 hashitem_T *hi;
21884 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885
Bram Moolenaar33570922005-01-25 22:26:29 +000021886 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021887 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021888 for (hi = ht->ht_array; todo > 0; ++hi)
21889 {
21890 if (!HASHITEM_EMPTY(hi))
21891 {
21892 --todo;
21893
Bram Moolenaar33570922005-01-25 22:26:29 +000021894 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021895 * ht_array might change then. hash_clear() takes care of it
21896 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 v = HI2DI(hi);
21898 if (free_val)
21899 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021900 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021902 }
21903 }
21904 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021905 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906}
21907
Bram Moolenaara7043832005-01-21 11:56:39 +000021908/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 * Delete a variable from hashtab "ht" at item "hi".
21910 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021913delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 hashtab_T *ht;
21915 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916{
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021918
21919 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021920 clear_tv(&di->di_tv);
21921 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922}
21923
21924/*
21925 * List the value of one internal variable.
21926 */
21927 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021928list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021931 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021933 char_u *tofree;
21934 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021935 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021936
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021937 current_copyID += COPYID_INC;
21938 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021939 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021940 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021941 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942}
21943
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021945list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 char_u *prefix;
21947 char_u *name;
21948 int type;
21949 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021950 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951{
Bram Moolenaar31859182007-08-14 20:41:13 +000021952 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21953 msg_start();
21954 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 if (name != NULL) /* "a:" vars don't have a name stored */
21956 msg_puts(name);
21957 msg_putchar(' ');
21958 msg_advance(22);
21959 if (type == VAR_NUMBER)
21960 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021961 else if (type == VAR_FUNC)
21962 msg_putchar('*');
21963 else if (type == VAR_LIST)
21964 {
21965 msg_putchar('[');
21966 if (*string == '[')
21967 ++string;
21968 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021969 else if (type == VAR_DICT)
21970 {
21971 msg_putchar('{');
21972 if (*string == '{')
21973 ++string;
21974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 else
21976 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021977
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021979
21980 if (type == VAR_FUNC)
21981 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021982 if (*first)
21983 {
21984 msg_clr_eos();
21985 *first = FALSE;
21986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987}
21988
21989/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 * If the variable already exists, the value is updated.
21992 * Otherwise the variable is created.
21993 */
21994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021995set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021997 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021998 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999{
Bram Moolenaar33570922005-01-25 22:26:29 +000022000 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022004 ht = find_var_ht(name, &varname);
22005 if (ht == NULL || *varname == NUL)
22006 {
22007 EMSG2(_(e_illvar), name);
22008 return;
22009 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022010 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022011
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022012 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22013 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022014
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022018 if (var_check_ro(v->di_flags, name, FALSE)
22019 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022020 return;
22021 if (v->di_tv.v_type != tv->v_type
22022 && !((v->di_tv.v_type == VAR_STRING
22023 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022024 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022025 || tv->v_type == VAR_NUMBER))
22026#ifdef FEAT_FLOAT
22027 && !((v->di_tv.v_type == VAR_NUMBER
22028 || v->di_tv.v_type == VAR_FLOAT)
22029 && (tv->v_type == VAR_NUMBER
22030 || tv->v_type == VAR_FLOAT))
22031#endif
22032 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022033 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022034 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022035 return;
22036 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022037
22038 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022039 * Handle setting internal v: variables separately where needed to
22040 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 */
22042 if (ht == &vimvarht)
22043 {
22044 if (v->di_tv.v_type == VAR_STRING)
22045 {
22046 vim_free(v->di_tv.vval.v_string);
22047 if (copy || tv->v_type != VAR_STRING)
22048 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22049 else
22050 {
22051 /* Take over the string to avoid an extra alloc/free. */
22052 v->di_tv.vval.v_string = tv->vval.v_string;
22053 tv->vval.v_string = NULL;
22054 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022055 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022056 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022057 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022058 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022059 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022060 if (STRCMP(varname, "searchforward") == 0)
22061 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022062#ifdef FEAT_SEARCH_EXTRA
22063 else if (STRCMP(varname, "hlsearch") == 0)
22064 {
22065 no_hlsearch = !v->di_tv.vval.v_number;
22066 redraw_all_later(SOME_VALID);
22067 }
22068#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022069 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022070 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022071 else if (v->di_tv.v_type != tv->v_type)
22072 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022073 }
22074
22075 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 }
22077 else /* add a new variable */
22078 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022079 /* Can't add "v:" variable. */
22080 if (ht == &vimvarht)
22081 {
22082 EMSG2(_(e_illvar), name);
22083 return;
22084 }
22085
Bram Moolenaar92124a32005-06-17 22:03:40 +000022086 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022087 if (!valid_varname(varname))
22088 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022089
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022090 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22091 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022092 if (v == NULL)
22093 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022094 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022095 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022097 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 return;
22099 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022100 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022102
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022103 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022105 else
22106 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022108 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022109 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111}
22112
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022113/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022114 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022115 * Also give an error message.
22116 */
22117 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022118var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 int flags;
22120 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022121 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022122{
22123 if (flags & DI_FLAGS_RO)
22124 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022125 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022126 return TRUE;
22127 }
22128 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22129 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022130 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022131 return TRUE;
22132 }
22133 return FALSE;
22134}
22135
22136/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022137 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22138 * Also give an error message.
22139 */
22140 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022141var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022142 int flags;
22143 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022144 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022145{
22146 if (flags & DI_FLAGS_FIX)
22147 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022148 EMSG2(_("E795: Cannot delete variable %s"),
22149 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022150 return TRUE;
22151 }
22152 return FALSE;
22153}
22154
22155/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022156 * Check if a funcref is assigned to a valid variable name.
22157 * Return TRUE and give an error if not.
22158 */
22159 static int
22160var_check_func_name(name, new_var)
22161 char_u *name; /* points to start of variable name */
22162 int new_var; /* TRUE when creating the variable */
22163{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022164 /* Allow for w: b: s: and t:. */
22165 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022166 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22167 ? name[2] : name[0]))
22168 {
22169 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22170 name);
22171 return TRUE;
22172 }
22173 /* Don't allow hiding a function. When "v" is not NULL we might be
22174 * assigning another function to the same var, the type is checked
22175 * below. */
22176 if (new_var && function_exists(name))
22177 {
22178 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22179 name);
22180 return TRUE;
22181 }
22182 return FALSE;
22183}
22184
22185/*
22186 * Check if a variable name is valid.
22187 * Return FALSE and give an error if not.
22188 */
22189 static int
22190valid_varname(varname)
22191 char_u *varname;
22192{
22193 char_u *p;
22194
22195 for (p = varname; *p != NUL; ++p)
22196 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22197 && *p != AUTOLOAD_CHAR)
22198 {
22199 EMSG2(_(e_illvar), varname);
22200 return FALSE;
22201 }
22202 return TRUE;
22203}
22204
22205/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022206 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022207 * Also give an error message, using "name" or _("name") when use_gettext is
22208 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022209 */
22210 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022211tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022212 int lock;
22213 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022214 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022215{
22216 if (lock & VAR_LOCKED)
22217 {
22218 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022219 name == NULL ? (char_u *)_("Unknown")
22220 : use_gettext ? (char_u *)_(name)
22221 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022222 return TRUE;
22223 }
22224 if (lock & VAR_FIXED)
22225 {
22226 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022227 name == NULL ? (char_u *)_("Unknown")
22228 : use_gettext ? (char_u *)_(name)
22229 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022230 return TRUE;
22231 }
22232 return FALSE;
22233}
22234
22235/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022237 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022238 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022239 * It is OK for "from" and "to" to point to the same item. This is used to
22240 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022242 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022243copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022244 typval_T *from;
22245 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022247 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022248 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022249 switch (from->v_type)
22250 {
22251 case VAR_NUMBER:
22252 to->vval.v_number = from->vval.v_number;
22253 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022254#ifdef FEAT_FLOAT
22255 case VAR_FLOAT:
22256 to->vval.v_float = from->vval.v_float;
22257 break;
22258#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022259 case VAR_STRING:
22260 case VAR_FUNC:
22261 if (from->vval.v_string == NULL)
22262 to->vval.v_string = NULL;
22263 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022265 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022266 if (from->v_type == VAR_FUNC)
22267 func_ref(to->vval.v_string);
22268 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022269 break;
22270 case VAR_LIST:
22271 if (from->vval.v_list == NULL)
22272 to->vval.v_list = NULL;
22273 else
22274 {
22275 to->vval.v_list = from->vval.v_list;
22276 ++to->vval.v_list->lv_refcount;
22277 }
22278 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022279 case VAR_DICT:
22280 if (from->vval.v_dict == NULL)
22281 to->vval.v_dict = NULL;
22282 else
22283 {
22284 to->vval.v_dict = from->vval.v_dict;
22285 ++to->vval.v_dict->dv_refcount;
22286 }
22287 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022288 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022289 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022290 break;
22291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292}
22293
22294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022295 * Make a copy of an item.
22296 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022297 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22298 * reference to an already copied list/dict can be used.
22299 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022300 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022301 static int
22302item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022303 typval_T *from;
22304 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022305 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022306 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022307{
22308 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022309 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022310
Bram Moolenaar33570922005-01-25 22:26:29 +000022311 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022312 {
22313 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022314 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022315 }
22316 ++recurse;
22317
22318 switch (from->v_type)
22319 {
22320 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022321#ifdef FEAT_FLOAT
22322 case VAR_FLOAT:
22323#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022324 case VAR_STRING:
22325 case VAR_FUNC:
22326 copy_tv(from, to);
22327 break;
22328 case VAR_LIST:
22329 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022330 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022331 if (from->vval.v_list == NULL)
22332 to->vval.v_list = NULL;
22333 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22334 {
22335 /* use the copy made earlier */
22336 to->vval.v_list = from->vval.v_list->lv_copylist;
22337 ++to->vval.v_list->lv_refcount;
22338 }
22339 else
22340 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22341 if (to->vval.v_list == NULL)
22342 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022343 break;
22344 case VAR_DICT:
22345 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022346 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022347 if (from->vval.v_dict == NULL)
22348 to->vval.v_dict = NULL;
22349 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22350 {
22351 /* use the copy made earlier */
22352 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22353 ++to->vval.v_dict->dv_refcount;
22354 }
22355 else
22356 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22357 if (to->vval.v_dict == NULL)
22358 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022359 break;
22360 default:
22361 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022362 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022363 }
22364 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022365 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022366}
22367
22368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 * ":echo expr1 ..." print each argument separated with a space, add a
22370 * newline at the end.
22371 * ":echon expr1 ..." print each argument plain.
22372 */
22373 void
22374ex_echo(eap)
22375 exarg_T *eap;
22376{
22377 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022379 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 char_u *p;
22381 int needclr = TRUE;
22382 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022383 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384
22385 if (eap->skip)
22386 ++emsg_skip;
22387 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22388 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022389 /* If eval1() causes an error message the text from the command may
22390 * still need to be cleared. E.g., "echo 22,44". */
22391 need_clr_eos = needclr;
22392
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022394 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 {
22396 /*
22397 * Report the invalid expression unless the expression evaluation
22398 * has been cancelled due to an aborting error, an interrupt, or an
22399 * exception.
22400 */
22401 if (!aborting())
22402 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022403 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 break;
22405 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022406 need_clr_eos = FALSE;
22407
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 if (!eap->skip)
22409 {
22410 if (atstart)
22411 {
22412 atstart = FALSE;
22413 /* Call msg_start() after eval1(), evaluating the expression
22414 * may cause a message to appear. */
22415 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022416 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022417 /* Mark the saved text as finishing the line, so that what
22418 * follows is displayed on a new line when scrolling back
22419 * at the more prompt. */
22420 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 }
22424 else if (eap->cmdidx == CMD_echo)
22425 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022426 current_copyID += COPYID_INC;
22427 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022428 if (p != NULL)
22429 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022431 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022433 if (*p != TAB && needclr)
22434 {
22435 /* remove any text still there from the command */
22436 msg_clr_eos();
22437 needclr = FALSE;
22438 }
22439 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 }
22441 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022442 {
22443#ifdef FEAT_MBYTE
22444 if (has_mbyte)
22445 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022446 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022447
22448 (void)msg_outtrans_len_attr(p, i, echo_attr);
22449 p += i - 1;
22450 }
22451 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022453 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022456 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022458 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 arg = skipwhite(arg);
22460 }
22461 eap->nextcmd = check_nextcmd(arg);
22462
22463 if (eap->skip)
22464 --emsg_skip;
22465 else
22466 {
22467 /* remove text that may still be there from the command */
22468 if (needclr)
22469 msg_clr_eos();
22470 if (eap->cmdidx == CMD_echo)
22471 msg_end();
22472 }
22473}
22474
22475/*
22476 * ":echohl {name}".
22477 */
22478 void
22479ex_echohl(eap)
22480 exarg_T *eap;
22481{
22482 int id;
22483
22484 id = syn_name2id(eap->arg);
22485 if (id == 0)
22486 echo_attr = 0;
22487 else
22488 echo_attr = syn_id2attr(id);
22489}
22490
22491/*
22492 * ":execute expr1 ..." execute the result of an expression.
22493 * ":echomsg expr1 ..." Print a message
22494 * ":echoerr expr1 ..." Print an error
22495 * Each gets spaces around each argument and a newline at the end for
22496 * echo commands
22497 */
22498 void
22499ex_execute(eap)
22500 exarg_T *eap;
22501{
22502 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022503 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 int ret = OK;
22505 char_u *p;
22506 garray_T ga;
22507 int len;
22508 int save_did_emsg;
22509
22510 ga_init2(&ga, 1, 80);
22511
22512 if (eap->skip)
22513 ++emsg_skip;
22514 while (*arg != NUL && *arg != '|' && *arg != '\n')
22515 {
22516 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022517 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 {
22519 /*
22520 * Report the invalid expression unless the expression evaluation
22521 * has been cancelled due to an aborting error, an interrupt, or an
22522 * exception.
22523 */
22524 if (!aborting())
22525 EMSG2(_(e_invexpr2), p);
22526 ret = FAIL;
22527 break;
22528 }
22529
22530 if (!eap->skip)
22531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022532 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 len = (int)STRLEN(p);
22534 if (ga_grow(&ga, len + 2) == FAIL)
22535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022536 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 ret = FAIL;
22538 break;
22539 }
22540 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 ga.ga_len += len;
22544 }
22545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022546 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 arg = skipwhite(arg);
22548 }
22549
22550 if (ret != FAIL && ga.ga_data != NULL)
22551 {
22552 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022555 out_flush();
22556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 else if (eap->cmdidx == CMD_echoerr)
22558 {
22559 /* We don't want to abort following commands, restore did_emsg. */
22560 save_did_emsg = did_emsg;
22561 EMSG((char_u *)ga.ga_data);
22562 if (!force_abort)
22563 did_emsg = save_did_emsg;
22564 }
22565 else if (eap->cmdidx == CMD_execute)
22566 do_cmdline((char_u *)ga.ga_data,
22567 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22568 }
22569
22570 ga_clear(&ga);
22571
22572 if (eap->skip)
22573 --emsg_skip;
22574
22575 eap->nextcmd = check_nextcmd(arg);
22576}
22577
22578/*
22579 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22580 * "arg" points to the "&" or '+' when called, to "option" when returning.
22581 * Returns NULL when no option name found. Otherwise pointer to the char
22582 * after the option name.
22583 */
22584 static char_u *
22585find_option_end(arg, opt_flags)
22586 char_u **arg;
22587 int *opt_flags;
22588{
22589 char_u *p = *arg;
22590
22591 ++p;
22592 if (*p == 'g' && p[1] == ':')
22593 {
22594 *opt_flags = OPT_GLOBAL;
22595 p += 2;
22596 }
22597 else if (*p == 'l' && p[1] == ':')
22598 {
22599 *opt_flags = OPT_LOCAL;
22600 p += 2;
22601 }
22602 else
22603 *opt_flags = 0;
22604
22605 if (!ASCII_ISALPHA(*p))
22606 return NULL;
22607 *arg = p;
22608
22609 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22610 p += 4; /* termcap option */
22611 else
22612 while (ASCII_ISALPHA(*p))
22613 ++p;
22614 return p;
22615}
22616
22617/*
22618 * ":function"
22619 */
22620 void
22621ex_function(eap)
22622 exarg_T *eap;
22623{
22624 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022625 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 int j;
22627 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022629 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 char_u *name = NULL;
22631 char_u *p;
22632 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022633 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 garray_T newargs;
22635 garray_T newlines;
22636 int varargs = FALSE;
22637 int mustend = FALSE;
22638 int flags = 0;
22639 ufunc_T *fp;
22640 int indent;
22641 int nesting;
22642 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 dictitem_T *v;
22644 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645 static int func_nr = 0; /* number for nameless function */
22646 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022647 hashtab_T *ht;
22648 int todo;
22649 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022650 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651
22652 /*
22653 * ":function" without argument: list functions.
22654 */
22655 if (ends_excmd(*eap->arg))
22656 {
22657 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022658 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022659 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022660 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022661 {
22662 if (!HASHITEM_EMPTY(hi))
22663 {
22664 --todo;
22665 fp = HI2UF(hi);
22666 if (!isdigit(*fp->uf_name))
22667 list_func_head(fp, FALSE);
22668 }
22669 }
22670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 eap->nextcmd = check_nextcmd(eap->arg);
22672 return;
22673 }
22674
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022675 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022676 * ":function /pat": list functions matching pattern.
22677 */
22678 if (*eap->arg == '/')
22679 {
22680 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22681 if (!eap->skip)
22682 {
22683 regmatch_T regmatch;
22684
22685 c = *p;
22686 *p = NUL;
22687 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22688 *p = c;
22689 if (regmatch.regprog != NULL)
22690 {
22691 regmatch.rm_ic = p_ic;
22692
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022693 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022694 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22695 {
22696 if (!HASHITEM_EMPTY(hi))
22697 {
22698 --todo;
22699 fp = HI2UF(hi);
22700 if (!isdigit(*fp->uf_name)
22701 && vim_regexec(&regmatch, fp->uf_name, 0))
22702 list_func_head(fp, FALSE);
22703 }
22704 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022705 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022706 }
22707 }
22708 if (*p == '/')
22709 ++p;
22710 eap->nextcmd = check_nextcmd(p);
22711 return;
22712 }
22713
22714 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022715 * Get the function name. There are these situations:
22716 * func normal function name
22717 * "name" == func, "fudi.fd_dict" == NULL
22718 * dict.func new dictionary entry
22719 * "name" == NULL, "fudi.fd_dict" set,
22720 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22721 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022722 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022723 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22724 * dict.func existing dict entry that's not a Funcref
22725 * "name" == NULL, "fudi.fd_dict" set,
22726 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022727 * s:func script-local function name
22728 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022731 name = trans_function_name(&p, eap->skip, 0, &fudi);
22732 paren = (vim_strchr(p, '(') != NULL);
22733 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 {
22735 /*
22736 * Return on an invalid expression in braces, unless the expression
22737 * evaluation has been cancelled due to an aborting error, an
22738 * interrupt, or an exception.
22739 */
22740 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022742 if (!eap->skip && fudi.fd_newkey != NULL)
22743 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 else
22748 eap->skip = TRUE;
22749 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022750
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 /* An error in a function call during evaluation of an expression in magic
22752 * braces should not cause the function not to be defined. */
22753 saved_did_emsg = did_emsg;
22754 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
22756 /*
22757 * ":function func" with only function name: list function.
22758 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022759 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 {
22761 if (!ends_excmd(*skipwhite(p)))
22762 {
22763 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022764 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 }
22766 eap->nextcmd = check_nextcmd(p);
22767 if (eap->nextcmd != NULL)
22768 *p = NUL;
22769 if (!eap->skip && !got_int)
22770 {
22771 fp = find_func(name);
22772 if (fp != NULL)
22773 {
22774 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022775 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022777 if (FUNCLINE(fp, j) == NULL)
22778 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 msg_putchar('\n');
22780 msg_outnum((long)(j + 1));
22781 if (j < 9)
22782 msg_putchar(' ');
22783 if (j < 99)
22784 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022785 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 out_flush(); /* show a line at a time */
22787 ui_breakcheck();
22788 }
22789 if (!got_int)
22790 {
22791 msg_putchar('\n');
22792 msg_puts((char_u *)" endfunction");
22793 }
22794 }
22795 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022796 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022798 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 }
22800
22801 /*
22802 * ":function name(arg1, arg2)" Define function.
22803 */
22804 p = skipwhite(p);
22805 if (*p != '(')
22806 {
22807 if (!eap->skip)
22808 {
22809 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022810 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 }
22812 /* attempt to continue by skipping some text */
22813 if (vim_strchr(p, '(') != NULL)
22814 p = vim_strchr(p, '(');
22815 }
22816 p = skipwhite(p + 1);
22817
22818 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22819 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22820
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022821 if (!eap->skip)
22822 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022823 /* Check the name of the function. Unless it's a dictionary function
22824 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022825 if (name != NULL)
22826 arg = name;
22827 else
22828 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022829 if (arg != NULL && (fudi.fd_di == NULL
22830 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022831 {
22832 if (*arg == K_SPECIAL)
22833 j = 3;
22834 else
22835 j = 0;
22836 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22837 : eval_isnamec(arg[j])))
22838 ++j;
22839 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022840 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022841 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022842 /* Disallow using the g: dict. */
22843 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22844 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022845 }
22846
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 /*
22848 * Isolate the arguments: "arg1, arg2, ...)"
22849 */
22850 while (*p != ')')
22851 {
22852 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22853 {
22854 varargs = TRUE;
22855 p += 3;
22856 mustend = TRUE;
22857 }
22858 else
22859 {
22860 arg = p;
22861 while (ASCII_ISALNUM(*p) || *p == '_')
22862 ++p;
22863 if (arg == p || isdigit(*arg)
22864 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22865 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22866 {
22867 if (!eap->skip)
22868 EMSG2(_("E125: Illegal argument: %s"), arg);
22869 break;
22870 }
22871 if (ga_grow(&newargs, 1) == FAIL)
22872 goto erret;
22873 c = *p;
22874 *p = NUL;
22875 arg = vim_strsave(arg);
22876 if (arg == NULL)
22877 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022878
22879 /* Check for duplicate argument name. */
22880 for (i = 0; i < newargs.ga_len; ++i)
22881 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22882 {
22883 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022884 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022885 goto erret;
22886 }
22887
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22889 *p = c;
22890 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 if (*p == ',')
22892 ++p;
22893 else
22894 mustend = TRUE;
22895 }
22896 p = skipwhite(p);
22897 if (mustend && *p != ')')
22898 {
22899 if (!eap->skip)
22900 EMSG2(_(e_invarg2), eap->arg);
22901 break;
22902 }
22903 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022904 if (*p != ')')
22905 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 ++p; /* skip the ')' */
22907
Bram Moolenaare9a41262005-01-15 22:18:47 +000022908 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 for (;;)
22910 {
22911 p = skipwhite(p);
22912 if (STRNCMP(p, "range", 5) == 0)
22913 {
22914 flags |= FC_RANGE;
22915 p += 5;
22916 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022917 else if (STRNCMP(p, "dict", 4) == 0)
22918 {
22919 flags |= FC_DICT;
22920 p += 4;
22921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 else if (STRNCMP(p, "abort", 5) == 0)
22923 {
22924 flags |= FC_ABORT;
22925 p += 5;
22926 }
22927 else
22928 break;
22929 }
22930
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022931 /* When there is a line break use what follows for the function body.
22932 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22933 if (*p == '\n')
22934 line_arg = p + 1;
22935 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 EMSG(_(e_trailing));
22937
22938 /*
22939 * Read the body of the function, until ":endfunction" is found.
22940 */
22941 if (KeyTyped)
22942 {
22943 /* Check if the function already exists, don't let the user type the
22944 * whole function before telling him it doesn't work! For a script we
22945 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 if (!eap->skip && !eap->forceit)
22947 {
22948 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22949 EMSG(_(e_funcdict));
22950 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022951 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022954 if (!eap->skip && did_emsg)
22955 goto erret;
22956
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 msg_putchar('\n'); /* don't overwrite the function name */
22958 cmdline_row = msg_row;
22959 }
22960
22961 indent = 2;
22962 nesting = 0;
22963 for (;;)
22964 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022965 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022966 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022967 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022968 saved_wait_return = FALSE;
22969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022971 sourcing_lnum_off = sourcing_lnum;
22972
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022973 if (line_arg != NULL)
22974 {
22975 /* Use eap->arg, split up in parts by line breaks. */
22976 theline = line_arg;
22977 p = vim_strchr(theline, '\n');
22978 if (p == NULL)
22979 line_arg += STRLEN(line_arg);
22980 else
22981 {
22982 *p = NUL;
22983 line_arg = p + 1;
22984 }
22985 }
22986 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 theline = getcmdline(':', 0L, indent);
22988 else
22989 theline = eap->getline(':', eap->cookie, indent);
22990 if (KeyTyped)
22991 lines_left = Rows - 1;
22992 if (theline == NULL)
22993 {
22994 EMSG(_("E126: Missing :endfunction"));
22995 goto erret;
22996 }
22997
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022998 /* Detect line continuation: sourcing_lnum increased more than one. */
22999 if (sourcing_lnum > sourcing_lnum_off + 1)
23000 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23001 else
23002 sourcing_lnum_off = 0;
23003
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 if (skip_until != NULL)
23005 {
23006 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23007 * don't check for ":endfunc". */
23008 if (STRCMP(theline, skip_until) == 0)
23009 {
23010 vim_free(skip_until);
23011 skip_until = NULL;
23012 }
23013 }
23014 else
23015 {
23016 /* skip ':' and blanks*/
23017 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23018 ;
23019
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023020 /* Check for "endfunction". */
23021 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023023 if (line_arg == NULL)
23024 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 break;
23026 }
23027
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023028 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 * at "end". */
23030 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23031 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023032 else if (STRNCMP(p, "if", 2) == 0
23033 || STRNCMP(p, "wh", 2) == 0
23034 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 || STRNCMP(p, "try", 3) == 0)
23036 indent += 2;
23037
23038 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023039 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023041 if (*p == '!')
23042 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023044 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23045 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023047 ++nesting;
23048 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 }
23050 }
23051
23052 /* Check for ":append" or ":insert". */
23053 p = skip_range(p, NULL);
23054 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23055 || (p[0] == 'i'
23056 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23057 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23058 skip_until = vim_strsave((char_u *)".");
23059
23060 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23061 arg = skipwhite(skiptowhite(p));
23062 if (arg[0] == '<' && arg[1] =='<'
23063 && ((p[0] == 'p' && p[1] == 'y'
23064 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23065 || (p[0] == 'p' && p[1] == 'e'
23066 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23067 || (p[0] == 't' && p[1] == 'c'
23068 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023069 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23070 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23072 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023073 || (p[0] == 'm' && p[1] == 'z'
23074 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 ))
23076 {
23077 /* ":python <<" continues until a dot, like ":append" */
23078 p = skipwhite(arg + 2);
23079 if (*p == NUL)
23080 skip_until = vim_strsave((char_u *)".");
23081 else
23082 skip_until = vim_strsave(p);
23083 }
23084 }
23085
23086 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023087 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023088 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023089 if (line_arg == NULL)
23090 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023092 }
23093
23094 /* Copy the line to newly allocated memory. get_one_sourceline()
23095 * allocates 250 bytes per line, this saves 80% on average. The cost
23096 * is an extra alloc/free. */
23097 p = vim_strsave(theline);
23098 if (p != NULL)
23099 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023100 if (line_arg == NULL)
23101 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023102 theline = p;
23103 }
23104
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023105 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23106
23107 /* Add NULL lines for continuation lines, so that the line count is
23108 * equal to the index in the growarray. */
23109 while (sourcing_lnum_off-- > 0)
23110 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023111
23112 /* Check for end of eap->arg. */
23113 if (line_arg != NULL && *line_arg == NUL)
23114 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 }
23116
23117 /* Don't define the function when skipping commands or when an error was
23118 * detected. */
23119 if (eap->skip || did_emsg)
23120 goto erret;
23121
23122 /*
23123 * If there are no errors, add the function
23124 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023125 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023126 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023127 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023129 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023130 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023131 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023132 goto erret;
23133 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023135 fp = find_func(name);
23136 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138 if (!eap->forceit)
23139 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023140 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023141 goto erret;
23142 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023143 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023144 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023145 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023146 name);
23147 goto erret;
23148 }
23149 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 ga_clear_strings(&(fp->uf_args));
23151 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023152 vim_free(name);
23153 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 }
23156 else
23157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023158 char numbuf[20];
23159
23160 fp = NULL;
23161 if (fudi.fd_newkey == NULL && !eap->forceit)
23162 {
23163 EMSG(_(e_funcdict));
23164 goto erret;
23165 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023166 if (fudi.fd_di == NULL)
23167 {
23168 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023169 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023170 goto erret;
23171 }
23172 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023173 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023174 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175
23176 /* Give the function a sequential number. Can only be used with a
23177 * Funcref! */
23178 vim_free(name);
23179 sprintf(numbuf, "%d", ++func_nr);
23180 name = vim_strsave((char_u *)numbuf);
23181 if (name == NULL)
23182 goto erret;
23183 }
23184
23185 if (fp == NULL)
23186 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023187 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023188 {
23189 int slen, plen;
23190 char_u *scriptname;
23191
23192 /* Check that the autoload name matches the script name. */
23193 j = FAIL;
23194 if (sourcing_name != NULL)
23195 {
23196 scriptname = autoload_name(name);
23197 if (scriptname != NULL)
23198 {
23199 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023200 plen = (int)STRLEN(p);
23201 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 if (slen > plen && fnamecmp(p,
23203 sourcing_name + slen - plen) == 0)
23204 j = OK;
23205 vim_free(scriptname);
23206 }
23207 }
23208 if (j == FAIL)
23209 {
23210 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23211 goto erret;
23212 }
23213 }
23214
23215 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 if (fp == NULL)
23217 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023218
23219 if (fudi.fd_dict != NULL)
23220 {
23221 if (fudi.fd_di == NULL)
23222 {
23223 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023224 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023225 if (fudi.fd_di == NULL)
23226 {
23227 vim_free(fp);
23228 goto erret;
23229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023230 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23231 {
23232 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023233 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023234 goto erret;
23235 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023236 }
23237 else
23238 /* overwrite existing dict entry */
23239 clear_tv(&fudi.fd_di->di_tv);
23240 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023241 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023242 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023243 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023244
23245 /* behave like "dict" was used */
23246 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247 }
23248
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023250 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023251 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23252 {
23253 vim_free(fp);
23254 goto erret;
23255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023257 fp->uf_args = newargs;
23258 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259#ifdef FEAT_PROFILE
23260 fp->uf_tml_count = NULL;
23261 fp->uf_tml_total = NULL;
23262 fp->uf_tml_self = NULL;
23263 fp->uf_profiling = FALSE;
23264 if (prof_def_func())
23265 func_do_profile(fp);
23266#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023267 fp->uf_varargs = varargs;
23268 fp->uf_flags = flags;
23269 fp->uf_calls = 0;
23270 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023271 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272
23273erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 ga_clear_strings(&newargs);
23275 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023276ret_free:
23277 vim_free(skip_until);
23278 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023281 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282}
23283
23284/*
23285 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023286 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023288 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023289 * TFN_INT: internal function name OK
23290 * TFN_QUIET: be quiet
23291 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 * Advances "pp" to just after the function name (if no error).
23293 */
23294 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023295trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 char_u **pp;
23297 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023298 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023299 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023301 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 char_u *start;
23303 char_u *end;
23304 int lead;
23305 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023308
23309 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023312
23313 /* Check for hard coded <SNR>: already translated function ID (from a user
23314 * command). */
23315 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23316 && (*pp)[2] == (int)KE_SNR)
23317 {
23318 *pp += 3;
23319 len = get_id_len(pp) + 3;
23320 return vim_strnsave(start, len);
23321 }
23322
23323 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23324 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023326 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023328
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023329 /* Note that TFN_ flags use the same values as GLV_ flags. */
23330 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023331 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023332 if (end == start)
23333 {
23334 if (!skip)
23335 EMSG(_("E129: Function name required"));
23336 goto theend;
23337 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023338 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023339 {
23340 /*
23341 * Report an invalid expression in braces, unless the expression
23342 * evaluation has been cancelled due to an aborting error, an
23343 * interrupt, or an exception.
23344 */
23345 if (!aborting())
23346 {
23347 if (end != NULL)
23348 EMSG2(_(e_invarg2), start);
23349 }
23350 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023351 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023352 goto theend;
23353 }
23354
23355 if (lv.ll_tv != NULL)
23356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357 if (fdp != NULL)
23358 {
23359 fdp->fd_dict = lv.ll_dict;
23360 fdp->fd_newkey = lv.ll_newkey;
23361 lv.ll_newkey = NULL;
23362 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023363 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023364 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23365 {
23366 name = vim_strsave(lv.ll_tv->vval.v_string);
23367 *pp = end;
23368 }
23369 else
23370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023371 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23372 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023373 EMSG(_(e_funcref));
23374 else
23375 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023376 name = NULL;
23377 }
23378 goto theend;
23379 }
23380
23381 if (lv.ll_name == NULL)
23382 {
23383 /* Error found, but continue after the function name. */
23384 *pp = end;
23385 goto theend;
23386 }
23387
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023388 /* Check if the name is a Funcref. If so, use the value. */
23389 if (lv.ll_exp_name != NULL)
23390 {
23391 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023392 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023393 if (name == lv.ll_exp_name)
23394 name = NULL;
23395 }
23396 else
23397 {
23398 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023399 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023400 if (name == *pp)
23401 name = NULL;
23402 }
23403 if (name != NULL)
23404 {
23405 name = vim_strsave(name);
23406 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023407 if (STRNCMP(name, "<SNR>", 5) == 0)
23408 {
23409 /* Change "<SNR>" to the byte sequence. */
23410 name[0] = K_SPECIAL;
23411 name[1] = KS_EXTRA;
23412 name[2] = (int)KE_SNR;
23413 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23414 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023415 goto theend;
23416 }
23417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023418 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023419 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023420 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023421 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23422 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23423 {
23424 /* When there was "s:" already or the name expanded to get a
23425 * leading "s:" then remove it. */
23426 lv.ll_name += 2;
23427 len -= 2;
23428 lead = 2;
23429 }
23430 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023431 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023432 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023433 /* skip over "s:" and "g:" */
23434 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023435 lv.ll_name += 2;
23436 len = (int)(end - lv.ll_name);
23437 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023438
23439 /*
23440 * Copy the function name to allocated memory.
23441 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23442 * Accept <SNR>123_name() outside a script.
23443 */
23444 if (skip)
23445 lead = 0; /* do nothing */
23446 else if (lead > 0)
23447 {
23448 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023449 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23450 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023451 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023452 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023453 if (current_SID <= 0)
23454 {
23455 EMSG(_(e_usingsid));
23456 goto theend;
23457 }
23458 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23459 lead += (int)STRLEN(sid_buf);
23460 }
23461 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023462 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023463 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023464 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023465 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023466 goto theend;
23467 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023468 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023469 {
23470 char_u *cp = vim_strchr(lv.ll_name, ':');
23471
23472 if (cp != NULL && cp < end)
23473 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023474 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023475 goto theend;
23476 }
23477 }
23478
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023479 name = alloc((unsigned)(len + lead + 1));
23480 if (name != NULL)
23481 {
23482 if (lead > 0)
23483 {
23484 name[0] = K_SPECIAL;
23485 name[1] = KS_EXTRA;
23486 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023487 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023488 STRCPY(name + 3, sid_buf);
23489 }
23490 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023491 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023492 }
23493 *pp = end;
23494
23495theend:
23496 clear_lval(&lv);
23497 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498}
23499
23500/*
23501 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23502 * Return 2 if "p" starts with "s:".
23503 * Return 0 otherwise.
23504 */
23505 static int
23506eval_fname_script(p)
23507 char_u *p;
23508{
23509 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23510 || STRNICMP(p + 1, "SNR>", 4) == 0))
23511 return 5;
23512 if (p[0] == 's' && p[1] == ':')
23513 return 2;
23514 return 0;
23515}
23516
23517/*
23518 * Return TRUE if "p" starts with "<SID>" or "s:".
23519 * Only works if eval_fname_script() returned non-zero for "p"!
23520 */
23521 static int
23522eval_fname_sid(p)
23523 char_u *p;
23524{
23525 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23526}
23527
23528/*
23529 * List the head of the function: "name(arg1, arg2)".
23530 */
23531 static void
23532list_func_head(fp, indent)
23533 ufunc_T *fp;
23534 int indent;
23535{
23536 int j;
23537
23538 msg_start();
23539 if (indent)
23540 MSG_PUTS(" ");
23541 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 {
23544 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023545 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 }
23547 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023548 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023550 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 {
23552 if (j)
23553 MSG_PUTS(", ");
23554 msg_puts(FUNCARG(fp, j));
23555 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023556 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 {
23558 if (j)
23559 MSG_PUTS(", ");
23560 MSG_PUTS("...");
23561 }
23562 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023563 if (fp->uf_flags & FC_ABORT)
23564 MSG_PUTS(" abort");
23565 if (fp->uf_flags & FC_RANGE)
23566 MSG_PUTS(" range");
23567 if (fp->uf_flags & FC_DICT)
23568 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023569 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023570 if (p_verbose > 0)
23571 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572}
23573
23574/*
23575 * Find a function by name, return pointer to it in ufuncs.
23576 * Return NULL for unknown function.
23577 */
23578 static ufunc_T *
23579find_func(name)
23580 char_u *name;
23581{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023582 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023584 hi = hash_find(&func_hashtab, name);
23585 if (!HASHITEM_EMPTY(hi))
23586 return HI2UF(hi);
23587 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588}
23589
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023590#if defined(EXITFREE) || defined(PROTO)
23591 void
23592free_all_functions()
23593{
23594 hashitem_T *hi;
23595
23596 /* Need to start all over every time, because func_free() may change the
23597 * hash table. */
23598 while (func_hashtab.ht_used > 0)
23599 for (hi = func_hashtab.ht_array; ; ++hi)
23600 if (!HASHITEM_EMPTY(hi))
23601 {
23602 func_free(HI2UF(hi));
23603 break;
23604 }
23605}
23606#endif
23607
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023608 int
23609translated_function_exists(name)
23610 char_u *name;
23611{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023612 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023613 return find_internal_func(name) >= 0;
23614 return find_func(name) != NULL;
23615}
23616
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023617/*
23618 * Return TRUE if a function "name" exists.
23619 */
23620 static int
23621function_exists(name)
23622 char_u *name;
23623{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023624 char_u *nm = name;
23625 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023626 int n = FALSE;
23627
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023628 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23629 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023630 nm = skipwhite(nm);
23631
23632 /* Only accept "funcname", "funcname ", "funcname (..." and
23633 * "funcname(...", not "funcname!...". */
23634 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023635 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023636 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023637 return n;
23638}
23639
Bram Moolenaara1544c02013-05-30 12:35:52 +020023640 char_u *
23641get_expanded_name(name, check)
23642 char_u *name;
23643 int check;
23644{
23645 char_u *nm = name;
23646 char_u *p;
23647
23648 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23649
23650 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023651 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023652 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023653
Bram Moolenaara1544c02013-05-30 12:35:52 +020023654 vim_free(p);
23655 return NULL;
23656}
23657
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023658/*
23659 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023660 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23661 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023662 */
23663 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023664builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023665 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023666 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023667{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023668 char_u *p;
23669
23670 if (!ASCII_ISLOWER(name[0]))
23671 return FALSE;
23672 p = vim_strchr(name, AUTOLOAD_CHAR);
23673 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023674}
23675
Bram Moolenaar05159a02005-02-26 23:04:13 +000023676#if defined(FEAT_PROFILE) || defined(PROTO)
23677/*
23678 * Start profiling function "fp".
23679 */
23680 static void
23681func_do_profile(fp)
23682 ufunc_T *fp;
23683{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023684 int len = fp->uf_lines.ga_len;
23685
23686 if (len == 0)
23687 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023688 fp->uf_tm_count = 0;
23689 profile_zero(&fp->uf_tm_self);
23690 profile_zero(&fp->uf_tm_total);
23691 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023692 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023693 if (fp->uf_tml_total == NULL)
23694 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023695 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023696 if (fp->uf_tml_self == NULL)
23697 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023698 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023699 fp->uf_tml_idx = -1;
23700 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23701 || fp->uf_tml_self == NULL)
23702 return; /* out of memory */
23703
23704 fp->uf_profiling = TRUE;
23705}
23706
23707/*
23708 * Dump the profiling results for all functions in file "fd".
23709 */
23710 void
23711func_dump_profile(fd)
23712 FILE *fd;
23713{
23714 hashitem_T *hi;
23715 int todo;
23716 ufunc_T *fp;
23717 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023718 ufunc_T **sorttab;
23719 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023720
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023721 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023722 if (todo == 0)
23723 return; /* nothing to dump */
23724
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023725 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023726
Bram Moolenaar05159a02005-02-26 23:04:13 +000023727 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23728 {
23729 if (!HASHITEM_EMPTY(hi))
23730 {
23731 --todo;
23732 fp = HI2UF(hi);
23733 if (fp->uf_profiling)
23734 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023735 if (sorttab != NULL)
23736 sorttab[st_len++] = fp;
23737
Bram Moolenaar05159a02005-02-26 23:04:13 +000023738 if (fp->uf_name[0] == K_SPECIAL)
23739 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23740 else
23741 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23742 if (fp->uf_tm_count == 1)
23743 fprintf(fd, "Called 1 time\n");
23744 else
23745 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23746 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23747 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23748 fprintf(fd, "\n");
23749 fprintf(fd, "count total (s) self (s)\n");
23750
23751 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23752 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023753 if (FUNCLINE(fp, i) == NULL)
23754 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023755 prof_func_line(fd, fp->uf_tml_count[i],
23756 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023757 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23758 }
23759 fprintf(fd, "\n");
23760 }
23761 }
23762 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023763
23764 if (sorttab != NULL && st_len > 0)
23765 {
23766 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23767 prof_total_cmp);
23768 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23769 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23770 prof_self_cmp);
23771 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23772 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023773
23774 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023775}
Bram Moolenaar73830342005-02-28 22:48:19 +000023776
23777 static void
23778prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23779 FILE *fd;
23780 ufunc_T **sorttab;
23781 int st_len;
23782 char *title;
23783 int prefer_self; /* when equal print only self time */
23784{
23785 int i;
23786 ufunc_T *fp;
23787
23788 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23789 fprintf(fd, "count total (s) self (s) function\n");
23790 for (i = 0; i < 20 && i < st_len; ++i)
23791 {
23792 fp = sorttab[i];
23793 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23794 prefer_self);
23795 if (fp->uf_name[0] == K_SPECIAL)
23796 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23797 else
23798 fprintf(fd, " %s()\n", fp->uf_name);
23799 }
23800 fprintf(fd, "\n");
23801}
23802
23803/*
23804 * Print the count and times for one function or function line.
23805 */
23806 static void
23807prof_func_line(fd, count, total, self, prefer_self)
23808 FILE *fd;
23809 int count;
23810 proftime_T *total;
23811 proftime_T *self;
23812 int prefer_self; /* when equal print only self time */
23813{
23814 if (count > 0)
23815 {
23816 fprintf(fd, "%5d ", count);
23817 if (prefer_self && profile_equal(total, self))
23818 fprintf(fd, " ");
23819 else
23820 fprintf(fd, "%s ", profile_msg(total));
23821 if (!prefer_self && profile_equal(total, self))
23822 fprintf(fd, " ");
23823 else
23824 fprintf(fd, "%s ", profile_msg(self));
23825 }
23826 else
23827 fprintf(fd, " ");
23828}
23829
23830/*
23831 * Compare function for total time sorting.
23832 */
23833 static int
23834#ifdef __BORLANDC__
23835_RTLENTRYF
23836#endif
23837prof_total_cmp(s1, s2)
23838 const void *s1;
23839 const void *s2;
23840{
23841 ufunc_T *p1, *p2;
23842
23843 p1 = *(ufunc_T **)s1;
23844 p2 = *(ufunc_T **)s2;
23845 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23846}
23847
23848/*
23849 * Compare function for self time sorting.
23850 */
23851 static int
23852#ifdef __BORLANDC__
23853_RTLENTRYF
23854#endif
23855prof_self_cmp(s1, s2)
23856 const void *s1;
23857 const void *s2;
23858{
23859 ufunc_T *p1, *p2;
23860
23861 p1 = *(ufunc_T **)s1;
23862 p2 = *(ufunc_T **)s2;
23863 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23864}
23865
Bram Moolenaar05159a02005-02-26 23:04:13 +000023866#endif
23867
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023868/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023869 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023870 * Return TRUE if a package was loaded.
23871 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023872 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023873script_autoload(name, reload)
23874 char_u *name;
23875 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023876{
23877 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023878 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023879 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023880 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023881
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023882 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023883 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023884 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023885 return FALSE;
23886
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023887 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023888
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023889 /* Find the name in the list of previously loaded package names. Skip
23890 * "autoload/", it's always the same. */
23891 for (i = 0; i < ga_loaded.ga_len; ++i)
23892 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23893 break;
23894 if (!reload && i < ga_loaded.ga_len)
23895 ret = FALSE; /* was loaded already */
23896 else
23897 {
23898 /* Remember the name if it wasn't loaded already. */
23899 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23900 {
23901 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23902 tofree = NULL;
23903 }
23904
23905 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023906 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023907 ret = TRUE;
23908 }
23909
23910 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023911 return ret;
23912}
23913
23914/*
23915 * Return the autoload script name for a function or variable name.
23916 * Returns NULL when out of memory.
23917 */
23918 static char_u *
23919autoload_name(name)
23920 char_u *name;
23921{
23922 char_u *p;
23923 char_u *scriptname;
23924
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023925 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023926 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23927 if (scriptname == NULL)
23928 return FALSE;
23929 STRCPY(scriptname, "autoload/");
23930 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023931 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023932 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023933 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023934 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023935 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023936}
23937
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23939
23940/*
23941 * Function given to ExpandGeneric() to obtain the list of user defined
23942 * function names.
23943 */
23944 char_u *
23945get_user_func_name(xp, idx)
23946 expand_T *xp;
23947 int idx;
23948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 static long_u done;
23950 static hashitem_T *hi;
23951 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952
23953 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023955 done = 0;
23956 hi = func_hashtab.ht_array;
23957 }
23958 if (done < func_hashtab.ht_used)
23959 {
23960 if (done++ > 0)
23961 ++hi;
23962 while (HASHITEM_EMPTY(hi))
23963 ++hi;
23964 fp = HI2UF(hi);
23965
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023966 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023967 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023968
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023969 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23970 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971
23972 cat_func_name(IObuff, fp);
23973 if (xp->xp_context != EXPAND_USER_FUNC)
23974 {
23975 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023976 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 STRCAT(IObuff, ")");
23978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 return IObuff;
23980 }
23981 return NULL;
23982}
23983
23984#endif /* FEAT_CMDL_COMPL */
23985
23986/*
23987 * Copy the function name of "fp" to buffer "buf".
23988 * "buf" must be able to hold the function name plus three bytes.
23989 * Takes care of script-local function names.
23990 */
23991 static void
23992cat_func_name(buf, fp)
23993 char_u *buf;
23994 ufunc_T *fp;
23995{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023996 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 {
23998 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023999 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 }
24001 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024002 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003}
24004
24005/*
24006 * ":delfunction {name}"
24007 */
24008 void
24009ex_delfunction(eap)
24010 exarg_T *eap;
24011{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 char_u *p;
24014 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024015 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016
24017 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024018 name = trans_function_name(&p, eap->skip, 0, &fudi);
24019 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024021 {
24022 if (fudi.fd_dict != NULL && !eap->skip)
24023 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 if (!ends_excmd(*skipwhite(p)))
24027 {
24028 vim_free(name);
24029 EMSG(_(e_trailing));
24030 return;
24031 }
24032 eap->nextcmd = check_nextcmd(p);
24033 if (eap->nextcmd != NULL)
24034 *p = NUL;
24035
24036 if (!eap->skip)
24037 fp = find_func(name);
24038 vim_free(name);
24039
24040 if (!eap->skip)
24041 {
24042 if (fp == NULL)
24043 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024044 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 return;
24046 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024047 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 {
24049 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24050 return;
24051 }
24052
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024053 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024055 /* Delete the dict item that refers to the function, it will
24056 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024057 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024059 else
24060 func_free(fp);
24061 }
24062}
24063
24064/*
24065 * Free a function and remove it from the list of functions.
24066 */
24067 static void
24068func_free(fp)
24069 ufunc_T *fp;
24070{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024071 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024072
24073 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024074 ga_clear_strings(&(fp->uf_args));
24075 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024076#ifdef FEAT_PROFILE
24077 vim_free(fp->uf_tml_count);
24078 vim_free(fp->uf_tml_total);
24079 vim_free(fp->uf_tml_self);
24080#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024081
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024082 /* remove the function from the function hashtable */
24083 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24084 if (HASHITEM_EMPTY(hi))
24085 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024086 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024087 hash_remove(&func_hashtab, hi);
24088
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024089 vim_free(fp);
24090}
24091
24092/*
24093 * Unreference a Function: decrement the reference count and free it when it
24094 * becomes zero. Only for numbered functions.
24095 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024096 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024097func_unref(name)
24098 char_u *name;
24099{
24100 ufunc_T *fp;
24101
24102 if (name != NULL && isdigit(*name))
24103 {
24104 fp = find_func(name);
24105 if (fp == NULL)
24106 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024107 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024108 {
24109 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024110 * when "uf_calls" becomes zero. */
24111 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024112 func_free(fp);
24113 }
24114 }
24115}
24116
24117/*
24118 * Count a reference to a Function.
24119 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024120 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024121func_ref(name)
24122 char_u *name;
24123{
24124 ufunc_T *fp;
24125
24126 if (name != NULL && isdigit(*name))
24127 {
24128 fp = find_func(name);
24129 if (fp == NULL)
24130 EMSG2(_(e_intern2), "func_ref()");
24131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 }
24134}
24135
24136/*
24137 * Call a user function.
24138 */
24139 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024140call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 ufunc_T *fp; /* pointer to function */
24142 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024143 typval_T *argvars; /* arguments */
24144 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 linenr_T firstline; /* first line of range */
24146 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024147 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148{
Bram Moolenaar33570922005-01-25 22:26:29 +000024149 char_u *save_sourcing_name;
24150 linenr_T save_sourcing_lnum;
24151 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024152 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024153 int save_did_emsg;
24154 static int depth = 0;
24155 dictitem_T *v;
24156 int fixvar_idx = 0; /* index in fixvar[] */
24157 int i;
24158 int ai;
24159 char_u numbuf[NUMBUFLEN];
24160 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024161 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024162#ifdef FEAT_PROFILE
24163 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024164 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166
24167 /* If depth of calling is getting too high, don't execute the function */
24168 if (depth >= p_mfd)
24169 {
24170 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024171 rettv->v_type = VAR_NUMBER;
24172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 return;
24174 }
24175 ++depth;
24176
24177 line_breakcheck(); /* check for CTRL-C hit */
24178
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024179 fc = (funccall_T *)alloc(sizeof(funccall_T));
24180 fc->caller = current_funccal;
24181 current_funccal = fc;
24182 fc->func = fp;
24183 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024184 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024185 fc->linenr = 0;
24186 fc->returned = FALSE;
24187 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024189 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24190 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024193 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024194 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24195 * each argument variable and saves a lot of time.
24196 */
24197 /*
24198 * Init l: variables.
24199 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024200 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024201 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024202 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024203 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24204 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024205 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024206 name = v->di_key;
24207 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024208 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024209 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024210 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024211 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024212 v->di_tv.vval.v_dict = selfdict;
24213 ++selfdict->dv_refcount;
24214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024215
Bram Moolenaar33570922005-01-25 22:26:29 +000024216 /*
24217 * Init a: variables.
24218 * Set a:0 to "argcount".
24219 * Set a:000 to a list with room for the "..." arguments.
24220 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024221 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024222 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024223 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024224 /* Use "name" to avoid a warning from some compiler that checks the
24225 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024226 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024227 name = v->di_key;
24228 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024229 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024230 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024231 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024232 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024233 v->di_tv.vval.v_list = &fc->l_varlist;
24234 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24235 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24236 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024237
24238 /*
24239 * Set a:firstline to "firstline" and a:lastline to "lastline".
24240 * Set a:name to named arguments.
24241 * Set a:N to the "..." arguments.
24242 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024243 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024244 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024245 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024246 (varnumber_T)lastline);
24247 for (i = 0; i < argcount; ++i)
24248 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024249 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024250 if (ai < 0)
24251 /* named argument a:name */
24252 name = FUNCARG(fp, i);
24253 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024254 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024255 /* "..." argument a:1, a:2, etc. */
24256 sprintf((char *)numbuf, "%d", ai + 1);
24257 name = numbuf;
24258 }
24259 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24260 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024261 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024262 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24263 }
24264 else
24265 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024266 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24267 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024268 if (v == NULL)
24269 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024270 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024271 }
24272 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024273 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024274
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024275 /* Note: the values are copied directly to avoid alloc/free.
24276 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024277 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024278 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024279
24280 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24281 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024282 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24283 fc->l_listitems[ai].li_tv = argvars[i];
24284 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024285 }
24286 }
24287
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 /* Don't redraw while executing the function. */
24289 ++RedrawingDisabled;
24290 save_sourcing_name = sourcing_name;
24291 save_sourcing_lnum = sourcing_lnum;
24292 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024293 /* need space for function name + ("function " + 3) or "[number]" */
24294 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24295 + STRLEN(fp->uf_name) + 20;
24296 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 if (sourcing_name != NULL)
24298 {
24299 if (save_sourcing_name != NULL
24300 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024301 sprintf((char *)sourcing_name, "%s[%d]..",
24302 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 else
24304 STRCPY(sourcing_name, "function ");
24305 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24306
24307 if (p_verbose >= 12)
24308 {
24309 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024310 verbose_enter_scroll();
24311
Bram Moolenaar555b2802005-05-19 21:08:39 +000024312 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313 if (p_verbose >= 14)
24314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024316 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024317 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024318 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319
24320 msg_puts((char_u *)"(");
24321 for (i = 0; i < argcount; ++i)
24322 {
24323 if (i > 0)
24324 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024325 if (argvars[i].v_type == VAR_NUMBER)
24326 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 else
24328 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024329 /* Do not want errors such as E724 here. */
24330 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024331 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024332 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024333 if (s != NULL)
24334 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024335 if (vim_strsize(s) > MSG_BUF_CLEN)
24336 {
24337 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24338 s = buf;
24339 }
24340 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024341 vim_free(tofree);
24342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 }
24344 }
24345 msg_puts((char_u *)")");
24346 }
24347 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024348
24349 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 --no_wait_return;
24351 }
24352 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024353#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024354 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024355 {
24356 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24357 func_do_profile(fp);
24358 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024359 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024360 {
24361 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024362 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024363 profile_zero(&fp->uf_tm_children);
24364 }
24365 script_prof_save(&wait_start);
24366 }
24367#endif
24368
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024370 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 save_did_emsg = did_emsg;
24372 did_emsg = FALSE;
24373
24374 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024375 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24377
24378 --RedrawingDisabled;
24379
24380 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024381 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024383 clear_tv(rettv);
24384 rettv->v_type = VAR_NUMBER;
24385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 }
24387
Bram Moolenaar05159a02005-02-26 23:04:13 +000024388#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024389 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024390 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024391 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024392 profile_end(&call_start);
24393 profile_sub_wait(&wait_start, &call_start);
24394 profile_add(&fp->uf_tm_total, &call_start);
24395 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024396 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024397 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024398 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24399 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024400 }
24401 }
24402#endif
24403
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 /* when being verbose, mention the return value */
24405 if (p_verbose >= 12)
24406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024408 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024411 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024412 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024413 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024414 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024417 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024418 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024419 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024420 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024421
Bram Moolenaar555b2802005-05-19 21:08:39 +000024422 /* The value may be very long. Skip the middle part, so that we
24423 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024424 * truncate it at the end. Don't want errors such as E724 here. */
24425 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024426 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024427 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024428 if (s != NULL)
24429 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024430 if (vim_strsize(s) > MSG_BUF_CLEN)
24431 {
24432 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24433 s = buf;
24434 }
24435 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024436 vim_free(tofree);
24437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438 }
24439 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024440
24441 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 --no_wait_return;
24443 }
24444
24445 vim_free(sourcing_name);
24446 sourcing_name = save_sourcing_name;
24447 sourcing_lnum = save_sourcing_lnum;
24448 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024449#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024450 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024451 script_prof_restore(&wait_start);
24452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453
24454 if (p_verbose >= 12 && sourcing_name != NULL)
24455 {
24456 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024457 verbose_enter_scroll();
24458
Bram Moolenaar555b2802005-05-19 21:08:39 +000024459 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024461
24462 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024463 --no_wait_return;
24464 }
24465
24466 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024467 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024469
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024470 /* If the a:000 list and the l: and a: dicts are not referenced we can
24471 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024472 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24473 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24474 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24475 {
24476 free_funccal(fc, FALSE);
24477 }
24478 else
24479 {
24480 hashitem_T *hi;
24481 listitem_T *li;
24482 int todo;
24483
24484 /* "fc" is still in use. This can happen when returning "a:000" or
24485 * assigning "l:" to a global variable.
24486 * Link "fc" in the list for garbage collection later. */
24487 fc->caller = previous_funccal;
24488 previous_funccal = fc;
24489
24490 /* Make a copy of the a: variables, since we didn't do that above. */
24491 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24492 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24493 {
24494 if (!HASHITEM_EMPTY(hi))
24495 {
24496 --todo;
24497 v = HI2DI(hi);
24498 copy_tv(&v->di_tv, &v->di_tv);
24499 }
24500 }
24501
24502 /* Make a copy of the a:000 items, since we didn't do that above. */
24503 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24504 copy_tv(&li->li_tv, &li->li_tv);
24505 }
24506}
24507
24508/*
24509 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024510 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024511 */
24512 static int
24513can_free_funccal(fc, copyID)
24514 funccall_T *fc;
24515 int copyID;
24516{
24517 return (fc->l_varlist.lv_copyID != copyID
24518 && fc->l_vars.dv_copyID != copyID
24519 && fc->l_avars.dv_copyID != copyID);
24520}
24521
24522/*
24523 * Free "fc" and what it contains.
24524 */
24525 static void
24526free_funccal(fc, free_val)
24527 funccall_T *fc;
24528 int free_val; /* a: vars were allocated */
24529{
24530 listitem_T *li;
24531
24532 /* The a: variables typevals may not have been allocated, only free the
24533 * allocated variables. */
24534 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24535
24536 /* free all l: variables */
24537 vars_clear(&fc->l_vars.dv_hashtab);
24538
24539 /* Free the a:000 variables if they were allocated. */
24540 if (free_val)
24541 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24542 clear_tv(&li->li_tv);
24543
24544 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545}
24546
24547/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024548 * Add a number variable "name" to dict "dp" with value "nr".
24549 */
24550 static void
24551add_nr_var(dp, v, name, nr)
24552 dict_T *dp;
24553 dictitem_T *v;
24554 char *name;
24555 varnumber_T nr;
24556{
24557 STRCPY(v->di_key, name);
24558 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24559 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24560 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024561 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024562 v->di_tv.vval.v_number = nr;
24563}
24564
24565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 * ":return [expr]"
24567 */
24568 void
24569ex_return(eap)
24570 exarg_T *eap;
24571{
24572 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024573 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 int returning = FALSE;
24575
24576 if (current_funccal == NULL)
24577 {
24578 EMSG(_("E133: :return not inside a function"));
24579 return;
24580 }
24581
24582 if (eap->skip)
24583 ++emsg_skip;
24584
24585 eap->nextcmd = NULL;
24586 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024587 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 {
24589 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024590 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593 }
24594 /* It's safer to return also on error. */
24595 else if (!eap->skip)
24596 {
24597 /*
24598 * Return unless the expression evaluation has been cancelled due to an
24599 * aborting error, an interrupt, or an exception.
24600 */
24601 if (!aborting())
24602 returning = do_return(eap, FALSE, TRUE, NULL);
24603 }
24604
24605 /* When skipping or the return gets pending, advance to the next command
24606 * in this line (!returning). Otherwise, ignore the rest of the line.
24607 * Following lines will be ignored by get_func_line(). */
24608 if (returning)
24609 eap->nextcmd = NULL;
24610 else if (eap->nextcmd == NULL) /* no argument */
24611 eap->nextcmd = check_nextcmd(arg);
24612
24613 if (eap->skip)
24614 --emsg_skip;
24615}
24616
24617/*
24618 * Return from a function. Possibly makes the return pending. Also called
24619 * for a pending return at the ":endtry" or after returning from an extra
24620 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024621 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024622 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623 * FALSE when the return gets pending.
24624 */
24625 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024626do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 exarg_T *eap;
24628 int reanimate;
24629 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024630 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631{
24632 int idx;
24633 struct condstack *cstack = eap->cstack;
24634
24635 if (reanimate)
24636 /* Undo the return. */
24637 current_funccal->returned = FALSE;
24638
24639 /*
24640 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24641 * not in its finally clause (which then is to be executed next) is found.
24642 * In this case, make the ":return" pending for execution at the ":endtry".
24643 * Otherwise, return normally.
24644 */
24645 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24646 if (idx >= 0)
24647 {
24648 cstack->cs_pending[idx] = CSTP_RETURN;
24649
24650 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024651 /* A pending return again gets pending. "rettv" points to an
24652 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024654 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 else
24656 {
24657 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024658 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024660 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024662 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 {
24664 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024665 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024666 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 else
24668 EMSG(_(e_outofmem));
24669 }
24670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024671 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672
24673 if (reanimate)
24674 {
24675 /* The pending return value could be overwritten by a ":return"
24676 * without argument in a finally clause; reset the default
24677 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024678 current_funccal->rettv->v_type = VAR_NUMBER;
24679 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680 }
24681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024682 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024683 }
24684 else
24685 {
24686 current_funccal->returned = TRUE;
24687
24688 /* If the return is carried out now, store the return value. For
24689 * a return immediately after reanimation, the value is already
24690 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024691 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024693 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024694 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024696 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 }
24698 }
24699
24700 return idx < 0;
24701}
24702
24703/*
24704 * Free the variable with a pending return value.
24705 */
24706 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024707discard_pending_return(rettv)
24708 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709{
Bram Moolenaar33570922005-01-25 22:26:29 +000024710 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711}
24712
24713/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024714 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 * is an allocated string. Used by report_pending() for verbose messages.
24716 */
24717 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024718get_return_cmd(rettv)
24719 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024721 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024722 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024723 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024725 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024726 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024727 if (s == NULL)
24728 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024729
24730 STRCPY(IObuff, ":return ");
24731 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24732 if (STRLEN(s) + 8 >= IOSIZE)
24733 STRCPY(IObuff + IOSIZE - 4, "...");
24734 vim_free(tofree);
24735 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736}
24737
24738/*
24739 * Get next function line.
24740 * Called by do_cmdline() to get the next line.
24741 * Returns allocated string, or NULL for end of function.
24742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743 char_u *
24744get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024745 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024747 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748{
Bram Moolenaar33570922005-01-25 22:26:29 +000024749 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024750 ufunc_T *fp = fcp->func;
24751 char_u *retval;
24752 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753
24754 /* If breakpoints have been added/deleted need to check for it. */
24755 if (fcp->dbg_tick != debug_tick)
24756 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024757 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 sourcing_lnum);
24759 fcp->dbg_tick = debug_tick;
24760 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024761#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024762 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024763 func_line_end(cookie);
24764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765
Bram Moolenaar05159a02005-02-26 23:04:13 +000024766 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024767 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24768 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 retval = NULL;
24770 else
24771 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024772 /* Skip NULL lines (continuation lines). */
24773 while (fcp->linenr < gap->ga_len
24774 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24775 ++fcp->linenr;
24776 if (fcp->linenr >= gap->ga_len)
24777 retval = NULL;
24778 else
24779 {
24780 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24781 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024782#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024783 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024784 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024785#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 }
24788
24789 /* Did we encounter a breakpoint? */
24790 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24791 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024792 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024794 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 sourcing_lnum);
24796 fcp->dbg_tick = debug_tick;
24797 }
24798
24799 return retval;
24800}
24801
Bram Moolenaar05159a02005-02-26 23:04:13 +000024802#if defined(FEAT_PROFILE) || defined(PROTO)
24803/*
24804 * Called when starting to read a function line.
24805 * "sourcing_lnum" must be correct!
24806 * When skipping lines it may not actually be executed, but we won't find out
24807 * until later and we need to store the time now.
24808 */
24809 void
24810func_line_start(cookie)
24811 void *cookie;
24812{
24813 funccall_T *fcp = (funccall_T *)cookie;
24814 ufunc_T *fp = fcp->func;
24815
24816 if (fp->uf_profiling && sourcing_lnum >= 1
24817 && sourcing_lnum <= fp->uf_lines.ga_len)
24818 {
24819 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024820 /* Skip continuation lines. */
24821 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24822 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024823 fp->uf_tml_execed = FALSE;
24824 profile_start(&fp->uf_tml_start);
24825 profile_zero(&fp->uf_tml_children);
24826 profile_get_wait(&fp->uf_tml_wait);
24827 }
24828}
24829
24830/*
24831 * Called when actually executing a function line.
24832 */
24833 void
24834func_line_exec(cookie)
24835 void *cookie;
24836{
24837 funccall_T *fcp = (funccall_T *)cookie;
24838 ufunc_T *fp = fcp->func;
24839
24840 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24841 fp->uf_tml_execed = TRUE;
24842}
24843
24844/*
24845 * Called when done with a function line.
24846 */
24847 void
24848func_line_end(cookie)
24849 void *cookie;
24850{
24851 funccall_T *fcp = (funccall_T *)cookie;
24852 ufunc_T *fp = fcp->func;
24853
24854 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24855 {
24856 if (fp->uf_tml_execed)
24857 {
24858 ++fp->uf_tml_count[fp->uf_tml_idx];
24859 profile_end(&fp->uf_tml_start);
24860 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024861 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024862 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24863 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024864 }
24865 fp->uf_tml_idx = -1;
24866 }
24867}
24868#endif
24869
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870/*
24871 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024872 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 */
24874 int
24875func_has_ended(cookie)
24876 void *cookie;
24877{
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879
24880 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24881 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024882 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 || fcp->returned);
24884}
24885
24886/*
24887 * return TRUE if cookie indicates a function which "abort"s on errors.
24888 */
24889 int
24890func_has_abort(cookie)
24891 void *cookie;
24892{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024893 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894}
24895
24896#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24897typedef enum
24898{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024899 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24900 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24901 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902} var_flavour_T;
24903
24904static var_flavour_T var_flavour __ARGS((char_u *varname));
24905
24906 static var_flavour_T
24907var_flavour(varname)
24908 char_u *varname;
24909{
24910 char_u *p = varname;
24911
24912 if (ASCII_ISUPPER(*p))
24913 {
24914 while (*(++p))
24915 if (ASCII_ISLOWER(*p))
24916 return VAR_FLAVOUR_SESSION;
24917 return VAR_FLAVOUR_VIMINFO;
24918 }
24919 else
24920 return VAR_FLAVOUR_DEFAULT;
24921}
24922#endif
24923
24924#if defined(FEAT_VIMINFO) || defined(PROTO)
24925/*
24926 * Restore global vars that start with a capital from the viminfo file
24927 */
24928 int
24929read_viminfo_varlist(virp, writing)
24930 vir_T *virp;
24931 int writing;
24932{
24933 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024934 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024935 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936
24937 if (!writing && (find_viminfo_parameter('!') != NULL))
24938 {
24939 tab = vim_strchr(virp->vir_line + 1, '\t');
24940 if (tab != NULL)
24941 {
24942 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024943 switch (*tab)
24944 {
24945 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024946#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024947 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024948#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024949 case 'D': type = VAR_DICT; break;
24950 case 'L': type = VAR_LIST; break;
24951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952
24953 tab = vim_strchr(tab, '\t');
24954 if (tab != NULL)
24955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024956 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024957 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024958 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024960#ifdef FEAT_FLOAT
24961 else if (type == VAR_FLOAT)
24962 (void)string2float(tab + 1, &tv.vval.v_float);
24963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024965 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024966 if (type == VAR_DICT || type == VAR_LIST)
24967 {
24968 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24969
24970 if (etv == NULL)
24971 /* Failed to parse back the dict or list, use it as a
24972 * string. */
24973 tv.v_type = VAR_STRING;
24974 else
24975 {
24976 vim_free(tv.vval.v_string);
24977 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024978 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024979 }
24980 }
24981
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024982 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024983
24984 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024985 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024986 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24987 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 }
24989 }
24990 }
24991
24992 return viminfo_readline(virp);
24993}
24994
24995/*
24996 * Write global vars that start with a capital to the viminfo file
24997 */
24998 void
24999write_viminfo_varlist(fp)
25000 FILE *fp;
25001{
Bram Moolenaar33570922005-01-25 22:26:29 +000025002 hashitem_T *hi;
25003 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025004 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025005 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025006 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025007 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025008 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009
25010 if (find_viminfo_parameter('!') == NULL)
25011 return;
25012
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025013 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025014
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025015 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025016 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025018 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025020 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025021 this_var = HI2DI(hi);
25022 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025023 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025024 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025025 {
25026 case VAR_STRING: s = "STR"; break;
25027 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025028#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025029 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025030#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025031 case VAR_DICT: s = "DIC"; break;
25032 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025033 default: continue;
25034 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025035 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025036 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025037 if (p != NULL)
25038 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025039 vim_free(tofree);
25040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 }
25042 }
25043}
25044#endif
25045
25046#if defined(FEAT_SESSION) || defined(PROTO)
25047 int
25048store_session_globals(fd)
25049 FILE *fd;
25050{
Bram Moolenaar33570922005-01-25 22:26:29 +000025051 hashitem_T *hi;
25052 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025053 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 char_u *p, *t;
25055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025056 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025057 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025059 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025061 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025062 this_var = HI2DI(hi);
25063 if ((this_var->di_tv.v_type == VAR_NUMBER
25064 || this_var->di_tv.v_type == VAR_STRING)
25065 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025066 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025067 /* Escape special characters with a backslash. Turn a LF and
25068 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025069 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025070 (char_u *)"\\\"\n\r");
25071 if (p == NULL) /* out of memory */
25072 break;
25073 for (t = p; *t != NUL; ++t)
25074 if (*t == '\n')
25075 *t = 'n';
25076 else if (*t == '\r')
25077 *t = 'r';
25078 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025079 this_var->di_key,
25080 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25081 : ' ',
25082 p,
25083 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25084 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025085 || put_eol(fd) == FAIL)
25086 {
25087 vim_free(p);
25088 return FAIL;
25089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090 vim_free(p);
25091 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025092#ifdef FEAT_FLOAT
25093 else if (this_var->di_tv.v_type == VAR_FLOAT
25094 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25095 {
25096 float_T f = this_var->di_tv.vval.v_float;
25097 int sign = ' ';
25098
25099 if (f < 0)
25100 {
25101 f = -f;
25102 sign = '-';
25103 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025104 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025105 this_var->di_key, sign, f) < 0)
25106 || put_eol(fd) == FAIL)
25107 return FAIL;
25108 }
25109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 }
25111 }
25112 return OK;
25113}
25114#endif
25115
Bram Moolenaar661b1822005-07-28 22:36:45 +000025116/*
25117 * Display script name where an item was last set.
25118 * Should only be invoked when 'verbose' is non-zero.
25119 */
25120 void
25121last_set_msg(scriptID)
25122 scid_T scriptID;
25123{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025124 char_u *p;
25125
Bram Moolenaar661b1822005-07-28 22:36:45 +000025126 if (scriptID != 0)
25127 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025128 p = home_replace_save(NULL, get_scriptname(scriptID));
25129 if (p != NULL)
25130 {
25131 verbose_enter();
25132 MSG_PUTS(_("\n\tLast set from "));
25133 MSG_PUTS(p);
25134 vim_free(p);
25135 verbose_leave();
25136 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025137 }
25138}
25139
Bram Moolenaard812df62008-11-09 12:46:09 +000025140/*
25141 * List v:oldfiles in a nice way.
25142 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025143 void
25144ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025145 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025146{
25147 list_T *l = vimvars[VV_OLDFILES].vv_list;
25148 listitem_T *li;
25149 int nr = 0;
25150
25151 if (l == NULL)
25152 msg((char_u *)_("No old files"));
25153 else
25154 {
25155 msg_start();
25156 msg_scroll = TRUE;
25157 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25158 {
25159 msg_outnum((long)++nr);
25160 MSG_PUTS(": ");
25161 msg_outtrans(get_tv_string(&li->li_tv));
25162 msg_putchar('\n');
25163 out_flush(); /* output one line at a time */
25164 ui_breakcheck();
25165 }
25166 /* Assume "got_int" was set to truncate the listing. */
25167 got_int = FALSE;
25168
25169#ifdef FEAT_BROWSE_CMD
25170 if (cmdmod.browse)
25171 {
25172 quit_more = FALSE;
25173 nr = prompt_for_number(FALSE);
25174 msg_starthere();
25175 if (nr > 0)
25176 {
25177 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25178 (long)nr);
25179
25180 if (p != NULL)
25181 {
25182 p = expand_env_save(p);
25183 eap->arg = p;
25184 eap->cmdidx = CMD_edit;
25185 cmdmod.browse = FALSE;
25186 do_exedit(eap, NULL);
25187 vim_free(p);
25188 }
25189 }
25190 }
25191#endif
25192 }
25193}
25194
Bram Moolenaar53744302015-07-17 17:38:22 +020025195/* reset v:option_new, v:option_old and v:option_type */
25196 void
25197reset_v_option_vars()
25198{
25199 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25200 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25201 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25202}
25203
25204
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205#endif /* FEAT_EVAL */
25206
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025208#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209
25210#ifdef WIN3264
25211/*
25212 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25213 */
25214static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25215static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25216static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25217
25218/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025219 * Get the short path (8.3) for the filename in "fnamep".
25220 * Only works for a valid file name.
25221 * When the path gets longer "fnamep" is changed and the allocated buffer
25222 * is put in "bufp".
25223 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25224 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 */
25226 static int
25227get_short_pathname(fnamep, bufp, fnamelen)
25228 char_u **fnamep;
25229 char_u **bufp;
25230 int *fnamelen;
25231{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025232 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 char_u *newbuf;
25234
25235 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 l = GetShortPathName(*fnamep, *fnamep, len);
25237 if (l > len - 1)
25238 {
25239 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241 newbuf = vim_strnsave(*fnamep, l);
25242 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244
25245 vim_free(*bufp);
25246 *fnamep = *bufp = newbuf;
25247
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025248 /* Really should always succeed, as the buffer is big enough. */
25249 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250 }
25251
25252 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025253 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254}
25255
25256/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025257 * Get the short path (8.3) for the filename in "fname". The converted
25258 * path is returned in "bufp".
25259 *
25260 * Some of the directories specified in "fname" may not exist. This function
25261 * will shorten the existing directories at the beginning of the path and then
25262 * append the remaining non-existing path.
25263 *
25264 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025265 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025266 * bufp - Pointer to an allocated buffer for the filename.
25267 * fnamelen - Length of the filename pointed to by fname
25268 *
25269 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 */
25271 static int
25272shortpath_for_invalid_fname(fname, bufp, fnamelen)
25273 char_u **fname;
25274 char_u **bufp;
25275 int *fnamelen;
25276{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025277 char_u *short_fname, *save_fname, *pbuf_unused;
25278 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025280 int old_len, len;
25281 int new_len, sfx_len;
25282 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283
25284 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025285 old_len = *fnamelen;
25286 save_fname = vim_strnsave(*fname, old_len);
25287 pbuf_unused = NULL;
25288 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025290 endp = save_fname + old_len - 1; /* Find the end of the copy */
25291 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025293 /*
25294 * Try shortening the supplied path till it succeeds by removing one
25295 * directory at a time from the tail of the path.
25296 */
25297 len = 0;
25298 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025300 /* go back one path-separator */
25301 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25302 --endp;
25303 if (endp <= save_fname)
25304 break; /* processed the complete path */
25305
25306 /*
25307 * Replace the path separator with a NUL and try to shorten the
25308 * resulting path.
25309 */
25310 ch = *endp;
25311 *endp = 0;
25312 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025313 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025314 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25315 {
25316 retval = FAIL;
25317 goto theend;
25318 }
25319 *endp = ch; /* preserve the string */
25320
25321 if (len > 0)
25322 break; /* successfully shortened the path */
25323
25324 /* failed to shorten the path. Skip the path separator */
25325 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326 }
25327
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025328 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025330 /*
25331 * Succeeded in shortening the path. Now concatenate the shortened
25332 * path with the remaining path at the tail.
25333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025335 /* Compute the length of the new path. */
25336 sfx_len = (int)(save_endp - endp) + 1;
25337 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025339 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025341 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025343 /* There is not enough space in the currently allocated string,
25344 * copy it to a buffer big enough. */
25345 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025347 {
25348 retval = FAIL;
25349 goto theend;
25350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351 }
25352 else
25353 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025354 /* Transfer short_fname to the main buffer (it's big enough),
25355 * unless get_short_pathname() did its work in-place. */
25356 *fname = *bufp = save_fname;
25357 if (short_fname != save_fname)
25358 vim_strncpy(save_fname, short_fname, len);
25359 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025361
25362 /* concat the not-shortened part of the path */
25363 vim_strncpy(*fname + len, endp, sfx_len);
25364 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025366
25367theend:
25368 vim_free(pbuf_unused);
25369 vim_free(save_fname);
25370
25371 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372}
25373
25374/*
25375 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025376 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 */
25378 static int
25379shortpath_for_partial(fnamep, bufp, fnamelen)
25380 char_u **fnamep;
25381 char_u **bufp;
25382 int *fnamelen;
25383{
25384 int sepcount, len, tflen;
25385 char_u *p;
25386 char_u *pbuf, *tfname;
25387 int hasTilde;
25388
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025389 /* Count up the path separators from the RHS.. so we know which part
25390 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025392 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025393 if (vim_ispathsep(*p))
25394 ++sepcount;
25395
25396 /* Need full path first (use expand_env() to remove a "~/") */
25397 hasTilde = (**fnamep == '~');
25398 if (hasTilde)
25399 pbuf = tfname = expand_env_save(*fnamep);
25400 else
25401 pbuf = tfname = FullName_save(*fnamep, FALSE);
25402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025403 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025405 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407
25408 if (len == 0)
25409 {
25410 /* Don't have a valid filename, so shorten the rest of the
25411 * path if we can. This CAN give us invalid 8.3 filenames, but
25412 * there's not a lot of point in guessing what it might be.
25413 */
25414 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025415 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25416 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 }
25418
25419 /* Count the paths backward to find the beginning of the desired string. */
25420 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025421 {
25422#ifdef FEAT_MBYTE
25423 if (has_mbyte)
25424 p -= mb_head_off(tfname, p);
25425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 if (vim_ispathsep(*p))
25427 {
25428 if (sepcount == 0 || (hasTilde && sepcount == 1))
25429 break;
25430 else
25431 sepcount --;
25432 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 if (hasTilde)
25435 {
25436 --p;
25437 if (p >= tfname)
25438 *p = '~';
25439 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 }
25442 else
25443 ++p;
25444
25445 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25446 vim_free(*bufp);
25447 *fnamelen = (int)STRLEN(p);
25448 *bufp = pbuf;
25449 *fnamep = p;
25450
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025451 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452}
25453#endif /* WIN3264 */
25454
25455/*
25456 * Adjust a filename, according to a string of modifiers.
25457 * *fnamep must be NUL terminated when called. When returning, the length is
25458 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025459 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 * When there is an error, *fnamep is set to NULL.
25461 */
25462 int
25463modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25464 char_u *src; /* string with modifiers */
25465 int *usedlen; /* characters after src that are used */
25466 char_u **fnamep; /* file name so far */
25467 char_u **bufp; /* buffer for allocated file name or NULL */
25468 int *fnamelen; /* length of fnamep */
25469{
25470 int valid = 0;
25471 char_u *tail;
25472 char_u *s, *p, *pbuf;
25473 char_u dirname[MAXPATHL];
25474 int c;
25475 int has_fullname = 0;
25476#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025477 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478 int has_shortname = 0;
25479#endif
25480
25481repeat:
25482 /* ":p" - full path/file_name */
25483 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25484 {
25485 has_fullname = 1;
25486
25487 valid |= VALID_PATH;
25488 *usedlen += 2;
25489
25490 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25491 if ((*fnamep)[0] == '~'
25492#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25493 && ((*fnamep)[1] == '/'
25494# ifdef BACKSLASH_IN_FILENAME
25495 || (*fnamep)[1] == '\\'
25496# endif
25497 || (*fnamep)[1] == NUL)
25498
25499#endif
25500 )
25501 {
25502 *fnamep = expand_env_save(*fnamep);
25503 vim_free(*bufp); /* free any allocated file name */
25504 *bufp = *fnamep;
25505 if (*fnamep == NULL)
25506 return -1;
25507 }
25508
25509 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025510 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 {
25512 if (vim_ispathsep(*p)
25513 && p[1] == '.'
25514 && (p[2] == NUL
25515 || vim_ispathsep(p[2])
25516 || (p[2] == '.'
25517 && (p[3] == NUL || vim_ispathsep(p[3])))))
25518 break;
25519 }
25520
25521 /* FullName_save() is slow, don't use it when not needed. */
25522 if (*p != NUL || !vim_isAbsName(*fnamep))
25523 {
25524 *fnamep = FullName_save(*fnamep, *p != NUL);
25525 vim_free(*bufp); /* free any allocated file name */
25526 *bufp = *fnamep;
25527 if (*fnamep == NULL)
25528 return -1;
25529 }
25530
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025531#ifdef WIN3264
25532# if _WIN32_WINNT >= 0x0500
25533 if (vim_strchr(*fnamep, '~') != NULL)
25534 {
25535 /* Expand 8.3 filename to full path. Needed to make sure the same
25536 * file does not have two different names.
25537 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25538 p = alloc(_MAX_PATH + 1);
25539 if (p != NULL)
25540 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025541 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025542 {
25543 vim_free(*bufp);
25544 *bufp = *fnamep = p;
25545 }
25546 else
25547 vim_free(p);
25548 }
25549 }
25550# endif
25551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 /* Append a path separator to a directory. */
25553 if (mch_isdir(*fnamep))
25554 {
25555 /* Make room for one or two extra characters. */
25556 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25557 vim_free(*bufp); /* free any allocated file name */
25558 *bufp = *fnamep;
25559 if (*fnamep == NULL)
25560 return -1;
25561 add_pathsep(*fnamep);
25562 }
25563 }
25564
25565 /* ":." - path relative to the current directory */
25566 /* ":~" - path relative to the home directory */
25567 /* ":8" - shortname path - postponed till after */
25568 while (src[*usedlen] == ':'
25569 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25570 {
25571 *usedlen += 2;
25572 if (c == '8')
25573 {
25574#ifdef WIN3264
25575 has_shortname = 1; /* Postpone this. */
25576#endif
25577 continue;
25578 }
25579 pbuf = NULL;
25580 /* Need full path first (use expand_env() to remove a "~/") */
25581 if (!has_fullname)
25582 {
25583 if (c == '.' && **fnamep == '~')
25584 p = pbuf = expand_env_save(*fnamep);
25585 else
25586 p = pbuf = FullName_save(*fnamep, FALSE);
25587 }
25588 else
25589 p = *fnamep;
25590
25591 has_fullname = 0;
25592
25593 if (p != NULL)
25594 {
25595 if (c == '.')
25596 {
25597 mch_dirname(dirname, MAXPATHL);
25598 s = shorten_fname(p, dirname);
25599 if (s != NULL)
25600 {
25601 *fnamep = s;
25602 if (pbuf != NULL)
25603 {
25604 vim_free(*bufp); /* free any allocated file name */
25605 *bufp = pbuf;
25606 pbuf = NULL;
25607 }
25608 }
25609 }
25610 else
25611 {
25612 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25613 /* Only replace it when it starts with '~' */
25614 if (*dirname == '~')
25615 {
25616 s = vim_strsave(dirname);
25617 if (s != NULL)
25618 {
25619 *fnamep = s;
25620 vim_free(*bufp);
25621 *bufp = s;
25622 }
25623 }
25624 }
25625 vim_free(pbuf);
25626 }
25627 }
25628
25629 tail = gettail(*fnamep);
25630 *fnamelen = (int)STRLEN(*fnamep);
25631
25632 /* ":h" - head, remove "/file_name", can be repeated */
25633 /* Don't remove the first "/" or "c:\" */
25634 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25635 {
25636 valid |= VALID_HEAD;
25637 *usedlen += 2;
25638 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025639 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025640 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 *fnamelen = (int)(tail - *fnamep);
25642#ifdef VMS
25643 if (*fnamelen > 0)
25644 *fnamelen += 1; /* the path separator is part of the path */
25645#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025646 if (*fnamelen == 0)
25647 {
25648 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25649 p = vim_strsave((char_u *)".");
25650 if (p == NULL)
25651 return -1;
25652 vim_free(*bufp);
25653 *bufp = *fnamep = tail = p;
25654 *fnamelen = 1;
25655 }
25656 else
25657 {
25658 while (tail > s && !after_pathsep(s, tail))
25659 mb_ptr_back(*fnamep, tail);
25660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 }
25662
25663 /* ":8" - shortname */
25664 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25665 {
25666 *usedlen += 2;
25667#ifdef WIN3264
25668 has_shortname = 1;
25669#endif
25670 }
25671
25672#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025673 /*
25674 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 */
25676 if (has_shortname)
25677 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025678 /* Copy the string if it is shortened by :h and when it wasn't copied
25679 * yet, because we are going to change it in place. Avoids changing
25680 * the buffer name for "%:8". */
25681 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 {
25683 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025684 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 return -1;
25686 vim_free(*bufp);
25687 *bufp = *fnamep = p;
25688 }
25689
25690 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025691 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 if (!has_fullname && !vim_isAbsName(*fnamep))
25693 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025694 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695 return -1;
25696 }
25697 else
25698 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025699 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700
Bram Moolenaardc935552011-08-17 15:23:23 +020025701 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025702 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025703 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 return -1;
25705
25706 if (l == 0)
25707 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025708 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025709 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025710 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711 return -1;
25712 }
25713 *fnamelen = l;
25714 }
25715 }
25716#endif /* WIN3264 */
25717
25718 /* ":t" - tail, just the basename */
25719 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25720 {
25721 *usedlen += 2;
25722 *fnamelen -= (int)(tail - *fnamep);
25723 *fnamep = tail;
25724 }
25725
25726 /* ":e" - extension, can be repeated */
25727 /* ":r" - root, without extension, can be repeated */
25728 while (src[*usedlen] == ':'
25729 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25730 {
25731 /* find a '.' in the tail:
25732 * - for second :e: before the current fname
25733 * - otherwise: The last '.'
25734 */
25735 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25736 s = *fnamep - 2;
25737 else
25738 s = *fnamep + *fnamelen - 1;
25739 for ( ; s > tail; --s)
25740 if (s[0] == '.')
25741 break;
25742 if (src[*usedlen + 1] == 'e') /* :e */
25743 {
25744 if (s > tail)
25745 {
25746 *fnamelen += (int)(*fnamep - (s + 1));
25747 *fnamep = s + 1;
25748#ifdef VMS
25749 /* cut version from the extension */
25750 s = *fnamep + *fnamelen - 1;
25751 for ( ; s > *fnamep; --s)
25752 if (s[0] == ';')
25753 break;
25754 if (s > *fnamep)
25755 *fnamelen = s - *fnamep;
25756#endif
25757 }
25758 else if (*fnamep <= tail)
25759 *fnamelen = 0;
25760 }
25761 else /* :r */
25762 {
25763 if (s > tail) /* remove one extension */
25764 *fnamelen = (int)(s - *fnamep);
25765 }
25766 *usedlen += 2;
25767 }
25768
25769 /* ":s?pat?foo?" - substitute */
25770 /* ":gs?pat?foo?" - global substitute */
25771 if (src[*usedlen] == ':'
25772 && (src[*usedlen + 1] == 's'
25773 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25774 {
25775 char_u *str;
25776 char_u *pat;
25777 char_u *sub;
25778 int sep;
25779 char_u *flags;
25780 int didit = FALSE;
25781
25782 flags = (char_u *)"";
25783 s = src + *usedlen + 2;
25784 if (src[*usedlen + 1] == 'g')
25785 {
25786 flags = (char_u *)"g";
25787 ++s;
25788 }
25789
25790 sep = *s++;
25791 if (sep)
25792 {
25793 /* find end of pattern */
25794 p = vim_strchr(s, sep);
25795 if (p != NULL)
25796 {
25797 pat = vim_strnsave(s, (int)(p - s));
25798 if (pat != NULL)
25799 {
25800 s = p + 1;
25801 /* find end of substitution */
25802 p = vim_strchr(s, sep);
25803 if (p != NULL)
25804 {
25805 sub = vim_strnsave(s, (int)(p - s));
25806 str = vim_strnsave(*fnamep, *fnamelen);
25807 if (sub != NULL && str != NULL)
25808 {
25809 *usedlen = (int)(p + 1 - src);
25810 s = do_string_sub(str, pat, sub, flags);
25811 if (s != NULL)
25812 {
25813 *fnamep = s;
25814 *fnamelen = (int)STRLEN(s);
25815 vim_free(*bufp);
25816 *bufp = s;
25817 didit = TRUE;
25818 }
25819 }
25820 vim_free(sub);
25821 vim_free(str);
25822 }
25823 vim_free(pat);
25824 }
25825 }
25826 /* after using ":s", repeat all the modifiers */
25827 if (didit)
25828 goto repeat;
25829 }
25830 }
25831
Bram Moolenaar26df0922014-02-23 23:39:13 +010025832 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25833 {
25834 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25835 if (p == NULL)
25836 return -1;
25837 vim_free(*bufp);
25838 *bufp = *fnamep = p;
25839 *fnamelen = (int)STRLEN(p);
25840 *usedlen += 2;
25841 }
25842
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843 return valid;
25844}
25845
25846/*
25847 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25848 * "flags" can be "g" to do a global substitute.
25849 * Returns an allocated string, NULL for error.
25850 */
25851 char_u *
25852do_string_sub(str, pat, sub, flags)
25853 char_u *str;
25854 char_u *pat;
25855 char_u *sub;
25856 char_u *flags;
25857{
25858 int sublen;
25859 regmatch_T regmatch;
25860 int i;
25861 int do_all;
25862 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025863 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 garray_T ga;
25865 char_u *ret;
25866 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025867 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868
25869 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25870 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025871 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872
25873 ga_init2(&ga, 1, 200);
25874
25875 do_all = (flags[0] == 'g');
25876
25877 regmatch.rm_ic = p_ic;
25878 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25879 if (regmatch.regprog != NULL)
25880 {
25881 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025882 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025883 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25884 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025885 /* Skip empty match except for first match. */
25886 if (regmatch.startp[0] == regmatch.endp[0])
25887 {
25888 if (zero_width == regmatch.startp[0])
25889 {
25890 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025891 i = MB_PTR2LEN(tail);
25892 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25893 (size_t)i);
25894 ga.ga_len += i;
25895 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025896 continue;
25897 }
25898 zero_width = regmatch.startp[0];
25899 }
25900
Bram Moolenaar071d4272004-06-13 20:20:40 +000025901 /*
25902 * Get some space for a temporary buffer to do the substitution
25903 * into. It will contain:
25904 * - The text up to where the match is.
25905 * - The substituted text.
25906 * - The text after the match.
25907 */
25908 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025909 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25911 {
25912 ga_clear(&ga);
25913 break;
25914 }
25915
25916 /* copy the text up to where the match is */
25917 i = (int)(regmatch.startp[0] - tail);
25918 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25919 /* add the substituted text */
25920 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25921 + ga.ga_len + i, TRUE, TRUE, FALSE);
25922 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025923 tail = regmatch.endp[0];
25924 if (*tail == NUL)
25925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 if (!do_all)
25927 break;
25928 }
25929
25930 if (ga.ga_data != NULL)
25931 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25932
Bram Moolenaar473de612013-06-08 18:19:48 +020025933 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 }
25935
25936 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25937 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025938 if (p_cpo == empty_option)
25939 p_cpo = save_cpo;
25940 else
25941 /* Darn, evaluating {sub} expression changed the value. */
25942 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943
25944 return ret;
25945}
25946
25947#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */