blob: 8250e84342077c3afe66e92077ca6fa391cfd744 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000371};
372
373/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_type vv_di.di_tv.v_type
375#define vv_nr vv_di.di_tv.vval.v_number
376#define vv_float vv_di.di_tv.vval.v_float
377#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000378#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200379#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000381
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200382static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000383#define vimvarht vimvardict.dv_hashtab
384
Bram Moolenaara40058a2005-07-11 22:42:07 +0000385static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
386static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
388static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
389static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
391static void list_glob_vars __ARGS((int *first));
392static void list_buf_vars __ARGS((int *first));
393static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000395static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_vim_vars __ARGS((int *first));
398static void list_script_vars __ARGS((int *first));
399static void list_func_vars __ARGS((int *first));
400static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000401static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
402static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100403static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404static void clear_lval __ARGS((lval_T *lp));
405static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
406static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
408static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
409static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
410static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
411static void item_lock __ARGS((typval_T *tv, int deep, int lock));
412static int tv_islocked __ARGS((typval_T *tv));
413
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
415static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000420static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
421static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000423static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000428static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100430static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
431static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
432static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000435static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100440static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000442static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200443static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100458static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200460static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200473static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100489static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100491static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200510static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
515static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200525static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200527#ifdef FEAT_FLOAT
528static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
529#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000532static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
539static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000543static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000552static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000554static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200558static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000561static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200562static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200572static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000573static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000574static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200577static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000578static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100584static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000587static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000601static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100606static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200624#ifdef FEAT_LUA
625static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000631static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200632static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100649static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200657#ifdef FEAT_PYTHON3
658static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
660#ifdef FEAT_PYTHON
661static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000665static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#ifdef FEAT_FLOAT
678static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
679#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200680static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100682static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000685static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000687static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200692static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000695static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000696static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000697static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000698static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200700static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000701static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100703#ifdef FEAT_CRYPT
704static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
705#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000706static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200707static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
710static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200711static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000712#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000714static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000715static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
719static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
721#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000722static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200723static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724#ifdef HAVE_STRFTIME
725static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
727static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200733static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200734static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000740static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200741static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200743static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000744static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000745static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000746static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000747static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000748static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000750static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200751#ifdef FEAT_FLOAT
752static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
754#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#ifdef FEAT_FLOAT
759static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200762static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200763static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100764static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100768static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000775static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100779static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780
Bram Moolenaar493c1782014-05-28 14:34:46 +0200781static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000782static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static int get_env_len __ARGS((char_u **arg));
784static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000786static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
787#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
788#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
789 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000790static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200793static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000794static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static typval_T *alloc_tv __ARGS((void));
796static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void init_tv __ARGS((typval_T *varp));
798static long get_tv_number __ARGS((typval_T *varp));
799static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000800static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static char_u *get_tv_string __ARGS((typval_T *varp));
802static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000803static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100804static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
805static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000806static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
807static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
808static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000809static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
810static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200812static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
813static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200814static int var_check_func_name __ARGS((char_u *name, int new_var));
815static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000817static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
819static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
820static int eval_fname_script __ARGS((char_u *p));
821static int eval_fname_sid __ARGS((char_u *p));
822static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static ufunc_T *find_func __ARGS((char_u *name));
824static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200825static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
827static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000828static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
829static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
830static int
831# ifdef __BORLANDC__
832 _RTLENTRYF
833# endif
834 prof_total_cmp __ARGS((const void *s1, const void *s2));
835static int
836# ifdef __BORLANDC__
837 _RTLENTRYF
838# endif
839 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200841static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000842static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000843static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000844static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000845static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000846static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
847static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000849static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
850static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000851static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000852static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200854static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200855static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200857
858#ifdef EBCDIC
859static int compare_func_name __ARGS((const void *s1, const void *s2));
860static void sortFunctions __ARGS(());
861#endif
862
Bram Moolenaar33570922005-01-25 22:26:29 +0000863/*
864 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000865 */
866 void
867eval_init()
868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 int i;
870 struct vimvar *p;
871
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200872 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
873 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200874 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
878 for (i = 0; i < VV_LEN; ++i)
879 {
880 p = &vimvars[i];
881 STRCPY(p->vv_di.di_key, p->vv_name);
882 if (p->vv_flags & VV_RO)
883 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
884 else if (p->vv_flags & VV_RO_SBX)
885 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
886 else
887 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000888
889 /* add to v: scope dict, unless the value is not always available */
890 if (p->vv_type != VAR_UNKNOWN)
891 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000893 /* add to compat scope dict */
894 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000896 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100897 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200898 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200899 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
902 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100903 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904 */
905 sortFunctions();
906#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000907}
908
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909#if defined(EXITFREE) || defined(PROTO)
910 void
911eval_clear()
912{
913 int i;
914 struct vimvar *p;
915
916 for (i = 0; i < VV_LEN; ++i)
917 {
918 p = &vimvars[i];
919 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000921 vim_free(p->vv_str);
922 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000923 }
924 else if (p->vv_di.di_tv.v_type == VAR_LIST)
925 {
926 list_unref(p->vv_list);
927 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000928 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929 }
930 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000931 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932 hash_clear(&compat_hashtab);
933
Bram Moolenaard9fba312005-06-26 22:34:35 +0000934 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200936 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100937# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938
939 /* global variables */
940 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000941
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000942 /* autoloaded script names */
943 ga_clear_strings(&ga_loaded);
944
Bram Moolenaarcca74132013-09-25 21:00:28 +0200945 /* Script-local variables. First clear all the variables and in a second
946 * loop free the scriptvar_T, because a variable in one script might hold
947 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200948 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200949 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200950 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200951 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200952 ga_clear(&ga_scripts);
953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000954 /* unreferenced lists and dicts */
955 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000956
957 /* functions */
958 free_all_functions();
959 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000960}
961#endif
962
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 * Return the name of the executed function.
965 */
966 char_u *
967func_name(cookie)
968 void *cookie;
969{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000970 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Return the address holding the next breakpoint line for a funccall cookie.
975 */
976 linenr_T *
977func_breakpoint(cookie)
978 void *cookie;
979{
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983/*
984 * Return the address holding the debug tick for a funccall cookie.
985 */
986 int *
987func_dbg_tick(cookie)
988 void *cookie;
989{
Bram Moolenaar33570922005-01-25 22:26:29 +0000990 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
993/*
994 * Return the nesting level for a funccall cookie.
995 */
996 int
997func_level(cookie)
998 void *cookie;
999{
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001004funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001006/* pointer to list of previously used funccal, still around because some
1007 * item in it is still being used. */
1008funccall_T *previous_funccal = NULL;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/*
1011 * Return TRUE when a function was ended by a ":return" command.
1012 */
1013 int
1014current_func_returned()
1015{
1016 return current_funccal->returned;
1017}
1018
1019
1020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Set an internal variable to a string value. Creates the variable if it does
1022 * not already exist.
1023 */
1024 void
1025set_internal_string_var(name, value)
1026 char_u *name;
1027 char_u *value;
1028{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001029 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 val = vim_strsave(value);
1033 if (val != NULL)
1034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001038 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 }
1042}
1043
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001045static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046static char_u *redir_endp = NULL;
1047static char_u *redir_varname = NULL;
1048
1049/*
1050 * Start recording command output to a variable
1051 * Returns OK if successfully completed the setup. FAIL otherwise.
1052 */
1053 int
1054var_redir_start(name, append)
1055 char_u *name;
1056 int append; /* append to an existing variable */
1057{
1058 int save_emsg;
1059 int err;
1060 typval_T tv;
1061
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001063 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 {
1065 EMSG(_(e_invarg));
1066 return FAIL;
1067 }
1068
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 redir_varname = vim_strsave(name);
1071 if (redir_varname == NULL)
1072 return FAIL;
1073
1074 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1075 if (redir_lval == NULL)
1076 {
1077 var_redir_stop();
1078 return FAIL;
1079 }
1080
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001081 /* The output is stored in growarray "redir_ga" until redirection ends. */
1082 ga_init2(&redir_ga, (int)sizeof(char), 500);
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001085 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1088 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_endp != NULL && *redir_endp != NUL)
1091 /* Trailing characters are present after the variable name */
1092 EMSG(_(e_trailing));
1093 else
1094 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 var_redir_stop();
1097 return FAIL;
1098 }
1099
1100 /* check if we can write to the variable: set it to or append an empty
1101 * string */
1102 save_emsg = did_emsg;
1103 did_emsg = FALSE;
1104 tv.v_type = VAR_STRING;
1105 tv.vval.v_string = (char_u *)"";
1106 if (append)
1107 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1108 else
1109 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001110 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001112 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (err)
1114 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 var_redir_stop();
1117 return FAIL;
1118 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
1120 return OK;
1121}
1122
1123/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 * Append "value[value_len]" to the variable set by var_redir_start().
1125 * The actual appending is postponed until redirection ends, because the value
1126 * appended may in fact be the string we write to, changing it may cause freed
1127 * memory to be used:
1128 * :redir => foo
1129 * :let foo
1130 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
1139 if (redir_lval == NULL)
1140 return;
1141
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001147 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 {
1149 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 }
1152 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154}
1155
1156/*
1157 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 */
1160 void
1161var_redir_stop()
1162{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 typval_T tv;
1164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 if (redir_lval != NULL)
1166 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* If there was no error: assign the text to the variable. */
1168 if (redir_endp != NULL)
1169 {
1170 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1171 tv.v_type = VAR_STRING;
1172 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001173 /* Call get_lval() again, if it's inside a Dict or List it may
1174 * have changed. */
1175 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1178 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1179 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001181
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 /* free the collected output */
1183 vim_free(redir_ga.ga_data);
1184 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 vim_free(redir_lval);
1187 redir_lval = NULL;
1188 }
1189 vim_free(redir_varname);
1190 redir_varname = NULL;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193# if defined(FEAT_MBYTE) || defined(PROTO)
1194 int
1195eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1196 char_u *enc_from;
1197 char_u *enc_to;
1198 char_u *fname_from;
1199 char_u *fname_to;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1204 set_vim_var_string(VV_CC_TO, enc_to, -1);
1205 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1206 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1207 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_CC_FROM, NULL, -1);
1210 set_vim_var_string(VV_CC_TO, NULL, -1);
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1213
1214 if (err)
1215 return FAIL;
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1221 int
1222eval_printexpr(fname, args)
1223 char_u *fname;
1224 char_u *args;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, fname, -1);
1229 set_vim_var_string(VV_CMDARG, args, -1);
1230 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1231 err = TRUE;
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_CMDARG, NULL, -1);
1234
1235 if (err)
1236 {
1237 mch_remove(fname);
1238 return FAIL;
1239 }
1240 return OK;
1241}
1242# endif
1243
1244# if defined(FEAT_DIFF) || defined(PROTO)
1245 void
1246eval_diff(origfile, newfile, outfile)
1247 char_u *origfile;
1248 char_u *newfile;
1249 char_u *outfile;
1250{
1251 int err = FALSE;
1252
1253 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1254 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1255 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1256 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260}
1261
1262 void
1263eval_patch(origfile, difffile, outfile)
1264 char_u *origfile;
1265 char_u *difffile;
1266 char_u *outfile;
1267{
1268 int err;
1269
1270 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1271 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1272 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1273 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1276 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1277}
1278# endif
1279
1280/*
1281 * Top level evaluation function, returning a boolean.
1282 * Sets "error" to TRUE if there was an error.
1283 * Return TRUE or FALSE.
1284 */
1285 int
1286eval_to_bool(arg, error, nextcmd, skip)
1287 char_u *arg;
1288 int *error;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int retval = FALSE;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 else
1300 {
1301 *error = FALSE;
1302 if (!skip)
1303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001304 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 }
1308 if (skip)
1309 --emsg_skip;
1310
1311 return retval;
1312}
1313
1314/*
1315 * Top level evaluation function, returning a string. If "skip" is TRUE,
1316 * only parsing to "nextcmd" is done, without reporting errors. Return
1317 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1318 */
1319 char_u *
1320eval_to_string_skip(arg, nextcmd, skip)
1321 char_u *arg;
1322 char_u **nextcmd;
1323 int skip; /* only parse, don't execute */
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *retval;
1327
1328 if (skip)
1329 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 retval = NULL;
1332 else
1333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 retval = vim_strsave(get_tv_string(&tv));
1335 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 if (skip)
1338 --emsg_skip;
1339
1340 return retval;
1341}
1342
1343/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344 * Skip over an expression at "*pp".
1345 * Return FAIL for an error, OK otherwise.
1346 */
1347 int
1348skip_expr(pp)
1349 char_u **pp;
1350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352
1353 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001355}
1356
1357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 * When "convert" is TRUE convert a List into a sequence of lines and convert
1360 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 * Return pointer to allocated memory, or NULL for failure.
1362 */
1363 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *arg;
1366 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001372#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 {
1382 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001383 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001384 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001385 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001386 if (tv.vval.v_list->lv_len > 0)
1387 ga_append(&ga, NL);
1388 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 ga_append(&ga, NUL);
1390 retval = (char_u *)ga.ga_data;
1391 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001392#ifdef FEAT_FLOAT
1393 else if (convert && tv.v_type == VAR_FLOAT)
1394 {
1395 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1396 retval = vim_strsave(numbuf);
1397 }
1398#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 else
1400 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403
1404 return retval;
1405}
1406
1407/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001408 * Call eval_to_string() without using current local variables and using
1409 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 */
1411 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *arg;
1414 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 char_u *retval;
1418 void *save_funccalp;
1419
1420 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 if (use_sandbox)
1422 ++sandbox;
1423 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001425 if (use_sandbox)
1426 --sandbox;
1427 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 restore_funccal(save_funccalp);
1429 return retval;
1430}
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432/*
1433 * Top level evaluation function, returning a number.
1434 * Evaluates "expr" silently.
1435 * Returns -1 for an error.
1436 */
1437 int
1438eval_to_number(expr)
1439 char_u *expr;
1440{
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001443 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 ++emsg_off;
1446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = -1;
1449 else
1450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454 --emsg_off;
1455
1456 return retval;
1457}
1458
Bram Moolenaara40058a2005-07-11 22:42:07 +00001459/*
1460 * Prepare v: variable "idx" to be used.
1461 * Save the current typeval in "save_tv".
1462 * When not used yet add the variable to the v: hashtable.
1463 */
1464 static void
1465prepare_vimvar(idx, save_tv)
1466 int idx;
1467 typval_T *save_tv;
1468{
1469 *save_tv = vimvars[idx].vv_tv;
1470 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1471 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1472}
1473
1474/*
1475 * Restore v: variable "idx" to typeval "save_tv".
1476 * When no longer defined, remove the variable from the v: hashtable.
1477 */
1478 static void
1479restore_vimvar(idx, save_tv)
1480 int idx;
1481 typval_T *save_tv;
1482{
1483 hashitem_T *hi;
1484
Bram Moolenaara40058a2005-07-11 22:42:07 +00001485 vimvars[idx].vv_tv = *save_tv;
1486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1487 {
1488 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1489 if (HASHITEM_EMPTY(hi))
1490 EMSG2(_(e_intern2), "restore_vimvar()");
1491 else
1492 hash_remove(&vimvarht, hi);
1493 }
1494}
1495
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001496#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497/*
1498 * Evaluate an expression to a list with suggestions.
1499 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001500 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 */
1502 list_T *
1503eval_spell_expr(badword, expr)
1504 char_u *badword;
1505 char_u *expr;
1506{
1507 typval_T save_val;
1508 typval_T rettv;
1509 list_T *list = NULL;
1510 char_u *p = skipwhite(expr);
1511
1512 /* Set "v:val" to the bad word. */
1513 prepare_vimvar(VV_VAL, &save_val);
1514 vimvars[VV_VAL].vv_type = VAR_STRING;
1515 vimvars[VV_VAL].vv_str = badword;
1516 if (p_verbose == 0)
1517 ++emsg_off;
1518
1519 if (eval1(&p, &rettv, TRUE) == OK)
1520 {
1521 if (rettv.v_type != VAR_LIST)
1522 clear_tv(&rettv);
1523 else
1524 list = rettv.vval.v_list;
1525 }
1526
1527 if (p_verbose == 0)
1528 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001529 restore_vimvar(VV_VAL, &save_val);
1530
1531 return list;
1532}
1533
1534/*
1535 * "list" is supposed to contain two items: a word and a number. Return the
1536 * word in "pp" and the number as the return value.
1537 * Return -1 if anything isn't right.
1538 * Used to get the good word and score from the eval_spell_expr() result.
1539 */
1540 int
1541get_spellword(list, pp)
1542 list_T *list;
1543 char_u **pp;
1544{
1545 listitem_T *li;
1546
1547 li = list->lv_first;
1548 if (li == NULL)
1549 return -1;
1550 *pp = get_tv_string(&li->li_tv);
1551
1552 li = li->li_next;
1553 if (li == NULL)
1554 return -1;
1555 return get_tv_number(&li->li_tv);
1556}
1557#endif
1558
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001559/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 * Top level evaluation function.
1561 * Returns an allocated typval_T with the result.
1562 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563 */
1564 typval_T *
1565eval_expr(arg, nextcmd)
1566 char_u *arg;
1567 char_u **nextcmd;
1568{
1569 typval_T *tv;
1570
1571 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001572 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001573 {
1574 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001575 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001576 }
1577
1578 return tv;
1579}
1580
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 * Uses argv[argc] for the function arguments. Only Number and String
1585 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001588 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 char_u *func;
1591 int argc;
1592 char_u **argv;
1593 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar33570922005-01-25 22:26:29 +00001597 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 long n;
1599 int len;
1600 int i;
1601 int doesrange;
1602 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001605 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 for (i = 0; i < argc; i++)
1610 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001611 /* Pass a NULL or empty argument as an empty string */
1612 if (argv[i] == NULL || *argv[i] == NUL)
1613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001614 argvars[i].v_type = VAR_STRING;
1615 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001616 continue;
1617 }
1618
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001619 if (str_arg_only)
1620 len = 0;
1621 else
1622 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001623 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (len != 0 && len == (int)STRLEN(argv[i]))
1625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001626 argvars[i].v_type = VAR_NUMBER;
1627 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 else
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_STRING;
1632 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
1635
1636 if (safe)
1637 {
1638 save_funccalp = save_funccal();
1639 ++sandbox;
1640 }
1641
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1643 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (safe)
1647 {
1648 --sandbox;
1649 restore_funccal(save_funccalp);
1650 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 vim_free(argvars);
1652
1653 if (ret == FAIL)
1654 clear_tv(rettv);
1655
1656 return ret;
1657}
1658
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001659/*
1660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1676 return -1;
1677
1678 retval = get_tv_number_chk(&rettv, NULL);
1679 clear_tv(&rettv);
1680 return retval;
1681}
1682
1683#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1684 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1685
Bram Moolenaar4f688582007-07-24 12:34:30 +00001686# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001688 * Call vimL function "func" and return the result as a string.
1689 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 * Uses argv[argc] for the function arguments.
1691 */
1692 void *
1693call_func_retstr(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001700 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001702 /* All arguments are passed as strings, no conversion to number. */
1703 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001704 return NULL;
1705
1706 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 return retval;
1709}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001712/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001713 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001715 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 */
1717 void *
1718call_func_retlist(func, argc, argv, safe)
1719 char_u *func;
1720 int argc;
1721 char_u **argv;
1722 int safe; /* use the sandbox */
1723{
1724 typval_T rettv;
1725
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001726 /* All arguments are passed as strings, no conversion to number. */
1727 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 return NULL;
1729
1730 if (rettv.v_type != VAR_LIST)
1731 {
1732 clear_tv(&rettv);
1733 return NULL;
1734 }
1735
1736 return rettv.vval.v_list;
1737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
1739
1740/*
1741 * Save the current function call pointer, and set it to NULL.
1742 * Used when executing autocommands and for ":source".
1743 */
1744 void *
1745save_funccal()
1746{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 current_funccal = NULL;
1750 return (void *)fc;
1751}
1752
1753 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754restore_funccal(vfc)
1755 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757 funccall_T *fc = (funccall_T *)vfc;
1758
1759 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762#if defined(FEAT_PROFILE) || defined(PROTO)
1763/*
1764 * Prepare profiling for entering a child or something else that is not
1765 * counted for the script/function itself.
1766 * Should always be called in pair with prof_child_exit().
1767 */
1768 void
1769prof_child_enter(tm)
1770 proftime_T *tm; /* place to store waittime */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 profile_start(&fc->prof_child);
1776 script_prof_save(tm);
1777}
1778
1779/*
1780 * Take care of time spent in a child.
1781 * Should always be called after prof_child_enter().
1782 */
1783 void
1784prof_child_exit(tm)
1785 proftime_T *tm; /* where waittime was stored */
1786{
1787 funccall_T *fc = current_funccal;
1788
1789 if (fc != NULL && fc->func->uf_profiling)
1790 {
1791 profile_end(&fc->prof_child);
1792 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1793 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1794 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1795 }
1796 script_prof_restore(tm);
1797}
1798#endif
1799
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#ifdef FEAT_FOLDING
1802/*
1803 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1804 * it in "*cp". Doesn't give error messages.
1805 */
1806 int
1807eval_foldexpr(arg, cp)
1808 char_u *arg;
1809 int *cp;
1810{
Bram Moolenaar33570922005-01-25 22:26:29 +00001811 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 int retval;
1813 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001814 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1815 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 ++sandbox;
1820 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 retval = 0;
1824 else
1825 {
1826 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 if (tv.v_type == VAR_NUMBER)
1828 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001829 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a string, check if there is a non-digit before
1834 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (!VIM_ISDIGIT(*s) && *s != '-')
1837 *cp = *s++;
1838 retval = atol((char *)s);
1839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001843 if (use_sandbox)
1844 --sandbox;
1845 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 return retval;
1848}
1849#endif
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 * ":let" list all variable values
1853 * ":let var1 var2" list variable values
1854 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 * ":let var += expr" assignment command.
1856 * ":let var -= expr" assignment command.
1857 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
1860 void
1861ex_let(eap)
1862 exarg_T *eap;
1863{
1864 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 int var_count = 0;
1869 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001871 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 argend = skip_var_list(arg, &var_count, &semicolon);
1875 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001877 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1878 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001879 expr = skipwhite(argend);
1880 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1881 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001883 /*
1884 * ":let" without "=": list variables
1885 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 if (*arg == '[')
1887 EMSG(_(e_invarg));
1888 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001894 list_glob_vars(&first);
1895 list_buf_vars(&first);
1896 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001897#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001899#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_script_vars(&first);
1901 list_func_vars(&first);
1902 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 eap->nextcmd = check_nextcmd(arg);
1905 }
1906 else
1907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 op[0] = '=';
1909 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1913 op[0] = *expr; /* +=, -= or .= */
1914 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 else
1917 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (eap->skip)
1920 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 {
1924 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 --emsg_skip;
1927 }
1928 else if (i != FAIL)
1929 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 }
1935}
1936
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937/*
1938 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1939 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 * When "nextchars" is not NULL it points to a string with characters that
1941 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1942 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 * Returns OK or FAIL;
1944 */
1945 static int
1946ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1947 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 int copy; /* copy values from "tv", don't move */
1950 int semicolon; /* from skip_var_list() */
1951 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953{
1954 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 listitem_T *item;
1958 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959
1960 if (*arg != '[')
1961 {
1962 /*
1963 * ":let var = expr" or ":for var in list"
1964 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 return OK;
1968 }
1969
1970 /*
1971 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1972 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001973 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 {
1975 EMSG(_(e_listreq));
1976 return FAIL;
1977 }
1978
1979 i = list_len(l);
1980 if (semicolon == 0 && var_count < i)
1981 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001982 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return FAIL;
1984 }
1985 if (var_count - semicolon > i)
1986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001987 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 return FAIL;
1989 }
1990
1991 item = l->lv_first;
1992 while (*arg != ']')
1993 {
1994 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 item = item->li_next;
1997 if (arg == NULL)
1998 return FAIL;
1999
2000 arg = skipwhite(arg);
2001 if (*arg == ';')
2002 {
2003 /* Put the rest of the list (may be empty) in the var after ';'.
2004 * Create a new list for this. */
2005 l = list_alloc();
2006 if (l == NULL)
2007 return FAIL;
2008 while (item != NULL)
2009 {
2010 list_append_tv(l, &item->li_tv);
2011 item = item->li_next;
2012 }
2013
2014 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002015 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 ltv.vval.v_list = l;
2017 l->lv_refcount = 1;
2018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2020 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 clear_tv(&ltv);
2022 if (arg == NULL)
2023 return FAIL;
2024 break;
2025 }
2026 else if (*arg != ',' && *arg != ']')
2027 {
2028 EMSG2(_(e_intern2), "ex_let_vars()");
2029 return FAIL;
2030 }
2031 }
2032
2033 return OK;
2034}
2035
2036/*
2037 * Skip over assignable variable "var" or list of variables "[var, var]".
2038 * Used for ":let varvar = expr" and ":for varvar in expr".
2039 * For "[var, var]" increment "*var_count" for each variable.
2040 * for "[var, var; var]" set "semicolon".
2041 * Return NULL for an error.
2042 */
2043 static char_u *
2044skip_var_list(arg, var_count, semicolon)
2045 char_u *arg;
2046 int *var_count;
2047 int *semicolon;
2048{
2049 char_u *p, *s;
2050
2051 if (*arg == '[')
2052 {
2053 /* "[var, var]": find the matching ']'. */
2054 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002055 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2058 s = skip_var_one(p);
2059 if (s == p)
2060 {
2061 EMSG2(_(e_invarg2), p);
2062 return NULL;
2063 }
2064 ++*var_count;
2065
2066 p = skipwhite(s);
2067 if (*p == ']')
2068 break;
2069 else if (*p == ';')
2070 {
2071 if (*semicolon == 1)
2072 {
2073 EMSG(_("Double ; in list of variables"));
2074 return NULL;
2075 }
2076 *semicolon = 1;
2077 }
2078 else if (*p != ',')
2079 {
2080 EMSG2(_(e_invarg2), p);
2081 return NULL;
2082 }
2083 }
2084 return p + 1;
2085 }
2086 else
2087 return skip_var_one(arg);
2088}
2089
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002091 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002092 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 static char_u *
2095skip_var_one(arg)
2096 char_u *arg;
2097{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 if (*arg == '@' && arg[1] != NUL)
2099 return arg + 2;
2100 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2101 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102}
2103
Bram Moolenaara7043832005-01-21 11:56:39 +00002104/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 * List variables for hashtab "ht" with prefix "prefix".
2106 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002114{
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 hashitem_T *hi;
2116 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 int todo;
2118
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2121 {
2122 if (!HASHITEM_EMPTY(hi))
2123 {
2124 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 di = HI2DI(hi);
2126 if (empty || di->di_tv.v_type != VAR_STRING
2127 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 }
2130 }
2131}
2132
2133/*
2134 * List global variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_glob_vars(first)
2138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
2143/*
2144 * List buffer variables.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_buf_vars(first)
2148 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_win_vars(first)
2165 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002167 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171#ifdef FEAT_WINDOWS
2172/*
2173 * List tab page variables.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_tab_vars(first)
2177 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181}
2182#endif
2183
Bram Moolenaara7043832005-01-21 11:56:39 +00002184/*
2185 * List Vim variables.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_vim_vars(first)
2189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192}
2193
2194/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195 * List script-local variables, if there is a script.
2196 */
2197 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_script_vars(first)
2199 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200{
2201 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2203 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204}
2205
2206/*
2207 * List function variables, if there is a function.
2208 */
2209 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210list_func_vars(first)
2211 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212{
2213 if (current_funccal != NULL)
2214 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216}
2217
2218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 * List variables in "arg".
2220 */
2221 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 exarg_T *eap;
2224 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226{
2227 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 char_u *name_start;
2231 char_u *arg_subsc;
2232 char_u *tofree;
2233 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234
2235 while (!ends_excmd(*arg) && !got_int)
2236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2241 {
2242 emsg_severe = TRUE;
2243 EMSG(_(e_trailing));
2244 break;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* get_name_len() takes care of expanding curly braces */
2250 name_start = name = arg;
2251 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2252 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* This is mainly to keep test 49 working: when expanding
2255 * curly braces fails overrule the exception error message. */
2256 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 emsg_severe = TRUE;
2259 EMSG2(_(e_invarg2), arg);
2260 break;
2261 }
2262 error = TRUE;
2263 }
2264 else
2265 {
2266 if (tofree != NULL)
2267 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002268 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 else
2271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* handle d.key, l[idx], f(expr) */
2273 arg_subsc = arg;
2274 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 case 'g': list_glob_vars(first); break;
2283 case 'b': list_buf_vars(first); break;
2284 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'v': list_vim_vars(first); break;
2289 case 's': list_script_vars(first); break;
2290 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 default:
2292 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 }
2295 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 {
2297 char_u numbuf[NUMBUFLEN];
2298 char_u *tf;
2299 int c;
2300 char_u *s;
2301
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002302 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 c = *arg;
2304 *arg = NUL;
2305 list_one_var_a((char_u *)"",
2306 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 tv.v_type,
2308 s == NULL ? (char_u *)"" : s,
2309 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 *arg = c;
2311 vim_free(tf);
2312 }
2313 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317
2318 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320
2321 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
2323
2324 return arg;
2325}
2326
2327/*
2328 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2329 * Returns a pointer to the char just after the var name.
2330 * Returns NULL if there is an error.
2331 */
2332 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002335 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002337 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339{
2340 int c1;
2341 char_u *name;
2342 char_u *p;
2343 char_u *arg_end = NULL;
2344 int len;
2345 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347
2348 /*
2349 * ":let $VAR = expr": Set environment variable.
2350 */
2351 if (*arg == '$')
2352 {
2353 /* Find the end of the name. */
2354 ++arg;
2355 name = arg;
2356 len = get_env_len(&arg);
2357 if (len == 0)
2358 EMSG2(_(e_invarg2), name - 1);
2359 else
2360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 if (op != NULL && (*op == '+' || *op == '-'))
2362 EMSG2(_(e_letwrong), op);
2363 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002366 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 {
2368 c1 = name[len];
2369 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 p = get_tv_string_chk(tv);
2371 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 {
2373 int mustfree = FALSE;
2374 char_u *s = vim_getenv(name, &mustfree);
2375
2376 if (s != NULL)
2377 {
2378 p = tofree = concat_str(s, p);
2379 if (mustfree)
2380 vim_free(s);
2381 }
2382 }
2383 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 if (STRICMP(name, "HOME") == 0)
2387 init_homedir();
2388 else if (didset_vim && STRICMP(name, "VIM") == 0)
2389 didset_vim = FALSE;
2390 else if (didset_vimruntime
2391 && STRICMP(name, "VIMRUNTIME") == 0)
2392 didset_vimruntime = FALSE;
2393 arg_end = arg;
2394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
2398 }
2399 }
2400
2401 /*
2402 * ":let &option = expr": Set option value.
2403 * ":let &l:option = expr": Set local option value.
2404 * ":let &g:option = expr": Set global option value.
2405 */
2406 else if (*arg == '&')
2407 {
2408 /* Find the end of the name. */
2409 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002410 if (p == NULL || (endchars != NULL
2411 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 EMSG(_(e_letunexp));
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 long n;
2416 int opt_type;
2417 long numval;
2418 char_u *stringval = NULL;
2419 char_u *s;
2420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 c1 = *p;
2422 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423
2424 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 s = get_tv_string_chk(tv); /* != NULL if number or string */
2426 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 opt_type = get_option_value(arg, &numval,
2429 &stringval, opt_flags);
2430 if ((opt_type == 1 && *op == '.')
2431 || (opt_type == 0 && *op != '.'))
2432 EMSG2(_(e_letwrong), op);
2433 else
2434 {
2435 if (opt_type == 1) /* number */
2436 {
2437 if (*op == '+')
2438 n = numval + n;
2439 else
2440 n = numval - n;
2441 }
2442 else if (opt_type == 0 && stringval != NULL) /* string */
2443 {
2444 s = concat_str(stringval, s);
2445 vim_free(stringval);
2446 stringval = s;
2447 }
2448 }
2449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 if (s != NULL)
2451 {
2452 set_option_value(arg, n, s, opt_flags);
2453 arg_end = p;
2454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 }
2458 }
2459
2460 /*
2461 * ":let @r = expr": Set register contents.
2462 */
2463 else if (*arg == '@')
2464 {
2465 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 if (op != NULL && (*op == '+' || *op == '-'))
2467 EMSG2(_(e_letwrong), op);
2468 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 char_u *s;
2475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 p = get_tv_string_chk(tv);
2477 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002479 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (s != NULL)
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 vim_free(s);
2484 }
2485 }
2486 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 arg_end = arg + 1;
2490 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 }
2493 }
2494
2495 /*
2496 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002499 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002503 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2507 EMSG(_(e_letunexp));
2508 else
2509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 arg_end = p;
2512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516
2517 else
2518 EMSG2(_(e_invarg2), arg);
2519
2520 return arg_end;
2521}
2522
2523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2525 */
2526 static int
2527check_changedtick(arg)
2528 char_u *arg;
2529{
2530 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2531 {
2532 EMSG2(_(e_readonlyvar), arg);
2533 return TRUE;
2534 }
2535 return FALSE;
2536}
2537
2538/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Get an lval: variable, Dict item or List item that can be assigned a value
2540 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2541 * "name.key", "name.key[expr]" etc.
2542 * Indexing only works if "name" is an existing List or Dictionary.
2543 * "name" points to the start of the name.
2544 * If "rettv" is not NULL it points to the value to be assigned.
2545 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2546 * wrong; must end in space or cmd separator.
2547 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 * flags:
2549 * GLV_QUIET: do not give error messages
2550 * GLV_NO_AUTOLOAD: do not use script autoloading
2551 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002553 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 */
2556 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 typval_T *rettv;
2560 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 int unlet;
2562 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002564 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 char_u *expr_start, *expr_end;
2568 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 dictitem_T *v;
2570 typval_T var1;
2571 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002577 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581
2582 if (skip)
2583 {
2584 /* When skipping just find the end of the name. */
2585 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002586 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 }
2588
2589 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (expr_start != NULL)
2592 {
2593 /* Don't expand the name when we already know there is an error. */
2594 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2595 && *p != '[' && *p != '.')
2596 {
2597 EMSG(_(e_trailing));
2598 return NULL;
2599 }
2600
2601 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2602 if (lp->ll_exp_name == NULL)
2603 {
2604 /* Report an invalid expression in braces, unless the
2605 * expression evaluation has been cancelled due to an
2606 * aborting error, an interrupt, or an exception. */
2607 if (!aborting() && !quiet)
2608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002609 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 EMSG2(_(e_invarg2), name);
2611 return NULL;
2612 }
2613 }
2614 lp->ll_name = lp->ll_exp_name;
2615 }
2616 else
2617 lp->ll_name = name;
2618
2619 /* Without [idx] or .key we are done. */
2620 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2621 return p;
2622
2623 cc = *p;
2624 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002625 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (v == NULL && !quiet)
2627 EMSG2(_(e_undefvar), lp->ll_name);
2628 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 if (v == NULL)
2630 return NULL;
2631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /*
2633 * Loop until no more [idx] or .key is following.
2634 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2639 && !(lp->ll_tv->v_type == VAR_DICT
2640 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_("E689: Can only index a List or Dictionary"));
2644 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E708: [:] must come last"));
2650 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 len = -1;
2654 if (*p == '.')
2655 {
2656 key = p + 1;
2657 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2658 ;
2659 if (len == 0)
2660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_(e_emptykey));
2663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = key + len;
2666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 else
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (*p == ':')
2672 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 else
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 empty1 = FALSE;
2676 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 if (get_tv_string_chk(&var1) == NULL)
2679 {
2680 /* not a number or string */
2681 clear_tv(&var1);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /* Optionally get the second index [ :expr]. */
2687 if (*p == ':')
2688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (rettv != NULL && (rettv->v_type != VAR_LIST
2698 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (!empty1)
2703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706 p = skipwhite(p + 1);
2707 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 else
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002718 if (get_tv_string_chk(&var2) == NULL)
2719 {
2720 /* not a number or string */
2721 if (!empty1)
2722 clear_tv(&var1);
2723 clear_tv(&var2);
2724 return NULL;
2725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (!quiet)
2735 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742
2743 /* Skip to past ']'. */
2744 ++p;
2745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
2749 if (len == -1)
2750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*key == NUL)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 lp->ll_list = NULL;
2762 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 /* When assigning to a scope dictionary check that a function and
2766 * variable name is valid (only variable name unless it is l: or
2767 * g: dictionary). Disallow overwriting a builtin function. */
2768 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 int prevval;
2771 int wrong;
2772
2773 if (len != -1)
2774 {
2775 prevval = key[len];
2776 key[len] = NUL;
2777 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002778 else
2779 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2781 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 || !valid_varname(key);
2784 if (len != -1)
2785 key[len] = prevval;
2786 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 return NULL;
2788 }
2789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 /* Can't add "v:" variable. */
2793 if (lp->ll_dict == &vimvardict)
2794 {
2795 EMSG2(_(e_illvar), name);
2796 return NULL;
2797 }
2798
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 p = NULL;
2816 break;
2817 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002819 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 return NULL;
2821
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 else
2827 {
2828 /*
2829 * Get the number and item for the only or first index of the List.
2830 */
2831 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 else
2834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002835 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 clear_tv(&var1);
2837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_list = lp->ll_tv->vval.v_list;
2840 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2841 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002843 if (lp->ll_n1 < 0)
2844 {
2845 lp->ll_n1 = 0;
2846 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2847 }
2848 }
2849 if (lp->ll_li == NULL)
2850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 if (!quiet)
2854 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857
2858 /*
2859 * May need to find the item or absolute index for the second
2860 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 * When no index given: "lp->ll_empty2" is TRUE.
2862 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 {
2873 if (!quiet)
2874 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2881 if (lp->ll_n1 < 0)
2882 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2883 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 {
2885 if (!quiet)
2886 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return p;
2896}
2897
2898/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 */
2901 static void
2902clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904{
2905 vim_free(lp->ll_exp_name);
2906 vim_free(lp->ll_newkey);
2907}
2908
2909/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 listitem_T *ri;
2924 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925
2926 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 cc = *endp;
2931 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002938 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 if ((di == NULL
2942 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2943 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2944 FALSE)))
2945 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 set_var(lp->ll_name, &tv, FALSE);
2947 clear_tv(&tv);
2948 }
2949 }
2950 else
2951 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002955 else if (tv_check_lock(lp->ll_newkey == NULL
2956 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002957 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 else if (lp->ll_range)
2960 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 listitem_T *ll_li = lp->ll_li;
2962 int ll_n1 = lp->ll_n1;
2963
2964 /*
2965 * Check whether any of the list items is locked
2966 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002967 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002970 return;
2971 ri = ri->li_next;
2972 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2973 break;
2974 ll_li = ll_li->li_next;
2975 ++ll_n1;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /*
2979 * Assign the List values to the list items.
2980 */
2981 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2985 else
2986 {
2987 clear_tv(&lp->ll_li->li_tv);
2988 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = ri->li_next;
2991 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2992 break;
2993 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002996 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = NULL;
2999 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 lp->ll_li = lp->ll_li->li_next;
3003 ++lp->ll_n1;
3004 }
3005 if (ri != NULL)
3006 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 else if (lp->ll_empty2
3008 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 : lp->ll_n1 != lp->ll_n2)
3010 EMSG(_("E711: List value has not enough items"));
3011 }
3012 else
3013 {
3014 /*
3015 * Assign to a List or Dictionary item.
3016 */
3017 if (lp->ll_newkey != NULL)
3018 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 if (op != NULL && *op != '=')
3020 {
3021 EMSG2(_(e_letwrong), op);
3022 return;
3023 }
3024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 if (di == NULL)
3028 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3030 {
3031 vim_free(di);
3032 return;
3033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 else if (op != NULL && *op != '=')
3037 {
3038 tv_op(lp->ll_tv, rettv, op);
3039 return;
3040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 /*
3045 * Assign the value to the variable or list item.
3046 */
3047 if (copy)
3048 copy_tv(rettv, lp->ll_tv);
3049 else
3050 {
3051 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003052 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 }
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056}
3057
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3060 * Returns OK or FAIL.
3061 */
3062 static int
3063tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 typval_T *tv1;
3065 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 char_u *op;
3067{
3068 long n;
3069 char_u numbuf[NUMBUFLEN];
3070 char_u *s;
3071
3072 /* Can't do anything with a Funcref or a Dict on the right. */
3073 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3074 {
3075 switch (tv1->v_type)
3076 {
3077 case VAR_DICT:
3078 case VAR_FUNC:
3079 break;
3080
3081 case VAR_LIST:
3082 if (*op != '+' || tv2->v_type != VAR_LIST)
3083 break;
3084 /* List += List */
3085 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3086 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3087 return OK;
3088
3089 case VAR_NUMBER:
3090 case VAR_STRING:
3091 if (tv2->v_type == VAR_LIST)
3092 break;
3093 if (*op == '+' || *op == '-')
3094 {
3095 /* nr += nr or nr -= nr*/
3096 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097#ifdef FEAT_FLOAT
3098 if (tv2->v_type == VAR_FLOAT)
3099 {
3100 float_T f = n;
3101
3102 if (*op == '+')
3103 f += tv2->vval.v_float;
3104 else
3105 f -= tv2->vval.v_float;
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_FLOAT;
3108 tv1->vval.v_float = f;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#endif
3112 {
3113 if (*op == '+')
3114 n += get_tv_number(tv2);
3115 else
3116 n -= get_tv_number(tv2);
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_NUMBER;
3119 tv1->vval.v_number = n;
3120 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 }
3122 else
3123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124 if (tv2->v_type == VAR_FLOAT)
3125 break;
3126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 /* str .= str */
3128 s = get_tv_string(tv1);
3129 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_STRING;
3132 tv1->vval.v_string = s;
3133 }
3134 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135
3136#ifdef FEAT_FLOAT
3137 case VAR_FLOAT:
3138 {
3139 float_T f;
3140
3141 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3142 && tv2->v_type != VAR_NUMBER
3143 && tv2->v_type != VAR_STRING))
3144 break;
3145 if (tv2->v_type == VAR_FLOAT)
3146 f = tv2->vval.v_float;
3147 else
3148 f = get_tv_number(tv2);
3149 if (*op == '+')
3150 tv1->vval.v_float += f;
3151 else
3152 tv1->vval.v_float -= f;
3153 }
3154 return OK;
3155#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 }
3157 }
3158
3159 EMSG2(_(e_letwrong), op);
3160 return FAIL;
3161}
3162
3163/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * Add a watcher to a list.
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
3171 lw->lw_next = l->lv_watch;
3172 l->lv_watch = lw;
3173}
3174
3175/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003176 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 * No warning when it isn't found...
3178 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003179 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 list_T *l;
3182 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 lwp = &l->lv_watch;
3187 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3188 {
3189 if (lw == lwrem)
3190 {
3191 *lwp = lw->lw_next;
3192 break;
3193 }
3194 lwp = &lw->lw_next;
3195 }
3196}
3197
3198/*
3199 * Just before removing an item from a list: advance watchers to the next
3200 * item.
3201 */
3202 static void
3203list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 list_T *l;
3205 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208
3209 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3210 if (lw->lw_item == item)
3211 lw->lw_item = item->li_next;
3212}
3213
3214/*
3215 * Evaluate the expression used in a ":for var in expr" command.
3216 * "arg" points to "var".
3217 * Set "*errp" to TRUE for an error, FALSE otherwise;
3218 * Return a pointer that holds the info. Null when there is an error.
3219 */
3220 void *
3221eval_for_line(arg, errp, nextcmdp, skip)
3222 char_u *arg;
3223 int *errp;
3224 char_u **nextcmdp;
3225 int skip;
3226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 typval_T tv;
3230 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 *errp = TRUE; /* default: there is an error */
3233
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 if (fi == NULL)
3236 return NULL;
3237
3238 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3239 if (expr == NULL)
3240 return fi;
3241
3242 expr = skipwhite(expr);
3243 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3244 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003245 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 return fi;
3247 }
3248
3249 if (skip)
3250 ++emsg_skip;
3251 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3252 {
3253 *errp = FALSE;
3254 if (!skip)
3255 {
3256 l = tv.vval.v_list;
3257 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 clear_tv(&tv);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 else
3263 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003264 /* No need to increment the refcount, it's already set for the
3265 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 fi->fi_list = l;
3267 list_add_watch(l, &fi->fi_lw);
3268 fi->fi_lw.lw_item = l->lv_first;
3269 }
3270 }
3271 }
3272 if (skip)
3273 --emsg_skip;
3274
3275 return fi;
3276}
3277
3278/*
3279 * Use the first item in a ":for" list. Advance to the next.
3280 * Assign the values to the variable (list). "arg" points to the first one.
3281 * Return TRUE when a valid item was found, FALSE when at end of list or
3282 * something wrong.
3283 */
3284 int
3285next_for_item(fi_void, arg)
3286 void *fi_void;
3287 char_u *arg;
3288{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292
3293 item = fi->fi_lw.lw_item;
3294 if (item == NULL)
3295 result = FALSE;
3296 else
3297 {
3298 fi->fi_lw.lw_item = item->li_next;
3299 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3300 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3301 }
3302 return result;
3303}
3304
3305/*
3306 * Free the structure used to store info used by ":for".
3307 */
3308 void
3309free_for_info(fi_void)
3310 void *fi_void;
3311{
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003314 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317 list_unref(fi->fi_list);
3318 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 vim_free(fi);
3320}
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3323
3324 void
3325set_context_for_expression(xp, arg, cmdidx)
3326 expand_T *xp;
3327 char_u *arg;
3328 cmdidx_T cmdidx;
3329{
3330 int got_eq = FALSE;
3331 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 if (cmdidx == CMD_let)
3335 {
3336 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003337 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 {
3339 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (vim_iswhite(*p))
3345 break;
3346 }
3347 return;
3348 }
3349 }
3350 else
3351 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3352 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 while ((xp->xp_pattern = vim_strpbrk(arg,
3354 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3355 {
3356 c = *xp->xp_pattern;
3357 if (c == '&')
3358 {
3359 c = xp->xp_pattern[1];
3360 if (c == '&')
3361 {
3362 ++xp->xp_pattern;
3363 xp->xp_context = cmdidx != CMD_let || got_eq
3364 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3365 }
3366 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003369 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3370 xp->xp_pattern += 2;
3371
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else if (c == '$')
3375 {
3376 /* environment variable */
3377 xp->xp_context = EXPAND_ENV_VARS;
3378 }
3379 else if (c == '=')
3380 {
3381 got_eq = TRUE;
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003384 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 && xp->xp_context == EXPAND_FUNCTIONS
3386 && vim_strchr(xp->xp_pattern, '(') == NULL)
3387 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 break;
3390 }
3391 else if (cmdidx != CMD_let || got_eq)
3392 {
3393 if (c == '"') /* string */
3394 {
3395 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3396 if (c == '\\' && xp->xp_pattern[1] != NUL)
3397 ++xp->xp_pattern;
3398 xp->xp_context = EXPAND_NOTHING;
3399 }
3400 else if (c == '\'') /* literal string */
3401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003402 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3404 /* skip */ ;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '|')
3408 {
3409 if (xp->xp_pattern[1] == '|')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_EXPRESSION;
3413 }
3414 else
3415 xp->xp_context = EXPAND_COMMANDS;
3416 }
3417 else
3418 xp->xp_context = EXPAND_EXPRESSION;
3419 }
3420 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003421 /* Doesn't look like something valid, expand as an expression
3422 * anyway. */
3423 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 arg = xp->xp_pattern;
3425 if (*arg != NUL)
3426 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3427 /* skip */ ;
3428 }
3429 xp->xp_pattern = arg;
3430}
3431
3432#endif /* FEAT_CMDL_COMPL */
3433
3434/*
3435 * ":1,25call func(arg1, arg2)" function call.
3436 */
3437 void
3438ex_call(eap)
3439 exarg_T *eap;
3440{
3441 char_u *arg = eap->arg;
3442 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003444 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 linenr_T lnum;
3448 int doesrange;
3449 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003452 if (eap->skip)
3453 {
3454 /* trans_function_name() doesn't work well when skipping, use eval0()
3455 * instead to skip to any following command, e.g. for:
3456 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003457 ++emsg_skip;
3458 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3459 clear_tv(&rettv);
3460 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 return;
3462 }
3463
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003464 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003465 if (fudi.fd_newkey != NULL)
3466 {
3467 /* Still need to give an error message for missing key. */
3468 EMSG2(_(e_dictkey), fudi.fd_newkey);
3469 vim_free(fudi.fd_newkey);
3470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 if (tofree == NULL)
3472 return;
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 /* Increase refcount on dictionary, it could get deleted when evaluating
3475 * the arguments. */
3476 if (fudi.fd_dict != NULL)
3477 ++fudi.fd_dict->dv_refcount;
3478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003480 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003481 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar532c7802005-01-27 14:44:31 +00003483 /* Skip white space to allow ":call func ()". Not good, but required for
3484 * backward compatibility. */
3485 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (*startarg != '(')
3489 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003490 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 goto end;
3492 }
3493
3494 /*
3495 * When skipping, evaluate the function once, to find the end of the
3496 * arguments.
3497 * When the function takes a range, this is discovered after the first
3498 * call, and the loop is broken.
3499 */
3500 if (eap->skip)
3501 {
3502 ++emsg_skip;
3503 lnum = eap->line2; /* do it once, also with an invalid range */
3504 }
3505 else
3506 lnum = eap->line1;
3507 for ( ; lnum <= eap->line2; ++lnum)
3508 {
3509 if (!eap->skip && eap->addr_count > 0)
3510 {
3511 curwin->w_cursor.lnum = lnum;
3512 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003513#ifdef FEAT_VIRTUALEDIT
3514 curwin->w_cursor.coladd = 0;
3515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003519 eap->line1, eap->line2, &doesrange,
3520 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 failed = TRUE;
3523 break;
3524 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003525
3526 /* Handle a function returning a Funcref, Dictionary or List. */
3527 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3528 {
3529 failed = TRUE;
3530 break;
3531 }
3532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (doesrange || eap->skip)
3535 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (aborting())
3542 break;
3543 }
3544 if (eap->skip)
3545 --emsg_skip;
3546
3547 if (!failed)
3548 {
3549 /* Check for trailing illegal characters and a following command. */
3550 if (!ends_excmd(*arg))
3551 {
3552 emsg_severe = TRUE;
3553 EMSG(_(e_trailing));
3554 }
3555 else
3556 eap->nextcmd = check_nextcmd(arg);
3557 }
3558
3559end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
3564/*
3565 * ":unlet[!] var1 ... " command.
3566 */
3567 void
3568ex_unlet(eap)
3569 exarg_T *eap;
3570{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 ex_unletlock(eap, eap->arg, 0);
3572}
3573
3574/*
3575 * ":lockvar" and ":unlockvar" commands
3576 */
3577 void
3578ex_lockvar(eap)
3579 exarg_T *eap;
3580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
3599ex_unletlock(eap, argstart, deep)
3600 exarg_T *eap;
3601 char_u *argstart;
3602 int deep;
3603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 int forceit;
3658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
3683 int ll_n1 = lp->ll_n1;
3684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003721do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di->di_tv.vval.v_dict;
3743 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hi = hash_find(ht, varname);
3745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003747 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 if (var_check_fixed(di->di_flags, name, FALSE)
3749 || var_check_ro(di->di_flags, name, FALSE)
3750 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 return FAIL;
3752 delete_var(ht, hi);
3753 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 if (forceit)
3757 return OK;
3758 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return FAIL;
3760}
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762/*
3763 * Lock or unlock variable indicated by "lp".
3764 * "deep" is the levels to go (-1 for unlimited);
3765 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3766 */
3767 static int
3768do_lock_var(lp, name_end, deep, lock)
3769 lval_T *lp;
3770 char_u *name_end;
3771 int deep;
3772 int lock;
3773{
3774 int ret = OK;
3775 int cc;
3776 dictitem_T *di;
3777
3778 if (deep == 0) /* nothing to do */
3779 return OK;
3780
3781 if (lp->ll_tv == NULL)
3782 {
3783 cc = *name_end;
3784 *name_end = NUL;
3785
3786 /* Normal name or expanded name. */
3787 if (check_changedtick(lp->ll_name))
3788 ret = FAIL;
3789 else
3790 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003791 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792 if (di == NULL)
3793 ret = FAIL;
3794 else
3795 {
3796 if (lock)
3797 di->di_flags |= DI_FLAGS_LOCK;
3798 else
3799 di->di_flags &= ~DI_FLAGS_LOCK;
3800 item_lock(&di->di_tv, deep, lock);
3801 }
3802 }
3803 *name_end = cc;
3804 }
3805 else if (lp->ll_range)
3806 {
3807 listitem_T *li = lp->ll_li;
3808
3809 /* (un)lock a range of List items. */
3810 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3811 {
3812 item_lock(&li->li_tv, deep, lock);
3813 li = li->li_next;
3814 ++lp->ll_n1;
3815 }
3816 }
3817 else if (lp->ll_list != NULL)
3818 /* (un)lock a List item. */
3819 item_lock(&lp->ll_li->li_tv, deep, lock);
3820 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003821 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 item_lock(&lp->ll_di->di_tv, deep, lock);
3823
3824 return ret;
3825}
3826
3827/*
3828 * Lock or unlock an item. "deep" is nr of levels to go.
3829 */
3830 static void
3831item_lock(tv, deep, lock)
3832 typval_T *tv;
3833 int deep;
3834 int lock;
3835{
3836 static int recurse = 0;
3837 list_T *l;
3838 listitem_T *li;
3839 dict_T *d;
3840 hashitem_T *hi;
3841 int todo;
3842
3843 if (recurse >= DICT_MAXNEST)
3844 {
3845 EMSG(_("E743: variable nested too deep for (un)lock"));
3846 return;
3847 }
3848 if (deep == 0)
3849 return;
3850 ++recurse;
3851
3852 /* lock/unlock the item itself */
3853 if (lock)
3854 tv->v_lock |= VAR_LOCKED;
3855 else
3856 tv->v_lock &= ~VAR_LOCKED;
3857
3858 switch (tv->v_type)
3859 {
3860 case VAR_LIST:
3861 if ((l = tv->vval.v_list) != NULL)
3862 {
3863 if (lock)
3864 l->lv_lock |= VAR_LOCKED;
3865 else
3866 l->lv_lock &= ~VAR_LOCKED;
3867 if (deep < 0 || deep > 1)
3868 /* recursive: lock/unlock the items the List contains */
3869 for (li = l->lv_first; li != NULL; li = li->li_next)
3870 item_lock(&li->li_tv, deep - 1, lock);
3871 }
3872 break;
3873 case VAR_DICT:
3874 if ((d = tv->vval.v_dict) != NULL)
3875 {
3876 if (lock)
3877 d->dv_lock |= VAR_LOCKED;
3878 else
3879 d->dv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 {
3882 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003883 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3885 {
3886 if (!HASHITEM_EMPTY(hi))
3887 {
3888 --todo;
3889 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3890 }
3891 }
3892 }
3893 }
3894 }
3895 --recurse;
3896}
3897
Bram Moolenaara40058a2005-07-11 22:42:07 +00003898/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003899 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3900 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003901 */
3902 static int
3903tv_islocked(tv)
3904 typval_T *tv;
3905{
3906 return (tv->v_lock & VAR_LOCKED)
3907 || (tv->v_type == VAR_LIST
3908 && tv->vval.v_list != NULL
3909 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3910 || (tv->v_type == VAR_DICT
3911 && tv->vval.v_dict != NULL
3912 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3913}
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3916/*
3917 * Delete all "menutrans_" variables.
3918 */
3919 void
3920del_menutrans_vars()
3921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003926 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 {
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3933 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 }
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938#endif
3939
3940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3941
3942/*
3943 * Local string buffer for the next two functions to store a variable name
3944 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3945 * get_user_var_name().
3946 */
3947
3948static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3949
3950static char_u *varnamebuf = NULL;
3951static int varnamebuflen = 0;
3952
3953/*
3954 * Function to concatenate a prefix and a variable name.
3955 */
3956 static char_u *
3957cat_prefix_varname(prefix, name)
3958 int prefix;
3959 char_u *name;
3960{
3961 int len;
3962
3963 len = (int)STRLEN(name) + 3;
3964 if (len > varnamebuflen)
3965 {
3966 vim_free(varnamebuf);
3967 len += 10; /* some additional space */
3968 varnamebuf = alloc(len);
3969 if (varnamebuf == NULL)
3970 {
3971 varnamebuflen = 0;
3972 return NULL;
3973 }
3974 varnamebuflen = len;
3975 }
3976 *varnamebuf = prefix;
3977 varnamebuf[1] = ':';
3978 STRCPY(varnamebuf + 2, name);
3979 return varnamebuf;
3980}
3981
3982/*
3983 * Function given to ExpandGeneric() to obtain the list of user defined
3984 * (global/buffer/window/built-in) variable names.
3985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u *
3987get_user_var_name(xp, idx)
3988 expand_T *xp;
3989 int idx;
3990{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 static long_u gdone;
3992 static long_u bdone;
3993 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994#ifdef FEAT_WINDOWS
3995 static long_u tdone;
3996#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static int vidx;
3998 static hashitem_T *hi;
3999 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 tdone = 0;
4006#endif
4007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004008
4009 /* Global variables */
4010 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 else
4015 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 while (HASHITEM_EMPTY(hi))
4017 ++hi;
4018 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4019 return cat_prefix_varname('g', hi->hi_key);
4020 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022
4023 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004024 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return (char_u *)"b:changedtick";
4039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
4041 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004042 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004045 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 else
4048 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 while (HASHITEM_EMPTY(hi))
4050 ++hi;
4051 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054#ifdef FEAT_WINDOWS
4055 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004056 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 if (tdone < ht->ht_used)
4058 {
4059 if (tdone++ == 0)
4060 hi = ht->ht_array;
4061 else
4062 ++hi;
4063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 return cat_prefix_varname('t', hi->hi_key);
4066 }
4067#endif
4068
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 /* v: variables */
4070 if (vidx < VV_LEN)
4071 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 vim_free(varnamebuf);
4074 varnamebuf = NULL;
4075 varnamebuflen = 0;
4076 return NULL;
4077}
4078
4079#endif /* FEAT_CMDL_COMPL */
4080
4081/*
4082 * types for expressions.
4083 */
4084typedef enum
4085{
4086 TYPE_UNKNOWN = 0
4087 , TYPE_EQUAL /* == */
4088 , TYPE_NEQUAL /* != */
4089 , TYPE_GREATER /* > */
4090 , TYPE_GEQUAL /* >= */
4091 , TYPE_SMALLER /* < */
4092 , TYPE_SEQUAL /* <= */
4093 , TYPE_MATCH /* =~ */
4094 , TYPE_NOMATCH /* !~ */
4095} exptype_T;
4096
4097/*
4098 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4101 */
4102
4103/*
4104 * Handle zero level expression.
4105 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004107 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * Return OK or FAIL.
4109 */
4110 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 char_u **nextcmd;
4115 int evaluate;
4116{
4117 int ret;
4118 char_u *p;
4119
4120 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (ret == FAIL || !ends_excmd(*p))
4123 {
4124 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /*
4127 * Report the invalid expression unless the expression evaluation has
4128 * been cancelled due to an aborting error, an interrupt, or an
4129 * exception.
4130 */
4131 if (!aborting())
4132 EMSG2(_(e_invexpr2), arg);
4133 ret = FAIL;
4134 }
4135 if (nextcmd != NULL)
4136 *nextcmd = check_nextcmd(p);
4137
4138 return ret;
4139}
4140
4141/*
4142 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004143 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004148 * Note: "rettv.v_lock" is not set.
4149 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
4158 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 if ((*arg)[0] == '?')
4168 {
4169 result = FALSE;
4170 if (evaluate)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
4173
4174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 /*
4182 * Get the second variable.
4183 */
4184 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 /*
4189 * Check for the ":".
4190 */
4191 if ((*arg)[0] != ':')
4192 {
4193 EMSG(_("E109: Missing ':' after '?'"));
4194 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197 }
4198
4199 /*
4200 * Get the third variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
4203 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4204 {
4205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208 }
4209 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212
4213 return OK;
4214}
4215
4216/*
4217 * Handle first level expression:
4218 * expr2 || expr2 || expr2 logical OR
4219 *
4220 * "arg" must point to the first non-white of the expression.
4221 * "arg" is advanced to the next non-white after the recognized expression.
4222 *
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int evaluate;
4230{
Bram Moolenaar33570922005-01-25 22:26:29 +00004231 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 long result;
4233 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 /*
4237 * Get the first variable.
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
4241
4242 /*
4243 * Repeat until there is no following "||".
4244 */
4245 first = TRUE;
4246 result = FALSE;
4247 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4248 {
4249 if (evaluate && first)
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (error)
4255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 first = FALSE;
4257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 2);
4263 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4264 return FAIL;
4265
4266 /*
4267 * Compute the result.
4268 */
4269 if (evaluate && !result)
4270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (error)
4275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 if (evaluate)
4278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 rettv->v_type = VAR_NUMBER;
4280 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283
4284 return OK;
4285}
4286
4287/*
4288 * Handle second level expression:
4289 * expr3 && expr3 && expr3 logical AND
4290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int evaluate;
4301{
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 long result;
4304 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /*
4308 * Get the first variable.
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return FAIL;
4312
4313 /*
4314 * Repeat until there is no following "&&".
4315 */
4316 first = TRUE;
4317 result = TRUE;
4318 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4319 {
4320 if (evaluate && first)
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 first = FALSE;
4328 }
4329
4330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 2);
4334 if (eval4(arg, &var2, evaluate && result) == FAIL)
4335 return FAIL;
4336
4337 /*
4338 * Compute the result.
4339 */
4340 if (evaluate && result)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (error)
4346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 if (evaluate)
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 rettv->v_type = VAR_NUMBER;
4351 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355 return OK;
4356}
4357
4358/*
4359 * Handle third level expression:
4360 * var1 == var2
4361 * var1 =~ var2
4362 * var1 != var2
4363 * var1 !~ var2
4364 * var1 > var2
4365 * var1 >= var2
4366 * var1 < var2
4367 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 * var1 is var2
4369 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
4434 if (!vim_isIDc(p[len]))
4435 {
4436 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4437 type_is = TRUE;
4438 }
4439 }
4440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442
4443 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004444 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 */
4446 if (type != TYPE_UNKNOWN)
4447 {
4448 /* extra question mark appended: ignore case */
4449 if (p[len] == '?')
4450 {
4451 ic = TRUE;
4452 ++len;
4453 }
4454 /* extra '#' appended: match case */
4455 else if (p[len] == '#')
4456 {
4457 ic = FALSE;
4458 ++len;
4459 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 else
4462 ic = p_ic;
4463
4464 /*
4465 * Get the second variable.
4466 */
4467 *arg = skipwhite(p + len);
4468 if (eval5(arg, &var2, evaluate) == FAIL)
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 return FAIL;
4472 }
4473
4474 if (evaluate)
4475 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 if (type_is && rettv->v_type != var2.v_type)
4477 {
4478 /* For "is" a different type always means FALSE, for "notis"
4479 * it means TRUE. */
4480 n1 = (type == TYPE_NEQUAL);
4481 }
4482 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4483 {
4484 if (type_is)
4485 {
4486 n1 = (rettv->v_type == var2.v_type
4487 && rettv->vval.v_list == var2.vval.v_list);
4488 if (type == TYPE_NEQUAL)
4489 n1 = !n1;
4490 }
4491 else if (rettv->v_type != var2.v_type
4492 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4493 {
4494 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004495 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004497 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 clear_tv(rettv);
4499 clear_tv(&var2);
4500 return FAIL;
4501 }
4502 else
4503 {
4504 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004505 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4506 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 if (type == TYPE_NEQUAL)
4508 n1 = !n1;
4509 }
4510 }
4511
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004512 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4513 {
4514 if (type_is)
4515 {
4516 n1 = (rettv->v_type == var2.v_type
4517 && rettv->vval.v_dict == var2.vval.v_dict);
4518 if (type == TYPE_NEQUAL)
4519 n1 = !n1;
4520 }
4521 else if (rettv->v_type != var2.v_type
4522 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4523 {
4524 if (rettv->v_type != var2.v_type)
4525 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4526 else
4527 EMSG(_("E736: Invalid operation for Dictionary"));
4528 clear_tv(rettv);
4529 clear_tv(&var2);
4530 return FAIL;
4531 }
4532 else
4533 {
4534 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004535 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4536 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004537 if (type == TYPE_NEQUAL)
4538 n1 = !n1;
4539 }
4540 }
4541
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004542 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4543 {
4544 if (rettv->v_type != var2.v_type
4545 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4546 {
4547 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004548 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004550 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 clear_tv(rettv);
4552 clear_tv(&var2);
4553 return FAIL;
4554 }
4555 else
4556 {
4557 /* Compare two Funcrefs for being equal or unequal. */
4558 if (rettv->vval.v_string == NULL
4559 || var2.vval.v_string == NULL)
4560 n1 = FALSE;
4561 else
4562 n1 = STRCMP(rettv->vval.v_string,
4563 var2.vval.v_string) == 0;
4564 if (type == TYPE_NEQUAL)
4565 n1 = !n1;
4566 }
4567 }
4568
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004569#ifdef FEAT_FLOAT
4570 /*
4571 * If one of the two variables is a float, compare as a float.
4572 * When using "=~" or "!~", always compare as string.
4573 */
4574 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4575 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4576 {
4577 float_T f1, f2;
4578
4579 if (rettv->v_type == VAR_FLOAT)
4580 f1 = rettv->vval.v_float;
4581 else
4582 f1 = get_tv_number(rettv);
4583 if (var2.v_type == VAR_FLOAT)
4584 f2 = var2.vval.v_float;
4585 else
4586 f2 = get_tv_number(&var2);
4587 n1 = FALSE;
4588 switch (type)
4589 {
4590 case TYPE_EQUAL: n1 = (f1 == f2); break;
4591 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4592 case TYPE_GREATER: n1 = (f1 > f2); break;
4593 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4594 case TYPE_SMALLER: n1 = (f1 < f2); break;
4595 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4596 case TYPE_UNKNOWN:
4597 case TYPE_MATCH:
4598 case TYPE_NOMATCH: break; /* avoid gcc warning */
4599 }
4600 }
4601#endif
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 /*
4604 * If one of the two variables is a number, compare as a number.
4605 * When using "=~" or "!~", always compare as string.
4606 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 n1 = get_tv_number(rettv);
4611 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 switch (type)
4613 {
4614 case TYPE_EQUAL: n1 = (n1 == n2); break;
4615 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4616 case TYPE_GREATER: n1 = (n1 > n2); break;
4617 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4618 case TYPE_SMALLER: n1 = (n1 < n2); break;
4619 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4620 case TYPE_UNKNOWN:
4621 case TYPE_MATCH:
4622 case TYPE_NOMATCH: break; /* avoid gcc warning */
4623 }
4624 }
4625 else
4626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 s1 = get_tv_string_buf(rettv, buf1);
4628 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4630 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4631 else
4632 i = 0;
4633 n1 = FALSE;
4634 switch (type)
4635 {
4636 case TYPE_EQUAL: n1 = (i == 0); break;
4637 case TYPE_NEQUAL: n1 = (i != 0); break;
4638 case TYPE_GREATER: n1 = (i > 0); break;
4639 case TYPE_GEQUAL: n1 = (i >= 0); break;
4640 case TYPE_SMALLER: n1 = (i < 0); break;
4641 case TYPE_SEQUAL: n1 = (i <= 0); break;
4642
4643 case TYPE_MATCH:
4644 case TYPE_NOMATCH:
4645 /* avoid 'l' flag in 'cpoptions' */
4646 save_cpo = p_cpo;
4647 p_cpo = (char_u *)"";
4648 regmatch.regprog = vim_regcomp(s2,
4649 RE_MAGIC + RE_STRING);
4650 regmatch.rm_ic = ic;
4651 if (regmatch.regprog != NULL)
4652 {
4653 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004654 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (type == TYPE_NOMATCH)
4656 n1 = !n1;
4657 }
4658 p_cpo = save_cpo;
4659 break;
4660
4661 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4662 }
4663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
4665 clear_tv(&var2);
4666 rettv->v_type = VAR_NUMBER;
4667 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 }
4670
4671 return OK;
4672}
4673
4674/*
4675 * Handle fourth level expression:
4676 * + number addition
4677 * - number subtraction
4678 * . string concatenation
4679 *
4680 * "arg" must point to the first non-white of the expression.
4681 * "arg" is advanced to the next non-white after the recognized expression.
4682 *
4683 * Return OK or FAIL.
4684 */
4685 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 int evaluate;
4690{
Bram Moolenaar33570922005-01-25 22:26:29 +00004691 typval_T var2;
4692 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 int op;
4694 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695#ifdef FEAT_FLOAT
4696 float_T f1 = 0, f2 = 0;
4697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 char_u *s1, *s2;
4699 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4700 char_u *p;
4701
4702 /*
4703 * Get the first variable.
4704 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004705 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return FAIL;
4707
4708 /*
4709 * Repeat computing, until no '+', '-' or '.' is following.
4710 */
4711 for (;;)
4712 {
4713 op = **arg;
4714 if (op != '+' && op != '-' && op != '.')
4715 break;
4716
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 if ((op != '+' || rettv->v_type != VAR_LIST)
4718#ifdef FEAT_FLOAT
4719 && (op == '.' || rettv->v_type != VAR_FLOAT)
4720#endif
4721 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 {
4723 /* For "list + ...", an illegal use of the first operand as
4724 * a number cannot be determined before evaluating the 2nd
4725 * operand: if this is also a list, all is ok.
4726 * For "something . ...", "something - ..." or "non-list + ...",
4727 * we know that the first operand needs to be a string or number
4728 * without evaluating the 2nd operand. So check before to avoid
4729 * side effects after an error. */
4730 if (evaluate && get_tv_string_chk(rettv) == NULL)
4731 {
4732 clear_tv(rettv);
4733 return FAIL;
4734 }
4735 }
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /*
4738 * Get the second variable.
4739 */
4740 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745 }
4746
4747 if (evaluate)
4748 {
4749 /*
4750 * Compute the result.
4751 */
4752 if (op == '.')
4753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4755 s2 = get_tv_string_buf_chk(&var2, buf2);
4756 if (s2 == NULL) /* type error ? */
4757 {
4758 clear_tv(rettv);
4759 clear_tv(&var2);
4760 return FAIL;
4761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004762 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
4764 rettv->v_type = VAR_STRING;
4765 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004767 else if (op == '+' && rettv->v_type == VAR_LIST
4768 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004769 {
4770 /* concatenate Lists */
4771 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4772 &var3) == FAIL)
4773 {
4774 clear_tv(rettv);
4775 clear_tv(&var2);
4776 return FAIL;
4777 }
4778 clear_tv(rettv);
4779 *rettv = var3;
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
4782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 int error = FALSE;
4784
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 f1 = rettv->vval.v_float;
4789 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 else
4792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 n1 = get_tv_number_chk(rettv, &error);
4795 if (error)
4796 {
4797 /* This can only happen for "list + non-list". For
4798 * "non-list + ..." or "something - ...", we returned
4799 * before evaluating the 2nd operand. */
4800 clear_tv(rettv);
4801 return FAIL;
4802 }
4803#ifdef FEAT_FLOAT
4804 if (var2.v_type == VAR_FLOAT)
4805 f1 = n1;
4806#endif
4807 }
4808#ifdef FEAT_FLOAT
4809 if (var2.v_type == VAR_FLOAT)
4810 {
4811 f2 = var2.vval.v_float;
4812 n2 = 0;
4813 }
4814 else
4815#endif
4816 {
4817 n2 = get_tv_number_chk(&var2, &error);
4818 if (error)
4819 {
4820 clear_tv(rettv);
4821 clear_tv(&var2);
4822 return FAIL;
4823 }
4824#ifdef FEAT_FLOAT
4825 if (rettv->v_type == VAR_FLOAT)
4826 f2 = n2;
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830
4831#ifdef FEAT_FLOAT
4832 /* If there is a float on either side the result is a float. */
4833 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4834 {
4835 if (op == '+')
4836 f1 = f1 + f2;
4837 else
4838 f1 = f1 - f2;
4839 rettv->v_type = VAR_FLOAT;
4840 rettv->vval.v_float = f1;
4841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843#endif
4844 {
4845 if (op == '+')
4846 n1 = n1 + n2;
4847 else
4848 n1 = n1 - n2;
4849 rettv->v_type = VAR_NUMBER;
4850 rettv->vval.v_number = n1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
4856 return OK;
4857}
4858
4859/*
4860 * Handle fifth level expression:
4861 * * number multiplication
4862 * / number division
4863 * % number modulo
4864 *
4865 * "arg" must point to the first non-white of the expression.
4866 * "arg" is advanced to the next non-white after the recognized expression.
4867 *
4868 * Return OK or FAIL.
4869 */
4870 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004871eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004875 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
Bram Moolenaar33570922005-01-25 22:26:29 +00004877 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 int op;
4879 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#ifdef FEAT_FLOAT
4881 int use_float = FALSE;
4882 float_T f1 = 0, f2;
4883#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /*
4887 * Get the first variable.
4888 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004889 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return FAIL;
4891
4892 /*
4893 * Repeat computing, until no '*', '/' or '%' is following.
4894 */
4895 for (;;)
4896 {
4897 op = **arg;
4898 if (op != '*' && op != '/' && op != '%')
4899 break;
4900
4901 if (evaluate)
4902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#ifdef FEAT_FLOAT
4904 if (rettv->v_type == VAR_FLOAT)
4905 {
4906 f1 = rettv->vval.v_float;
4907 use_float = TRUE;
4908 n1 = 0;
4909 }
4910 else
4911#endif
4912 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914 if (error)
4915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 n1 = 0;
4919
4920 /*
4921 * Get the second variable.
4922 */
4923 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004924 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (var2.v_type == VAR_FLOAT)
4931 {
4932 if (!use_float)
4933 {
4934 f1 = n1;
4935 use_float = TRUE;
4936 }
4937 f2 = var2.vval.v_float;
4938 n2 = 0;
4939 }
4940 else
4941#endif
4942 {
4943 n2 = get_tv_number_chk(&var2, &error);
4944 clear_tv(&var2);
4945 if (error)
4946 return FAIL;
4947#ifdef FEAT_FLOAT
4948 if (use_float)
4949 f2 = n2;
4950#endif
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 /*
4954 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#ifdef FEAT_FLOAT
4958 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 if (op == '*')
4961 f1 = f1 * f2;
4962 else if (op == '/')
4963 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964# ifdef VMS
4965 /* VMS crashes on divide by zero, work around it */
4966 if (f2 == 0.0)
4967 {
4968 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 }
4975 else
4976 f1 = f1 / f2;
4977# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 /* We rely on the floating point library to handle divide
4979 * by zero to result in "inf" and not a crash. */
4980 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004985 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 return FAIL;
4987 }
4988 rettv->v_type = VAR_FLOAT;
4989 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 if (op == '*')
4995 n1 = n1 * n2;
4996 else if (op == '/')
4997 {
4998 if (n2 == 0) /* give an error message? */
4999 {
5000 if (n1 == 0)
5001 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5002 else if (n1 < 0)
5003 n1 = -0x7fffffffL;
5004 else
5005 n1 = 0x7fffffffL;
5006 }
5007 else
5008 n1 = n1 / n2;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012 if (n2 == 0) /* give an error message? */
5013 n1 = 0;
5014 else
5015 n1 = n1 % n2;
5016 }
5017 rettv->v_type = VAR_NUMBER;
5018 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 }
5022
5023 return OK;
5024}
5025
5026/*
5027 * Handle sixth level expression:
5028 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005029 * "string" string constant
5030 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * &option-name option value
5032 * @r register contents
5033 * identifier variable value
5034 * function() function call
5035 * $VAR environment variable
5036 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005037 * [expr, expr] List
5038 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *
5040 * Also handle:
5041 * ! in front logical NOT
5042 * - in front unary minus
5043 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * trailing [] subscript in String or List
5045 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * "arg" must point to the first non-white of the expression.
5048 * "arg" is advanced to the next non-white after the recognized expression.
5049 *
5050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005053eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005057 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 long n;
5060 int len;
5061 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *start_leader, *end_leader;
5063 int ret = OK;
5064 char_u *alias;
5065
5066 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 /*
5073 * Skip '!' and '-' characters. They are handled later.
5074 */
5075 start_leader = *arg;
5076 while (**arg == '!' || **arg == '-' || **arg == '+')
5077 *arg = skipwhite(*arg + 1);
5078 end_leader = *arg;
5079
5080 switch (**arg)
5081 {
5082 /*
5083 * Number constant.
5084 */
5085 case '0':
5086 case '1':
5087 case '2':
5088 case '3':
5089 case '4':
5090 case '5':
5091 case '6':
5092 case '7':
5093 case '8':
5094 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 {
5096#ifdef FEAT_FLOAT
5097 char_u *p = skipdigits(*arg + 1);
5098 int get_float = FALSE;
5099
5100 /* We accept a float when the format matches
5101 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005102 * strict to avoid backwards compatibility problems.
5103 * Don't look for a float after the "." operator, so that
5104 * ":let vers = 1.2.3" doesn't fail. */
5105 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 get_float = TRUE;
5108 p = skipdigits(p + 2);
5109 if (*p == 'e' || *p == 'E')
5110 {
5111 ++p;
5112 if (*p == '-' || *p == '+')
5113 ++p;
5114 if (!vim_isdigit(*p))
5115 get_float = FALSE;
5116 else
5117 p = skipdigits(p + 1);
5118 }
5119 if (ASCII_ISALPHA(*p) || *p == '.')
5120 get_float = FALSE;
5121 }
5122 if (get_float)
5123 {
5124 float_T f;
5125
5126 *arg += string2float(*arg, &f);
5127 if (evaluate)
5128 {
5129 rettv->v_type = VAR_FLOAT;
5130 rettv->vval.v_float = f;
5131 }
5132 }
5133 else
5134#endif
5135 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005136 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 *arg += len;
5138 if (evaluate)
5139 {
5140 rettv->v_type = VAR_NUMBER;
5141 rettv->vval.v_number = n;
5142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 /*
5148 * String constant: "string".
5149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152
5153 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 break;
5158
5159 /*
5160 * List: [expr, expr]
5161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 * Dictionary: {key: val, key: val}
5167 */
5168 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5169 break;
5170
5171 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Environment variable: $VAR.
5179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Register contents: @r.
5185 */
5186 case '@': ++*arg;
5187 if (evaluate)
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005190 rettv->vval.v_string = get_reg_contents(**arg,
5191 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 if (**arg != NUL)
5194 ++*arg;
5195 break;
5196
5197 /*
5198 * nested expression: (expression).
5199 */
5200 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (**arg == ')')
5203 ++*arg;
5204 else if (ret == OK)
5205 {
5206 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ret = FAIL;
5209 }
5210 break;
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215
5216 if (ret == NOTDONE)
5217 {
5218 /*
5219 * Must be a variable or function name.
5220 * Can also be a curly-braces kind of name: {expr}.
5221 */
5222 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (alias != NULL)
5225 s = alias;
5226
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 ret = FAIL;
5229 else
5230 {
5231 if (**arg == '(') /* recursive! */
5232 {
5233 /* If "s" is the name of a variable of type VAR_FUNC
5234 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005235 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236
5237 /* Invoke the function. */
5238 ret = get_func_tv(s, len, rettv, arg,
5239 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005240 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005241
5242 /* If evaluate is FALSE rettv->v_type was not set in
5243 * get_func_tv, but it's needed in handle_subscript() to parse
5244 * what follows. So set it here. */
5245 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5246 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005247 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248 rettv->v_type = VAR_FUNC;
5249 }
5250
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /* Stop the expression evaluation when immediately
5252 * aborting on error, or when an interrupt occurred or
5253 * an exception was thrown but not caught. */
5254 if (aborting())
5255 {
5256 if (ret == OK)
5257 clear_tv(rettv);
5258 ret = FAIL;
5259 }
5260 }
5261 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005262 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005263 else
5264 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005266 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 *arg = skipwhite(*arg);
5270
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5272 * expr(expr). */
5273 if (ret == OK)
5274 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 /*
5277 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5278 */
5279 if (ret == OK && evaluate && end_leader > start_leader)
5280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 int val = 0;
5283#ifdef FEAT_FLOAT
5284 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005286 if (rettv->v_type == VAR_FLOAT)
5287 f = rettv->vval.v_float;
5288 else
5289#endif
5290 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 clear_tv(rettv);
5294 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 else
5297 {
5298 while (end_leader > start_leader)
5299 {
5300 --end_leader;
5301 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302 {
5303#ifdef FEAT_FLOAT
5304 if (rettv->v_type == VAR_FLOAT)
5305 f = !f;
5306 else
5307#endif
5308 val = !val;
5309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 {
5312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 f = -f;
5315 else
5316#endif
5317 val = -val;
5318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 {
5323 clear_tv(rettv);
5324 rettv->vval.v_float = f;
5325 }
5326 else
5327#endif
5328 {
5329 clear_tv(rettv);
5330 rettv->v_type = VAR_NUMBER;
5331 rettv->vval.v_number = val;
5332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
5336 return ret;
5337}
5338
5339/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005340 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5341 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5343 */
5344 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005349 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350{
5351 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 long len = -1;
5355 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005359 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 if (verbose)
5362 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 return FAIL;
5364 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365#ifdef FEAT_FLOAT
5366 else if (rettv->v_type == VAR_FLOAT)
5367 {
5368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
5371 }
5372#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 /*
5377 * dict.name
5378 */
5379 key = *arg + 1;
5380 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5381 ;
5382 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 /*
5389 * something[idx]
5390 *
5391 * Get the (first) variable from inside the [].
5392 */
5393 *arg = skipwhite(*arg + 1);
5394 if (**arg == ':')
5395 empty1 = TRUE;
5396 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5397 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005398 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5399 {
5400 /* not a number or string */
5401 clear_tv(&var1);
5402 return FAIL;
5403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404
5405 /*
5406 * Get the second variable from inside the [:].
5407 */
5408 if (**arg == ':')
5409 {
5410 range = TRUE;
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ']')
5413 empty2 = TRUE;
5414 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 if (!empty1)
5417 clear_tv(&var1);
5418 return FAIL;
5419 }
5420 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5421 {
5422 /* not a number or string */
5423 if (!empty1)
5424 clear_tv(&var1);
5425 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426 return FAIL;
5427 }
5428 }
5429
5430 /* Check for the ']'. */
5431 if (**arg != ']')
5432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005433 if (verbose)
5434 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 clear_tv(&var1);
5436 if (range)
5437 clear_tv(&var2);
5438 return FAIL;
5439 }
5440 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 }
5442
5443 if (evaluate)
5444 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 n1 = 0;
5446 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 n1 = get_tv_number(&var1);
5449 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451 if (range)
5452 {
5453 if (empty2)
5454 n2 = -1;
5455 else
5456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 n2 = get_tv_number(&var2);
5458 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 }
5461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 {
5464 case VAR_NUMBER:
5465 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 len = (long)STRLEN(s);
5468 if (range)
5469 {
5470 /* The resulting variable is a substring. If the indexes
5471 * are out of range the result is empty. */
5472 if (n1 < 0)
5473 {
5474 n1 = len + n1;
5475 if (n1 < 0)
5476 n1 = 0;
5477 }
5478 if (n2 < 0)
5479 n2 = len + n2;
5480 else if (n2 >= len)
5481 n2 = len;
5482 if (n1 >= len || n2 < 0 || n1 > n2)
5483 s = NULL;
5484 else
5485 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5486 }
5487 else
5488 {
5489 /* The resulting variable is a string of a single
5490 * character. If the index is too big or negative the
5491 * result is empty. */
5492 if (n1 >= len || n1 < 0)
5493 s = NULL;
5494 else
5495 s = vim_strnsave(s + n1, 1);
5496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 clear_tv(rettv);
5498 rettv->v_type = VAR_STRING;
5499 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 break;
5501
5502 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 if (n1 < 0)
5505 n1 = len + n1;
5506 if (!empty1 && (n1 < 0 || n1 >= len))
5507 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005508 /* For a range we allow invalid values and return an empty
5509 * list. A list index out of range is an error. */
5510 if (!range)
5511 {
5512 if (verbose)
5513 EMSGN(_(e_listidx), n1);
5514 return FAIL;
5515 }
5516 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 }
5518 if (range)
5519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 list_T *l;
5521 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522
5523 if (n2 < 0)
5524 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005525 else if (n2 >= len)
5526 n2 = len - 1;
5527 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005528 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 l = list_alloc();
5530 if (l == NULL)
5531 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 n1 <= n2; ++n1)
5534 {
5535 if (list_append_tv(l, &item->li_tv) == FAIL)
5536 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005537 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 return FAIL;
5539 }
5540 item = item->li_next;
5541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 clear_tv(rettv);
5543 rettv->v_type = VAR_LIST;
5544 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005545 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 }
5547 else
5548 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005549 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 }
5553 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554
5555 case VAR_DICT:
5556 if (range)
5557 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005558 if (verbose)
5559 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 if (len == -1)
5561 clear_tv(&var1);
5562 return FAIL;
5563 }
5564 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005565 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566
5567 if (len == -1)
5568 {
5569 key = get_tv_string(&var1);
5570 if (*key == NUL)
5571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005572 if (verbose)
5573 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 clear_tv(&var1);
5575 return FAIL;
5576 }
5577 }
5578
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005579 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005582 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 if (len == -1)
5584 clear_tv(&var1);
5585 if (item == NULL)
5586 return FAIL;
5587
5588 copy_tv(&item->di_tv, &var1);
5589 clear_tv(rettv);
5590 *rettv = var1;
5591 }
5592 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 }
5594 }
5595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 return OK;
5597}
5598
5599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 * Get an option value.
5601 * "arg" points to the '&' or '+' before the option name.
5602 * "arg" is advanced to character after the option name.
5603 * Return OK or FAIL.
5604 */
5605 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005608 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 int evaluate;
5610{
5611 char_u *option_end;
5612 long numval;
5613 char_u *stringval;
5614 int opt_type;
5615 int c;
5616 int working = (**arg == '+'); /* has("+option") */
5617 int ret = OK;
5618 int opt_flags;
5619
5620 /*
5621 * Isolate the option name and find its value.
5622 */
5623 option_end = find_option_end(arg, &opt_flags);
5624 if (option_end == NULL)
5625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 EMSG2(_("E112: Option name missing: %s"), *arg);
5628 return FAIL;
5629 }
5630
5631 if (!evaluate)
5632 {
5633 *arg = option_end;
5634 return OK;
5635 }
5636
5637 c = *option_end;
5638 *option_end = NUL;
5639 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642 if (opt_type == -3) /* invalid name */
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 EMSG2(_("E113: Unknown option: %s"), *arg);
5646 ret = FAIL;
5647 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
5650 if (opt_type == -2) /* hidden string option */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv->v_type = VAR_STRING;
5653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 else if (opt_type == -1) /* hidden number option */
5656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 rettv->v_type = VAR_NUMBER;
5658 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else if (opt_type == 1) /* number option */
5661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 rettv->v_type = VAR_NUMBER;
5663 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665 else /* string option */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 }
5671 else if (working && (opt_type == -2 || opt_type == -1))
5672 ret = FAIL;
5673
5674 *option_end = c; /* put back for error messages */
5675 *arg = option_end;
5676
5677 return ret;
5678}
5679
5680/*
5681 * Allocate a variable for a string constant.
5682 * Return OK or FAIL.
5683 */
5684 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 int evaluate;
5689{
5690 char_u *p;
5691 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 int extra = 0;
5693
5694 /*
5695 * Find the end of the string, skipping backslashed characters.
5696 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 if (*p == '\\' && p[1] != NUL)
5700 {
5701 ++p;
5702 /* A "\<x>" form occupies at least 4 characters, and produces up
5703 * to 6 characters: reserve space for 2 extra */
5704 if (*p == '<')
5705 extra += 2;
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708
5709 if (*p != '"')
5710 {
5711 EMSG2(_("E114: Missing quote: %s"), *arg);
5712 return FAIL;
5713 }
5714
5715 /* If only parsing, set *arg and return here */
5716 if (!evaluate)
5717 {
5718 *arg = p + 1;
5719 return OK;
5720 }
5721
5722 /*
5723 * Copy the string into allocated memory, handling backslashed
5724 * characters.
5725 */
5726 name = alloc((unsigned)(p - *arg + extra));
5727 if (name == NULL)
5728 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 rettv->v_type = VAR_STRING;
5730 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
5734 if (*p == '\\')
5735 {
5736 switch (*++p)
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 case 'b': *name++ = BS; ++p; break;
5739 case 'e': *name++ = ESC; ++p; break;
5740 case 'f': *name++ = FF; ++p; break;
5741 case 'n': *name++ = NL; ++p; break;
5742 case 'r': *name++ = CAR; ++p; break;
5743 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 case 'X': /* hex: "\x1", "\x12" */
5746 case 'x':
5747 case 'u': /* Unicode: "\u0023" */
5748 case 'U':
5749 if (vim_isxdigit(p[1]))
5750 {
5751 int n, nr;
5752 int c = toupper(*p);
5753
5754 if (c == 'X')
5755 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005756 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005758 else
5759 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 nr = 0;
5761 while (--n >= 0 && vim_isxdigit(p[1]))
5762 {
5763 ++p;
5764 nr = (nr << 4) + hex2nr(*p);
5765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#ifdef FEAT_MBYTE
5768 /* For "\u" store the number according to
5769 * 'encoding'. */
5770 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
5773#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 break;
5777
5778 /* octal: "\1", "\12", "\123" */
5779 case '0':
5780 case '1':
5781 case '2':
5782 case '3':
5783 case '4':
5784 case '5':
5785 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 case '7': *name = *p++ - '0';
5787 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 *name = (*name << 3) + *p++ - '0';
5790 if (*p >= '0' && *p <= '7')
5791 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (extra != 0)
5799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802 }
5803 /* FALLTHROUGH */
5804
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808 }
5809 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 *arg = p + 1;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 return OK;
5817}
5818
5819/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * Return OK or FAIL.
5822 */
5823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005824get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 int evaluate;
5828{
5829 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 int reduce = 0;
5832
5833 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 */
5836 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 break;
5842 ++reduce;
5843 ++p;
5844 }
5845 }
5846
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 return FAIL;
5851 }
5852
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 if (!evaluate)
5855 {
5856 *arg = p + 1;
5857 return OK;
5858 }
5859
5860 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 */
5863 str = alloc((unsigned)((p - *arg) - reduce));
5864 if (str == NULL)
5865 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 rettv->v_type = VAR_STRING;
5867 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 break;
5875 ++p;
5876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 *arg = p + 1;
5881
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 return OK;
5883}
5884
5885/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 * Allocate a variable for a List and fill it from "*arg".
5887 * Return OK or FAIL.
5888 */
5889 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005890get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 int evaluate;
5894{
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 list_T *l = NULL;
5896 typval_T tv;
5897 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898
5899 if (evaluate)
5900 {
5901 l = list_alloc();
5902 if (l == NULL)
5903 return FAIL;
5904 }
5905
5906 *arg = skipwhite(*arg + 1);
5907 while (**arg != ']' && **arg != NUL)
5908 {
5909 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5910 goto failret;
5911 if (evaluate)
5912 {
5913 item = listitem_alloc();
5914 if (item != NULL)
5915 {
5916 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005917 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 list_append(l, item);
5919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005920 else
5921 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 }
5923
5924 if (**arg == ']')
5925 break;
5926 if (**arg != ',')
5927 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 goto failret;
5930 }
5931 *arg = skipwhite(*arg + 1);
5932 }
5933
5934 if (**arg != ']')
5935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937failret:
5938 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 return FAIL;
5941 }
5942
5943 *arg = skipwhite(*arg + 1);
5944 if (evaluate)
5945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 rettv->v_type = VAR_LIST;
5947 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 ++l->lv_refcount;
5949 }
5950
5951 return OK;
5952}
5953
5954/*
5955 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005956 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005958 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959list_alloc()
5960{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005961 list_T *l;
5962
5963 l = (list_T *)alloc_clear(sizeof(list_T));
5964 if (l != NULL)
5965 {
5966 /* Prepend the list to the list of lists for garbage collection. */
5967 if (first_list != NULL)
5968 first_list->lv_used_prev = l;
5969 l->lv_used_prev = NULL;
5970 l->lv_used_next = first_list;
5971 first_list = l;
5972 }
5973 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974}
5975
5976/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005977 * Allocate an empty list for a return value.
5978 * Returns OK or FAIL.
5979 */
5980 static int
5981rettv_list_alloc(rettv)
5982 typval_T *rettv;
5983{
5984 list_T *l = list_alloc();
5985
5986 if (l == NULL)
5987 return FAIL;
5988
5989 rettv->vval.v_list = l;
5990 rettv->v_type = VAR_LIST;
5991 ++l->lv_refcount;
5992 return OK;
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Unreference a list: decrement the reference count and free it when it
5997 * becomes zero.
5998 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005999 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006003 if (l != NULL && --l->lv_refcount <= 0)
6004 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006008 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 * Ignores the reference count.
6010 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006011 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006012list_free(l, recurse)
6013 list_T *l;
6014 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015{
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 /* Remove the list from the list of lists for garbage collection. */
6019 if (l->lv_used_prev == NULL)
6020 first_list = l->lv_used_next;
6021 else
6022 l->lv_used_prev->lv_used_next = l->lv_used_next;
6023 if (l->lv_used_next != NULL)
6024 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6025
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006028 /* Remove the item before deleting it. */
6029 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006030 if (recurse || (item->li_tv.v_type != VAR_LIST
6031 && item->li_tv.v_type != VAR_DICT))
6032 clear_tv(&item->li_tv);
6033 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 }
6035 vim_free(l);
6036}
6037
6038/*
6039 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006040 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006042 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043listitem_alloc()
6044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046}
6047
6048/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006049 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006051 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 vim_free(item);
6057}
6058
6059/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006060 * Remove a list item from a List and free it. Also clears the value.
6061 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006062 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006063listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 list_T *l;
6065 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006067 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006068 listitem_free(item);
6069}
6070
6071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 * Get the number of items in a list.
6073 */
6074 static long
6075list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 if (l == NULL)
6079 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081}
6082
6083/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006084 * Return TRUE when two lists have exactly the same values.
6085 */
6086 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 list_T *l1;
6089 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092{
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006095 if (l1 == NULL || l2 == NULL)
6096 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006097 if (l1 == l2)
6098 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006099 if (list_len(l1) != list_len(l2))
6100 return FALSE;
6101
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 for (item1 = l1->lv_first, item2 = l2->lv_first;
6103 item1 != NULL && item2 != NULL;
6104 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 return FALSE;
6107 return item1 == NULL && item2 == NULL;
6108}
6109
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006110#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6111 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006112/*
6113 * Return the dictitem that an entry in a hashtable points to.
6114 */
6115 dictitem_T *
6116dict_lookup(hi)
6117 hashitem_T *hi;
6118{
6119 return HI2DI(hi);
6120}
6121#endif
6122
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124 * Return TRUE when two dictionaries have exactly the same key/values.
6125 */
6126 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006127dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 dict_T *d1;
6129 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006131 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
6134 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006137 if (d1 == NULL || d2 == NULL)
6138 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006139 if (d1 == d2)
6140 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 if (dict_len(d1) != dict_len(d2))
6142 return FALSE;
6143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006144 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 if (!HASHITEM_EMPTY(hi))
6148 {
6149 item2 = dict_find(d2, hi->hi_key, -1);
6150 if (item2 == NULL)
6151 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 return FALSE;
6154 --todo;
6155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 }
6157 return TRUE;
6158}
6159
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160static int tv_equal_recurse_limit;
6161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 */
6167 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 typval_T *tv1;
6170 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 int ic; /* ignore case */
6172 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173{
6174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 * recursiveness to a limit. We guess they are equal then.
6184 * A fixed limit has the problem of still taking an awful long time.
6185 * Reduce the limit every time running into it. That should work fine for
6186 * deeply linked structures that are not recursively linked and catch
6187 * recursiveness quickly. */
6188 if (!recursive)
6189 tv_equal_recurse_limit = 1000;
6190 if (recursive_cnt >= tv_equal_recurse_limit)
6191 {
6192 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006193 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 ++recursive_cnt;
6200 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6201 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006202 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 ++recursive_cnt;
6206 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006209
6210 case VAR_FUNC:
6211 return (tv1->vval.v_string != NULL
6212 && tv2->vval.v_string != NULL
6213 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6214
6215 case VAR_NUMBER:
6216 return tv1->vval.v_number == tv2->vval.v_number;
6217
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006218#ifdef FEAT_FLOAT
6219 case VAR_FLOAT:
6220 return tv1->vval.v_float == tv2->vval.v_float;
6221#endif
6222
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223 case VAR_STRING:
6224 s1 = get_tv_string_buf(tv1, buf1);
6225 s2 = get_tv_string_buf(tv2, buf2);
6226 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 return TRUE;
6231}
6232
6233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 * Locate item with index "n" in list "l" and return it.
6235 * A negative index is counted from the end; -1 is the last item.
6236 * Returns NULL when "n" is out of range.
6237 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 long n;
6242{
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 long idx;
6245
6246 if (l == NULL)
6247 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006248
6249 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006251 n = l->lv_len + n;
6252
6253 /* Check for index out of range. */
6254 if (n < 0 || n >= l->lv_len)
6255 return NULL;
6256
6257 /* When there is a cached index may start search from there. */
6258 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260 if (n < l->lv_idx / 2)
6261 {
6262 /* closest to the start of the list */
6263 item = l->lv_first;
6264 idx = 0;
6265 }
6266 else if (n > (l->lv_idx + l->lv_len) / 2)
6267 {
6268 /* closest to the end of the list */
6269 item = l->lv_last;
6270 idx = l->lv_len - 1;
6271 }
6272 else
6273 {
6274 /* closest to the cached index */
6275 item = l->lv_idx_item;
6276 idx = l->lv_idx;
6277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 }
6279 else
6280 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281 if (n < l->lv_len / 2)
6282 {
6283 /* closest to the start of the list */
6284 item = l->lv_first;
6285 idx = 0;
6286 }
6287 else
6288 {
6289 /* closest to the end of the list */
6290 item = l->lv_last;
6291 idx = l->lv_len - 1;
6292 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294
6295 while (n > idx)
6296 {
6297 /* search forward */
6298 item = item->li_next;
6299 ++idx;
6300 }
6301 while (n < idx)
6302 {
6303 /* search backward */
6304 item = item->li_prev;
6305 --idx;
6306 }
6307
6308 /* cache the used index */
6309 l->lv_idx = idx;
6310 l->lv_idx_item = item;
6311
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 return item;
6313}
6314
6315/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006316 * Get list item "l[idx]" as a number.
6317 */
6318 static long
6319list_find_nr(l, idx, errorp)
6320 list_T *l;
6321 long idx;
6322 int *errorp; /* set to TRUE when something wrong */
6323{
6324 listitem_T *li;
6325
6326 li = list_find(l, idx);
6327 if (li == NULL)
6328 {
6329 if (errorp != NULL)
6330 *errorp = TRUE;
6331 return -1L;
6332 }
6333 return get_tv_number_chk(&li->li_tv, errorp);
6334}
6335
6336/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006337 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6338 */
6339 char_u *
6340list_find_str(l, idx)
6341 list_T *l;
6342 long idx;
6343{
6344 listitem_T *li;
6345
6346 li = list_find(l, idx - 1);
6347 if (li == NULL)
6348 {
6349 EMSGN(_(e_listidx), idx);
6350 return NULL;
6351 }
6352 return get_tv_string(&li->li_tv);
6353}
6354
6355/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006356 * Locate "item" list "l" and return its index.
6357 * Returns -1 when "item" is not in the list.
6358 */
6359 static long
6360list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 list_T *l;
6362 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006363{
6364 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366
6367 if (l == NULL)
6368 return -1;
6369 idx = 0;
6370 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6371 ++idx;
6372 if (li == NULL)
6373 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006374 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375}
6376
6377/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 * Append item "item" to the end of list "l".
6379 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006380 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 list_T *l;
6383 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384{
6385 if (l->lv_last == NULL)
6386 {
6387 /* empty list */
6388 l->lv_first = item;
6389 l->lv_last = item;
6390 item->li_prev = NULL;
6391 }
6392 else
6393 {
6394 l->lv_last->li_next = item;
6395 item->li_prev = l->lv_last;
6396 l->lv_last = item;
6397 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 item->li_next = NULL;
6400}
6401
6402/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006406 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 list_T *l;
6409 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006411 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412
Bram Moolenaar05159a02005-02-26 23:04:13 +00006413 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 copy_tv(tv, &li->li_tv);
6416 list_append(l, li);
6417 return OK;
6418}
6419
6420/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006421 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 * Return FAIL when out of memory.
6423 */
6424 int
6425list_append_dict(list, dict)
6426 list_T *list;
6427 dict_T *dict;
6428{
6429 listitem_T *li = listitem_alloc();
6430
6431 if (li == NULL)
6432 return FAIL;
6433 li->li_tv.v_type = VAR_DICT;
6434 li->li_tv.v_lock = 0;
6435 li->li_tv.vval.v_dict = dict;
6436 list_append(list, li);
6437 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 return OK;
6439}
6440
6441/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006442 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006443 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 * Returns FAIL when out of memory.
6445 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006446 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006447list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 list_T *l;
6449 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451{
6452 listitem_T *li = listitem_alloc();
6453
6454 if (li == NULL)
6455 return FAIL;
6456 list_append(l, li);
6457 li->li_tv.v_type = VAR_STRING;
6458 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006459 if (str == NULL)
6460 li->li_tv.vval.v_string = NULL;
6461 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006462 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 return FAIL;
6464 return OK;
6465}
6466
6467/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006468 * Append "n" to list "l".
6469 * Returns FAIL when out of memory.
6470 */
6471 static int
6472list_append_number(l, n)
6473 list_T *l;
6474 varnumber_T n;
6475{
6476 listitem_T *li;
6477
6478 li = listitem_alloc();
6479 if (li == NULL)
6480 return FAIL;
6481 li->li_tv.v_type = VAR_NUMBER;
6482 li->li_tv.v_lock = 0;
6483 li->li_tv.vval.v_number = n;
6484 list_append(l, li);
6485 return OK;
6486}
6487
6488/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490 * If "item" is NULL append at the end.
6491 * Return FAIL when out of memory.
6492 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006493 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 list_T *l;
6496 typval_T *tv;
6497 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498{
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500
6501 if (ni == NULL)
6502 return FAIL;
6503 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006504 list_insert(l, ni, item);
6505 return OK;
6506}
6507
6508 void
6509list_insert(l, ni, item)
6510 list_T *l;
6511 listitem_T *ni;
6512 listitem_T *item;
6513{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 if (item == NULL)
6515 /* Append new item at end of list. */
6516 list_append(l, ni);
6517 else
6518 {
6519 /* Insert new item before existing item. */
6520 ni->li_prev = item->li_prev;
6521 ni->li_next = item;
6522 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 ++l->lv_idx;
6526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006530 l->lv_idx_item = NULL;
6531 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535}
6536
6537/*
6538 * Extend "l1" with "l2".
6539 * If "bef" is NULL append at the end, otherwise insert before this item.
6540 * Returns FAIL when out of memory.
6541 */
6542 static int
6543list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 list_T *l1;
6545 list_T *l2;
6546 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547{
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006549 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006551 /* We also quit the loop when we have inserted the original item count of
6552 * the list, avoid a hang when we extend a list with itself. */
6553 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6555 return FAIL;
6556 return OK;
6557}
6558
6559/*
6560 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6561 * Return FAIL when out of memory.
6562 */
6563 static int
6564list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 list_T *l1;
6566 list_T *l2;
6567 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568{
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006571 if (l1 == NULL || l2 == NULL)
6572 return FAIL;
6573
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 if (l == NULL)
6577 return FAIL;
6578 tv->v_type = VAR_LIST;
6579 tv->vval.v_list = l;
6580
6581 /* append all items from the second list */
6582 return list_extend(l, l2, NULL);
6583}
6584
6585/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 * Returns NULL when out of memory.
6590 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006593 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596{
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 list_T *copy;
6598 listitem_T *item;
6599 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600
6601 if (orig == NULL)
6602 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 copy = list_alloc();
6605 if (copy != NULL)
6606 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 if (copyID != 0)
6608 {
6609 /* Do this before adding the items, because one of the items may
6610 * refer back to this list. */
6611 orig->lv_copyID = copyID;
6612 orig->lv_copylist = copy;
6613 }
6614 for (item = orig->lv_first; item != NULL && !got_int;
6615 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 {
6617 ni = listitem_alloc();
6618 if (ni == NULL)
6619 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 {
6622 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6623 {
6624 vim_free(ni);
6625 break;
6626 }
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006629 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 list_append(copy, ni);
6631 }
6632 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 if (item != NULL)
6634 {
6635 list_unref(copy);
6636 copy = NULL;
6637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 }
6639
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 return copy;
6641}
6642
6643/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006645 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006646 * This used to be called list_remove, but that conflicts with a Sun header
6647 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006649 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006650vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 list_T *l;
6652 listitem_T *item;
6653 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006654{
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657 /* notify watchers */
6658 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661 list_fix_watch(l, ip);
6662 if (ip == item2)
6663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665
6666 if (item2->li_next == NULL)
6667 l->lv_last = item->li_prev;
6668 else
6669 item2->li_next->li_prev = item->li_prev;
6670 if (item->li_prev == NULL)
6671 l->lv_first = item2->li_next;
6672 else
6673 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006674 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675}
6676
6677/*
6678 * Return an allocated string with the string representation of a list.
6679 * May return NULL.
6680 */
6681 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006684 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685{
6686 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687
6688 if (tv->vval.v_list == NULL)
6689 return NULL;
6690 ga_init2(&ga, (int)sizeof(char), 80);
6691 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006693 {
6694 vim_free(ga.ga_data);
6695 return NULL;
6696 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 ga_append(&ga, ']');
6698 ga_append(&ga, NUL);
6699 return (char_u *)ga.ga_data;
6700}
6701
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702typedef struct join_S {
6703 char_u *s;
6704 char_u *tofree;
6705} join_T;
6706
6707 static int
6708list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6709 garray_T *gap; /* to store the result in */
6710 list_T *l;
6711 char_u *sep;
6712 int echo_style;
6713 int copyID;
6714 garray_T *join_gap; /* to keep each list item string */
6715{
6716 int i;
6717 join_T *p;
6718 int len;
6719 int sumlen = 0;
6720 int first = TRUE;
6721 char_u *tofree;
6722 char_u numbuf[NUMBUFLEN];
6723 listitem_T *item;
6724 char_u *s;
6725
6726 /* Stringify each item in the list. */
6727 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6728 {
6729 if (echo_style)
6730 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6731 else
6732 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6733 if (s == NULL)
6734 return FAIL;
6735
6736 len = (int)STRLEN(s);
6737 sumlen += len;
6738
Bram Moolenaarcde88542015-08-11 19:14:00 +02006739 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6741 if (tofree != NULL || s != numbuf)
6742 {
6743 p->s = s;
6744 p->tofree = tofree;
6745 }
6746 else
6747 {
6748 p->s = vim_strnsave(s, len);
6749 p->tofree = p->s;
6750 }
6751
6752 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006753 if (did_echo_string_emsg) /* recursion error, bail out */
6754 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006755 }
6756
6757 /* Allocate result buffer with its total size, avoid re-allocation and
6758 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6759 if (join_gap->ga_len >= 2)
6760 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6761 if (ga_grow(gap, sumlen + 2) == FAIL)
6762 return FAIL;
6763
6764 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6765 {
6766 if (first)
6767 first = FALSE;
6768 else
6769 ga_concat(gap, sep);
6770 p = ((join_T *)join_gap->ga_data) + i;
6771
6772 if (p->s != NULL)
6773 ga_concat(gap, p->s);
6774 line_breakcheck();
6775 }
6776
6777 return OK;
6778}
6779
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006782 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006785 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006786list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006790 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006791 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006793 garray_T join_ga;
6794 int retval;
6795 join_T *p;
6796 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797
Bram Moolenaard39a7512015-04-16 22:51:22 +02006798 if (l->lv_len < 1)
6799 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006800 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6801 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6802
6803 /* Dispose each item in join_ga. */
6804 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806 p = (join_T *)join_ga.ga_data;
6807 for (i = 0; i < join_ga.ga_len; ++i)
6808 {
6809 vim_free(p->tofree);
6810 ++p;
6811 }
6812 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814
6815 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816}
6817
6818/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 * Garbage collection for lists and dictionaries.
6820 *
6821 * We use reference counts to be able to free most items right away when they
6822 * are no longer used. But for composite items it's possible that it becomes
6823 * unused while the reference count is > 0: When there is a recursive
6824 * reference. Example:
6825 * :let l = [1, 2, 3]
6826 * :let d = {9: l}
6827 * :let l[1] = d
6828 *
6829 * Since this is quite unusual we handle this with garbage collection: every
6830 * once in a while find out which lists and dicts are not referenced from any
6831 * variable.
6832 *
6833 * Here is a good reference text about garbage collection (refers to Python
6834 * but it applies to all reference-counting mechanisms):
6835 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837
6838/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 * Do garbage collection for lists and dicts.
6840 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006841 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 int
6843garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006845 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006846 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 buf_T *buf;
6848 win_T *wp;
6849 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006850 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006851 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006853#ifdef FEAT_WINDOWS
6854 tabpage_T *tp;
6855#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006857 /* Only do this once. */
6858 want_garbage_collect = FALSE;
6859 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006860 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006861
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 /* We advance by two because we add one for items referenced through
6863 * previous_funccal. */
6864 current_copyID += COPYID_INC;
6865 copyID = current_copyID;
6866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
6868 * 1. Go through all accessible variables and mark all lists and dicts
6869 * with copyID.
6870 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871
6872 /* Don't free variables in the previous_funccal list unless they are only
6873 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006874 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6876 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6878 NULL);
6879 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6880 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 }
6882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 /* script-local variables */
6884 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* buffer-local variables */
6888 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
6892 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006896#ifdef FEAT_AUTOCMD
6897 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6899 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006900#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#ifdef FEAT_WINDOWS
6903 /* tabpage-local variables */
6904 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907#endif
6908
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911
6912 /* function-local variables */
6913 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6914 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6916 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 }
6918
Bram Moolenaard812df62008-11-09 12:46:09 +00006919 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006921
Bram Moolenaar1dced572012-04-05 16:54:08 +02006922#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006924#endif
6925
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006928#endif
6929
6930#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006932#endif
6933
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006935 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 /*
6937 * 2. Free lists and dictionaries that are not referenced.
6938 */
6939 did_free = free_unref_items(copyID);
6940
6941 /*
6942 * 3. Check if any funccal can be freed now.
6943 */
6944 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (can_free_funccal(*pfc, copyID))
6947 {
6948 fc = *pfc;
6949 *pfc = fc->caller;
6950 free_funccal(fc, TRUE);
6951 did_free = TRUE;
6952 did_free_funccal = TRUE;
6953 }
6954 else
6955 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 if (did_free_funccal)
6958 /* When a funccal was freed some more items might be garbage
6959 * collected, so run again. */
6960 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 else if (p_verbose > 0)
6963 {
6964 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6965 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966
6967 return did_free;
6968}
6969
6970/*
6971 * Free lists and dictionaries that are no longer referenced.
6972 */
6973 static int
6974free_unref_items(copyID)
6975 int copyID;
6976{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006977 dict_T *dd, *dd_next;
6978 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 int did_free = FALSE;
6980
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 */
6984 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 {
6986 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006989 /* Free the Dictionary and ordinary items it contains, but don't
6990 * recurse into Lists and Dictionaries, they will be in the list
6991 * of dicts or list of lists. */
6992 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006995 dd = dd_next;
6996 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997
6998 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 * Go through the list of lists and free items without the copyID.
7000 * But don't free a list that has a watcher (used in a for loop), these
7001 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7007 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007009 /* Free the List and ordinary items it contains, but don't recurse
7010 * into Lists and Dictionaries, they will be in the list of dicts
7011 * or list of lists. */
7012 list_free(ll, 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 ll = ll_next;
7016 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 return did_free;
7018}
7019
7020/*
7021 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 * "list_stack" is used to add lists to be marked. Can be NULL.
7023 *
7024 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 int
7027set_ref_in_ht(ht, copyID, list_stack)
7028 hashtab_T *ht;
7029 int copyID;
7030 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031{
7032 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 hashtab_T *cur_ht;
7036 ht_stack_T *ht_stack = NULL;
7037 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 cur_ht = ht;
7040 for (;;)
7041 {
7042 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 /* Mark each item in the hashtab. If the item contains a hashtab
7045 * it is added to ht_stack, if it contains a list it is added to
7046 * list_stack. */
7047 todo = (int)cur_ht->ht_used;
7048 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7049 if (!HASHITEM_EMPTY(hi))
7050 {
7051 --todo;
7052 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7053 &ht_stack, list_stack);
7054 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056
7057 if (ht_stack == NULL)
7058 break;
7059
7060 /* take an item from the stack */
7061 cur_ht = ht_stack->ht;
7062 tempitem = ht_stack;
7063 ht_stack = ht_stack->prev;
7064 free(tempitem);
7065 }
7066
7067 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068}
7069
7070/*
7071 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7073 *
7074 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 int
7077set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 list_T *l;
7079 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082 listitem_T *li;
7083 int abort = FALSE;
7084 list_T *cur_l;
7085 list_stack_T *list_stack = NULL;
7086 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 cur_l = l;
7089 for (;;)
7090 {
7091 if (!abort)
7092 /* Mark each item in the list. If the item contains a hashtab
7093 * it is added to ht_stack, if it contains a list it is added to
7094 * list_stack. */
7095 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7096 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7097 ht_stack, &list_stack);
7098 if (list_stack == NULL)
7099 break;
7100
7101 /* take an item from the stack */
7102 cur_l = list_stack->list;
7103 tempitem = list_stack;
7104 list_stack = list_stack->prev;
7105 free(tempitem);
7106 }
7107
7108 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109}
7110
7111/*
7112 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 * "list_stack" is used to add lists to be marked. Can be NULL.
7114 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7115 *
7116 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 int
7119set_ref_in_item(tv, copyID, ht_stack, list_stack)
7120 typval_T *tv;
7121 int copyID;
7122 ht_stack_T **ht_stack;
7123 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124{
7125 dict_T *dd;
7126 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128
7129 switch (tv->v_type)
7130 {
7131 case VAR_DICT:
7132 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007133 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007135 /* Didn't see this dict yet. */
7136 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 if (ht_stack == NULL)
7138 {
7139 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7140 }
7141 else
7142 {
7143 ht_stack_T *newitem = (ht_stack_T*)malloc(
7144 sizeof(ht_stack_T));
7145 if (newitem == NULL)
7146 abort = TRUE;
7147 else
7148 {
7149 newitem->ht = &dd->dv_hashtab;
7150 newitem->prev = *ht_stack;
7151 *ht_stack = newitem;
7152 }
7153 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007155 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007156
7157 case VAR_LIST:
7158 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007159 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007161 /* Didn't see this list yet. */
7162 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007163 if (list_stack == NULL)
7164 {
7165 abort = set_ref_in_list(ll, copyID, ht_stack);
7166 }
7167 else
7168 {
7169 list_stack_T *newitem = (list_stack_T*)malloc(
7170 sizeof(list_stack_T));
7171 if (newitem == NULL)
7172 abort = TRUE;
7173 else
7174 {
7175 newitem->list = ll;
7176 newitem->prev = *list_stack;
7177 *list_stack = newitem;
7178 }
7179 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007181 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007184}
7185
7186/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187 * Allocate an empty header for a dictionary.
7188 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007189 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190dict_alloc()
7191{
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007197 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198 if (first_dict != NULL)
7199 first_dict->dv_used_prev = d;
7200 d->dv_used_next = first_dict;
7201 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007202 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007205 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007206 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007207 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211}
7212
7213/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007214 * Allocate an empty dict for a return value.
7215 * Returns OK or FAIL.
7216 */
7217 static int
7218rettv_dict_alloc(rettv)
7219 typval_T *rettv;
7220{
7221 dict_T *d = dict_alloc();
7222
7223 if (d == NULL)
7224 return FAIL;
7225
7226 rettv->vval.v_dict = d;
7227 rettv->v_type = VAR_DICT;
7228 ++d->dv_refcount;
7229 return OK;
7230}
7231
7232
7233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Unreference a Dictionary: decrement the reference count and free it when it
7235 * becomes zero.
7236 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007237 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007241 if (d != NULL && --d->dv_refcount <= 0)
7242 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243}
7244
7245/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007246 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 * Ignores the reference count.
7248 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007249 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007250dict_free(d, recurse)
7251 dict_T *d;
7252 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007254 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007256 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007258 /* Remove the dict from the list of dicts for garbage collection. */
7259 if (d->dv_used_prev == NULL)
7260 first_dict = d->dv_used_next;
7261 else
7262 d->dv_used_prev->dv_used_next = d->dv_used_next;
7263 if (d->dv_used_next != NULL)
7264 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7265
7266 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007267 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007268 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 if (!HASHITEM_EMPTY(hi))
7272 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007273 /* Remove the item before deleting it, just in case there is
7274 * something recursive causing trouble. */
7275 di = HI2DI(hi);
7276 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007277 if (recurse || (di->di_tv.v_type != VAR_LIST
7278 && di->di_tv.v_type != VAR_DICT))
7279 clear_tv(&di->di_tv);
7280 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 --todo;
7282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 vim_free(d);
7286}
7287
7288/*
7289 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 * The "key" is copied to the new item.
7291 * Note that the value of the item "di_tv" still needs to be initialized!
7292 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007294 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295dictitem_alloc(key)
7296 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007304 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007305 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307}
7308
7309/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 * Make a copy of a Dictionary item.
7311 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315{
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007318 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7319 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 if (di != NULL)
7321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007323 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 copy_tv(&org->di_tv, &di->di_tv);
7325 }
7326 return di;
7327}
7328
7329/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 * Remove item "item" from Dictionary "dict" and free it.
7331 */
7332 static void
7333dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 dict_T *dict;
7335 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336{
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 if (HASHITEM_EMPTY(hi))
7341 EMSG2(_(e_intern2), "dictitem_remove()");
7342 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 dictitem_free(item);
7345}
7346
7347/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348 * Free a dict item. Also clears the value.
7349 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007350 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007355 if (item->di_flags & DI_FLAGS_ALLOC)
7356 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357}
7358
7359/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7361 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 * Returns NULL when out of memory.
7364 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370{
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dict_T *copy;
7372 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375
7376 if (orig == NULL)
7377 return NULL;
7378
7379 copy = dict_alloc();
7380 if (copy != NULL)
7381 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007382 if (copyID != 0)
7383 {
7384 orig->dv_copyID = copyID;
7385 orig->dv_copydict = copy;
7386 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007387 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 --todo;
7393
7394 di = dictitem_alloc(hi->hi_key);
7395 if (di == NULL)
7396 break;
7397 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 {
7399 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7400 copyID) == FAIL)
7401 {
7402 vim_free(di);
7403 break;
7404 }
7405 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406 else
7407 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7408 if (dict_add(copy, di) == FAIL)
7409 {
7410 dictitem_free(di);
7411 break;
7412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (todo > 0)
7418 {
7419 dict_unref(copy);
7420 copy = NULL;
7421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
7423
7424 return copy;
7425}
7426
7427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007429 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007431 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 dict_T *d;
7434 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435{
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437}
7438
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007440 * Add a number or string entry to dictionary "d".
7441 * When "str" is NULL use number "nr", otherwise use "str".
7442 * Returns FAIL when out of memory and when key already exists.
7443 */
7444 int
7445dict_add_nr_str(d, key, nr, str)
7446 dict_T *d;
7447 char *key;
7448 long nr;
7449 char_u *str;
7450{
7451 dictitem_T *item;
7452
7453 item = dictitem_alloc((char_u *)key);
7454 if (item == NULL)
7455 return FAIL;
7456 item->di_tv.v_lock = 0;
7457 if (str == NULL)
7458 {
7459 item->di_tv.v_type = VAR_NUMBER;
7460 item->di_tv.vval.v_number = nr;
7461 }
7462 else
7463 {
7464 item->di_tv.v_type = VAR_STRING;
7465 item->di_tv.vval.v_string = vim_strsave(str);
7466 }
7467 if (dict_add(d, item) == FAIL)
7468 {
7469 dictitem_free(item);
7470 return FAIL;
7471 }
7472 return OK;
7473}
7474
7475/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007476 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007477 * Returns FAIL when out of memory and when key already exists.
7478 */
7479 int
7480dict_add_list(d, key, list)
7481 dict_T *d;
7482 char *key;
7483 list_T *list;
7484{
7485 dictitem_T *item;
7486
7487 item = dictitem_alloc((char_u *)key);
7488 if (item == NULL)
7489 return FAIL;
7490 item->di_tv.v_lock = 0;
7491 item->di_tv.v_type = VAR_LIST;
7492 item->di_tv.vval.v_list = list;
7493 if (dict_add(d, item) == FAIL)
7494 {
7495 dictitem_free(item);
7496 return FAIL;
7497 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007498 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007499 return OK;
7500}
7501
7502/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503 * Get the number of items in a Dictionary.
7504 */
7505 static long
7506dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007509 if (d == NULL)
7510 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007511 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512}
7513
7514/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 * Find item "key[len]" in Dictionary "d".
7516 * If "len" is negative use strlen(key).
7517 * Returns NULL when not found.
7518 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007519 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 char_u *key;
7523 int len;
7524{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525#define AKEYLEN 200
7526 char_u buf[AKEYLEN];
7527 char_u *akey;
7528 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (len < 0)
7532 akey = key;
7533 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007534 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 tofree = akey = vim_strnsave(key, len);
7536 if (akey == NULL)
7537 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 else
7540 {
7541 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007542 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 akey = buf;
7544 }
7545
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 vim_free(tofree);
7548 if (HASHITEM_EMPTY(hi))
7549 return NULL;
7550 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551}
7552
7553/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 * Get a string item from a dictionary.
7555 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007556 * Returns NULL if the entry doesn't exist or out of memory.
7557 */
7558 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007559get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007560 dict_T *d;
7561 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563{
7564 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007565 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007566
7567 di = dict_find(d, key, -1);
7568 if (di == NULL)
7569 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007570 s = get_tv_string(&di->di_tv);
7571 if (save && s != NULL)
7572 s = vim_strsave(s);
7573 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007574}
7575
7576/*
7577 * Get a number item from a dictionary.
7578 * Returns 0 if the entry doesn't exist or out of memory.
7579 */
7580 long
7581get_dict_number(d, key)
7582 dict_T *d;
7583 char_u *key;
7584{
7585 dictitem_T *di;
7586
7587 di = dict_find(d, key, -1);
7588 if (di == NULL)
7589 return 0;
7590 return get_tv_number(&di->di_tv);
7591}
7592
7593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 * Return an allocated string with the string representation of a Dictionary.
7595 * May return NULL.
7596 */
7597 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007600 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601{
7602 garray_T ga;
7603 int first = TRUE;
7604 char_u *tofree;
7605 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007609 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007611 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 return NULL;
7613 ga_init2(&ga, (int)sizeof(char), 80);
7614 ga_append(&ga, '{');
7615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007616 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007617 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 --todo;
7622
7623 if (first)
7624 first = FALSE;
7625 else
7626 ga_concat(&ga, (char_u *)", ");
7627
7628 tofree = string_quote(hi->hi_key, FALSE);
7629 if (tofree != NULL)
7630 {
7631 ga_concat(&ga, tofree);
7632 vim_free(tofree);
7633 }
7634 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 if (s != NULL)
7637 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007639 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007640 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007641 line_breakcheck();
7642
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007645 if (todo > 0)
7646 {
7647 vim_free(ga.ga_data);
7648 return NULL;
7649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
7651 ga_append(&ga, '}');
7652 ga_append(&ga, NUL);
7653 return (char_u *)ga.ga_data;
7654}
7655
7656/*
7657 * Allocate a variable for a Dictionary and fill it from "*arg".
7658 * Return OK or FAIL. Returns NOTDONE for {expr}.
7659 */
7660 static int
7661get_dict_tv(arg, rettv, evaluate)
7662 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 int evaluate;
7665{
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 dict_T *d = NULL;
7667 typval_T tvkey;
7668 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007669 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673
7674 /*
7675 * First check if it's not a curly-braces thing: {expr}.
7676 * Must do this without evaluating, otherwise a function may be called
7677 * twice. Unfortunately this means we need to call eval1() twice for the
7678 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007679 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 if (*start != '}')
7682 {
7683 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7684 return FAIL;
7685 if (*start == '}')
7686 return NOTDONE;
7687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 if (evaluate)
7690 {
7691 d = dict_alloc();
7692 if (d == NULL)
7693 return FAIL;
7694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 tvkey.v_type = VAR_UNKNOWN;
7696 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697
7698 *arg = skipwhite(*arg + 1);
7699 while (**arg != '}' && **arg != NUL)
7700 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007701 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 goto failret;
7703 if (**arg != ':')
7704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007705 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 goto failret;
7708 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007709 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007711 key = get_tv_string_buf_chk(&tvkey, buf);
7712 if (key == NULL || *key == NUL)
7713 {
7714 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7715 if (key != NULL)
7716 EMSG(_(e_emptykey));
7717 clear_tv(&tvkey);
7718 goto failret;
7719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721
7722 *arg = skipwhite(*arg + 1);
7723 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7724 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007725 if (evaluate)
7726 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 goto failret;
7728 }
7729 if (evaluate)
7730 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007731 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 if (item != NULL)
7733 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007734 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007735 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 clear_tv(&tv);
7737 goto failret;
7738 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 item = dictitem_alloc(key);
7740 clear_tv(&tvkey);
7741 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007744 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007745 if (dict_add(d, item) == FAIL)
7746 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 }
7748 }
7749
7750 if (**arg == '}')
7751 break;
7752 if (**arg != ',')
7753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007754 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 goto failret;
7756 }
7757 *arg = skipwhite(*arg + 1);
7758 }
7759
7760 if (**arg != '}')
7761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007762 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763failret:
7764 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007765 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 return FAIL;
7767 }
7768
7769 *arg = skipwhite(*arg + 1);
7770 if (evaluate)
7771 {
7772 rettv->v_type = VAR_DICT;
7773 rettv->vval.v_dict = d;
7774 ++d->dv_refcount;
7775 }
7776
7777 return OK;
7778}
7779
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 * Return a string with the string representation of a variable.
7782 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007783 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007785 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007786 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007787 */
7788 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007789echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007790 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007791 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007792 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007793 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 static int recurse = 0;
7796 char_u *r = NULL;
7797
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007800 if (!did_echo_string_emsg)
7801 {
7802 /* Only give this message once for a recursive call to avoid
7803 * flooding the user with errors. And stop iterating over lists
7804 * and dicts. */
7805 did_echo_string_emsg = TRUE;
7806 EMSG(_("E724: variable nested too deep for displaying"));
7807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007809 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007810 }
7811 ++recurse;
7812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 switch (tv->v_type)
7814 {
7815 case VAR_FUNC:
7816 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 r = tv->vval.v_string;
7818 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007821 if (tv->vval.v_list == NULL)
7822 {
7823 *tofree = NULL;
7824 r = NULL;
7825 }
7826 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7827 {
7828 *tofree = NULL;
7829 r = (char_u *)"[...]";
7830 }
7831 else
7832 {
7833 tv->vval.v_list->lv_copyID = copyID;
7834 *tofree = list2string(tv, copyID);
7835 r = *tofree;
7836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_dict == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"{...}";
7849 }
7850 else
7851 {
7852 tv->vval.v_dict->dv_copyID = copyID;
7853 *tofree = dict2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 case VAR_STRING:
7859 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 *tofree = NULL;
7861 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007862 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 case VAR_FLOAT:
7866 *tofree = NULL;
7867 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7868 r = numbuf;
7869 break;
7870#endif
7871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876
Bram Moolenaar8502c702014-06-17 12:51:16 +02007877 if (--recurse == 0)
7878 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880}
7881
7882/*
7883 * Return a string with the string representation of a variable.
7884 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7885 * "numbuf" is used for a number.
7886 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007887 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 */
7889 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007890tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 char_u **tofree;
7893 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007894 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895{
7896 switch (tv->v_type)
7897 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898 case VAR_FUNC:
7899 *tofree = string_quote(tv->vval.v_string, TRUE);
7900 return *tofree;
7901 case VAR_STRING:
7902 *tofree = string_quote(tv->vval.v_string, FALSE);
7903 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#ifdef FEAT_FLOAT
7905 case VAR_FLOAT:
7906 *tofree = NULL;
7907 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7908 return numbuf;
7909#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007912 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007917 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007918}
7919
7920/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 * Return string "str" in ' quotes, doubling ' characters.
7922 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 */
7925 static char_u *
7926string_quote(str, function)
7927 char_u *str;
7928 int function;
7929{
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 char_u *p, *r, *s;
7932
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 len = (function ? 13 : 3);
7934 if (str != NULL)
7935 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007936 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 for (p = str; *p != NUL; mb_ptr_adv(p))
7938 if (*p == '\'')
7939 ++len;
7940 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007941 s = r = alloc(len);
7942 if (r != NULL)
7943 {
7944 if (function)
7945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 r += 10;
7948 }
7949 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 if (str != NULL)
7952 for (p = str; *p != NUL; )
7953 {
7954 if (*p == '\'')
7955 *r++ = '\'';
7956 MB_COPY_CHAR(p, r);
7957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 if (function)
7960 *r++ = ')';
7961 *r++ = NUL;
7962 }
7963 return s;
7964}
7965
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#ifdef FEAT_FLOAT
7967/*
7968 * Convert the string "text" to a floating point number.
7969 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7970 * this always uses a decimal point.
7971 * Returns the length of the text that was consumed.
7972 */
7973 static int
7974string2float(text, value)
7975 char_u *text;
7976 float_T *value; /* result stored here */
7977{
7978 char *s = (char *)text;
7979 float_T f;
7980
7981 f = strtod(s, &s);
7982 *value = f;
7983 return (int)((char_u *)s - text);
7984}
7985#endif
7986
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 * Get the value of an environment variable.
7989 * "arg" is pointing to the '$'. It is advanced to after the name.
7990 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007991 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 */
7993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 int evaluate;
7998{
7999 char_u *string = NULL;
8000 int len;
8001 int cc;
8002 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008003 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005 ++*arg;
8006 name = *arg;
8007 len = get_env_len(arg);
8008 if (evaluate)
8009 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008010 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008011 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008012
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008013 cc = name[len];
8014 name[len] = NUL;
8015 /* first try vim_getenv(), fast for normal environment vars */
8016 string = vim_getenv(name, &mustfree);
8017 if (string != NULL && *string != NUL)
8018 {
8019 if (!mustfree)
8020 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 else
8023 {
8024 if (mustfree)
8025 vim_free(string);
8026
8027 /* next try expanding things like $VIM and ${HOME} */
8028 string = expand_env_save(name - 1);
8029 if (string != NULL && *string == '$')
8030 {
8031 vim_free(string);
8032 string = NULL;
8033 }
8034 }
8035 name[len] = cc;
8036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037 rettv->v_type = VAR_STRING;
8038 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040
8041 return OK;
8042}
8043
8044/*
8045 * Array with names and number of arguments of all internal functions
8046 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8047 */
8048static struct fst
8049{
8050 char *f_name; /* function name */
8051 char f_min_argc; /* minimal number of arguments */
8052 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008054 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055} functions[] =
8056{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#ifdef FEAT_FLOAT
8058 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008059 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008061 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008062 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"append", 2, 2, f_append},
8064 {"argc", 0, 0, f_argc},
8065 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008066 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008067 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008071 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008074 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"bufexists", 1, 1, f_bufexists},
8076 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8077 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8078 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8079 {"buflisted", 1, 1, f_buflisted},
8080 {"bufloaded", 1, 1, f_bufloaded},
8081 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008082 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"bufwinnr", 1, 1, f_bufwinnr},
8084 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008085 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008086 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008087 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#ifdef FEAT_FLOAT
8089 {"ceil", 1, 1, f_ceil},
8090#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008091 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008092 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008094 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008096#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008097 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008098 {"complete_add", 1, 1, f_complete_add},
8099 {"complete_check", 0, 0, f_complete_check},
8100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008109 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008110 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"delete", 1, 1, f_delete},
8112 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008113 {"diff_filler", 1, 1, f_diff_filler},
8114 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008115 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"eventhandler", 0, 0, f_eventhandler},
8119 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008120 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122#ifdef FEAT_FLOAT
8123 {"exp", 1, 1, f_exp},
8124#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008125 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008127 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8129 {"filereadable", 1, 1, f_filereadable},
8130 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008131 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008132 {"finddir", 1, 3, f_finddir},
8133 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134#ifdef FEAT_FLOAT
8135 {"float2nr", 1, 1, f_float2nr},
8136 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008137 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008138#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008139 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"fnamemodify", 2, 2, f_fnamemodify},
8141 {"foldclosed", 1, 1, f_foldclosed},
8142 {"foldclosedend", 1, 1, f_foldclosedend},
8143 {"foldlevel", 1, 1, f_foldlevel},
8144 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008145 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008148 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008149 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008150 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008151 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"getchar", 0, 1, f_getchar},
8153 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008154 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"getcmdline", 0, 0, f_getcmdline},
8156 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008157 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008158 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008159 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008161 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008162 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"getfsize", 1, 1, f_getfsize},
8164 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008165 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008167 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008168 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008169 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008170 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008171 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008172 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008174 {"gettabvar", 2, 3, f_gettabvar},
8175 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getwinposx", 0, 0, f_getwinposx},
8177 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008178 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008179 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008180 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008181 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008184 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008185 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8187 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8188 {"histadd", 2, 2, f_histadd},
8189 {"histdel", 1, 2, f_histdel},
8190 {"histget", 1, 2, f_histget},
8191 {"histnr", 1, 1, f_histnr},
8192 {"hlID", 1, 1, f_hlID},
8193 {"hlexists", 1, 1, f_hlexists},
8194 {"hostname", 0, 0, f_hostname},
8195 {"iconv", 3, 3, f_iconv},
8196 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008197 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008198 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008200 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"inputrestore", 0, 0, f_inputrestore},
8202 {"inputsave", 0, 0, f_inputsave},
8203 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008204 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008205 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008207 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008208 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008209 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008210 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008212 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"libcall", 3, 3, f_libcall},
8214 {"libcallnr", 3, 3, f_libcallnr},
8215 {"line", 1, 1, f_line},
8216 {"line2byte", 1, 1, f_line2byte},
8217 {"lispindent", 1, 1, f_lispindent},
8218 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008219#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008220 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008221 {"log10", 1, 1, f_log10},
8222#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008223#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008224 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008225#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008227 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008228 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008229 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008230 {"matchadd", 2, 5, f_matchadd},
8231 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008232 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008233 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008234 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008235 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008236 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008237 {"max", 1, 1, f_max},
8238 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008239#ifdef vim_mkdir
8240 {"mkdir", 1, 3, f_mkdir},
8241#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008243#ifdef FEAT_MZSCHEME
8244 {"mzeval", 1, 1, f_mzeval},
8245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008247 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008248 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008249 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008250#ifdef FEAT_FLOAT
8251 {"pow", 2, 2, f_pow},
8252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008254 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008255 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008256#ifdef FEAT_PYTHON3
8257 {"py3eval", 1, 1, f_py3eval},
8258#endif
8259#ifdef FEAT_PYTHON
8260 {"pyeval", 1, 1, f_pyeval},
8261#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008262 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008263 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008264 {"reltime", 0, 2, f_reltime},
8265 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"remote_expr", 2, 3, f_remote_expr},
8267 {"remote_foreground", 1, 1, f_remote_foreground},
8268 {"remote_peek", 1, 2, f_remote_peek},
8269 {"remote_read", 1, 1, f_remote_read},
8270 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008271 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008273 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008275 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008276#ifdef FEAT_FLOAT
8277 {"round", 1, 1, f_round},
8278#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008279 {"screenattr", 2, 2, f_screenattr},
8280 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008281 {"screencol", 0, 0, f_screencol},
8282 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008283 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008284 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008285 {"searchpair", 3, 7, f_searchpair},
8286 {"searchpairpos", 3, 7, f_searchpairpos},
8287 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"server2client", 2, 2, f_server2client},
8289 {"serverlist", 0, 0, f_serverlist},
8290 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008291 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"setcmdpos", 1, 1, f_setcmdpos},
8293 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008294 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008295 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008296 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008297 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008299 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008300 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008302#ifdef FEAT_CRYPT
8303 {"sha256", 1, 1, f_sha256},
8304#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008305 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008306 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008308#ifdef FEAT_FLOAT
8309 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008310 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008312 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008313 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008314 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008315 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008316 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008317#ifdef FEAT_FLOAT
8318 {"sqrt", 1, 1, f_sqrt},
8319 {"str2float", 1, 1, f_str2float},
8320#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008321 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008322 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008323 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#ifdef HAVE_STRFTIME
8325 {"strftime", 1, 2, f_strftime},
8326#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"strlen", 1, 1, f_strlen},
8330 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008331 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008333 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008334 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"substitute", 4, 4, f_substitute},
8336 {"synID", 3, 3, f_synID},
8337 {"synIDattr", 2, 3, f_synIDattr},
8338 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008339 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008340 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008341 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008342 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008343 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008344 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008345 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008346 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008347 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008348#ifdef FEAT_FLOAT
8349 {"tan", 1, 1, f_tan},
8350 {"tanh", 1, 1, f_tanh},
8351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008353 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"tolower", 1, 1, f_tolower},
8355 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008356 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008357#ifdef FEAT_FLOAT
8358 {"trunc", 1, 1, f_trunc},
8359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008361 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008362 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008363 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008364 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"virtcol", 1, 1, f_virtcol},
8366 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008367 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"winbufnr", 1, 1, f_winbufnr},
8369 {"wincol", 0, 0, f_wincol},
8370 {"winheight", 1, 1, f_winheight},
8371 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008372 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008374 {"winrestview", 1, 1, f_winrestview},
8375 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008377 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008378 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379};
8380
8381#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8382
8383/*
8384 * Function given to ExpandGeneric() to obtain the list of internal
8385 * or user defined function names.
8386 */
8387 char_u *
8388get_function_name(xp, idx)
8389 expand_T *xp;
8390 int idx;
8391{
8392 static int intidx = -1;
8393 char_u *name;
8394
8395 if (idx == 0)
8396 intidx = -1;
8397 if (intidx < 0)
8398 {
8399 name = get_user_func_name(xp, idx);
8400 if (name != NULL)
8401 return name;
8402 }
8403 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8404 {
8405 STRCPY(IObuff, functions[intidx].f_name);
8406 STRCAT(IObuff, "(");
8407 if (functions[intidx].f_max_argc == 0)
8408 STRCAT(IObuff, ")");
8409 return IObuff;
8410 }
8411
8412 return NULL;
8413}
8414
8415/*
8416 * Function given to ExpandGeneric() to obtain the list of internal or
8417 * user defined variable or function names.
8418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 char_u *
8420get_expr_name(xp, idx)
8421 expand_T *xp;
8422 int idx;
8423{
8424 static int intidx = -1;
8425 char_u *name;
8426
8427 if (idx == 0)
8428 intidx = -1;
8429 if (intidx < 0)
8430 {
8431 name = get_function_name(xp, idx);
8432 if (name != NULL)
8433 return name;
8434 }
8435 return get_user_var_name(xp, ++intidx);
8436}
8437
8438#endif /* FEAT_CMDL_COMPL */
8439
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008440#if defined(EBCDIC) || defined(PROTO)
8441/*
8442 * Compare struct fst by function name.
8443 */
8444 static int
8445compare_func_name(s1, s2)
8446 const void *s1;
8447 const void *s2;
8448{
8449 struct fst *p1 = (struct fst *)s1;
8450 struct fst *p2 = (struct fst *)s2;
8451
8452 return STRCMP(p1->f_name, p2->f_name);
8453}
8454
8455/*
8456 * Sort the function table by function name.
8457 * The sorting of the table above is ASCII dependant.
8458 * On machines using EBCDIC we have to sort it.
8459 */
8460 static void
8461sortFunctions()
8462{
8463 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8464
8465 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8466}
8467#endif
8468
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470/*
8471 * Find internal function in table above.
8472 * Return index, or -1 if not found
8473 */
8474 static int
8475find_internal_func(name)
8476 char_u *name; /* name of the function */
8477{
8478 int first = 0;
8479 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8480 int cmp;
8481 int x;
8482
8483 /*
8484 * Find the function name in the table. Binary search.
8485 */
8486 while (first <= last)
8487 {
8488 x = first + ((unsigned)(last - first) >> 1);
8489 cmp = STRCMP(name, functions[x].f_name);
8490 if (cmp < 0)
8491 last = x - 1;
8492 else if (cmp > 0)
8493 first = x + 1;
8494 else
8495 return x;
8496 }
8497 return -1;
8498}
8499
8500/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8502 * name it contains, otherwise return "name".
8503 */
8504 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008505deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 char_u *name;
8507 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008508 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509{
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 int cc;
8512
8513 cc = name[*lenp];
8514 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008515 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 {
8521 *lenp = 0;
8522 return (char_u *)""; /* just in case */
8523 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008524 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 }
8527
8528 return name;
8529}
8530
8531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 * Allocate a variable for the result of a function.
8533 * Return OK or FAIL.
8534 */
8535 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8537 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 char_u *name; /* name of the function */
8539 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 char_u **arg; /* argument, pointing to the '(' */
8542 linenr_T firstline; /* first line of range */
8543 linenr_T lastline; /* last line of range */
8544 int *doesrange; /* return: function handled range */
8545 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547{
8548 char_u *argp;
8549 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008550 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 int argcount = 0; /* number of arguments found */
8552
8553 /*
8554 * Get the arguments.
8555 */
8556 argp = *arg;
8557 while (argcount < MAX_FUNC_ARGS)
8558 {
8559 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8560 if (*argp == ')' || *argp == ',' || *argp == NUL)
8561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8563 {
8564 ret = FAIL;
8565 break;
8566 }
8567 ++argcount;
8568 if (*argp != ',')
8569 break;
8570 }
8571 if (*argp == ')')
8572 ++argp;
8573 else
8574 ret = FAIL;
8575
8576 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008578 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 {
8581 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008582 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008584 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586
8587 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 *arg = skipwhite(argp);
8591 return ret;
8592}
8593
8594
8595/*
8596 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008597 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008598 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 */
8600 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008601call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008603 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008607 typval_T *argvars; /* vars for arguments, must have "argcount"
8608 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 linenr_T firstline; /* first line of range */
8610 linenr_T lastline; /* last line of range */
8611 int *doesrange; /* return: function handled range */
8612 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008613 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616#define ERROR_UNKNOWN 0
8617#define ERROR_TOOMANY 1
8618#define ERROR_TOOFEW 2
8619#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620#define ERROR_DICT 4
8621#define ERROR_NONE 5
8622#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 int error = ERROR_NONE;
8624 int i;
8625 int llen;
8626 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#define FLEN_FIXED 40
8628 char_u fname_buf[FLEN_FIXED + 1];
8629 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008630 char_u *name;
8631
8632 /* Make a copy of the name, if it comes from a funcref variable it could
8633 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008634 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008635 if (name == NULL)
8636 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638 /*
8639 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8640 * Change <SNR>123_name() to K_SNR 123_name().
8641 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 llen = eval_fname_script(name);
8644 if (llen > 0)
8645 {
8646 fname_buf[0] = K_SPECIAL;
8647 fname_buf[1] = KS_EXTRA;
8648 fname_buf[2] = (int)KE_SNR;
8649 i = 3;
8650 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8651 {
8652 if (current_SID <= 0)
8653 error = ERROR_SCRIPT;
8654 else
8655 {
8656 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8657 i = (int)STRLEN(fname_buf);
8658 }
8659 }
8660 if (i + STRLEN(name + llen) < FLEN_FIXED)
8661 {
8662 STRCPY(fname_buf + i, name + llen);
8663 fname = fname_buf;
8664 }
8665 else
8666 {
8667 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8668 if (fname == NULL)
8669 error = ERROR_OTHER;
8670 else
8671 {
8672 mch_memmove(fname, fname_buf, (size_t)i);
8673 STRCPY(fname + i, name + llen);
8674 }
8675 }
8676 }
8677 else
8678 fname = name;
8679
8680 *doesrange = FALSE;
8681
8682
8683 /* execute the function if no errors detected and executing */
8684 if (evaluate && error == ERROR_NONE)
8685 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008686 char_u *rfname = fname;
8687
8688 /* Ignore "g:" before a function name. */
8689 if (fname[0] == 'g' && fname[1] == ':')
8690 rfname = fname + 2;
8691
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008692 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8693 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 error = ERROR_UNKNOWN;
8695
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008696 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
8698 /*
8699 * User defined function.
8700 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 /* Trigger FuncUndefined event, may load the function. */
8705 if (fp == NULL
8706 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008715 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008716 {
8717 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 }
8720
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (fp != NULL)
8722 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 else
8732 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008733 int did_save_redo = FALSE;
8734
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 /*
8736 * Call the user function.
8737 * Save and restore search patterns, script variables and
8738 * redo buffer.
8739 */
8740 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008741 if (!ins_compl_active())
8742 {
8743 saveRedobuff();
8744 did_save_redo = TRUE;
8745 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008746 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8750 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8751 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008752 /* Function was unreferenced while being used, free it
8753 * now. */
8754 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 if (did_save_redo)
8756 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 restore_search_patterns();
8758 error = ERROR_NONE;
8759 }
8760 }
8761 }
8762 else
8763 {
8764 /*
8765 * Find the function name in the table, call its implementation.
8766 */
8767 i = find_internal_func(fname);
8768 if (i >= 0)
8769 {
8770 if (argcount < functions[i].f_min_argc)
8771 error = ERROR_TOOFEW;
8772 else if (argcount > functions[i].f_max_argc)
8773 error = ERROR_TOOMANY;
8774 else
8775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008776 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 error = ERROR_NONE;
8779 }
8780 }
8781 }
8782 /*
8783 * The function call (or "FuncUndefined" autocommand sequence) might
8784 * have been aborted by an error, an interrupt, or an explicitly thrown
8785 * exception that has not been caught so far. This situation can be
8786 * tested for by calling aborting(). For an error in an internal
8787 * function or for the "E132" error in call_user_func(), however, the
8788 * throw point at which the "force_abort" flag (temporarily reset by
8789 * emsg()) is normally updated has not been reached yet. We need to
8790 * update that flag first to make aborting() reliable.
8791 */
8792 update_force_abort();
8793 }
8794 if (error == ERROR_NONE)
8795 ret = OK;
8796
8797 /*
8798 * Report an error unless the argument evaluation or function call has been
8799 * cancelled due to an aborting error, an interrupt, or an exception.
8800 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008801 if (!aborting())
8802 {
8803 switch (error)
8804 {
8805 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008806 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008807 break;
8808 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008809 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008810 break;
8811 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 name);
8814 break;
8815 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008817 name);
8818 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008819 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008820 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 name);
8822 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 }
8824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (fname != name && fname != fname_buf)
8827 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008828 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
8830 return ret;
8831}
8832
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833/*
8834 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008835 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 */
8837 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008838emsg_funcname(ermsg, name)
8839 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008840 char_u *name;
8841{
8842 char_u *p;
8843
8844 if (*name == K_SPECIAL)
8845 p = concat_str((char_u *)"<SNR>", name + 3);
8846 else
8847 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008848 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008849 if (p != name)
8850 vim_free(p);
8851}
8852
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008853/*
8854 * Return TRUE for a non-zero Number and a non-empty String.
8855 */
8856 static int
8857non_zero_arg(argvars)
8858 typval_T *argvars;
8859{
8860 return ((argvars[0].v_type == VAR_NUMBER
8861 && argvars[0].vval.v_number != 0)
8862 || (argvars[0].v_type == VAR_STRING
8863 && argvars[0].vval.v_string != NULL
8864 && *argvars[0].vval.v_string != NUL));
8865}
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867/*********************************************
8868 * Implementation of the built-in functions
8869 */
8870
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008871#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008872static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8873
8874/*
8875 * Get the float value of "argvars[0]" into "f".
8876 * Returns FAIL when the argument is not a Number or Float.
8877 */
8878 static int
8879get_float_arg(argvars, f)
8880 typval_T *argvars;
8881 float_T *f;
8882{
8883 if (argvars[0].v_type == VAR_FLOAT)
8884 {
8885 *f = argvars[0].vval.v_float;
8886 return OK;
8887 }
8888 if (argvars[0].v_type == VAR_NUMBER)
8889 {
8890 *f = (float_T)argvars[0].vval.v_number;
8891 return OK;
8892 }
8893 EMSG(_("E808: Number or Float required"));
8894 return FAIL;
8895}
8896
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008897/*
8898 * "abs(expr)" function
8899 */
8900 static void
8901f_abs(argvars, rettv)
8902 typval_T *argvars;
8903 typval_T *rettv;
8904{
8905 if (argvars[0].v_type == VAR_FLOAT)
8906 {
8907 rettv->v_type = VAR_FLOAT;
8908 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8909 }
8910 else
8911 {
8912 varnumber_T n;
8913 int error = FALSE;
8914
8915 n = get_tv_number_chk(&argvars[0], &error);
8916 if (error)
8917 rettv->vval.v_number = -1;
8918 else if (n > 0)
8919 rettv->vval.v_number = n;
8920 else
8921 rettv->vval.v_number = -n;
8922 }
8923}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008924
8925/*
8926 * "acos()" function
8927 */
8928 static void
8929f_acos(argvars, rettv)
8930 typval_T *argvars;
8931 typval_T *rettv;
8932{
8933 float_T f;
8934
8935 rettv->v_type = VAR_FLOAT;
8936 if (get_float_arg(argvars, &f) == OK)
8937 rettv->vval.v_float = acos(f);
8938 else
8939 rettv->vval.v_float = 0.0;
8940}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008941#endif
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008944 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 */
8946 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008947f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *argvars;
8949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950{
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008956 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008957 && !tv_check_lock(l->lv_lock,
8958 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008959 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008961 }
8962 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008963 EMSG(_(e_listreq));
8964}
8965
8966/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008967 * "and(expr, expr)" function
8968 */
8969 static void
8970f_and(argvars, rettv)
8971 typval_T *argvars;
8972 typval_T *rettv;
8973{
8974 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8975 & get_tv_number_chk(&argvars[1], NULL);
8976}
8977
8978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008979 * "append(lnum, string/list)" function
8980 */
8981 static void
8982f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 typval_T *argvars;
8984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985{
8986 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 list_T *l = NULL;
8989 listitem_T *li = NULL;
8990 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 long added = 0;
8992
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008993 /* When coming here from Insert mode, sync undo, so that this can be
8994 * undone separately from what was previously inserted. */
8995 if (u_sync_once == 2)
8996 {
8997 u_sync_once = 1; /* notify that u_sync() was called */
8998 u_sync(TRUE);
8999 }
9000
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001 lnum = get_tv_lnum(argvars);
9002 if (lnum >= 0
9003 && lnum <= curbuf->b_ml.ml_line_count
9004 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 l = argvars[1].vval.v_list;
9009 if (l == NULL)
9010 return;
9011 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013 for (;;)
9014 {
9015 if (l == NULL)
9016 tv = &argvars[1]; /* append a string */
9017 else if (li == NULL)
9018 break; /* end of list */
9019 else
9020 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 line = get_tv_string_chk(tv);
9022 if (line == NULL) /* type error */
9023 {
9024 rettv->vval.v_number = 1; /* Failed */
9025 break;
9026 }
9027 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 ++added;
9029 if (l == NULL)
9030 break;
9031 li = li->li_next;
9032 }
9033
9034 appended_lines_mark(lnum, added);
9035 if (curwin->w_cursor.lnum > lnum)
9036 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 else
9039 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040}
9041
9042/*
9043 * "argc()" function
9044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053/*
9054 * "argidx()" function
9055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009058 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
9063
9064/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009065 * "arglistid()" function
9066 */
9067 static void
9068f_arglistid(argvars, rettv)
9069 typval_T *argvars UNUSED;
9070 typval_T *rettv;
9071{
9072 win_T *wp;
9073 tabpage_T *tp = NULL;
9074 long n;
9075
9076 rettv->vval.v_number = -1;
9077 if (argvars[0].v_type != VAR_UNKNOWN)
9078 {
9079 if (argvars[1].v_type != VAR_UNKNOWN)
9080 {
9081 n = get_tv_number(&argvars[1]);
9082 if (n >= 0)
9083 tp = find_tabpage(n);
9084 }
9085 else
9086 tp = curtab;
9087
9088 if (tp != NULL)
9089 {
9090 wp = find_win_by_nr(&argvars[0], tp);
9091 if (wp != NULL)
9092 rettv->vval.v_number = wp->w_alist->id;
9093 }
9094 }
9095 else
9096 rettv->vval.v_number = curwin->w_alist->id;
9097}
9098
9099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 * "argv(nr)" function
9101 */
9102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009104 typval_T *argvars;
9105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 int idx;
9108
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009109 if (argvars[0].v_type != VAR_UNKNOWN)
9110 {
9111 idx = get_tv_number_chk(&argvars[0], NULL);
9112 if (idx >= 0 && idx < ARGCOUNT)
9113 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9114 else
9115 rettv->vval.v_string = NULL;
9116 rettv->v_type = VAR_STRING;
9117 }
9118 else if (rettv_list_alloc(rettv) == OK)
9119 for (idx = 0; idx < ARGCOUNT; ++idx)
9120 list_append_string(rettv->vval.v_list,
9121 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009124#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009125/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009126 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009127 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009128 static void
9129f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009130 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009131 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009132{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009133 float_T f;
9134
9135 rettv->v_type = VAR_FLOAT;
9136 if (get_float_arg(argvars, &f) == OK)
9137 rettv->vval.v_float = asin(f);
9138 else
9139 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009140}
9141
9142/*
9143 * "atan()" function
9144 */
9145 static void
9146f_atan(argvars, rettv)
9147 typval_T *argvars;
9148 typval_T *rettv;
9149{
9150 float_T f;
9151
9152 rettv->v_type = VAR_FLOAT;
9153 if (get_float_arg(argvars, &f) == OK)
9154 rettv->vval.v_float = atan(f);
9155 else
9156 rettv->vval.v_float = 0.0;
9157}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009158
9159/*
9160 * "atan2()" function
9161 */
9162 static void
9163f_atan2(argvars, rettv)
9164 typval_T *argvars;
9165 typval_T *rettv;
9166{
9167 float_T fx, fy;
9168
9169 rettv->v_type = VAR_FLOAT;
9170 if (get_float_arg(argvars, &fx) == OK
9171 && get_float_arg(&argvars[1], &fy) == OK)
9172 rettv->vval.v_float = atan2(fx, fy);
9173 else
9174 rettv->vval.v_float = 0.0;
9175}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009176#endif
9177
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178/*
9179 * "browse(save, title, initdir, default)" function
9180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186#ifdef FEAT_BROWSE
9187 int save;
9188 char_u *title;
9189 char_u *initdir;
9190 char_u *defname;
9191 char_u buf[NUMBUFLEN];
9192 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 save = get_tv_number_chk(&argvars[0], &error);
9196 title = get_tv_string_chk(&argvars[1]);
9197 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9198 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 if (error || title == NULL || initdir == NULL || defname == NULL)
9201 rettv->vval.v_string = NULL;
9202 else
9203 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009204 do_browse(save ? BROWSE_SAVE : 0,
9205 title, defname, NULL, initdir, NULL, curbuf);
9206#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009208#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009210}
9211
9212/*
9213 * "browsedir(title, initdir)" function
9214 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009217 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009218 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009219{
9220#ifdef FEAT_BROWSE
9221 char_u *title;
9222 char_u *initdir;
9223 char_u buf[NUMBUFLEN];
9224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 title = get_tv_string_chk(&argvars[0]);
9226 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 if (title == NULL || initdir == NULL)
9229 rettv->vval.v_string = NULL;
9230 else
9231 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009232 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237}
9238
Bram Moolenaar33570922005-01-25 22:26:29 +00009239static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009240
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241/*
9242 * Find a buffer by number or exact name.
9243 */
9244 static buf_T *
9245find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009246 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 if (avar->v_type == VAR_NUMBER)
9251 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009252 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009255 if (buf == NULL)
9256 {
9257 /* No full path name match, try a match with a URL or a "nofile"
9258 * buffer, these don't use the full path. */
9259 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9260 if (buf->b_fname != NULL
9261 && (path_with_url(buf->b_fname)
9262#ifdef FEAT_QUICKFIX
9263 || bt_nofile(buf)
9264#endif
9265 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009266 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009267 break;
9268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 return buf;
9271}
9272
9273/*
9274 * "bufexists(expr)" function
9275 */
9276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282}
9283
9284/*
9285 * "buflisted(expr)" function
9286 */
9287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009288f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 typval_T *argvars;
9290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291{
9292 buf_T *buf;
9293
9294 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296}
9297
9298/*
9299 * "bufloaded(expr)" function
9300 */
9301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009303 typval_T *argvars;
9304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 buf_T *buf;
9307
9308 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
9311
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009312static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009313
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314/*
9315 * Get buffer by number or pattern.
9316 */
9317 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009318get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009320 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 int save_magic;
9324 char_u *save_cpo;
9325 buf_T *buf;
9326
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 if (tv->v_type == VAR_NUMBER)
9328 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009329 if (tv->v_type != VAR_STRING)
9330 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 if (name == NULL || *name == NUL)
9332 return curbuf;
9333 if (name[0] == '$' && name[1] == NUL)
9334 return lastbuf;
9335
9336 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9337 save_magic = p_magic;
9338 p_magic = TRUE;
9339 save_cpo = p_cpo;
9340 p_cpo = (char_u *)"";
9341
9342 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009343 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 p_magic = save_magic;
9346 p_cpo = save_cpo;
9347
9348 /* If not found, try expanding the name, like done for bufexists(). */
9349 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
9352 return buf;
9353}
9354
9355/*
9356 * "bufname(expr)" function
9357 */
9358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009360 typval_T *argvars;
9361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 buf_T *buf;
9364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009367 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 --emsg_off;
9374}
9375
9376/*
9377 * "bufnr(expr)" function
9378 */
9379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *argvars;
9382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009385 int error = FALSE;
9386 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009390 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009391 --emsg_off;
9392
9393 /* If the buffer isn't found and the second argument is not zero create a
9394 * new buffer. */
9395 if (buf == NULL
9396 && argvars[1].v_type != VAR_UNKNOWN
9397 && get_tv_number_chk(&argvars[1], &error) != 0
9398 && !error
9399 && (name = get_tv_string_chk(&argvars[0])) != NULL
9400 && !error)
9401 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409/*
9410 * "bufwinnr(nr)" function
9411 */
9412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 typval_T *argvars;
9415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417#ifdef FEAT_WINDOWS
9418 win_T *wp;
9419 int winnr = 0;
9420#endif
9421 buf_T *buf;
9422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009425 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#ifdef FEAT_WINDOWS
9427 for (wp = firstwin; wp; wp = wp->w_next)
9428 {
9429 ++winnr;
9430 if (wp->w_buffer == buf)
9431 break;
9432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436#endif
9437 --emsg_off;
9438}
9439
9440/*
9441 * "byte2line(byte)" function
9442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009445 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#else
9451 long boff = 0;
9452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 (linenr_T)0, &boff);
9459#endif
9460}
9461
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009462 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009463byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009464 typval_T *argvars;
9465 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009466 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009467{
9468#ifdef FEAT_MBYTE
9469 char_u *t;
9470#endif
9471 char_u *str;
9472 long idx;
9473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 str = get_tv_string_chk(&argvars[0]);
9475 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009478 return;
9479
9480#ifdef FEAT_MBYTE
9481 t = str;
9482 for ( ; idx > 0; idx--)
9483 {
9484 if (*t == NUL) /* EOL reached */
9485 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009486 if (enc_utf8 && comp)
9487 t += utf_ptr2len(t);
9488 else
9489 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009490 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009491 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009492#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009495#endif
9496}
9497
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009498/*
9499 * "byteidx()" function
9500 */
9501 static void
9502f_byteidx(argvars, rettv)
9503 typval_T *argvars;
9504 typval_T *rettv;
9505{
9506 byteidx(argvars, rettv, FALSE);
9507}
9508
9509/*
9510 * "byteidxcomp()" function
9511 */
9512 static void
9513f_byteidxcomp(argvars, rettv)
9514 typval_T *argvars;
9515 typval_T *rettv;
9516{
9517 byteidx(argvars, rettv, TRUE);
9518}
9519
Bram Moolenaardb913952012-06-29 12:54:53 +02009520 int
9521func_call(name, args, selfdict, rettv)
9522 char_u *name;
9523 typval_T *args;
9524 dict_T *selfdict;
9525 typval_T *rettv;
9526{
9527 listitem_T *item;
9528 typval_T argv[MAX_FUNC_ARGS + 1];
9529 int argc = 0;
9530 int dummy;
9531 int r = 0;
9532
9533 for (item = args->vval.v_list->lv_first; item != NULL;
9534 item = item->li_next)
9535 {
9536 if (argc == MAX_FUNC_ARGS)
9537 {
9538 EMSG(_("E699: Too many arguments"));
9539 break;
9540 }
9541 /* Make a copy of each argument. This is needed to be able to set
9542 * v_lock to VAR_FIXED in the copy without changing the original list.
9543 */
9544 copy_tv(&item->li_tv, &argv[argc++]);
9545 }
9546
9547 if (item == NULL)
9548 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9549 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9550 &dummy, TRUE, selfdict);
9551
9552 /* Free the arguments. */
9553 while (argc > 0)
9554 clear_tv(&argv[--argc]);
9555
9556 return r;
9557}
9558
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009559/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009560 * "call(func, arglist)" function
9561 */
9562 static void
9563f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009564 typval_T *argvars;
9565 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566{
9567 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009569
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009570 if (argvars[1].v_type != VAR_LIST)
9571 {
9572 EMSG(_(e_listreq));
9573 return;
9574 }
9575 if (argvars[1].vval.v_list == NULL)
9576 return;
9577
9578 if (argvars[0].v_type == VAR_FUNC)
9579 func = argvars[0].vval.v_string;
9580 else
9581 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 if (*func == NUL)
9583 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009584
Bram Moolenaare9a41262005-01-15 22:18:47 +00009585 if (argvars[2].v_type != VAR_UNKNOWN)
9586 {
9587 if (argvars[2].v_type != VAR_DICT)
9588 {
9589 EMSG(_(e_dictreq));
9590 return;
9591 }
9592 selfdict = argvars[2].vval.v_dict;
9593 }
9594
Bram Moolenaardb913952012-06-29 12:54:53 +02009595 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596}
9597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598#ifdef FEAT_FLOAT
9599/*
9600 * "ceil({float})" function
9601 */
9602 static void
9603f_ceil(argvars, rettv)
9604 typval_T *argvars;
9605 typval_T *rettv;
9606{
9607 float_T f;
9608
9609 rettv->v_type = VAR_FLOAT;
9610 if (get_float_arg(argvars, &f) == OK)
9611 rettv->vval.v_float = ceil(f);
9612 else
9613 rettv->vval.v_float = 0.0;
9614}
9615#endif
9616
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009617/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009618 * "changenr()" function
9619 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009620 static void
9621f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009622 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009623 typval_T *rettv;
9624{
9625 rettv->vval.v_number = curbuf->b_u_seq_cur;
9626}
9627
9628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * "char2nr(string)" function
9630 */
9631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 typval_T *argvars;
9634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636#ifdef FEAT_MBYTE
9637 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009638 {
9639 int utf8 = 0;
9640
9641 if (argvars[1].v_type != VAR_UNKNOWN)
9642 utf8 = get_tv_number_chk(&argvars[1], NULL);
9643
9644 if (utf8)
9645 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9646 else
9647 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 else
9650#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652}
9653
9654/*
9655 * "cindent(lnum)" function
9656 */
9657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662#ifdef FEAT_CINDENT
9663 pos_T pos;
9664 linenr_T lnum;
9665
9666 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9669 {
9670 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 curwin->w_cursor = pos;
9673 }
9674 else
9675#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677}
9678
9679/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009680 * "clearmatches()" function
9681 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009682 static void
9683f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009684 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009685 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009686{
9687#ifdef FEAT_SEARCH_EXTRA
9688 clear_matches(curwin);
9689#endif
9690}
9691
9692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 * "col(string)" function
9694 */
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 colnr_T col = 0;
9701 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009702 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009704 fp = var2fpos(&argvars[0], FALSE, &fnum);
9705 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 {
9707 if (fp->col == MAXCOL)
9708 {
9709 /* '> can be MAXCOL, get the length of the line then */
9710 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009711 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 else
9713 col = MAXCOL;
9714 }
9715 else
9716 {
9717 col = fp->col + 1;
9718#ifdef FEAT_VIRTUALEDIT
9719 /* col(".") when the cursor is on the NUL at the end of the line
9720 * because of "coladd" can be seen as an extra column. */
9721 if (virtual_active() && fp == &curwin->w_cursor)
9722 {
9723 char_u *p = ml_get_cursor();
9724
9725 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9726 curwin->w_virtcol - curwin->w_cursor.coladd))
9727 {
9728# ifdef FEAT_MBYTE
9729 int l;
9730
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009731 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 col += l;
9733# else
9734 if (*p != NUL && p[1] == NUL)
9735 ++col;
9736# endif
9737 }
9738 }
9739#endif
9740 }
9741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
Bram Moolenaar572cb562005-08-05 21:35:02 +00009745#if defined(FEAT_INS_EXPAND)
9746/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009747 * "complete()" function
9748 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009749 static void
9750f_complete(argvars, rettv)
9751 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009752 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009753{
9754 int startcol;
9755
9756 if ((State & INSERT) == 0)
9757 {
9758 EMSG(_("E785: complete() can only be used in Insert mode"));
9759 return;
9760 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009761
9762 /* Check for undo allowed here, because if something was already inserted
9763 * the line was already saved for undo and this check isn't done. */
9764 if (!undo_allowed())
9765 return;
9766
Bram Moolenaarade00832006-03-10 21:46:58 +00009767 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9768 {
9769 EMSG(_(e_invarg));
9770 return;
9771 }
9772
9773 startcol = get_tv_number_chk(&argvars[0], NULL);
9774 if (startcol <= 0)
9775 return;
9776
9777 set_completion(startcol - 1, argvars[1].vval.v_list);
9778}
9779
9780/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009781 * "complete_add()" function
9782 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009783 static void
9784f_complete_add(argvars, rettv)
9785 typval_T *argvars;
9786 typval_T *rettv;
9787{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009788 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009789}
9790
9791/*
9792 * "complete_check()" function
9793 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009794 static void
9795f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009796 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009797 typval_T *rettv;
9798{
9799 int saved = RedrawingDisabled;
9800
9801 RedrawingDisabled = 0;
9802 ins_compl_check_keys(0);
9803 rettv->vval.v_number = compl_interrupted;
9804 RedrawingDisabled = saved;
9805}
9806#endif
9807
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808/*
9809 * "confirm(message, buttons[, default [, type]])" function
9810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009813 typval_T *argvars UNUSED;
9814 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9817 char_u *message;
9818 char_u *buttons = NULL;
9819 char_u buf[NUMBUFLEN];
9820 char_u buf2[NUMBUFLEN];
9821 int def = 1;
9822 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 char_u *typestr;
9824 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 message = get_tv_string_chk(&argvars[0]);
9827 if (message == NULL)
9828 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9832 if (buttons == NULL)
9833 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009836 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009837 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9840 if (typestr == NULL)
9841 error = TRUE;
9842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 switch (TOUPPER_ASC(*typestr))
9845 {
9846 case 'E': type = VIM_ERROR; break;
9847 case 'Q': type = VIM_QUESTION; break;
9848 case 'I': type = VIM_INFO; break;
9849 case 'W': type = VIM_WARNING; break;
9850 case 'G': type = VIM_GENERIC; break;
9851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 }
9853 }
9854 }
9855 }
9856
9857 if (buttons == NULL || *buttons == NUL)
9858 buttons = (char_u *)_("&Ok");
9859
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009860 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009862 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863#endif
9864}
9865
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009866/*
9867 * "copy()" function
9868 */
9869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009873{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009874 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009875}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009877#ifdef FEAT_FLOAT
9878/*
9879 * "cos()" function
9880 */
9881 static void
9882f_cos(argvars, rettv)
9883 typval_T *argvars;
9884 typval_T *rettv;
9885{
9886 float_T f;
9887
9888 rettv->v_type = VAR_FLOAT;
9889 if (get_float_arg(argvars, &f) == OK)
9890 rettv->vval.v_float = cos(f);
9891 else
9892 rettv->vval.v_float = 0.0;
9893}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009894
9895/*
9896 * "cosh()" function
9897 */
9898 static void
9899f_cosh(argvars, rettv)
9900 typval_T *argvars;
9901 typval_T *rettv;
9902{
9903 float_T f;
9904
9905 rettv->v_type = VAR_FLOAT;
9906 if (get_float_arg(argvars, &f) == OK)
9907 rettv->vval.v_float = cosh(f);
9908 else
9909 rettv->vval.v_float = 0.0;
9910}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009911#endif
9912
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009914 * "count()" function
9915 */
9916 static void
9917f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 typval_T *argvars;
9919 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009921 long n = 0;
9922 int ic = FALSE;
9923
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 listitem_T *li;
9927 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009929
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 if ((l = argvars[0].vval.v_list) != NULL)
9931 {
9932 li = l->lv_first;
9933 if (argvars[2].v_type != VAR_UNKNOWN)
9934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 int error = FALSE;
9936
9937 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 if (argvars[3].v_type != VAR_UNKNOWN)
9939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009940 idx = get_tv_number_chk(&argvars[3], &error);
9941 if (!error)
9942 {
9943 li = list_find(l, idx);
9944 if (li == NULL)
9945 EMSGN(_(e_listidx), idx);
9946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 if (error)
9949 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 }
9951
9952 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009953 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 ++n;
9955 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 else if (argvars[0].v_type == VAR_DICT)
9958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009959 int todo;
9960 dict_T *d;
9961 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009962
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009963 if ((d = argvars[0].vval.v_dict) != NULL)
9964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 int error = FALSE;
9966
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 if (argvars[2].v_type != VAR_UNKNOWN)
9968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009969 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 if (argvars[3].v_type != VAR_UNKNOWN)
9971 EMSG(_(e_invarg));
9972 }
9973
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009974 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009976 {
9977 if (!HASHITEM_EMPTY(hi))
9978 {
9979 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009980 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009981 ++n;
9982 }
9983 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984 }
9985 }
9986 else
9987 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009988 rettv->vval.v_number = n;
9989}
9990
9991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9993 *
9994 * Checks the existence of a cscope connection.
9995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009998 typval_T *argvars UNUSED;
9999 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001#ifdef FEAT_CSCOPE
10002 int num = 0;
10003 char_u *dbpath = NULL;
10004 char_u *prepend = NULL;
10005 char_u buf[NUMBUFLEN];
10006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010007 if (argvars[0].v_type != VAR_UNKNOWN
10008 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 num = (int)get_tv_number(&argvars[0]);
10011 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 }
10015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017#endif
10018}
10019
10020/*
10021 * "cursor(lnum, col)" function
10022 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010023 * Moves the cursor to the specified line and column.
10024 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010032#ifdef FEAT_VIRTUALEDIT
10033 long coladd = 0;
10034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010036 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010037 if (argvars[1].v_type == VAR_UNKNOWN)
10038 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010039 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010040 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010041
Bram Moolenaar493c1782014-05-28 14:34:46 +020010042 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010043 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010044 line = pos.lnum;
10045 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010046#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010047 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010048#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010049 if (curswant >= 0)
10050 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010051 }
10052 else
10053 {
10054 line = get_tv_lnum(argvars);
10055 col = get_tv_number_chk(&argvars[1], NULL);
10056#ifdef FEAT_VIRTUALEDIT
10057 if (argvars[2].v_type != VAR_UNKNOWN)
10058 coladd = get_tv_number_chk(&argvars[2], NULL);
10059#endif
10060 }
10061 if (line < 0 || col < 0
10062#ifdef FEAT_VIRTUALEDIT
10063 || coladd < 0
10064#endif
10065 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (line > 0)
10068 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (col > 0)
10070 curwin->w_cursor.col = col - 1;
10071#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010072 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073#endif
10074
10075 /* Make sure the cursor is in a valid position. */
10076 check_cursor();
10077#ifdef FEAT_MBYTE
10078 /* Correct cursor for multi-byte character. */
10079 if (has_mbyte)
10080 mb_adjust_cursor();
10081#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010082
10083 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010084 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085}
10086
10087/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010088 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 */
10090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010095 int noref = 0;
10096
10097 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010099 if (noref < 0 || noref > 1)
10100 EMSG(_(e_invarg));
10101 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010102 {
10103 current_copyID += COPYID_INC;
10104 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106}
10107
10108/*
10109 * "delete()" function
10110 */
10111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010113 typval_T *argvars;
10114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115{
10116 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121
10122/*
10123 * "did_filetype()" function
10124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010127 typval_T *argvars UNUSED;
10128 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129{
10130#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#endif
10133}
10134
10135/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010136 * "diff_filler()" function
10137 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010140 typval_T *argvars UNUSED;
10141 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010142{
10143#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010145#endif
10146}
10147
10148/*
10149 * "diff_hlID()" function
10150 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010153 typval_T *argvars UNUSED;
10154 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010155{
10156#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010158 static linenr_T prev_lnum = 0;
10159 static int changedtick = 0;
10160 static int fnum = 0;
10161 static int change_start = 0;
10162 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010163 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010164 int filler_lines;
10165 int col;
10166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 if (lnum < 0) /* ignore type error in {lnum} arg */
10168 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010169 if (lnum != prev_lnum
10170 || changedtick != curbuf->b_changedtick
10171 || fnum != curbuf->b_fnum)
10172 {
10173 /* New line, buffer, change: need to get the values. */
10174 filler_lines = diff_check(curwin, lnum);
10175 if (filler_lines < 0)
10176 {
10177 if (filler_lines == -1)
10178 {
10179 change_start = MAXCOL;
10180 change_end = -1;
10181 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10182 hlID = HLF_ADD; /* added line */
10183 else
10184 hlID = HLF_CHD; /* changed line */
10185 }
10186 else
10187 hlID = HLF_ADD; /* added line */
10188 }
10189 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010190 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010191 prev_lnum = lnum;
10192 changedtick = curbuf->b_changedtick;
10193 fnum = curbuf->b_fnum;
10194 }
10195
10196 if (hlID == HLF_CHD || hlID == HLF_TXD)
10197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010199 if (col >= change_start && col <= change_end)
10200 hlID = HLF_TXD; /* changed text */
10201 else
10202 hlID = HLF_CHD; /* changed line */
10203 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010204 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010205#endif
10206}
10207
10208/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010209 * "empty({expr})" function
10210 */
10211 static void
10212f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 typval_T *argvars;
10214 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010215{
10216 int n;
10217
10218 switch (argvars[0].v_type)
10219 {
10220 case VAR_STRING:
10221 case VAR_FUNC:
10222 n = argvars[0].vval.v_string == NULL
10223 || *argvars[0].vval.v_string == NUL;
10224 break;
10225 case VAR_NUMBER:
10226 n = argvars[0].vval.v_number == 0;
10227 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010228#ifdef FEAT_FLOAT
10229 case VAR_FLOAT:
10230 n = argvars[0].vval.v_float == 0.0;
10231 break;
10232#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010233 case VAR_LIST:
10234 n = argvars[0].vval.v_list == NULL
10235 || argvars[0].vval.v_list->lv_first == NULL;
10236 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 case VAR_DICT:
10238 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010239 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010241 default:
10242 EMSG2(_(e_intern2), "f_empty()");
10243 n = 0;
10244 }
10245
10246 rettv->vval.v_number = n;
10247}
10248
10249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 * "escape({string}, {chars})" function
10251 */
10252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 typval_T *argvars;
10255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 char_u buf[NUMBUFLEN];
10258
Bram Moolenaar758711c2005-02-02 23:11:38 +000010259 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10260 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262}
10263
10264/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 * "eval()" function
10266 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267 static void
10268f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 typval_T *argvars;
10270 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010271{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010272 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 s = get_tv_string_chk(&argvars[0]);
10275 if (s != NULL)
10276 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010277
Bram Moolenaar615b9972015-01-14 17:15:05 +010010278 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10280 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010281 if (p != NULL && !aborting())
10282 EMSG2(_(e_invexpr2), p);
10283 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010284 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010285 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010287 else if (*s != NUL)
10288 EMSG(_(e_trailing));
10289}
10290
10291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 * "eventhandler()" function
10293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010296 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300}
10301
10302/*
10303 * "executable()" function
10304 */
10305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010307 typval_T *argvars;
10308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010310 char_u *name = get_tv_string(&argvars[0]);
10311
10312 /* Check in $PATH and also check directly if there is a directory name. */
10313 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10314 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010315}
10316
10317/*
10318 * "exepath()" function
10319 */
10320 static void
10321f_exepath(argvars, rettv)
10322 typval_T *argvars;
10323 typval_T *rettv;
10324{
10325 char_u *p = NULL;
10326
Bram Moolenaarb5971142015-03-21 17:32:19 +010010327 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010328 rettv->v_type = VAR_STRING;
10329 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330}
10331
10332/*
10333 * "exists()" function
10334 */
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
10340 char_u *p;
10341 char_u *name;
10342 int n = FALSE;
10343 int len = 0;
10344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 if (*p == '$') /* environment variable */
10347 {
10348 /* first try "normal" environment variables (fast) */
10349 if (mch_getenv(p + 1) != NULL)
10350 n = TRUE;
10351 else
10352 {
10353 /* try expanding things like $VIM and ${HOME} */
10354 p = expand_env_save(p);
10355 if (p != NULL && *p != '$')
10356 n = TRUE;
10357 vim_free(p);
10358 }
10359 }
10360 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010363 if (*skipwhite(p) != NUL)
10364 n = FALSE; /* trailing garbage */
10365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 else if (*p == '*') /* internal or user defined function */
10367 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
10370 else if (*p == ':')
10371 {
10372 n = cmd_exists(p + 1);
10373 }
10374 else if (*p == '#')
10375 {
10376#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010377 if (p[1] == '#')
10378 n = autocmd_supported(p + 2);
10379 else
10380 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381#endif
10382 }
10383 else /* internal variable */
10384 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010385 char_u *tofree;
10386 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010388 /* get_name_len() takes care of expanding curly braces */
10389 name = p;
10390 len = get_name_len(&p, &tofree, TRUE, FALSE);
10391 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010393 if (tofree != NULL)
10394 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010395 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010396 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010398 /* handle d.key, l[idx], f(expr) */
10399 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10400 if (n)
10401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 }
10403 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010404 if (*p != NUL)
10405 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010407 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 }
10409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010413#ifdef FEAT_FLOAT
10414/*
10415 * "exp()" function
10416 */
10417 static void
10418f_exp(argvars, rettv)
10419 typval_T *argvars;
10420 typval_T *rettv;
10421{
10422 float_T f;
10423
10424 rettv->v_type = VAR_FLOAT;
10425 if (get_float_arg(argvars, &f) == OK)
10426 rettv->vval.v_float = exp(f);
10427 else
10428 rettv->vval.v_float = 0.0;
10429}
10430#endif
10431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432/*
10433 * "expand()" function
10434 */
10435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 typval_T *argvars;
10438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439{
10440 char_u *s;
10441 int len;
10442 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010443 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010446 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010449 if (argvars[1].v_type != VAR_UNKNOWN
10450 && argvars[2].v_type != VAR_UNKNOWN
10451 && get_tv_number_chk(&argvars[2], &error)
10452 && !error)
10453 {
10454 rettv->v_type = VAR_LIST;
10455 rettv->vval.v_list = NULL;
10456 }
10457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 if (*s == '%' || *s == '#' || *s == '<')
10460 {
10461 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010462 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010464 if (rettv->v_type == VAR_LIST)
10465 {
10466 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10467 list_append_string(rettv->vval.v_list, result, -1);
10468 else
10469 vim_free(result);
10470 }
10471 else
10472 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 }
10474 else
10475 {
10476 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010477 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 if (argvars[1].v_type != VAR_UNKNOWN
10479 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010480 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 if (!error)
10482 {
10483 ExpandInit(&xpc);
10484 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010485 if (p_wic)
10486 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010487 if (rettv->v_type == VAR_STRING)
10488 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10489 options, WILD_ALL);
10490 else if (rettv_list_alloc(rettv) != FAIL)
10491 {
10492 int i;
10493
10494 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10495 for (i = 0; i < xpc.xp_numfiles; i++)
10496 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10497 ExpandCleanup(&xpc);
10498 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 }
10500 else
10501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 }
10503}
10504
10505/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010506 * Go over all entries in "d2" and add them to "d1".
10507 * When "action" is "error" then a duplicate key is an error.
10508 * When "action" is "force" then a duplicate key is overwritten.
10509 * Otherwise duplicate keys are ignored ("action" is "keep").
10510 */
10511 void
10512dict_extend(d1, d2, action)
10513 dict_T *d1;
10514 dict_T *d2;
10515 char_u *action;
10516{
10517 dictitem_T *di1;
10518 hashitem_T *hi2;
10519 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010520 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010521
10522 todo = (int)d2->dv_hashtab.ht_used;
10523 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10524 {
10525 if (!HASHITEM_EMPTY(hi2))
10526 {
10527 --todo;
10528 di1 = dict_find(d1, hi2->hi_key, -1);
10529 if (d1->dv_scope != 0)
10530 {
10531 /* Disallow replacing a builtin function in l: and g:.
10532 * Check the key to be valid when adding to any
10533 * scope. */
10534 if (d1->dv_scope == VAR_DEF_SCOPE
10535 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10536 && var_check_func_name(hi2->hi_key,
10537 di1 == NULL))
10538 break;
10539 if (!valid_varname(hi2->hi_key))
10540 break;
10541 }
10542 if (di1 == NULL)
10543 {
10544 di1 = dictitem_copy(HI2DI(hi2));
10545 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10546 dictitem_free(di1);
10547 }
10548 else if (*action == 'e')
10549 {
10550 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10551 break;
10552 }
10553 else if (*action == 'f' && HI2DI(hi2) != di1)
10554 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010555 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10556 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010557 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010558 clear_tv(&di1->di_tv);
10559 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10560 }
10561 }
10562 }
10563}
10564
10565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010566 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010568 */
10569 static void
10570f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010573{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010574 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010575
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 list_T *l1, *l2;
10579 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010582
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 l1 = argvars[0].vval.v_list;
10584 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010585 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010586 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 {
10588 if (argvars[2].v_type != VAR_UNKNOWN)
10589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 before = get_tv_number_chk(&argvars[2], &error);
10591 if (error)
10592 return; /* type error; errmsg already given */
10593
Bram Moolenaar758711c2005-02-02 23:11:38 +000010594 if (before == l1->lv_len)
10595 item = NULL;
10596 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010598 item = list_find(l1, before);
10599 if (item == NULL)
10600 {
10601 EMSGN(_(e_listidx), before);
10602 return;
10603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 }
10605 }
10606 else
10607 item = NULL;
10608 list_extend(l1, l2, item);
10609
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 copy_tv(&argvars[0], rettv);
10611 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10614 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010615 dict_T *d1, *d2;
10616 char_u *action;
10617 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618
10619 d1 = argvars[0].vval.v_dict;
10620 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010621 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010622 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010623 {
10624 /* Check the third argument. */
10625 if (argvars[2].v_type != VAR_UNKNOWN)
10626 {
10627 static char *(av[]) = {"keep", "force", "error"};
10628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 action = get_tv_string_chk(&argvars[2]);
10630 if (action == NULL)
10631 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010632 for (i = 0; i < 3; ++i)
10633 if (STRCMP(action, av[i]) == 0)
10634 break;
10635 if (i == 3)
10636 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010637 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 return;
10639 }
10640 }
10641 else
10642 action = (char_u *)"force";
10643
Bram Moolenaara9922d62013-05-30 13:01:18 +020010644 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010645
Bram Moolenaare9a41262005-01-15 22:18:47 +000010646 copy_tv(&argvars[0], rettv);
10647 }
10648 }
10649 else
10650 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010651}
10652
10653/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010654 * "feedkeys()" function
10655 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010656 static void
10657f_feedkeys(argvars, rettv)
10658 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010659 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010660{
10661 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010662 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663 char_u *keys, *flags;
10664 char_u nbuf[NUMBUFLEN];
10665 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010666 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010667
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010668 /* This is not allowed in the sandbox. If the commands would still be
10669 * executed in the sandbox it would be OK, but it probably happens later,
10670 * when "sandbox" is no longer set. */
10671 if (check_secure())
10672 return;
10673
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010674 keys = get_tv_string(&argvars[0]);
10675 if (*keys != NUL)
10676 {
10677 if (argvars[1].v_type != VAR_UNKNOWN)
10678 {
10679 flags = get_tv_string_buf(&argvars[1], nbuf);
10680 for ( ; *flags != NUL; ++flags)
10681 {
10682 switch (*flags)
10683 {
10684 case 'n': remap = FALSE; break;
10685 case 'm': remap = TRUE; break;
10686 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010687 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010688 }
10689 }
10690 }
10691
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010692 /* Need to escape K_SPECIAL and CSI before putting the string in the
10693 * typeahead buffer. */
10694 keys_esc = vim_strsave_escape_csi(keys);
10695 if (keys_esc != NULL)
10696 {
10697 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010698 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010699 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010700 if (vgetc_busy)
10701 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010702 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010703 }
10704}
10705
10706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 * "filereadable()" function
10708 */
10709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *argvars;
10712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010714 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 char_u *p;
10716 int n;
10717
Bram Moolenaarc236c162008-07-13 17:41:49 +000010718#ifndef O_NONBLOCK
10719# define O_NONBLOCK 0
10720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010722 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10723 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 {
10725 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010726 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 }
10728 else
10729 n = FALSE;
10730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732}
10733
10734/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010735 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 * rights to write into.
10737 */
10738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010740 typval_T *argvars;
10741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010743 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744}
10745
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010746static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010747
10748 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010749findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010751 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010752 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010753{
10754#ifdef FEAT_SEARCHPATH
10755 char_u *fname;
10756 char_u *fresult = NULL;
10757 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10758 char_u *p;
10759 char_u pathbuf[NUMBUFLEN];
10760 int count = 1;
10761 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010762 int error = FALSE;
10763#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010764
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010765 rettv->vval.v_string = NULL;
10766 rettv->v_type = VAR_STRING;
10767
10768#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10774 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010775 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 else
10777 {
10778 if (*p != NUL)
10779 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010782 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010784 }
10785
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010786 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10787 error = TRUE;
10788
10789 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 do
10792 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010793 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010794 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 fresult = find_file_in_path_option(first ? fname : NULL,
10796 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010797 0, first, path,
10798 find_what,
10799 curbuf->b_ffname,
10800 find_what == FINDFILE_DIR
10801 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010802 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010803
10804 if (fresult != NULL && rettv->v_type == VAR_LIST)
10805 list_append_string(rettv->vval.v_list, fresult, -1);
10806
10807 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010809
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010810 if (rettv->v_type == VAR_STRING)
10811 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010812#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010813}
10814
Bram Moolenaar33570922005-01-25 22:26:29 +000010815static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10816static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010817
10818/*
10819 * Implementation of map() and filter().
10820 */
10821 static void
10822filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *argvars;
10824 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010825 int map;
10826{
10827 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 listitem_T *li, *nli;
10830 list_T *l = NULL;
10831 dictitem_T *di;
10832 hashtab_T *ht;
10833 hashitem_T *hi;
10834 dict_T *d = NULL;
10835 typval_T save_val;
10836 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010838 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010839 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010840 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010841 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010842 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010843 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010844
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010847 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010848 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 return;
10850 }
10851 else if (argvars[0].v_type == VAR_DICT)
10852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010853 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010854 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 return;
10856 }
10857 else
10858 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010859 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 return;
10861 }
10862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 expr = get_tv_string_buf_chk(&argvars[1], buf);
10864 /* On type errors, the preceding call has already displayed an error
10865 * message. Avoid a misleading error message for an empty string that
10866 * was not passed as argument. */
10867 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 prepare_vimvar(VV_VAL, &save_val);
10870 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010871
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010872 /* We reset "did_emsg" to be able to detect whether an error
10873 * occurred during evaluation of the expression. */
10874 save_did_emsg = did_emsg;
10875 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010876
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010877 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 vimvars[VV_KEY].vv_type = VAR_STRING;
10881
10882 ht = &d->dv_hashtab;
10883 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010884 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 if (!HASHITEM_EMPTY(hi))
10888 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010889 int r;
10890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 --todo;
10892 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010893 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010894 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10895 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 break;
10897 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010898 r = filter_map_one(&di->di_tv, expr, map, &rem);
10899 clear_tv(&vimvars[VV_KEY].vv_tv);
10900 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 break;
10902 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010903 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010904 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10905 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010906 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010908 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 }
10910 }
10911 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 }
10913 else
10914 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010915 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 for (li = l->lv_first; li != NULL; li = nli)
10918 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010919 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010920 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010922 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010923 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010924 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010925 break;
10926 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010928 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010931
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010932 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010934
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010935 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937
10938 copy_tv(&argvars[0], rettv);
10939}
10940
10941 static int
10942filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010943 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 char_u *expr;
10945 int map;
10946 int *remp;
10947{
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010950 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 s = expr;
10954 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010955 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 if (*s != NUL) /* check for trailing chars after expr */
10957 {
10958 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010959 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010960 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 }
10962 if (map)
10963 {
10964 /* map(): replace the list item value */
10965 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010966 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 *tv = rettv;
10968 }
10969 else
10970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 int error = FALSE;
10972
Bram Moolenaare9a41262005-01-15 22:18:47 +000010973 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976 /* On type error, nothing has been removed; return FAIL to stop the
10977 * loop. The error message was given by get_tv_number_chk(). */
10978 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010979 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010980 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010981 retval = OK;
10982theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010984 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010985}
10986
10987/*
10988 * "filter()" function
10989 */
10990 static void
10991f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010992 typval_T *argvars;
10993 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010994{
10995 filter_map(argvars, rettv, FALSE);
10996}
10997
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010999 * "finddir({fname}[, {path}[, {count}]])" function
11000 */
11001 static void
11002f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *argvars;
11004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011006 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007}
11008
11009/*
11010 * "findfile({fname}[, {path}[, {count}]])" function
11011 */
11012 static void
11013f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011016{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011017 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011018}
11019
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011020#ifdef FEAT_FLOAT
11021/*
11022 * "float2nr({float})" function
11023 */
11024 static void
11025f_float2nr(argvars, rettv)
11026 typval_T *argvars;
11027 typval_T *rettv;
11028{
11029 float_T f;
11030
11031 if (get_float_arg(argvars, &f) == OK)
11032 {
11033 if (f < -0x7fffffff)
11034 rettv->vval.v_number = -0x7fffffff;
11035 else if (f > 0x7fffffff)
11036 rettv->vval.v_number = 0x7fffffff;
11037 else
11038 rettv->vval.v_number = (varnumber_T)f;
11039 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011040}
11041
11042/*
11043 * "floor({float})" function
11044 */
11045 static void
11046f_floor(argvars, rettv)
11047 typval_T *argvars;
11048 typval_T *rettv;
11049{
11050 float_T f;
11051
11052 rettv->v_type = VAR_FLOAT;
11053 if (get_float_arg(argvars, &f) == OK)
11054 rettv->vval.v_float = floor(f);
11055 else
11056 rettv->vval.v_float = 0.0;
11057}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011058
11059/*
11060 * "fmod()" function
11061 */
11062 static void
11063f_fmod(argvars, rettv)
11064 typval_T *argvars;
11065 typval_T *rettv;
11066{
11067 float_T fx, fy;
11068
11069 rettv->v_type = VAR_FLOAT;
11070 if (get_float_arg(argvars, &fx) == OK
11071 && get_float_arg(&argvars[1], &fy) == OK)
11072 rettv->vval.v_float = fmod(fx, fy);
11073 else
11074 rettv->vval.v_float = 0.0;
11075}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011076#endif
11077
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011079 * "fnameescape({string})" function
11080 */
11081 static void
11082f_fnameescape(argvars, rettv)
11083 typval_T *argvars;
11084 typval_T *rettv;
11085{
11086 rettv->vval.v_string = vim_strsave_fnameescape(
11087 get_tv_string(&argvars[0]), FALSE);
11088 rettv->v_type = VAR_STRING;
11089}
11090
11091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * "fnamemodify({fname}, {mods})" function
11093 */
11094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011096 typval_T *argvars;
11097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 char_u *fname;
11100 char_u *mods;
11101 int usedlen = 0;
11102 int len;
11103 char_u *fbuf = NULL;
11104 char_u buf[NUMBUFLEN];
11105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 fname = get_tv_string_chk(&argvars[0]);
11107 mods = get_tv_string_buf_chk(&argvars[1], buf);
11108 if (fname == NULL || mods == NULL)
11109 fname = NULL;
11110 else
11111 {
11112 len = (int)STRLEN(fname);
11113 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 vim_free(fbuf);
11122}
11123
Bram Moolenaar33570922005-01-25 22:26:29 +000011124static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125
11126/*
11127 * "foldclosed()" function
11128 */
11129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011132 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011133 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134{
11135#ifdef FEAT_FOLDING
11136 linenr_T lnum;
11137 linenr_T first, last;
11138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11141 {
11142 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11143 {
11144 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 return;
11149 }
11150 }
11151#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153}
11154
11155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156 * "foldclosed()" function
11157 */
11158 static void
11159f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *argvars;
11161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162{
11163 foldclosed_both(argvars, rettv, FALSE);
11164}
11165
11166/*
11167 * "foldclosedend()" function
11168 */
11169 static void
11170f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173{
11174 foldclosed_both(argvars, rettv, TRUE);
11175}
11176
11177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 * "foldlevel()" function
11179 */
11180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011182 typval_T *argvars UNUSED;
11183 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185#ifdef FEAT_FOLDING
11186 linenr_T lnum;
11187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192}
11193
11194/*
11195 * "foldtext()" function
11196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
11202#ifdef FEAT_FOLDING
11203 linenr_T lnum;
11204 char_u *s;
11205 char_u *r;
11206 int len;
11207 char *txt;
11208#endif
11209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->v_type = VAR_STRING;
11211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11214 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11215 <= curbuf->b_ml.ml_line_count
11216 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 {
11218 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11220 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 {
11222 if (!linewhite(lnum))
11223 break;
11224 ++lnum;
11225 }
11226
11227 /* Find interesting text in this line. */
11228 s = skipwhite(ml_get(lnum));
11229 /* skip C comment-start */
11230 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011233 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011235 {
11236 s = skipwhite(ml_get(lnum + 1));
11237 if (*s == '*')
11238 s = skipwhite(s + 1);
11239 }
11240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 txt = _("+-%s%3ld lines: ");
11242 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011243 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 + 20 /* for %3ld */
11245 + STRLEN(s))); /* concatenated */
11246 if (r != NULL)
11247 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11249 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11250 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 len = (int)STRLEN(r);
11252 STRCAT(r, s);
11253 /* remove 'foldmarker' and 'commentstring' */
11254 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 }
11257 }
11258#endif
11259}
11260
11261/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011262 * "foldtextresult(lnum)" function
11263 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011266 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011268{
11269#ifdef FEAT_FOLDING
11270 linenr_T lnum;
11271 char_u *text;
11272 char_u buf[51];
11273 foldinfo_T foldinfo;
11274 int fold_count;
11275#endif
11276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->v_type = VAR_STRING;
11278 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011279#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 /* treat illegal types and illegal string values for {lnum} the same */
11282 if (lnum < 0)
11283 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011284 fold_count = foldedCount(curwin, lnum, &foldinfo);
11285 if (fold_count > 0)
11286 {
11287 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11288 &foldinfo, buf);
11289 if (text == buf)
11290 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011292 }
11293#endif
11294}
11295
11296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 * "foreground()" function
11298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011301 typval_T *argvars UNUSED;
11302 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304#ifdef FEAT_GUI
11305 if (gui.in_use)
11306 gui_mch_set_foreground();
11307#else
11308# ifdef WIN32
11309 win32_set_foreground();
11310# endif
11311#endif
11312}
11313
11314/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011315 * "function()" function
11316 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011321{
11322 char_u *s;
11323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011325 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011326 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011327 /* Don't check an autoload name for existence here. */
11328 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011329 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011330 else
11331 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011332 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011333 {
11334 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011335 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011336
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011337 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11338 * also be called from another script. Using trans_function_name()
11339 * would also work, but some plugins depend on the name being
11340 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011341 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011342 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011343 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 if (rettv->vval.v_string != NULL)
11345 {
11346 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011347 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011348 }
11349 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011350 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011351 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011353 }
11354}
11355
11356/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011357 * "garbagecollect()" function
11358 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011359 static void
11360f_garbagecollect(argvars, rettv)
11361 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011362 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011363{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011364 /* This is postponed until we are back at the toplevel, because we may be
11365 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11366 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011367
11368 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11369 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011370}
11371
11372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011373 * "get()" function
11374 */
11375 static void
11376f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379{
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 listitem_T *li;
11381 list_T *l;
11382 dictitem_T *di;
11383 dict_T *d;
11384 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385
Bram Moolenaare9a41262005-01-15 22:18:47 +000011386 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011387 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011388 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 int error = FALSE;
11391
11392 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11393 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011395 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011397 else if (argvars[0].v_type == VAR_DICT)
11398 {
11399 if ((d = argvars[0].vval.v_dict) != NULL)
11400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011401 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011402 if (di != NULL)
11403 tv = &di->di_tv;
11404 }
11405 }
11406 else
11407 EMSG2(_(e_listdictarg), "get()");
11408
11409 if (tv == NULL)
11410 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 copy_tv(&argvars[2], rettv);
11413 }
11414 else
11415 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011416}
11417
Bram Moolenaar342337a2005-07-21 21:11:17 +000011418static 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 +000011419
11420/*
11421 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011422 * Return a range (from start to end) of lines in rettv from the specified
11423 * buffer.
11424 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011425 */
11426 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011427get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428 buf_T *buf;
11429 linenr_T start;
11430 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011431 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011432 typval_T *rettv;
11433{
11434 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011435
Bram Moolenaar959a1432013-12-14 12:17:38 +010011436 rettv->v_type = VAR_STRING;
11437 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011438 if (retlist && rettv_list_alloc(rettv) == FAIL)
11439 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011440
11441 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11442 return;
11443
11444 if (!retlist)
11445 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011446 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11447 p = ml_get_buf(buf, start, FALSE);
11448 else
11449 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011450 rettv->vval.v_string = vim_strsave(p);
11451 }
11452 else
11453 {
11454 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011455 return;
11456
11457 if (start < 1)
11458 start = 1;
11459 if (end > buf->b_ml.ml_line_count)
11460 end = buf->b_ml.ml_line_count;
11461 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011462 if (list_append_string(rettv->vval.v_list,
11463 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011464 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011465 }
11466}
11467
11468/*
11469 * "getbufline()" function
11470 */
11471 static void
11472f_getbufline(argvars, rettv)
11473 typval_T *argvars;
11474 typval_T *rettv;
11475{
11476 linenr_T lnum;
11477 linenr_T end;
11478 buf_T *buf;
11479
11480 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11481 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011482 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011483 --emsg_off;
11484
Bram Moolenaar661b1822005-07-28 22:36:45 +000011485 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011486 if (argvars[2].v_type == VAR_UNKNOWN)
11487 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011488 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011489 end = get_tv_lnum_buf(&argvars[2], buf);
11490
Bram Moolenaar342337a2005-07-21 21:11:17 +000011491 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011492}
11493
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494/*
11495 * "getbufvar()" function
11496 */
11497 static void
11498f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501{
11502 buf_T *buf;
11503 buf_T *save_curbuf;
11504 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011506 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11509 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011511 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011512
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011513 rettv->v_type = VAR_STRING;
11514 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011515
11516 if (buf != NULL && varname != NULL)
11517 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011518 /* set curbuf to be our buf, temporarily */
11519 save_curbuf = curbuf;
11520 curbuf = buf;
11521
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011523 {
11524 if (get_option_tv(&varname, rettv, TRUE) == OK)
11525 done = TRUE;
11526 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011527 else if (STRCMP(varname, "changedtick") == 0)
11528 {
11529 rettv->v_type = VAR_NUMBER;
11530 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011531 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011532 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533 else
11534 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011535 /* Look up the variable. */
11536 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11537 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11538 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011540 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011542 done = TRUE;
11543 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011545
11546 /* restore previous notion of curbuf */
11547 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548 }
11549
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011550 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11551 /* use the default value */
11552 copy_tv(&argvars[2], rettv);
11553
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554 --emsg_off;
11555}
11556
11557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 * "getchar()" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564{
11565 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011568 /* Position the cursor. Needed after a message that ends in a space. */
11569 windgoto(msg_row, msg_col);
11570
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 ++no_mapping;
11572 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011573 for (;;)
11574 {
11575 if (argvars[0].v_type == VAR_UNKNOWN)
11576 /* getchar(): blocking wait. */
11577 n = safe_vgetc();
11578 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11579 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011580 n = vpeekc_any();
11581 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011582 /* illegal argument or getchar(0) and no char avail: return zero */
11583 n = 0;
11584 else
11585 /* getchar(0) and char avail: return char */
11586 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011587
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011588 if (n == K_IGNORE)
11589 continue;
11590 break;
11591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 --no_mapping;
11593 --allow_keys;
11594
Bram Moolenaar219b8702006-11-01 14:32:36 +000011595 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11596 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11597 vimvars[VV_MOUSE_COL].vv_nr = 0;
11598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 if (IS_SPECIAL(n) || mod_mask != 0)
11601 {
11602 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11603 int i = 0;
11604
11605 /* Turn a special key into three bytes, plus modifier. */
11606 if (mod_mask != 0)
11607 {
11608 temp[i++] = K_SPECIAL;
11609 temp[i++] = KS_MODIFIER;
11610 temp[i++] = mod_mask;
11611 }
11612 if (IS_SPECIAL(n))
11613 {
11614 temp[i++] = K_SPECIAL;
11615 temp[i++] = K_SECOND(n);
11616 temp[i++] = K_THIRD(n);
11617 }
11618#ifdef FEAT_MBYTE
11619 else if (has_mbyte)
11620 i += (*mb_char2bytes)(n, temp + i);
11621#endif
11622 else
11623 temp[i++] = n;
11624 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 rettv->v_type = VAR_STRING;
11626 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011627
11628#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011629 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011630 {
11631 int row = mouse_row;
11632 int col = mouse_col;
11633 win_T *win;
11634 linenr_T lnum;
11635# ifdef FEAT_WINDOWS
11636 win_T *wp;
11637# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011638 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011639
11640 if (row >= 0 && col >= 0)
11641 {
11642 /* Find the window at the mouse coordinates and compute the
11643 * text position. */
11644 win = mouse_find_win(&row, &col);
11645 (void)mouse_comp_pos(win, &row, &col, &lnum);
11646# ifdef FEAT_WINDOWS
11647 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011648 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011649# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011650 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011651 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11652 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11653 }
11654 }
11655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 }
11657}
11658
11659/*
11660 * "getcharmod()" function
11661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668}
11669
11670/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011671 * "getcharsearch()" function
11672 */
11673 static void
11674f_getcharsearch(argvars, rettv)
11675 typval_T *argvars UNUSED;
11676 typval_T *rettv;
11677{
11678 if (rettv_dict_alloc(rettv) != FAIL)
11679 {
11680 dict_T *dict = rettv->vval.v_dict;
11681
11682 dict_add_nr_str(dict, "char", 0L, last_csearch());
11683 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11684 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11685 }
11686}
11687
11688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 * "getcmdline()" function
11690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011693 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->v_type = VAR_STRING;
11697 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698}
11699
11700/*
11701 * "getcmdpos()" function
11702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709}
11710
11711/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011712 * "getcmdtype()" function
11713 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011714 static void
11715f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011716 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011717 typval_T *rettv;
11718{
11719 rettv->v_type = VAR_STRING;
11720 rettv->vval.v_string = alloc(2);
11721 if (rettv->vval.v_string != NULL)
11722 {
11723 rettv->vval.v_string[0] = get_cmdline_type();
11724 rettv->vval.v_string[1] = NUL;
11725 }
11726}
11727
11728/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011729 * "getcmdwintype()" function
11730 */
11731 static void
11732f_getcmdwintype(argvars, rettv)
11733 typval_T *argvars UNUSED;
11734 typval_T *rettv;
11735{
11736 rettv->v_type = VAR_STRING;
11737 rettv->vval.v_string = NULL;
11738#ifdef FEAT_CMDWIN
11739 rettv->vval.v_string = alloc(2);
11740 if (rettv->vval.v_string != NULL)
11741 {
11742 rettv->vval.v_string[0] = cmdwin_type;
11743 rettv->vval.v_string[1] = NUL;
11744 }
11745#endif
11746}
11747
11748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 * "getcwd()" function
11750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011756 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011759 rettv->vval.v_string = NULL;
11760 cwd = alloc(MAXPATHL);
11761 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011763 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11764 {
11765 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011767 if (rettv->vval.v_string != NULL)
11768 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011770 }
11771 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 }
11773}
11774
11775/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011776 * "getfontname()" function
11777 */
11778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011781 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 rettv->v_type = VAR_STRING;
11784 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011785#ifdef FEAT_GUI
11786 if (gui.in_use)
11787 {
11788 GuiFont font;
11789 char_u *name = NULL;
11790
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011791 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011792 {
11793 /* Get the "Normal" font. Either the name saved by
11794 * hl_set_font_name() or from the font ID. */
11795 font = gui.norm_font;
11796 name = hl_get_font_name();
11797 }
11798 else
11799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011801 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11802 return;
11803 font = gui_mch_get_font(name, FALSE);
11804 if (font == NOFONT)
11805 return; /* Invalid font name, return empty string. */
11806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011808 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011809 gui_mch_free_font(font);
11810 }
11811#endif
11812}
11813
11814/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011815 * "getfperm({fname})" function
11816 */
11817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *argvars;
11820 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011821{
11822 char_u *fname;
11823 struct stat st;
11824 char_u *perm = NULL;
11825 char_u flags[] = "rwx";
11826 int i;
11827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011831 if (mch_stat((char *)fname, &st) >= 0)
11832 {
11833 perm = vim_strsave((char_u *)"---------");
11834 if (perm != NULL)
11835 {
11836 for (i = 0; i < 9; i++)
11837 {
11838 if (st.st_mode & (1 << (8 - i)))
11839 perm[i] = flags[i % 3];
11840 }
11841 }
11842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011844}
11845
11846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 * "getfsize({fname})" function
11848 */
11849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 typval_T *argvars;
11852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
11854 char_u *fname;
11855 struct stat st;
11856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860
11861 if (mch_stat((char *)fname, &st) >= 0)
11862 {
11863 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011868
11869 /* non-perfect check for overflow */
11870 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11871 rettv->vval.v_number = -2;
11872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 }
11874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876}
11877
11878/*
11879 * "getftime({fname})" function
11880 */
11881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885{
11886 char_u *fname;
11887 struct stat st;
11888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890
11891 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
11897/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011898 * "getftype({fname})" function
11899 */
11900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011904{
11905 char_u *fname;
11906 struct stat st;
11907 char_u *type = NULL;
11908 char *t;
11909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011913 if (mch_lstat((char *)fname, &st) >= 0)
11914 {
11915#ifdef S_ISREG
11916 if (S_ISREG(st.st_mode))
11917 t = "file";
11918 else if (S_ISDIR(st.st_mode))
11919 t = "dir";
11920# ifdef S_ISLNK
11921 else if (S_ISLNK(st.st_mode))
11922 t = "link";
11923# endif
11924# ifdef S_ISBLK
11925 else if (S_ISBLK(st.st_mode))
11926 t = "bdev";
11927# endif
11928# ifdef S_ISCHR
11929 else if (S_ISCHR(st.st_mode))
11930 t = "cdev";
11931# endif
11932# ifdef S_ISFIFO
11933 else if (S_ISFIFO(st.st_mode))
11934 t = "fifo";
11935# endif
11936# ifdef S_ISSOCK
11937 else if (S_ISSOCK(st.st_mode))
11938 t = "fifo";
11939# endif
11940 else
11941 t = "other";
11942#else
11943# ifdef S_IFMT
11944 switch (st.st_mode & S_IFMT)
11945 {
11946 case S_IFREG: t = "file"; break;
11947 case S_IFDIR: t = "dir"; break;
11948# ifdef S_IFLNK
11949 case S_IFLNK: t = "link"; break;
11950# endif
11951# ifdef S_IFBLK
11952 case S_IFBLK: t = "bdev"; break;
11953# endif
11954# ifdef S_IFCHR
11955 case S_IFCHR: t = "cdev"; break;
11956# endif
11957# ifdef S_IFIFO
11958 case S_IFIFO: t = "fifo"; break;
11959# endif
11960# ifdef S_IFSOCK
11961 case S_IFSOCK: t = "socket"; break;
11962# endif
11963 default: t = "other";
11964 }
11965# else
11966 if (mch_isdir(fname))
11967 t = "dir";
11968 else
11969 t = "file";
11970# endif
11971#endif
11972 type = vim_strsave((char_u *)t);
11973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011975}
11976
11977/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011978 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011979 */
11980 static void
11981f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 typval_T *argvars;
11983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984{
11985 linenr_T lnum;
11986 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011987 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011988
11989 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011990 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011991 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011992 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011993 retlist = FALSE;
11994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011996 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011997 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011998 retlist = TRUE;
11999 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012000
Bram Moolenaar342337a2005-07-21 21:11:17 +000012001 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002}
12003
12004/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012005 * "getmatches()" function
12006 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012007 static void
12008f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012009 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012010 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012011{
12012#ifdef FEAT_SEARCH_EXTRA
12013 dict_T *dict;
12014 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012015 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012016
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012017 if (rettv_list_alloc(rettv) == OK)
12018 {
12019 while (cur != NULL)
12020 {
12021 dict = dict_alloc();
12022 if (dict == NULL)
12023 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012024 if (cur->match.regprog == NULL)
12025 {
12026 /* match added with matchaddpos() */
12027 for (i = 0; i < MAXPOSMATCH; ++i)
12028 {
12029 llpos_T *llpos;
12030 char buf[6];
12031 list_T *l;
12032
12033 llpos = &cur->pos.pos[i];
12034 if (llpos->lnum == 0)
12035 break;
12036 l = list_alloc();
12037 if (l == NULL)
12038 break;
12039 list_append_number(l, (varnumber_T)llpos->lnum);
12040 if (llpos->col > 0)
12041 {
12042 list_append_number(l, (varnumber_T)llpos->col);
12043 list_append_number(l, (varnumber_T)llpos->len);
12044 }
12045 sprintf(buf, "pos%d", i + 1);
12046 dict_add_list(dict, buf, l);
12047 }
12048 }
12049 else
12050 {
12051 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12052 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012053 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012054 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12055 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012056# ifdef FEAT_CONCEAL
12057 if (cur->conceal_char)
12058 {
12059 char_u buf[MB_MAXBYTES + 1];
12060
12061 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12062 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12063 }
12064# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012065 list_append_dict(rettv->vval.v_list, dict);
12066 cur = cur->next;
12067 }
12068 }
12069#endif
12070}
12071
12072/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012073 * "getpid()" function
12074 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012075 static void
12076f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012077 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012078 typval_T *rettv;
12079{
12080 rettv->vval.v_number = mch_get_pid();
12081}
12082
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012083static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12084
12085/*
12086 * "getcurpos()" function
12087 */
12088 static void
12089f_getcurpos(argvars, rettv)
12090 typval_T *argvars;
12091 typval_T *rettv;
12092{
12093 getpos_both(argvars, rettv, TRUE);
12094}
12095
Bram Moolenaar18081e32008-02-20 19:11:07 +000012096/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012097 * "getpos(string)" function
12098 */
12099 static void
12100f_getpos(argvars, rettv)
12101 typval_T *argvars;
12102 typval_T *rettv;
12103{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012104 getpos_both(argvars, rettv, FALSE);
12105}
12106
12107 static void
12108getpos_both(argvars, rettv, getcurpos)
12109 typval_T *argvars;
12110 typval_T *rettv;
12111 int getcurpos;
12112{
Bram Moolenaara5525202006-03-02 22:52:09 +000012113 pos_T *fp;
12114 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012115 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012116
12117 if (rettv_list_alloc(rettv) == OK)
12118 {
12119 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012120 if (getcurpos)
12121 fp = &curwin->w_cursor;
12122 else
12123 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012124 if (fnum != -1)
12125 list_append_number(l, (varnumber_T)fnum);
12126 else
12127 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012128 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12129 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012130 list_append_number(l, (fp != NULL)
12131 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012132 : (varnumber_T)0);
12133 list_append_number(l,
12134#ifdef FEAT_VIRTUALEDIT
12135 (fp != NULL) ? (varnumber_T)fp->coladd :
12136#endif
12137 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012138 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012139 list_append_number(l, curwin->w_curswant == MAXCOL ?
12140 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012141 }
12142 else
12143 rettv->vval.v_number = FALSE;
12144}
12145
12146/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012147 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012148 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012149 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012150f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012151 typval_T *argvars UNUSED;
12152 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012153{
12154#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012155 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012156#endif
12157
Bram Moolenaar2641f772005-03-25 21:58:17 +000012158#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012159 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012160 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012161 wp = NULL;
12162 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12163 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012164 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012165 if (wp == NULL)
12166 return;
12167 }
12168
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012169 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012170 }
12171#endif
12172}
12173
12174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 * "getreg()" function
12176 */
12177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 typval_T *argvars;
12180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182 char_u *strregname;
12183 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012184 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012185 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012190 strregname = get_tv_string_chk(&argvars[0]);
12191 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012192 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012194 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012195 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12196 return_list = get_tv_number_chk(&argvars[2], &error);
12197 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012200 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012201
12202 if (error)
12203 return;
12204
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 regname = (strregname == NULL ? '"' : *strregname);
12206 if (regname == 0)
12207 regname = '"';
12208
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012209 if (return_list)
12210 {
12211 rettv->v_type = VAR_LIST;
12212 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12213 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012214 if (rettv->vval.v_list != NULL)
12215 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012216 }
12217 else
12218 {
12219 rettv->v_type = VAR_STRING;
12220 rettv->vval.v_string = get_reg_contents(regname,
12221 arg2 ? GREG_EXPR_SRC : 0);
12222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223}
12224
12225/*
12226 * "getregtype()" function
12227 */
12228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012230 typval_T *argvars;
12231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232{
12233 char_u *strregname;
12234 int regname;
12235 char_u buf[NUMBUFLEN + 2];
12236 long reglen = 0;
12237
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012238 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 {
12240 strregname = get_tv_string_chk(&argvars[0]);
12241 if (strregname == NULL) /* type error; errmsg already given */
12242 {
12243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = NULL;
12245 return;
12246 }
12247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 else
12249 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012250 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
12252 regname = (strregname == NULL ? '"' : *strregname);
12253 if (regname == 0)
12254 regname = '"';
12255
12256 buf[0] = NUL;
12257 buf[1] = NUL;
12258 switch (get_reg_type(regname, &reglen))
12259 {
12260 case MLINE: buf[0] = 'V'; break;
12261 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 case MBLOCK:
12263 buf[0] = Ctrl_V;
12264 sprintf((char *)buf + 1, "%ld", reglen + 1);
12265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->v_type = VAR_STRING;
12268 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269}
12270
12271/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012272 * "gettabvar()" function
12273 */
12274 static void
12275f_gettabvar(argvars, rettv)
12276 typval_T *argvars;
12277 typval_T *rettv;
12278{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012279 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012280 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012281 dictitem_T *v;
12282 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012283 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012284
12285 rettv->v_type = VAR_STRING;
12286 rettv->vval.v_string = NULL;
12287
12288 varname = get_tv_string_chk(&argvars[1]);
12289 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12290 if (tp != NULL && varname != NULL)
12291 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012292 /* Set tp to be our tabpage, temporarily. Also set the window to the
12293 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012294 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12295 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012296 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012297 /* look up the variable */
12298 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12299 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12300 if (v != NULL)
12301 {
12302 copy_tv(&v->di_tv, rettv);
12303 done = TRUE;
12304 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012305 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012306
12307 /* restore previous notion of curwin */
12308 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012309 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012310
12311 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012312 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012313}
12314
12315/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012316 * "gettabwinvar()" function
12317 */
12318 static void
12319f_gettabwinvar(argvars, rettv)
12320 typval_T *argvars;
12321 typval_T *rettv;
12322{
12323 getwinvar(argvars, rettv, 1);
12324}
12325
12326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 * "getwinposx()" function
12328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335#ifdef FEAT_GUI
12336 if (gui.in_use)
12337 {
12338 int x, y;
12339
12340 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 }
12343#endif
12344}
12345
12346/*
12347 * "getwinposy()" function
12348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355#ifdef FEAT_GUI
12356 if (gui.in_use)
12357 {
12358 int x, y;
12359
12360 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 }
12363#endif
12364}
12365
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012366/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012367 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012368 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012369 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012370find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012371 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012372 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012373{
12374#ifdef FEAT_WINDOWS
12375 win_T *wp;
12376#endif
12377 int nr;
12378
12379 nr = get_tv_number_chk(vp, NULL);
12380
12381#ifdef FEAT_WINDOWS
12382 if (nr < 0)
12383 return NULL;
12384 if (nr == 0)
12385 return curwin;
12386
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012387 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12388 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012389 if (--nr <= 0)
12390 break;
12391 return wp;
12392#else
12393 if (nr == 0 || nr == 1)
12394 return curwin;
12395 return NULL;
12396#endif
12397}
12398
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399/*
12400 * "getwinvar()" function
12401 */
12402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012404 typval_T *argvars;
12405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012407 getwinvar(argvars, rettv, 0);
12408}
12409
12410/*
12411 * getwinvar() and gettabwinvar()
12412 */
12413 static void
12414getwinvar(argvars, rettv, off)
12415 typval_T *argvars;
12416 typval_T *rettv;
12417 int off; /* 1 for gettabwinvar() */
12418{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 win_T *win, *oldcurwin;
12420 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012422 tabpage_T *tp = NULL;
12423 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012424 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012426#ifdef FEAT_WINDOWS
12427 if (off == 1)
12428 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12429 else
12430 tp = curtab;
12431#endif
12432 win = find_win_by_nr(&argvars[off], tp);
12433 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438
12439 if (win != NULL && varname != NULL)
12440 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012441 /* Set curwin to be our win, temporarily. Also set the tabpage,
12442 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012443 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012444 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012445 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012446 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012447 if (get_option_tv(&varname, rettv, 1) == OK)
12448 done = TRUE;
12449 }
12450 else
12451 {
12452 /* Look up the variable. */
12453 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12454 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12455 varname, FALSE);
12456 if (v != NULL)
12457 {
12458 copy_tv(&v->di_tv, rettv);
12459 done = TRUE;
12460 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012463
12464 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012465 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 }
12467
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012468 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12469 /* use the default return value */
12470 copy_tv(&argvars[off + 2], rettv);
12471
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 --emsg_off;
12473}
12474
12475/*
12476 * "glob()" function
12477 */
12478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 typval_T *argvars;
12481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012483 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012485 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012487 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012488 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012490 if (argvars[1].v_type != VAR_UNKNOWN)
12491 {
12492 if (get_tv_number_chk(&argvars[1], &error))
12493 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012494 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012495 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012496 if (get_tv_number_chk(&argvars[2], &error))
12497 {
12498 rettv->v_type = VAR_LIST;
12499 rettv->vval.v_list = NULL;
12500 }
12501 if (argvars[3].v_type != VAR_UNKNOWN
12502 && get_tv_number_chk(&argvars[3], &error))
12503 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012504 }
12505 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012506 if (!error)
12507 {
12508 ExpandInit(&xpc);
12509 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012510 if (p_wic)
12511 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012512 if (rettv->v_type == VAR_STRING)
12513 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012514 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012515 else if (rettv_list_alloc(rettv) != FAIL)
12516 {
12517 int i;
12518
12519 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12520 NULL, options, WILD_ALL_KEEP);
12521 for (i = 0; i < xpc.xp_numfiles; i++)
12522 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12523
12524 ExpandCleanup(&xpc);
12525 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012526 }
12527 else
12528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529}
12530
12531/*
12532 * "globpath()" function
12533 */
12534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 typval_T *argvars;
12537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012539 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012542 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012543 garray_T ga;
12544 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012546 /* When the optional second argument is non-zero, don't remove matches
12547 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012548 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012549 if (argvars[2].v_type != VAR_UNKNOWN)
12550 {
12551 if (get_tv_number_chk(&argvars[2], &error))
12552 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012553 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012554 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012555 if (get_tv_number_chk(&argvars[3], &error))
12556 {
12557 rettv->v_type = VAR_LIST;
12558 rettv->vval.v_list = NULL;
12559 }
12560 if (argvars[4].v_type != VAR_UNKNOWN
12561 && get_tv_number_chk(&argvars[4], &error))
12562 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012563 }
12564 }
12565 if (file != NULL && !error)
12566 {
12567 ga_init2(&ga, (int)sizeof(char_u *), 10);
12568 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12569 if (rettv->v_type == VAR_STRING)
12570 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12571 else if (rettv_list_alloc(rettv) != FAIL)
12572 for (i = 0; i < ga.ga_len; ++i)
12573 list_append_string(rettv->vval.v_list,
12574 ((char_u **)(ga.ga_data))[i], -1);
12575 ga_clear_strings(&ga);
12576 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579}
12580
12581/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012582 * "glob2regpat()" function
12583 */
12584 static void
12585f_glob2regpat(argvars, rettv)
12586 typval_T *argvars;
12587 typval_T *rettv;
12588{
12589 char_u *pat = get_tv_string_chk(&argvars[0]);
12590
12591 rettv->v_type = VAR_STRING;
12592 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12593}
12594
12595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 * "has()" function
12597 */
12598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 typval_T *argvars;
12601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602{
12603 int i;
12604 char_u *name;
12605 int n = FALSE;
12606 static char *(has_list[]) =
12607 {
12608#ifdef AMIGA
12609 "amiga",
12610# ifdef FEAT_ARP
12611 "arp",
12612# endif
12613#endif
12614#ifdef __BEOS__
12615 "beos",
12616#endif
12617#ifdef MSDOS
12618# ifdef DJGPP
12619 "dos32",
12620# else
12621 "dos16",
12622# endif
12623#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012624#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 "mac",
12626#endif
12627#if defined(MACOS_X_UNIX)
12628 "macunix",
12629#endif
12630#ifdef OS2
12631 "os2",
12632#endif
12633#ifdef __QNX__
12634 "qnx",
12635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636#ifdef UNIX
12637 "unix",
12638#endif
12639#ifdef VMS
12640 "vms",
12641#endif
12642#ifdef WIN16
12643 "win16",
12644#endif
12645#ifdef WIN32
12646 "win32",
12647#endif
12648#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12649 "win32unix",
12650#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012651#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 "win64",
12653#endif
12654#ifdef EBCDIC
12655 "ebcdic",
12656#endif
12657#ifndef CASE_INSENSITIVE_FILENAME
12658 "fname_case",
12659#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012660#ifdef HAVE_ACL
12661 "acl",
12662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663#ifdef FEAT_ARABIC
12664 "arabic",
12665#endif
12666#ifdef FEAT_AUTOCMD
12667 "autocmd",
12668#endif
12669#ifdef FEAT_BEVAL
12670 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012671# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12672 "balloon_multiline",
12673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674#endif
12675#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12676 "builtin_terms",
12677# ifdef ALL_BUILTIN_TCAPS
12678 "all_builtin_terms",
12679# endif
12680#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012681#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12682 || defined(FEAT_GUI_W32) \
12683 || defined(FEAT_GUI_MOTIF))
12684 "browsefilter",
12685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686#ifdef FEAT_BYTEOFF
12687 "byte_offset",
12688#endif
12689#ifdef FEAT_CINDENT
12690 "cindent",
12691#endif
12692#ifdef FEAT_CLIENTSERVER
12693 "clientserver",
12694#endif
12695#ifdef FEAT_CLIPBOARD
12696 "clipboard",
12697#endif
12698#ifdef FEAT_CMDL_COMPL
12699 "cmdline_compl",
12700#endif
12701#ifdef FEAT_CMDHIST
12702 "cmdline_hist",
12703#endif
12704#ifdef FEAT_COMMENTS
12705 "comments",
12706#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012707#ifdef FEAT_CONCEAL
12708 "conceal",
12709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#ifdef FEAT_CRYPT
12711 "cryptv",
12712#endif
12713#ifdef FEAT_CSCOPE
12714 "cscope",
12715#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012716#ifdef FEAT_CURSORBIND
12717 "cursorbind",
12718#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012719#ifdef CURSOR_SHAPE
12720 "cursorshape",
12721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#ifdef DEBUG
12723 "debug",
12724#endif
12725#ifdef FEAT_CON_DIALOG
12726 "dialog_con",
12727#endif
12728#ifdef FEAT_GUI_DIALOG
12729 "dialog_gui",
12730#endif
12731#ifdef FEAT_DIFF
12732 "diff",
12733#endif
12734#ifdef FEAT_DIGRAPHS
12735 "digraphs",
12736#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012737#ifdef FEAT_DIRECTX
12738 "directx",
12739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#ifdef FEAT_DND
12741 "dnd",
12742#endif
12743#ifdef FEAT_EMACS_TAGS
12744 "emacs_tags",
12745#endif
12746 "eval", /* always present, of course! */
12747#ifdef FEAT_EX_EXTRA
12748 "ex_extra",
12749#endif
12750#ifdef FEAT_SEARCH_EXTRA
12751 "extra_search",
12752#endif
12753#ifdef FEAT_FKMAP
12754 "farsi",
12755#endif
12756#ifdef FEAT_SEARCHPATH
12757 "file_in_path",
12758#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012759#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012760 "filterpipe",
12761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762#ifdef FEAT_FIND_ID
12763 "find_in_path",
12764#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012765#ifdef FEAT_FLOAT
12766 "float",
12767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768#ifdef FEAT_FOLDING
12769 "folding",
12770#endif
12771#ifdef FEAT_FOOTER
12772 "footer",
12773#endif
12774#if !defined(USE_SYSTEM) && defined(UNIX)
12775 "fork",
12776#endif
12777#ifdef FEAT_GETTEXT
12778 "gettext",
12779#endif
12780#ifdef FEAT_GUI
12781 "gui",
12782#endif
12783#ifdef FEAT_GUI_ATHENA
12784# ifdef FEAT_GUI_NEXTAW
12785 "gui_neXtaw",
12786# else
12787 "gui_athena",
12788# endif
12789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790#ifdef FEAT_GUI_GTK
12791 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012794#ifdef FEAT_GUI_GNOME
12795 "gui_gnome",
12796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797#ifdef FEAT_GUI_MAC
12798 "gui_mac",
12799#endif
12800#ifdef FEAT_GUI_MOTIF
12801 "gui_motif",
12802#endif
12803#ifdef FEAT_GUI_PHOTON
12804 "gui_photon",
12805#endif
12806#ifdef FEAT_GUI_W16
12807 "gui_win16",
12808#endif
12809#ifdef FEAT_GUI_W32
12810 "gui_win32",
12811#endif
12812#ifdef FEAT_HANGULIN
12813 "hangul_input",
12814#endif
12815#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12816 "iconv",
12817#endif
12818#ifdef FEAT_INS_EXPAND
12819 "insert_expand",
12820#endif
12821#ifdef FEAT_JUMPLIST
12822 "jumplist",
12823#endif
12824#ifdef FEAT_KEYMAP
12825 "keymap",
12826#endif
12827#ifdef FEAT_LANGMAP
12828 "langmap",
12829#endif
12830#ifdef FEAT_LIBCALL
12831 "libcall",
12832#endif
12833#ifdef FEAT_LINEBREAK
12834 "linebreak",
12835#endif
12836#ifdef FEAT_LISP
12837 "lispindent",
12838#endif
12839#ifdef FEAT_LISTCMDS
12840 "listcmds",
12841#endif
12842#ifdef FEAT_LOCALMAP
12843 "localmap",
12844#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012845#ifdef FEAT_LUA
12846# ifndef DYNAMIC_LUA
12847 "lua",
12848# endif
12849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850#ifdef FEAT_MENU
12851 "menu",
12852#endif
12853#ifdef FEAT_SESSION
12854 "mksession",
12855#endif
12856#ifdef FEAT_MODIFY_FNAME
12857 "modify_fname",
12858#endif
12859#ifdef FEAT_MOUSE
12860 "mouse",
12861#endif
12862#ifdef FEAT_MOUSESHAPE
12863 "mouseshape",
12864#endif
12865#if defined(UNIX) || defined(VMS)
12866# ifdef FEAT_MOUSE_DEC
12867 "mouse_dec",
12868# endif
12869# ifdef FEAT_MOUSE_GPM
12870 "mouse_gpm",
12871# endif
12872# ifdef FEAT_MOUSE_JSB
12873 "mouse_jsbterm",
12874# endif
12875# ifdef FEAT_MOUSE_NET
12876 "mouse_netterm",
12877# endif
12878# ifdef FEAT_MOUSE_PTERM
12879 "mouse_pterm",
12880# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012881# ifdef FEAT_MOUSE_SGR
12882 "mouse_sgr",
12883# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012884# ifdef FEAT_SYSMOUSE
12885 "mouse_sysmouse",
12886# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012887# ifdef FEAT_MOUSE_URXVT
12888 "mouse_urxvt",
12889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890# ifdef FEAT_MOUSE_XTERM
12891 "mouse_xterm",
12892# endif
12893#endif
12894#ifdef FEAT_MBYTE
12895 "multi_byte",
12896#endif
12897#ifdef FEAT_MBYTE_IME
12898 "multi_byte_ime",
12899#endif
12900#ifdef FEAT_MULTI_LANG
12901 "multi_lang",
12902#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012903#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012904#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012905 "mzscheme",
12906#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#ifdef FEAT_OLE
12909 "ole",
12910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911#ifdef FEAT_PATH_EXTRA
12912 "path_extra",
12913#endif
12914#ifdef FEAT_PERL
12915#ifndef DYNAMIC_PERL
12916 "perl",
12917#endif
12918#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012919#ifdef FEAT_PERSISTENT_UNDO
12920 "persistent_undo",
12921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#ifdef FEAT_PYTHON
12923#ifndef DYNAMIC_PYTHON
12924 "python",
12925#endif
12926#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012927#ifdef FEAT_PYTHON3
12928#ifndef DYNAMIC_PYTHON3
12929 "python3",
12930#endif
12931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932#ifdef FEAT_POSTSCRIPT
12933 "postscript",
12934#endif
12935#ifdef FEAT_PRINTER
12936 "printer",
12937#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012938#ifdef FEAT_PROFILE
12939 "profile",
12940#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012941#ifdef FEAT_RELTIME
12942 "reltime",
12943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944#ifdef FEAT_QUICKFIX
12945 "quickfix",
12946#endif
12947#ifdef FEAT_RIGHTLEFT
12948 "rightleft",
12949#endif
12950#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12951 "ruby",
12952#endif
12953#ifdef FEAT_SCROLLBIND
12954 "scrollbind",
12955#endif
12956#ifdef FEAT_CMDL_INFO
12957 "showcmd",
12958 "cmdline_info",
12959#endif
12960#ifdef FEAT_SIGNS
12961 "signs",
12962#endif
12963#ifdef FEAT_SMARTINDENT
12964 "smartindent",
12965#endif
12966#ifdef FEAT_SNIFF
12967 "sniff",
12968#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012969#ifdef STARTUPTIME
12970 "startuptime",
12971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972#ifdef FEAT_STL_OPT
12973 "statusline",
12974#endif
12975#ifdef FEAT_SUN_WORKSHOP
12976 "sun_workshop",
12977#endif
12978#ifdef FEAT_NETBEANS_INTG
12979 "netbeans_intg",
12980#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012981#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012982 "spell",
12983#endif
12984#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 "syntax",
12986#endif
12987#if defined(USE_SYSTEM) || !defined(UNIX)
12988 "system",
12989#endif
12990#ifdef FEAT_TAG_BINS
12991 "tag_binary",
12992#endif
12993#ifdef FEAT_TAG_OLDSTATIC
12994 "tag_old_static",
12995#endif
12996#ifdef FEAT_TAG_ANYWHITE
12997 "tag_any_white",
12998#endif
12999#ifdef FEAT_TCL
13000# ifndef DYNAMIC_TCL
13001 "tcl",
13002# endif
13003#endif
13004#ifdef TERMINFO
13005 "terminfo",
13006#endif
13007#ifdef FEAT_TERMRESPONSE
13008 "termresponse",
13009#endif
13010#ifdef FEAT_TEXTOBJ
13011 "textobjects",
13012#endif
13013#ifdef HAVE_TGETENT
13014 "tgetent",
13015#endif
13016#ifdef FEAT_TITLE
13017 "title",
13018#endif
13019#ifdef FEAT_TOOLBAR
13020 "toolbar",
13021#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013022#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13023 "unnamedplus",
13024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025#ifdef FEAT_USR_CMDS
13026 "user-commands", /* was accidentally included in 5.4 */
13027 "user_commands",
13028#endif
13029#ifdef FEAT_VIMINFO
13030 "viminfo",
13031#endif
13032#ifdef FEAT_VERTSPLIT
13033 "vertsplit",
13034#endif
13035#ifdef FEAT_VIRTUALEDIT
13036 "virtualedit",
13037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039#ifdef FEAT_VISUALEXTRA
13040 "visualextra",
13041#endif
13042#ifdef FEAT_VREPLACE
13043 "vreplace",
13044#endif
13045#ifdef FEAT_WILDIGN
13046 "wildignore",
13047#endif
13048#ifdef FEAT_WILDMENU
13049 "wildmenu",
13050#endif
13051#ifdef FEAT_WINDOWS
13052 "windows",
13053#endif
13054#ifdef FEAT_WAK
13055 "winaltkeys",
13056#endif
13057#ifdef FEAT_WRITEBACKUP
13058 "writebackup",
13059#endif
13060#ifdef FEAT_XIM
13061 "xim",
13062#endif
13063#ifdef FEAT_XFONTSET
13064 "xfontset",
13065#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013066#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013067 "xpm",
13068 "xpm_w32", /* for backward compatibility */
13069#else
13070# if defined(HAVE_XPM)
13071 "xpm",
13072# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074#ifdef USE_XSMP
13075 "xsmp",
13076#endif
13077#ifdef USE_XSMP_INTERACT
13078 "xsmp_interact",
13079#endif
13080#ifdef FEAT_XCLIPBOARD
13081 "xterm_clipboard",
13082#endif
13083#ifdef FEAT_XTERM_SAVE
13084 "xterm_save",
13085#endif
13086#if defined(UNIX) && defined(FEAT_X11)
13087 "X11",
13088#endif
13089 NULL
13090 };
13091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 for (i = 0; has_list[i] != NULL; ++i)
13094 if (STRICMP(name, has_list[i]) == 0)
13095 {
13096 n = TRUE;
13097 break;
13098 }
13099
13100 if (n == FALSE)
13101 {
13102 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013103 {
13104 if (name[5] == '-'
13105 && STRLEN(name) > 11
13106 && vim_isdigit(name[6])
13107 && vim_isdigit(name[8])
13108 && vim_isdigit(name[10]))
13109 {
13110 int major = atoi((char *)name + 6);
13111 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013112
13113 /* Expect "patch-9.9.01234". */
13114 n = (major < VIM_VERSION_MAJOR
13115 || (major == VIM_VERSION_MAJOR
13116 && (minor < VIM_VERSION_MINOR
13117 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013118 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013119 }
13120 else
13121 n = has_patch(atoi((char *)name + 5));
13122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 else if (STRICMP(name, "vim_starting") == 0)
13124 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013125#ifdef FEAT_MBYTE
13126 else if (STRICMP(name, "multi_byte_encoding") == 0)
13127 n = has_mbyte;
13128#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013129#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13130 else if (STRICMP(name, "balloon_multiline") == 0)
13131 n = multiline_balloon_available();
13132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133#ifdef DYNAMIC_TCL
13134 else if (STRICMP(name, "tcl") == 0)
13135 n = tcl_enabled(FALSE);
13136#endif
13137#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13138 else if (STRICMP(name, "iconv") == 0)
13139 n = iconv_enabled(FALSE);
13140#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013141#ifdef DYNAMIC_LUA
13142 else if (STRICMP(name, "lua") == 0)
13143 n = lua_enabled(FALSE);
13144#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013145#ifdef DYNAMIC_MZSCHEME
13146 else if (STRICMP(name, "mzscheme") == 0)
13147 n = mzscheme_enabled(FALSE);
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149#ifdef DYNAMIC_RUBY
13150 else if (STRICMP(name, "ruby") == 0)
13151 n = ruby_enabled(FALSE);
13152#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013153#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154#ifdef DYNAMIC_PYTHON
13155 else if (STRICMP(name, "python") == 0)
13156 n = python_enabled(FALSE);
13157#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013158#endif
13159#ifdef FEAT_PYTHON3
13160#ifdef DYNAMIC_PYTHON3
13161 else if (STRICMP(name, "python3") == 0)
13162 n = python3_enabled(FALSE);
13163#endif
13164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165#ifdef DYNAMIC_PERL
13166 else if (STRICMP(name, "perl") == 0)
13167 n = perl_enabled(FALSE);
13168#endif
13169#ifdef FEAT_GUI
13170 else if (STRICMP(name, "gui_running") == 0)
13171 n = (gui.in_use || gui.starting);
13172# ifdef FEAT_GUI_W32
13173 else if (STRICMP(name, "gui_win32s") == 0)
13174 n = gui_is_win32s();
13175# endif
13176# ifdef FEAT_BROWSE
13177 else if (STRICMP(name, "browse") == 0)
13178 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13179# endif
13180#endif
13181#ifdef FEAT_SYN_HL
13182 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013183 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184#endif
13185#if defined(WIN3264)
13186 else if (STRICMP(name, "win95") == 0)
13187 n = mch_windows95();
13188#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013189#ifdef FEAT_NETBEANS_INTG
13190 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013191 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 }
13194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013199 * "has_key()" function
13200 */
13201 static void
13202f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013205{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013206 if (argvars[0].v_type != VAR_DICT)
13207 {
13208 EMSG(_(e_dictreq));
13209 return;
13210 }
13211 if (argvars[0].vval.v_dict == NULL)
13212 return;
13213
13214 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013215 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013216}
13217
13218/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013219 * "haslocaldir()" function
13220 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013221 static void
13222f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013223 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013224 typval_T *rettv;
13225{
13226 rettv->vval.v_number = (curwin->w_localdir != NULL);
13227}
13228
13229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 * "hasmapto()" function
13231 */
13232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013234 typval_T *argvars;
13235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236{
13237 char_u *name;
13238 char_u *mode;
13239 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013240 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013243 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 mode = (char_u *)"nvo";
13245 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013248 if (argvars[2].v_type != VAR_UNKNOWN)
13249 abbr = get_tv_number(&argvars[2]);
13250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251
Bram Moolenaar2c932302006-03-18 21:42:09 +000013252 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
13258/*
13259 * "histadd()" function
13260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013263 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265{
13266#ifdef FEAT_CMDHIST
13267 int histype;
13268 char_u *str;
13269 char_u buf[NUMBUFLEN];
13270#endif
13271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 if (check_restricted() || check_secure())
13274 return;
13275#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13277 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 if (histype >= 0)
13279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013280 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 if (*str != NUL)
13282 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013283 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 return;
13287 }
13288 }
13289#endif
13290}
13291
13292/*
13293 * "histdel()" function
13294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013297 typval_T *argvars UNUSED;
13298 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299{
13300#ifdef FEAT_CMDHIST
13301 int n;
13302 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13306 if (str == NULL)
13307 n = 0;
13308 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 else
13316 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 get_tv_string_buf(&argvars[1], buf));
13319 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#endif
13321}
13322
13323/*
13324 * "histget()" function
13325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013327f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013328 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330{
13331#ifdef FEAT_CMDHIST
13332 int type;
13333 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013336 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13337 if (str == NULL)
13338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 {
13341 type = get_histtype(str);
13342 if (argvars[1].v_type == VAR_UNKNOWN)
13343 idx = get_history_idx(type);
13344 else
13345 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13346 /* -1 on type error */
13347 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353}
13354
13355/*
13356 * "histnr()" function
13357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362{
13363 int i;
13364
13365#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 char_u *history = get_tv_string_chk(&argvars[0]);
13367
13368 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 if (i >= HIST_CMD && i < HIST_COUNT)
13370 i = get_history_idx(i);
13371 else
13372#endif
13373 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375}
13376
13377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 * "highlightID(name)" function
13379 */
13380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
13383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386}
13387
13388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013389 * "highlight_exists()" function
13390 */
13391 static void
13392f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 typval_T *argvars;
13394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395{
13396 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13397}
13398
13399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 * "hostname()" function
13401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013404 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406{
13407 char_u hostname[256];
13408
13409 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->v_type = VAR_STRING;
13411 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412}
13413
13414/*
13415 * iconv() function
13416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013419 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421{
13422#ifdef FEAT_MBYTE
13423 char_u buf1[NUMBUFLEN];
13424 char_u buf2[NUMBUFLEN];
13425 char_u *from, *to, *str;
13426 vimconv_T vimconv;
13427#endif
13428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->v_type = VAR_STRING;
13430 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431
13432#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433 str = get_tv_string(&argvars[0]);
13434 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13435 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 vimconv.vc_type = CONV_NONE;
13437 convert_setup(&vimconv, from, to);
13438
13439 /* If the encodings are equal, no conversion needed. */
13440 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444
13445 convert_setup(&vimconv, NULL, NULL);
13446 vim_free(from);
13447 vim_free(to);
13448#endif
13449}
13450
13451/*
13452 * "indent()" function
13453 */
13454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459 linenr_T lnum;
13460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466}
13467
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013468/*
13469 * "index()" function
13470 */
13471 static void
13472f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *argvars;
13474 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013475{
Bram Moolenaar33570922005-01-25 22:26:29 +000013476 list_T *l;
13477 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013478 long idx = 0;
13479 int ic = FALSE;
13480
13481 rettv->vval.v_number = -1;
13482 if (argvars[0].v_type != VAR_LIST)
13483 {
13484 EMSG(_(e_listreq));
13485 return;
13486 }
13487 l = argvars[0].vval.v_list;
13488 if (l != NULL)
13489 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013490 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013491 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 int error = FALSE;
13494
Bram Moolenaar758711c2005-02-02 23:11:38 +000013495 /* Start at specified item. Use the cached index that list_find()
13496 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013498 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013499 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013500 ic = get_tv_number_chk(&argvars[3], &error);
13501 if (error)
13502 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013503 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013504
Bram Moolenaar758711c2005-02-02 23:11:38 +000013505 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013506 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013507 {
13508 rettv->vval.v_number = idx;
13509 break;
13510 }
13511 }
13512}
13513
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514static int inputsecret_flag = 0;
13515
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013516static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13517
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013519 * This function is used by f_input() and f_inputdialog() functions. The third
13520 * argument to f_input() specifies the type of completion to use at the
13521 * prompt. The third argument to f_inputdialog() specifies the value to return
13522 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 */
13524 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013525get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 typval_T *argvars;
13527 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013528 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 char_u *p = NULL;
13532 int c;
13533 char_u buf[NUMBUFLEN];
13534 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013535 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013536 int xp_type = EXPAND_NOTHING;
13537 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541
13542#ifdef NO_CONSOLE_INPUT
13543 /* While starting up, there is no place to enter text. */
13544 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546#endif
13547
13548 cmd_silent = FALSE; /* Want to see the prompt. */
13549 if (prompt != NULL)
13550 {
13551 /* Only the part of the message after the last NL is considered as
13552 * prompt for the command line */
13553 p = vim_strrchr(prompt, '\n');
13554 if (p == NULL)
13555 p = prompt;
13556 else
13557 {
13558 ++p;
13559 c = *p;
13560 *p = NUL;
13561 msg_start();
13562 msg_clr_eos();
13563 msg_puts_attr(prompt, echo_attr);
13564 msg_didout = FALSE;
13565 msg_starthere();
13566 *p = c;
13567 }
13568 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 if (argvars[1].v_type != VAR_UNKNOWN)
13571 {
13572 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13573 if (defstr != NULL)
13574 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013576 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013577 {
13578 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013579 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013580 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013581
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013582 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013583 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013584
Bram Moolenaar4463f292005-09-25 22:20:24 +000013585 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13586 if (xp_name == NULL)
13587 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013588
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013589 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013590
Bram Moolenaar4463f292005-09-25 22:20:24 +000013591 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13592 &xp_arg) == FAIL)
13593 return;
13594 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013595 }
13596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013598 {
13599# ifdef FEAT_EX_EXTRA
13600 int save_ex_normal_busy = ex_normal_busy;
13601 ex_normal_busy = 0;
13602# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013604 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13605 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013606# ifdef FEAT_EX_EXTRA
13607 ex_normal_busy = save_ex_normal_busy;
13608# endif
13609 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013610 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013611 && argvars[1].v_type != VAR_UNKNOWN
13612 && argvars[2].v_type != VAR_UNKNOWN)
13613 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13614 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013615
13616 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 /* since the user typed this, no need to wait for return */
13619 need_wait_return = FALSE;
13620 msg_didout = FALSE;
13621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 cmd_silent = cmd_silent_save;
13623}
13624
13625/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013626 * "input()" function
13627 * Also handles inputsecret() when inputsecret is set.
13628 */
13629 static void
13630f_input(argvars, rettv)
13631 typval_T *argvars;
13632 typval_T *rettv;
13633{
13634 get_user_input(argvars, rettv, FALSE);
13635}
13636
13637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 * "inputdialog()" function
13639 */
13640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *argvars;
13643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
13645#if defined(FEAT_GUI_TEXTDIALOG)
13646 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13647 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13648 {
13649 char_u *message;
13650 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 message = get_tv_string_chk(&argvars[0]);
13654 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013655 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013656 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 else
13658 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 if (message != NULL && defstr != NULL
13660 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013661 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 else
13664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 if (message != NULL && defstr != NULL
13666 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013667 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->vval.v_string = vim_strsave(
13669 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 }
13675 else
13676#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013677 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678}
13679
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013680/*
13681 * "inputlist()" function
13682 */
13683 static void
13684f_inputlist(argvars, rettv)
13685 typval_T *argvars;
13686 typval_T *rettv;
13687{
13688 listitem_T *li;
13689 int selected;
13690 int mouse_used;
13691
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013692#ifdef NO_CONSOLE_INPUT
13693 /* While starting up, there is no place to enter text. */
13694 if (no_console_input())
13695 return;
13696#endif
13697 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13698 {
13699 EMSG2(_(e_listarg), "inputlist()");
13700 return;
13701 }
13702
13703 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013704 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013705 lines_left = Rows; /* avoid more prompt */
13706 msg_scroll = TRUE;
13707 msg_clr_eos();
13708
13709 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13710 {
13711 msg_puts(get_tv_string(&li->li_tv));
13712 msg_putchar('\n');
13713 }
13714
13715 /* Ask for choice. */
13716 selected = prompt_for_number(&mouse_used);
13717 if (mouse_used)
13718 selected -= lines_left;
13719
13720 rettv->vval.v_number = selected;
13721}
13722
13723
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13725
13726/*
13727 * "inputrestore()" function
13728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013731 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733{
13734 if (ga_userinput.ga_len > 0)
13735 {
13736 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13738 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013739 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 }
13741 else if (p_verbose > 1)
13742 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013743 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 }
13746}
13747
13748/*
13749 * "inputsave()" function
13750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013756 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 if (ga_grow(&ga_userinput, 1) == OK)
13758 {
13759 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13760 + ga_userinput.ga_len);
13761 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013762 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 }
13764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766}
13767
13768/*
13769 * "inputsecret()" function
13770 */
13771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776 ++cmdline_star;
13777 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 --cmdline_star;
13780 --inputsecret_flag;
13781}
13782
13783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013784 * "insert()" function
13785 */
13786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013788 typval_T *argvars;
13789 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013790{
13791 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 listitem_T *item;
13793 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013795
13796 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013798 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013799 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013800 {
13801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802 before = get_tv_number_chk(&argvars[2], &error);
13803 if (error)
13804 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013805
Bram Moolenaar758711c2005-02-02 23:11:38 +000013806 if (before == l->lv_len)
13807 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013808 else
13809 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013810 item = list_find(l, before);
13811 if (item == NULL)
13812 {
13813 EMSGN(_(e_listidx), before);
13814 l = NULL;
13815 }
13816 }
13817 if (l != NULL)
13818 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013819 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013820 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013821 }
13822 }
13823}
13824
13825/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013826 * "invert(expr)" function
13827 */
13828 static void
13829f_invert(argvars, rettv)
13830 typval_T *argvars;
13831 typval_T *rettv;
13832{
13833 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13834}
13835
13836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 * "isdirectory()" function
13838 */
13839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013840f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013841 typval_T *argvars;
13842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845}
13846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013847/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013848 * "islocked()" function
13849 */
13850 static void
13851f_islocked(argvars, rettv)
13852 typval_T *argvars;
13853 typval_T *rettv;
13854{
13855 lval_T lv;
13856 char_u *end;
13857 dictitem_T *di;
13858
13859 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013860 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13861 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013862 if (end != NULL && lv.ll_name != NULL)
13863 {
13864 if (*end != NUL)
13865 EMSG(_(e_trailing));
13866 else
13867 {
13868 if (lv.ll_tv == NULL)
13869 {
13870 if (check_changedtick(lv.ll_name))
13871 rettv->vval.v_number = 1; /* always locked */
13872 else
13873 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013874 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013875 if (di != NULL)
13876 {
13877 /* Consider a variable locked when:
13878 * 1. the variable itself is locked
13879 * 2. the value of the variable is locked.
13880 * 3. the List or Dict value is locked.
13881 */
13882 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13883 || tv_islocked(&di->di_tv));
13884 }
13885 }
13886 }
13887 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013888 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013889 else if (lv.ll_newkey != NULL)
13890 EMSG2(_(e_dictkey), lv.ll_newkey);
13891 else if (lv.ll_list != NULL)
13892 /* List item. */
13893 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13894 else
13895 /* Dictionary item. */
13896 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13897 }
13898 }
13899
13900 clear_lval(&lv);
13901}
13902
Bram Moolenaar33570922005-01-25 22:26:29 +000013903static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904
13905/*
13906 * Turn a dict into a list:
13907 * "what" == 0: list of keys
13908 * "what" == 1: list of values
13909 * "what" == 2: list of items
13910 */
13911 static void
13912dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013913 typval_T *argvars;
13914 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013915 int what;
13916{
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 list_T *l2;
13918 dictitem_T *di;
13919 hashitem_T *hi;
13920 listitem_T *li;
13921 listitem_T *li2;
13922 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013923 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013924
Bram Moolenaar8c711452005-01-14 21:53:12 +000013925 if (argvars[0].v_type != VAR_DICT)
13926 {
13927 EMSG(_(e_dictreq));
13928 return;
13929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013930 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013931 return;
13932
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013933 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013934 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013935
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013936 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013938 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013939 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013940 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013941 --todo;
13942 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013944 li = listitem_alloc();
13945 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013946 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013947 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013948
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013949 if (what == 0)
13950 {
13951 /* keys() */
13952 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013953 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013954 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13955 }
13956 else if (what == 1)
13957 {
13958 /* values() */
13959 copy_tv(&di->di_tv, &li->li_tv);
13960 }
13961 else
13962 {
13963 /* items() */
13964 l2 = list_alloc();
13965 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013966 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013967 li->li_tv.vval.v_list = l2;
13968 if (l2 == NULL)
13969 break;
13970 ++l2->lv_refcount;
13971
13972 li2 = listitem_alloc();
13973 if (li2 == NULL)
13974 break;
13975 list_append(l2, li2);
13976 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013977 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013978 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13979
13980 li2 = listitem_alloc();
13981 if (li2 == NULL)
13982 break;
13983 list_append(l2, li2);
13984 copy_tv(&di->di_tv, &li2->li_tv);
13985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013986 }
13987 }
13988}
13989
13990/*
13991 * "items(dict)" function
13992 */
13993 static void
13994f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013995 typval_T *argvars;
13996 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013997{
13998 dict_list(argvars, rettv, 2);
13999}
14000
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 * "join()" function
14003 */
14004 static void
14005f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014006 typval_T *argvars;
14007 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014008{
14009 garray_T ga;
14010 char_u *sep;
14011
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014012 if (argvars[0].v_type != VAR_LIST)
14013 {
14014 EMSG(_(e_listreq));
14015 return;
14016 }
14017 if (argvars[0].vval.v_list == NULL)
14018 return;
14019 if (argvars[1].v_type == VAR_UNKNOWN)
14020 sep = (char_u *)" ";
14021 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014023
14024 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025
14026 if (sep != NULL)
14027 {
14028 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014029 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 ga_append(&ga, NUL);
14031 rettv->vval.v_string = (char_u *)ga.ga_data;
14032 }
14033 else
14034 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014035}
14036
14037/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014038 * "keys()" function
14039 */
14040 static void
14041f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 typval_T *argvars;
14043 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014044{
14045 dict_list(argvars, rettv, 0);
14046}
14047
14048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 * "last_buffer_nr()" function.
14050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014052f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014053 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055{
14056 int n = 0;
14057 buf_T *buf;
14058
14059 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14060 if (n < buf->b_fnum)
14061 n = buf->b_fnum;
14062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064}
14065
14066/*
14067 * "len()" function
14068 */
14069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014071 typval_T *argvars;
14072 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014073{
14074 switch (argvars[0].v_type)
14075 {
14076 case VAR_STRING:
14077 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 rettv->vval.v_number = (varnumber_T)STRLEN(
14079 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080 break;
14081 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014084 case VAR_DICT:
14085 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14086 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014088 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014089 break;
14090 }
14091}
14092
Bram Moolenaar33570922005-01-25 22:26:29 +000014093static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014094
14095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014097 typval_T *argvars;
14098 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014099 int type;
14100{
14101#ifdef FEAT_LIBCALL
14102 char_u *string_in;
14103 char_u **string_result;
14104 int nr_result;
14105#endif
14106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014108 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014110
14111 if (check_restricted() || check_secure())
14112 return;
14113
14114#ifdef FEAT_LIBCALL
14115 /* The first two args must be strings, otherwise its meaningless */
14116 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14117 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014118 string_in = NULL;
14119 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014120 string_in = argvars[2].vval.v_string;
14121 if (type == VAR_NUMBER)
14122 string_result = NULL;
14123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014125 if (mch_libcall(argvars[0].vval.v_string,
14126 argvars[1].vval.v_string,
14127 string_in,
14128 argvars[2].vval.v_number,
14129 string_result,
14130 &nr_result) == OK
14131 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014133 }
14134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135}
14136
14137/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138 * "libcall()" function
14139 */
14140 static void
14141f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014142 typval_T *argvars;
14143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014144{
14145 libcall_common(argvars, rettv, VAR_STRING);
14146}
14147
14148/*
14149 * "libcallnr()" function
14150 */
14151 static void
14152f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014153 typval_T *argvars;
14154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155{
14156 libcall_common(argvars, rettv, VAR_NUMBER);
14157}
14158
14159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 * "line(string)" function
14161 */
14162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014164 typval_T *argvars;
14165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166{
14167 linenr_T lnum = 0;
14168 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014169 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014171 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 if (fp != NULL)
14173 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175}
14176
14177/*
14178 * "line2byte(lnum)" function
14179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184{
14185#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014186 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187#else
14188 linenr_T lnum;
14189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014192 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14195 if (rettv->vval.v_number >= 0)
14196 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197#endif
14198}
14199
14200/*
14201 * "lispindent(lnum)" function
14202 */
14203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014205 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207{
14208#ifdef FEAT_LISP
14209 pos_T pos;
14210 linenr_T lnum;
14211
14212 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14215 {
14216 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 curwin->w_cursor = pos;
14219 }
14220 else
14221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223}
14224
14225/*
14226 * "localtime()" function
14227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014230 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234}
14235
Bram Moolenaar33570922005-01-25 22:26:29 +000014236static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237
14238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *argvars;
14241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 int exact;
14243{
14244 char_u *keys;
14245 char_u *which;
14246 char_u buf[NUMBUFLEN];
14247 char_u *keys_buf = NULL;
14248 char_u *rhs;
14249 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014250 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014251 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014252 mapblock_T *mp;
14253 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
14255 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->v_type = VAR_STRING;
14257 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 if (*keys == NUL)
14261 return;
14262
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014263 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014266 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014267 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014268 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014269 if (argvars[3].v_type != VAR_UNKNOWN)
14270 get_dict = get_tv_number(&argvars[3]);
14271 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 else
14274 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 if (which == NULL)
14276 return;
14277
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 mode = get_map_mode(&which, 0);
14279
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014280 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014281 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014283
14284 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014286 /* Return a string. */
14287 if (rhs != NULL)
14288 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289
Bram Moolenaarbd743252010-10-20 21:23:33 +020014290 }
14291 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14292 {
14293 /* Return a dictionary. */
14294 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14295 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14296 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaarbd743252010-10-20 21:23:33 +020014298 dict_add_nr_str(dict, "lhs", 0L, lhs);
14299 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14300 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14301 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14302 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14303 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14304 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014305 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014306 dict_add_nr_str(dict, "mode", 0L, mapmode);
14307
14308 vim_free(lhs);
14309 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 }
14311}
14312
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014313#ifdef FEAT_FLOAT
14314/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014315 * "log()" function
14316 */
14317 static void
14318f_log(argvars, rettv)
14319 typval_T *argvars;
14320 typval_T *rettv;
14321{
14322 float_T f;
14323
14324 rettv->v_type = VAR_FLOAT;
14325 if (get_float_arg(argvars, &f) == OK)
14326 rettv->vval.v_float = log(f);
14327 else
14328 rettv->vval.v_float = 0.0;
14329}
14330
14331/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014332 * "log10()" function
14333 */
14334 static void
14335f_log10(argvars, rettv)
14336 typval_T *argvars;
14337 typval_T *rettv;
14338{
14339 float_T f;
14340
14341 rettv->v_type = VAR_FLOAT;
14342 if (get_float_arg(argvars, &f) == OK)
14343 rettv->vval.v_float = log10(f);
14344 else
14345 rettv->vval.v_float = 0.0;
14346}
14347#endif
14348
Bram Moolenaar1dced572012-04-05 16:54:08 +020014349#ifdef FEAT_LUA
14350/*
14351 * "luaeval()" function
14352 */
14353 static void
14354f_luaeval(argvars, rettv)
14355 typval_T *argvars;
14356 typval_T *rettv;
14357{
14358 char_u *str;
14359 char_u buf[NUMBUFLEN];
14360
14361 str = get_tv_string_buf(&argvars[0], buf);
14362 do_luaeval(str, argvars + 1, rettv);
14363}
14364#endif
14365
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367 * "map()" function
14368 */
14369 static void
14370f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014373{
14374 filter_map(argvars, rettv, TRUE);
14375}
14376
14377/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 */
14380 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *argvars;
14383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386}
14387
14388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 */
14391 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014392f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014396 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397}
14398
Bram Moolenaar33570922005-01-25 22:26:29 +000014399static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
14401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014403 typval_T *argvars;
14404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 int type;
14406{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014407 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014408 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014409 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 char_u *pat;
14411 regmatch_T regmatch;
14412 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014413 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 char_u *save_cpo;
14415 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014416 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014417 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014418 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014419 list_T *l = NULL;
14420 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014421 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014422 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423
14424 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14425 save_cpo = p_cpo;
14426 p_cpo = (char_u *)"";
14427
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014428 rettv->vval.v_number = -1;
14429 if (type == 3)
14430 {
14431 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014432 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014433 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014434 }
14435 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->v_type = VAR_STRING;
14438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014441 if (argvars[0].v_type == VAR_LIST)
14442 {
14443 if ((l = argvars[0].vval.v_list) == NULL)
14444 goto theend;
14445 li = l->lv_first;
14446 }
14447 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014448 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014449 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014450 len = (long)STRLEN(str);
14451 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14454 if (pat == NULL)
14455 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 int error = FALSE;
14460
14461 start = get_tv_number_chk(&argvars[2], &error);
14462 if (error)
14463 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014464 if (l != NULL)
14465 {
14466 li = list_find(l, start);
14467 if (li == NULL)
14468 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014469 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014470 }
14471 else
14472 {
14473 if (start < 0)
14474 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014475 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014477 /* When "count" argument is there ignore matches before "start",
14478 * otherwise skip part of the string. Differs when pattern is "^"
14479 * or "\<". */
14480 if (argvars[3].v_type != VAR_UNKNOWN)
14481 startcol = start;
14482 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014483 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014484 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014485 len -= start;
14486 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014487 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014489 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 nth = get_tv_number_chk(&argvars[3], &error);
14491 if (error)
14492 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 }
14494
14495 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14496 if (regmatch.regprog != NULL)
14497 {
14498 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014499
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014500 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014501 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502 if (l != NULL)
14503 {
14504 if (li == NULL)
14505 {
14506 match = FALSE;
14507 break;
14508 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014509 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014510 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014511 if (str == NULL)
14512 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014513 }
14514
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014515 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014517 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014518 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014519 if (l == NULL && !match)
14520 break;
14521
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014522 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014523 if (l != NULL)
14524 {
14525 li = li->li_next;
14526 ++idx;
14527 }
14528 else
14529 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014530#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014531 startcol = (colnr_T)(regmatch.startp[0]
14532 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014533#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014534 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014535#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014536 if (startcol > (colnr_T)len
14537 || str + startcol <= regmatch.startp[0])
14538 {
14539 match = FALSE;
14540 break;
14541 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014542 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014543 }
14544
14545 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014547 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 int i;
14550
14551 /* return list with matched string and submatches */
14552 for (i = 0; i < NSUBEXP; ++i)
14553 {
14554 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014555 {
14556 if (list_append_string(rettv->vval.v_list,
14557 (char_u *)"", 0) == FAIL)
14558 break;
14559 }
14560 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014561 regmatch.startp[i],
14562 (int)(regmatch.endp[i] - regmatch.startp[i]))
14563 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014564 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565 }
14566 }
14567 else if (type == 2)
14568 {
14569 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014570 if (l != NULL)
14571 copy_tv(&li->li_tv, rettv);
14572 else
14573 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014575 }
14576 else if (l != NULL)
14577 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 else
14579 {
14580 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 (varnumber_T)(regmatch.startp[0] - str);
14583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014586 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 }
14588 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014589 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 }
14591
14592theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 p_cpo = save_cpo;
14595}
14596
14597/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598 * "match()" function
14599 */
14600 static void
14601f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 typval_T *argvars;
14603 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604{
14605 find_some_match(argvars, rettv, 1);
14606}
14607
14608/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014609 * "matchadd()" function
14610 */
14611 static void
14612f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014613 typval_T *argvars UNUSED;
14614 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014615{
14616#ifdef FEAT_SEARCH_EXTRA
14617 char_u buf[NUMBUFLEN];
14618 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14619 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14620 int prio = 10; /* default priority */
14621 int id = -1;
14622 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014623 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014624
14625 rettv->vval.v_number = -1;
14626
14627 if (grp == NULL || pat == NULL)
14628 return;
14629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014630 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014631 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014632 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014633 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014634 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014635 if (argvars[4].v_type != VAR_UNKNOWN)
14636 {
14637 if (argvars[4].v_type != VAR_DICT)
14638 {
14639 EMSG(_(e_dictreq));
14640 return;
14641 }
14642 if (dict_find(argvars[4].vval.v_dict,
14643 (char_u *)"conceal", -1) != NULL)
14644 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14645 (char_u *)"conceal", FALSE);
14646 }
14647 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014648 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014649 if (error == TRUE)
14650 return;
14651 if (id >= 1 && id <= 3)
14652 {
14653 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14654 return;
14655 }
14656
Bram Moolenaar6561d522015-07-21 15:48:27 +020014657 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14658 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014659#endif
14660}
14661
14662/*
14663 * "matchaddpos()" function
14664 */
14665 static void
14666f_matchaddpos(argvars, rettv)
14667 typval_T *argvars UNUSED;
14668 typval_T *rettv UNUSED;
14669{
14670#ifdef FEAT_SEARCH_EXTRA
14671 char_u buf[NUMBUFLEN];
14672 char_u *group;
14673 int prio = 10;
14674 int id = -1;
14675 int error = FALSE;
14676 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014677 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014678
14679 rettv->vval.v_number = -1;
14680
14681 group = get_tv_string_buf_chk(&argvars[0], buf);
14682 if (group == NULL)
14683 return;
14684
14685 if (argvars[1].v_type != VAR_LIST)
14686 {
14687 EMSG2(_(e_listarg), "matchaddpos()");
14688 return;
14689 }
14690 l = argvars[1].vval.v_list;
14691 if (l == NULL)
14692 return;
14693
14694 if (argvars[2].v_type != VAR_UNKNOWN)
14695 {
14696 prio = get_tv_number_chk(&argvars[2], &error);
14697 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014698 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014699 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014700 if (argvars[4].v_type != VAR_UNKNOWN)
14701 {
14702 if (argvars[4].v_type != VAR_DICT)
14703 {
14704 EMSG(_(e_dictreq));
14705 return;
14706 }
14707 if (dict_find(argvars[4].vval.v_dict,
14708 (char_u *)"conceal", -1) != NULL)
14709 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14710 (char_u *)"conceal", FALSE);
14711 }
14712 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014713 }
14714 if (error == TRUE)
14715 return;
14716
14717 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14718 if (id == 1 || id == 2)
14719 {
14720 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14721 return;
14722 }
14723
Bram Moolenaar6561d522015-07-21 15:48:27 +020014724 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14725 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014726#endif
14727}
14728
14729/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014730 * "matcharg()" function
14731 */
14732 static void
14733f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014734 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014735 typval_T *rettv;
14736{
14737 if (rettv_list_alloc(rettv) == OK)
14738 {
14739#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014740 int id = get_tv_number(&argvars[0]);
14741 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014742
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014743 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014744 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014745 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14746 {
14747 list_append_string(rettv->vval.v_list,
14748 syn_id2name(m->hlg_id), -1);
14749 list_append_string(rettv->vval.v_list, m->pattern, -1);
14750 }
14751 else
14752 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014753 list_append_string(rettv->vval.v_list, NULL, -1);
14754 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014755 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014756 }
14757#endif
14758 }
14759}
14760
14761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014762 * "matchdelete()" function
14763 */
14764 static void
14765f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014766 typval_T *argvars UNUSED;
14767 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014768{
14769#ifdef FEAT_SEARCH_EXTRA
14770 rettv->vval.v_number = match_delete(curwin,
14771 (int)get_tv_number(&argvars[0]), TRUE);
14772#endif
14773}
14774
14775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776 * "matchend()" function
14777 */
14778 static void
14779f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 typval_T *argvars;
14781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014782{
14783 find_some_match(argvars, rettv, 0);
14784}
14785
14786/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014787 * "matchlist()" function
14788 */
14789 static void
14790f_matchlist(argvars, rettv)
14791 typval_T *argvars;
14792 typval_T *rettv;
14793{
14794 find_some_match(argvars, rettv, 3);
14795}
14796
14797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798 * "matchstr()" function
14799 */
14800 static void
14801f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 typval_T *argvars;
14803 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014804{
14805 find_some_match(argvars, rettv, 2);
14806}
14807
Bram Moolenaar33570922005-01-25 22:26:29 +000014808static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014809
14810 static void
14811max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014812 typval_T *argvars;
14813 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014814 int domax;
14815{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014816 long n = 0;
14817 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014819
14820 if (argvars[0].v_type == VAR_LIST)
14821 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014822 list_T *l;
14823 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014824
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014825 l = argvars[0].vval.v_list;
14826 if (l != NULL)
14827 {
14828 li = l->lv_first;
14829 if (li != NULL)
14830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014832 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014833 {
14834 li = li->li_next;
14835 if (li == NULL)
14836 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014837 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014838 if (domax ? i > n : i < n)
14839 n = i;
14840 }
14841 }
14842 }
14843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014844 else if (argvars[0].v_type == VAR_DICT)
14845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014847 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014849 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014850
14851 d = argvars[0].vval.v_dict;
14852 if (d != NULL)
14853 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014854 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014857 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014858 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014859 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014860 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014861 if (first)
14862 {
14863 n = i;
14864 first = FALSE;
14865 }
14866 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014867 n = i;
14868 }
14869 }
14870 }
14871 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014872 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014873 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014875}
14876
14877/*
14878 * "max()" function
14879 */
14880 static void
14881f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 typval_T *argvars;
14883 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014884{
14885 max_min(argvars, rettv, TRUE);
14886}
14887
14888/*
14889 * "min()" function
14890 */
14891 static void
14892f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 typval_T *argvars;
14894 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014895{
14896 max_min(argvars, rettv, FALSE);
14897}
14898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014899static int mkdir_recurse __ARGS((char_u *dir, int prot));
14900
14901/*
14902 * Create the directory in which "dir" is located, and higher levels when
14903 * needed.
14904 */
14905 static int
14906mkdir_recurse(dir, prot)
14907 char_u *dir;
14908 int prot;
14909{
14910 char_u *p;
14911 char_u *updir;
14912 int r = FAIL;
14913
14914 /* Get end of directory name in "dir".
14915 * We're done when it's "/" or "c:/". */
14916 p = gettail_sep(dir);
14917 if (p <= get_past_head(dir))
14918 return OK;
14919
14920 /* If the directory exists we're done. Otherwise: create it.*/
14921 updir = vim_strnsave(dir, (int)(p - dir));
14922 if (updir == NULL)
14923 return FAIL;
14924 if (mch_isdir(updir))
14925 r = OK;
14926 else if (mkdir_recurse(updir, prot) == OK)
14927 r = vim_mkdir_emsg(updir, prot);
14928 vim_free(updir);
14929 return r;
14930}
14931
14932#ifdef vim_mkdir
14933/*
14934 * "mkdir()" function
14935 */
14936 static void
14937f_mkdir(argvars, rettv)
14938 typval_T *argvars;
14939 typval_T *rettv;
14940{
14941 char_u *dir;
14942 char_u buf[NUMBUFLEN];
14943 int prot = 0755;
14944
14945 rettv->vval.v_number = FAIL;
14946 if (check_restricted() || check_secure())
14947 return;
14948
14949 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014950 if (*dir == NUL)
14951 rettv->vval.v_number = FAIL;
14952 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014953 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014954 if (*gettail(dir) == NUL)
14955 /* remove trailing slashes */
14956 *gettail_sep(dir) = NUL;
14957
14958 if (argvars[1].v_type != VAR_UNKNOWN)
14959 {
14960 if (argvars[2].v_type != VAR_UNKNOWN)
14961 prot = get_tv_number_chk(&argvars[2], NULL);
14962 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14963 mkdir_recurse(dir, prot);
14964 }
14965 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014966 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014967}
14968#endif
14969
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 * "mode()" function
14972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014975 typval_T *argvars;
14976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014978 char_u buf[3];
14979
14980 buf[1] = NUL;
14981 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 if (VIsual_active)
14984 {
14985 if (VIsual_select)
14986 buf[0] = VIsual_mode + 's' - 'v';
14987 else
14988 buf[0] = VIsual_mode;
14989 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014990 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014991 || State == CONFIRM)
14992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014994 if (State == ASKMORE)
14995 buf[1] = 'm';
14996 else if (State == CONFIRM)
14997 buf[1] = '?';
14998 }
14999 else if (State == EXTERNCMD)
15000 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 else if (State & INSERT)
15002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015003#ifdef FEAT_VREPLACE
15004 if (State & VREPLACE_FLAG)
15005 {
15006 buf[0] = 'R';
15007 buf[1] = 'v';
15008 }
15009 else
15010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 if (State & REPLACE_FLAG)
15012 buf[0] = 'R';
15013 else
15014 buf[0] = 'i';
15015 }
15016 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015019 if (exmode_active)
15020 buf[1] = 'v';
15021 }
15022 else if (exmode_active)
15023 {
15024 buf[0] = 'c';
15025 buf[1] = 'e';
15026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015030 if (finish_op)
15031 buf[1] = 'o';
15032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015034 /* Clear out the minor mode when the argument is not a non-zero number or
15035 * non-empty string. */
15036 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015037 buf[1] = NUL;
15038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->vval.v_string = vim_strsave(buf);
15040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041}
15042
Bram Moolenaar429fa852013-04-15 12:27:36 +020015043#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015044/*
15045 * "mzeval()" function
15046 */
15047 static void
15048f_mzeval(argvars, rettv)
15049 typval_T *argvars;
15050 typval_T *rettv;
15051{
15052 char_u *str;
15053 char_u buf[NUMBUFLEN];
15054
15055 str = get_tv_string_buf(&argvars[0], buf);
15056 do_mzeval(str, rettv);
15057}
Bram Moolenaar75676462013-01-30 14:55:42 +010015058
15059 void
15060mzscheme_call_vim(name, args, rettv)
15061 char_u *name;
15062 typval_T *args;
15063 typval_T *rettv;
15064{
15065 typval_T argvars[3];
15066
15067 argvars[0].v_type = VAR_STRING;
15068 argvars[0].vval.v_string = name;
15069 copy_tv(args, &argvars[1]);
15070 argvars[2].v_type = VAR_UNKNOWN;
15071 f_call(argvars, rettv);
15072 clear_tv(&argvars[1]);
15073}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015074#endif
15075
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 * "nextnonblank()" function
15078 */
15079 static void
15080f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 typval_T *argvars;
15082 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083{
15084 linenr_T lnum;
15085
15086 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 {
15090 lnum = 0;
15091 break;
15092 }
15093 if (*skipwhite(ml_get(lnum)) != NUL)
15094 break;
15095 }
15096 rettv->vval.v_number = lnum;
15097}
15098
15099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 * "nr2char()" function
15101 */
15102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015103f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 typval_T *argvars;
15105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106{
15107 char_u buf[NUMBUFLEN];
15108
15109#ifdef FEAT_MBYTE
15110 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015111 {
15112 int utf8 = 0;
15113
15114 if (argvars[1].v_type != VAR_UNKNOWN)
15115 utf8 = get_tv_number_chk(&argvars[1], NULL);
15116 if (utf8)
15117 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15118 else
15119 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 else
15122#endif
15123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 buf[1] = NUL;
15126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->v_type = VAR_STRING;
15128 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015129}
15130
15131/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015132 * "or(expr, expr)" function
15133 */
15134 static void
15135f_or(argvars, rettv)
15136 typval_T *argvars;
15137 typval_T *rettv;
15138{
15139 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15140 | get_tv_number_chk(&argvars[1], NULL);
15141}
15142
15143/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015144 * "pathshorten()" function
15145 */
15146 static void
15147f_pathshorten(argvars, rettv)
15148 typval_T *argvars;
15149 typval_T *rettv;
15150{
15151 char_u *p;
15152
15153 rettv->v_type = VAR_STRING;
15154 p = get_tv_string_chk(&argvars[0]);
15155 if (p == NULL)
15156 rettv->vval.v_string = NULL;
15157 else
15158 {
15159 p = vim_strsave(p);
15160 rettv->vval.v_string = p;
15161 if (p != NULL)
15162 shorten_dir(p);
15163 }
15164}
15165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015166#ifdef FEAT_FLOAT
15167/*
15168 * "pow()" function
15169 */
15170 static void
15171f_pow(argvars, rettv)
15172 typval_T *argvars;
15173 typval_T *rettv;
15174{
15175 float_T fx, fy;
15176
15177 rettv->v_type = VAR_FLOAT;
15178 if (get_float_arg(argvars, &fx) == OK
15179 && get_float_arg(&argvars[1], &fy) == OK)
15180 rettv->vval.v_float = pow(fx, fy);
15181 else
15182 rettv->vval.v_float = 0.0;
15183}
15184#endif
15185
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015187 * "prevnonblank()" function
15188 */
15189 static void
15190f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015191 typval_T *argvars;
15192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193{
15194 linenr_T lnum;
15195
15196 lnum = get_tv_lnum(argvars);
15197 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15198 lnum = 0;
15199 else
15200 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15201 --lnum;
15202 rettv->vval.v_number = lnum;
15203}
15204
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015205#ifdef HAVE_STDARG_H
15206/* This dummy va_list is here because:
15207 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15208 * - locally in the function results in a "used before set" warning
15209 * - using va_start() to initialize it gives "function with fixed args" error */
15210static va_list ap;
15211#endif
15212
Bram Moolenaar8c711452005-01-14 21:53:12 +000015213/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015214 * "printf()" function
15215 */
15216 static void
15217f_printf(argvars, rettv)
15218 typval_T *argvars;
15219 typval_T *rettv;
15220{
15221 rettv->v_type = VAR_STRING;
15222 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015223#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015224 {
15225 char_u buf[NUMBUFLEN];
15226 int len;
15227 char_u *s;
15228 int saved_did_emsg = did_emsg;
15229 char *fmt;
15230
15231 /* Get the required length, allocate the buffer and do it for real. */
15232 did_emsg = FALSE;
15233 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015234 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015235 if (!did_emsg)
15236 {
15237 s = alloc(len + 1);
15238 if (s != NULL)
15239 {
15240 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015241 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015242 }
15243 }
15244 did_emsg |= saved_did_emsg;
15245 }
15246#endif
15247}
15248
15249/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015250 * "pumvisible()" function
15251 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015252 static void
15253f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015254 typval_T *argvars UNUSED;
15255 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257#ifdef FEAT_INS_EXPAND
15258 if (pum_visible())
15259 rettv->vval.v_number = 1;
15260#endif
15261}
15262
Bram Moolenaardb913952012-06-29 12:54:53 +020015263#ifdef FEAT_PYTHON3
15264/*
15265 * "py3eval()" function
15266 */
15267 static void
15268f_py3eval(argvars, rettv)
15269 typval_T *argvars;
15270 typval_T *rettv;
15271{
15272 char_u *str;
15273 char_u buf[NUMBUFLEN];
15274
15275 str = get_tv_string_buf(&argvars[0], buf);
15276 do_py3eval(str, rettv);
15277}
15278#endif
15279
15280#ifdef FEAT_PYTHON
15281/*
15282 * "pyeval()" function
15283 */
15284 static void
15285f_pyeval(argvars, rettv)
15286 typval_T *argvars;
15287 typval_T *rettv;
15288{
15289 char_u *str;
15290 char_u buf[NUMBUFLEN];
15291
15292 str = get_tv_string_buf(&argvars[0], buf);
15293 do_pyeval(str, rettv);
15294}
15295#endif
15296
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015297/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015298 * "range()" function
15299 */
15300 static void
15301f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015302 typval_T *argvars;
15303 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304{
15305 long start;
15306 long end;
15307 long stride = 1;
15308 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015312 if (argvars[1].v_type == VAR_UNKNOWN)
15313 {
15314 end = start - 1;
15315 start = 0;
15316 }
15317 else
15318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015322 }
15323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 if (error)
15325 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015327 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015328 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015329 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 else
15331 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015334 if (list_append_number(rettv->vval.v_list,
15335 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015336 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015337 }
15338}
15339
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015340/*
15341 * "readfile()" function
15342 */
15343 static void
15344f_readfile(argvars, rettv)
15345 typval_T *argvars;
15346 typval_T *rettv;
15347{
15348 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015349 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 char_u *fname;
15351 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015352 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15353 int io_size = sizeof(buf);
15354 int readlen; /* size of last fread() */
15355 char_u *prev = NULL; /* previously read bytes, if any */
15356 long prevlen = 0; /* length of data in prev */
15357 long prevsize = 0; /* size of prev buffer */
15358 long maxline = MAXLNUM;
15359 long cnt = 0;
15360 char_u *p; /* position in buf */
15361 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015363 if (argvars[1].v_type != VAR_UNKNOWN)
15364 {
15365 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15366 binary = TRUE;
15367 if (argvars[2].v_type != VAR_UNKNOWN)
15368 maxline = get_tv_number(&argvars[2]);
15369 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015371 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373
15374 /* Always open the file in binary mode, library functions have a mind of
15375 * their own about CR-LF conversion. */
15376 fname = get_tv_string(&argvars[0]);
15377 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15378 {
15379 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15380 return;
15381 }
15382
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015383 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015384 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015385 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015386
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015387 /* This for loop processes what was read, but is also entered at end
15388 * of file so that either:
15389 * - an incomplete line gets written
15390 * - a "binary" file gets an empty line at the end if it ends in a
15391 * newline. */
15392 for (p = buf, start = buf;
15393 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15394 ++p)
15395 {
15396 if (*p == '\n' || readlen <= 0)
15397 {
15398 listitem_T *li;
15399 char_u *s = NULL;
15400 long_u len = p - start;
15401
15402 /* Finished a line. Remove CRs before NL. */
15403 if (readlen > 0 && !binary)
15404 {
15405 while (len > 0 && start[len - 1] == '\r')
15406 --len;
15407 /* removal may cross back to the "prev" string */
15408 if (len == 0)
15409 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15410 --prevlen;
15411 }
15412 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015413 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 else
15415 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015416 /* Change "prev" buffer to be the right size. This way
15417 * the bytes are only copied once, and very long lines are
15418 * allocated only once. */
15419 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015420 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015421 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015422 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015423 prev = NULL; /* the list will own the string */
15424 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425 }
15426 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015427 if (s == NULL)
15428 {
15429 do_outofmem_msg((long_u) prevlen + len + 1);
15430 failed = TRUE;
15431 break;
15432 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015434 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015435 {
15436 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015437 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015438 break;
15439 }
15440 li->li_tv.v_type = VAR_STRING;
15441 li->li_tv.v_lock = 0;
15442 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015443 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015444
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015445 start = p + 1; /* step over newline */
15446 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015447 break;
15448 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015449 else if (*p == NUL)
15450 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015451#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015452 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15453 * when finding the BF and check the previous two bytes. */
15454 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015455 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15457 * + 1, these may be in the "prev" string. */
15458 char_u back1 = p >= buf + 1 ? p[-1]
15459 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15460 char_u back2 = p >= buf + 2 ? p[-2]
15461 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15462 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015463
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015464 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015465 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015466 char_u *dest = p - 2;
15467
15468 /* Usually a BOM is at the beginning of a file, and so at
15469 * the beginning of a line; then we can just step over it.
15470 */
15471 if (start == dest)
15472 start = p + 1;
15473 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015474 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015475 /* have to shuffle buf to close gap */
15476 int adjust_prevlen = 0;
15477
15478 if (dest < buf)
15479 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015480 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015481 dest = buf;
15482 }
15483 if (readlen > p - buf + 1)
15484 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15485 readlen -= 3 - adjust_prevlen;
15486 prevlen -= adjust_prevlen;
15487 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015488 }
15489 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015490 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015491#endif
15492 } /* for */
15493
15494 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15495 break;
15496 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015497 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015498 /* There's part of a line in buf, store it in "prev". */
15499 if (p - start + prevlen >= prevsize)
15500 {
15501 /* need bigger "prev" buffer */
15502 char_u *newprev;
15503
15504 /* A common use case is ordinary text files and "prev" gets a
15505 * fragment of a line, so the first allocation is made
15506 * small, to avoid repeatedly 'allocing' large and
15507 * 'reallocing' small. */
15508 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015509 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015510 else
15511 {
15512 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015513 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 prevsize = grow50pc > growmin ? grow50pc : growmin;
15515 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015516 newprev = prev == NULL ? alloc(prevsize)
15517 : vim_realloc(prev, prevsize);
15518 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015519 {
15520 do_outofmem_msg((long_u)prevsize);
15521 failed = TRUE;
15522 break;
15523 }
15524 prev = newprev;
15525 }
15526 /* Add the line part to end of "prev". */
15527 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015528 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015530 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015531
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015532 /*
15533 * For a negative line count use only the lines at the end of the file,
15534 * free the rest.
15535 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015536 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015537 while (cnt > -maxline)
15538 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015539 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015540 --cnt;
15541 }
15542
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015543 if (failed)
15544 {
15545 list_free(rettv->vval.v_list, TRUE);
15546 /* readfile doc says an empty list is returned on error */
15547 rettv->vval.v_list = list_alloc();
15548 }
15549
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015550 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551 fclose(fd);
15552}
15553
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015554#if defined(FEAT_RELTIME)
15555static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15556
15557/*
15558 * Convert a List to proftime_T.
15559 * Return FAIL when there is something wrong.
15560 */
15561 static int
15562list2proftime(arg, tm)
15563 typval_T *arg;
15564 proftime_T *tm;
15565{
15566 long n1, n2;
15567 int error = FALSE;
15568
15569 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15570 || arg->vval.v_list->lv_len != 2)
15571 return FAIL;
15572 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15573 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15574# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015575 tm->HighPart = n1;
15576 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015577# else
15578 tm->tv_sec = n1;
15579 tm->tv_usec = n2;
15580# endif
15581 return error ? FAIL : OK;
15582}
15583#endif /* FEAT_RELTIME */
15584
15585/*
15586 * "reltime()" function
15587 */
15588 static void
15589f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015590 typval_T *argvars UNUSED;
15591 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015592{
15593#ifdef FEAT_RELTIME
15594 proftime_T res;
15595 proftime_T start;
15596
15597 if (argvars[0].v_type == VAR_UNKNOWN)
15598 {
15599 /* No arguments: get current time. */
15600 profile_start(&res);
15601 }
15602 else if (argvars[1].v_type == VAR_UNKNOWN)
15603 {
15604 if (list2proftime(&argvars[0], &res) == FAIL)
15605 return;
15606 profile_end(&res);
15607 }
15608 else
15609 {
15610 /* Two arguments: compute the difference. */
15611 if (list2proftime(&argvars[0], &start) == FAIL
15612 || list2proftime(&argvars[1], &res) == FAIL)
15613 return;
15614 profile_sub(&res, &start);
15615 }
15616
15617 if (rettv_list_alloc(rettv) == OK)
15618 {
15619 long n1, n2;
15620
15621# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015622 n1 = res.HighPart;
15623 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015624# else
15625 n1 = res.tv_sec;
15626 n2 = res.tv_usec;
15627# endif
15628 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15629 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15630 }
15631#endif
15632}
15633
15634/*
15635 * "reltimestr()" function
15636 */
15637 static void
15638f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015639 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015640 typval_T *rettv;
15641{
15642#ifdef FEAT_RELTIME
15643 proftime_T tm;
15644#endif
15645
15646 rettv->v_type = VAR_STRING;
15647 rettv->vval.v_string = NULL;
15648#ifdef FEAT_RELTIME
15649 if (list2proftime(&argvars[0], &tm) == OK)
15650 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15651#endif
15652}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015653
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15655static void make_connection __ARGS((void));
15656static int check_connection __ARGS((void));
15657
15658 static void
15659make_connection()
15660{
15661 if (X_DISPLAY == NULL
15662# ifdef FEAT_GUI
15663 && !gui.in_use
15664# endif
15665 )
15666 {
15667 x_force_connect = TRUE;
15668 setup_term_clip();
15669 x_force_connect = FALSE;
15670 }
15671}
15672
15673 static int
15674check_connection()
15675{
15676 make_connection();
15677 if (X_DISPLAY == NULL)
15678 {
15679 EMSG(_("E240: No connection to Vim server"));
15680 return FAIL;
15681 }
15682 return OK;
15683}
15684#endif
15685
15686#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015687static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688
15689 static void
15690remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *argvars;
15692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693 int expr;
15694{
15695 char_u *server_name;
15696 char_u *keys;
15697 char_u *r = NULL;
15698 char_u buf[NUMBUFLEN];
15699# ifdef WIN32
15700 HWND w;
15701# else
15702 Window w;
15703# endif
15704
15705 if (check_restricted() || check_secure())
15706 return;
15707
15708# ifdef FEAT_X11
15709 if (check_connection() == FAIL)
15710 return;
15711# endif
15712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 server_name = get_tv_string_chk(&argvars[0]);
15714 if (server_name == NULL)
15715 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 keys = get_tv_string_buf(&argvars[1], buf);
15717# ifdef WIN32
15718 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15719# else
15720 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15721 < 0)
15722# endif
15723 {
15724 if (r != NULL)
15725 EMSG(r); /* sending worked but evaluation failed */
15726 else
15727 EMSG2(_("E241: Unable to send to %s"), server_name);
15728 return;
15729 }
15730
15731 rettv->vval.v_string = r;
15732
15733 if (argvars[2].v_type != VAR_UNKNOWN)
15734 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015735 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015736 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015737 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015739 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015740 v.di_tv.v_type = VAR_STRING;
15741 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 idvar = get_tv_string_chk(&argvars[2]);
15743 if (idvar != NULL)
15744 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015745 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 }
15747}
15748#endif
15749
15750/*
15751 * "remote_expr()" function
15752 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 static void
15754f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757{
15758 rettv->v_type = VAR_STRING;
15759 rettv->vval.v_string = NULL;
15760#ifdef FEAT_CLIENTSERVER
15761 remote_common(argvars, rettv, TRUE);
15762#endif
15763}
15764
15765/*
15766 * "remote_foreground()" function
15767 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 static void
15769f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015770 typval_T *argvars UNUSED;
15771 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773#ifdef FEAT_CLIENTSERVER
15774# ifdef WIN32
15775 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015776 {
15777 char_u *server_name = get_tv_string_chk(&argvars[0]);
15778
15779 if (server_name != NULL)
15780 serverForeground(server_name);
15781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782# else
15783 /* Send a foreground() expression to the server. */
15784 argvars[1].v_type = VAR_STRING;
15785 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15786 argvars[2].v_type = VAR_UNKNOWN;
15787 remote_common(argvars, rettv, TRUE);
15788 vim_free(argvars[1].vval.v_string);
15789# endif
15790#endif
15791}
15792
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793 static void
15794f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015796 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797{
15798#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015799 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800 char_u *s = NULL;
15801# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015802 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015803# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805
15806 if (check_restricted() || check_secure())
15807 {
15808 rettv->vval.v_number = -1;
15809 return;
15810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 serverid = get_tv_string_chk(&argvars[0]);
15812 if (serverid == NULL)
15813 {
15814 rettv->vval.v_number = -1;
15815 return; /* type error; errmsg already given */
15816 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015818 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819 if (n == 0)
15820 rettv->vval.v_number = -1;
15821 else
15822 {
15823 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15824 rettv->vval.v_number = (s != NULL);
15825 }
15826# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 if (check_connection() == FAIL)
15828 return;
15829
15830 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015831 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832# endif
15833
15834 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015836 char_u *retvar;
15837
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 v.di_tv.v_type = VAR_STRING;
15839 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015840 retvar = get_tv_string_chk(&argvars[1]);
15841 if (retvar != NULL)
15842 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 }
15845#else
15846 rettv->vval.v_number = -1;
15847#endif
15848}
15849
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850 static void
15851f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854{
15855 char_u *r = NULL;
15856
15857#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 char_u *serverid = get_tv_string_chk(&argvars[0]);
15859
15860 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 {
15862# ifdef WIN32
15863 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015864 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015865
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015866 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867 if (n != 0)
15868 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15869 if (r == NULL)
15870# else
15871 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015873# endif
15874 EMSG(_("E277: Unable to read a server reply"));
15875 }
15876#endif
15877 rettv->v_type = VAR_STRING;
15878 rettv->vval.v_string = r;
15879}
15880
15881/*
15882 * "remote_send()" function
15883 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015884 static void
15885f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015886 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888{
15889 rettv->v_type = VAR_STRING;
15890 rettv->vval.v_string = NULL;
15891#ifdef FEAT_CLIENTSERVER
15892 remote_common(argvars, rettv, FALSE);
15893#endif
15894}
15895
15896/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015897 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015898 */
15899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015900f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 typval_T *argvars;
15902 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015903{
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 list_T *l;
15905 listitem_T *item, *item2;
15906 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015908 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015909 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 dict_T *d;
15911 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015912 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913
Bram Moolenaar8c711452005-01-14 21:53:12 +000015914 if (argvars[0].v_type == VAR_DICT)
15915 {
15916 if (argvars[2].v_type != VAR_UNKNOWN)
15917 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015918 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015919 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015921 key = get_tv_string_chk(&argvars[1]);
15922 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 di = dict_find(d, key, -1);
15925 if (di == NULL)
15926 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015927 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15928 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 {
15930 *rettv = di->di_tv;
15931 init_tv(&di->di_tv);
15932 dictitem_remove(d, di);
15933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015935 }
15936 }
15937 else if (argvars[0].v_type != VAR_LIST)
15938 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015939 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015940 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015942 int error = FALSE;
15943
15944 idx = get_tv_number_chk(&argvars[1], &error);
15945 if (error)
15946 ; /* type error: do nothing, errmsg already given */
15947 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015948 EMSGN(_(e_listidx), idx);
15949 else
15950 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015951 if (argvars[2].v_type == VAR_UNKNOWN)
15952 {
15953 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015954 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955 *rettv = item->li_tv;
15956 vim_free(item);
15957 }
15958 else
15959 {
15960 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015961 end = get_tv_number_chk(&argvars[2], &error);
15962 if (error)
15963 ; /* type error: do nothing */
15964 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015965 EMSGN(_(e_listidx), end);
15966 else
15967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015968 int cnt = 0;
15969
15970 for (li = item; li != NULL; li = li->li_next)
15971 {
15972 ++cnt;
15973 if (li == item2)
15974 break;
15975 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015976 if (li == NULL) /* didn't find "item2" after "item" */
15977 EMSG(_(e_invrange));
15978 else
15979 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015980 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015981 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015982 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015983 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015984 l->lv_first = item;
15985 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015986 item->li_prev = NULL;
15987 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015988 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015989 }
15990 }
15991 }
15992 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015993 }
15994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995}
15996
15997/*
15998 * "rename({from}, {to})" function
15999 */
16000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 typval_T *argvars;
16003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004{
16005 char_u buf[NUMBUFLEN];
16006
16007 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16011 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012}
16013
16014/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016015 * "repeat()" function
16016 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016017 static void
16018f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 typval_T *argvars;
16020 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016021{
16022 char_u *p;
16023 int n;
16024 int slen;
16025 int len;
16026 char_u *r;
16027 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016028
16029 n = get_tv_number(&argvars[1]);
16030 if (argvars[0].v_type == VAR_LIST)
16031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016032 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016033 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016034 if (list_extend(rettv->vval.v_list,
16035 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016036 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016037 }
16038 else
16039 {
16040 p = get_tv_string(&argvars[0]);
16041 rettv->v_type = VAR_STRING;
16042 rettv->vval.v_string = NULL;
16043
16044 slen = (int)STRLEN(p);
16045 len = slen * n;
16046 if (len <= 0)
16047 return;
16048
16049 r = alloc(len + 1);
16050 if (r != NULL)
16051 {
16052 for (i = 0; i < n; i++)
16053 mch_memmove(r + i * slen, p, (size_t)slen);
16054 r[len] = NUL;
16055 }
16056
16057 rettv->vval.v_string = r;
16058 }
16059}
16060
16061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 * "resolve()" function
16063 */
16064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 typval_T *argvars;
16067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068{
16069 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016070#ifdef HAVE_READLINK
16071 char_u *buf = NULL;
16072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075#ifdef FEAT_SHORTCUT
16076 {
16077 char_u *v = NULL;
16078
16079 v = mch_resolve_shortcut(p);
16080 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 }
16085#else
16086# ifdef HAVE_READLINK
16087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 char_u *cpy;
16089 int len;
16090 char_u *remain = NULL;
16091 char_u *q;
16092 int is_relative_to_current = FALSE;
16093 int has_trailing_pathsep = FALSE;
16094 int limit = 100;
16095
16096 p = vim_strsave(p);
16097
16098 if (p[0] == '.' && (vim_ispathsep(p[1])
16099 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16100 is_relative_to_current = TRUE;
16101
16102 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016103 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016106 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108
16109 q = getnextcomp(p);
16110 if (*q != NUL)
16111 {
16112 /* Separate the first path component in "p", and keep the
16113 * remainder (beginning with the path separator). */
16114 remain = vim_strsave(q - 1);
16115 q[-1] = NUL;
16116 }
16117
Bram Moolenaard9462e32011-04-11 21:35:11 +020016118 buf = alloc(MAXPATHL + 1);
16119 if (buf == NULL)
16120 goto fail;
16121
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 for (;;)
16123 {
16124 for (;;)
16125 {
16126 len = readlink((char *)p, (char *)buf, MAXPATHL);
16127 if (len <= 0)
16128 break;
16129 buf[len] = NUL;
16130
16131 if (limit-- == 0)
16132 {
16133 vim_free(p);
16134 vim_free(remain);
16135 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 goto fail;
16138 }
16139
16140 /* Ensure that the result will have a trailing path separator
16141 * if the argument has one. */
16142 if (remain == NULL && has_trailing_pathsep)
16143 add_pathsep(buf);
16144
16145 /* Separate the first path component in the link value and
16146 * concatenate the remainders. */
16147 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16148 if (*q != NUL)
16149 {
16150 if (remain == NULL)
16151 remain = vim_strsave(q - 1);
16152 else
16153 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016154 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 if (cpy != NULL)
16156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 vim_free(remain);
16158 remain = cpy;
16159 }
16160 }
16161 q[-1] = NUL;
16162 }
16163
16164 q = gettail(p);
16165 if (q > p && *q == NUL)
16166 {
16167 /* Ignore trailing path separator. */
16168 q[-1] = NUL;
16169 q = gettail(p);
16170 }
16171 if (q > p && !mch_isFullName(buf))
16172 {
16173 /* symlink is relative to directory of argument */
16174 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16175 if (cpy != NULL)
16176 {
16177 STRCPY(cpy, p);
16178 STRCPY(gettail(cpy), buf);
16179 vim_free(p);
16180 p = cpy;
16181 }
16182 }
16183 else
16184 {
16185 vim_free(p);
16186 p = vim_strsave(buf);
16187 }
16188 }
16189
16190 if (remain == NULL)
16191 break;
16192
16193 /* Append the first path component of "remain" to "p". */
16194 q = getnextcomp(remain + 1);
16195 len = q - remain - (*q != NUL);
16196 cpy = vim_strnsave(p, STRLEN(p) + len);
16197 if (cpy != NULL)
16198 {
16199 STRNCAT(cpy, remain, len);
16200 vim_free(p);
16201 p = cpy;
16202 }
16203 /* Shorten "remain". */
16204 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016205 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 else
16207 {
16208 vim_free(remain);
16209 remain = NULL;
16210 }
16211 }
16212
16213 /* If the result is a relative path name, make it explicitly relative to
16214 * the current directory if and only if the argument had this form. */
16215 if (!vim_ispathsep(*p))
16216 {
16217 if (is_relative_to_current
16218 && *p != NUL
16219 && !(p[0] == '.'
16220 && (p[1] == NUL
16221 || vim_ispathsep(p[1])
16222 || (p[1] == '.'
16223 && (p[2] == NUL
16224 || vim_ispathsep(p[2]))))))
16225 {
16226 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016227 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 if (cpy != NULL)
16229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 vim_free(p);
16231 p = cpy;
16232 }
16233 }
16234 else if (!is_relative_to_current)
16235 {
16236 /* Strip leading "./". */
16237 q = p;
16238 while (q[0] == '.' && vim_ispathsep(q[1]))
16239 q += 2;
16240 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016241 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 }
16243 }
16244
16245 /* Ensure that the result will have no trailing path separator
16246 * if the argument had none. But keep "/" or "//". */
16247 if (!has_trailing_pathsep)
16248 {
16249 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016250 if (after_pathsep(p, q))
16251 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 }
16253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 }
16256# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258# endif
16259#endif
16260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262
16263#ifdef HAVE_READLINK
16264fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016265 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268}
16269
16270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 */
16273 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 typval_T *argvars;
16276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277{
Bram Moolenaar33570922005-01-25 22:26:29 +000016278 list_T *l;
16279 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
Bram Moolenaar0d660222005-01-07 21:51:51 +000016281 if (argvars[0].v_type != VAR_LIST)
16282 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016283 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016284 && !tv_check_lock(l->lv_lock,
16285 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 {
16287 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016288 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016289 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 while (li != NULL)
16291 {
16292 ni = li->li_prev;
16293 list_append(l, li);
16294 li = ni;
16295 }
16296 rettv->vval.v_list = l;
16297 rettv->v_type = VAR_LIST;
16298 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016299 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301}
16302
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016303#define SP_NOMOVE 0x01 /* don't move cursor */
16304#define SP_REPEAT 0x02 /* repeat to find outer pair */
16305#define SP_RETCOUNT 0x04 /* return matchcount */
16306#define SP_SETPCMARK 0x08 /* set previous context mark */
16307#define SP_START 0x10 /* accept match at start position */
16308#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16309#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016310
Bram Moolenaar33570922005-01-25 22:26:29 +000016311static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312
16313/*
16314 * Get flags for a search function.
16315 * Possibly sets "p_ws".
16316 * Returns BACKWARD, FORWARD or zero (for an error).
16317 */
16318 static int
16319get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016320 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 int *flagsp;
16322{
16323 int dir = FORWARD;
16324 char_u *flags;
16325 char_u nbuf[NUMBUFLEN];
16326 int mask;
16327
16328 if (varp->v_type != VAR_UNKNOWN)
16329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016330 flags = get_tv_string_buf_chk(varp, nbuf);
16331 if (flags == NULL)
16332 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333 while (*flags != NUL)
16334 {
16335 switch (*flags)
16336 {
16337 case 'b': dir = BACKWARD; break;
16338 case 'w': p_ws = TRUE; break;
16339 case 'W': p_ws = FALSE; break;
16340 default: mask = 0;
16341 if (flagsp != NULL)
16342 switch (*flags)
16343 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016344 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016345 case 'e': mask = SP_END; break;
16346 case 'm': mask = SP_RETCOUNT; break;
16347 case 'n': mask = SP_NOMOVE; break;
16348 case 'p': mask = SP_SUBPAT; break;
16349 case 'r': mask = SP_REPEAT; break;
16350 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 }
16352 if (mask == 0)
16353 {
16354 EMSG2(_(e_invarg2), flags);
16355 dir = 0;
16356 }
16357 else
16358 *flagsp |= mask;
16359 }
16360 if (dir == 0)
16361 break;
16362 ++flags;
16363 }
16364 }
16365 return dir;
16366}
16367
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016369 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016371 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016372search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016373 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016374 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016375 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016377 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 char_u *pat;
16379 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016380 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 int save_p_ws = p_ws;
16382 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016384 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016385 proftime_T tm;
16386#ifdef FEAT_RELTIME
16387 long time_limit = 0;
16388#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016389 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016390 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016392 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016393 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016394 if (dir == 0)
16395 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016396 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016397 if (flags & SP_START)
16398 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016399 if (flags & SP_END)
16400 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016401
Bram Moolenaar76929292008-01-06 19:07:36 +000016402 /* Optional arguments: line number to stop searching and timeout. */
16403 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016404 {
16405 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16406 if (lnum_stop < 0)
16407 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016408#ifdef FEAT_RELTIME
16409 if (argvars[3].v_type != VAR_UNKNOWN)
16410 {
16411 time_limit = get_tv_number_chk(&argvars[3], NULL);
16412 if (time_limit < 0)
16413 goto theend;
16414 }
16415#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016416 }
16417
Bram Moolenaar76929292008-01-06 19:07:36 +000016418#ifdef FEAT_RELTIME
16419 /* Set the time limit, if there is one. */
16420 profile_setlimit(time_limit, &tm);
16421#endif
16422
Bram Moolenaar231334e2005-07-25 20:46:57 +000016423 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016424 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016425 * Check to make sure only those flags are set.
16426 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16427 * flags cannot be set. Check for that condition also.
16428 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016429 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016430 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016432 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016433 goto theend;
16434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016436 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016437 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016438 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016439 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016441 if (flags & SP_SUBPAT)
16442 retval = subpatnum;
16443 else
16444 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016445 if (flags & SP_SETPCMARK)
16446 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016448 if (match_pos != NULL)
16449 {
16450 /* Store the match cursor position */
16451 match_pos->lnum = pos.lnum;
16452 match_pos->col = pos.col + 1;
16453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 /* "/$" will put the cursor after the end of the line, may need to
16455 * correct that here */
16456 check_cursor();
16457 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016458
16459 /* If 'n' flag is used: restore cursor position. */
16460 if (flags & SP_NOMOVE)
16461 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016462 else
16463 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016464theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016466
16467 return retval;
16468}
16469
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016470#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016471
16472/*
16473 * round() is not in C90, use ceil() or floor() instead.
16474 */
16475 float_T
16476vim_round(f)
16477 float_T f;
16478{
16479 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16480}
16481
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016482/*
16483 * "round({float})" function
16484 */
16485 static void
16486f_round(argvars, rettv)
16487 typval_T *argvars;
16488 typval_T *rettv;
16489{
16490 float_T f;
16491
16492 rettv->v_type = VAR_FLOAT;
16493 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016494 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016495 else
16496 rettv->vval.v_float = 0.0;
16497}
16498#endif
16499
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016500/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016501 * "screenattr()" function
16502 */
16503 static void
16504f_screenattr(argvars, rettv)
16505 typval_T *argvars UNUSED;
16506 typval_T *rettv;
16507{
16508 int row;
16509 int col;
16510 int c;
16511
16512 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16513 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16514 if (row < 0 || row >= screen_Rows
16515 || col < 0 || col >= screen_Columns)
16516 c = -1;
16517 else
16518 c = ScreenAttrs[LineOffset[row] + col];
16519 rettv->vval.v_number = c;
16520}
16521
16522/*
16523 * "screenchar()" function
16524 */
16525 static void
16526f_screenchar(argvars, rettv)
16527 typval_T *argvars UNUSED;
16528 typval_T *rettv;
16529{
16530 int row;
16531 int col;
16532 int off;
16533 int c;
16534
16535 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16536 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16537 if (row < 0 || row >= screen_Rows
16538 || col < 0 || col >= screen_Columns)
16539 c = -1;
16540 else
16541 {
16542 off = LineOffset[row] + col;
16543#ifdef FEAT_MBYTE
16544 if (enc_utf8 && ScreenLinesUC[off] != 0)
16545 c = ScreenLinesUC[off];
16546 else
16547#endif
16548 c = ScreenLines[off];
16549 }
16550 rettv->vval.v_number = c;
16551}
16552
16553/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016554 * "screencol()" function
16555 *
16556 * First column is 1 to be consistent with virtcol().
16557 */
16558 static void
16559f_screencol(argvars, rettv)
16560 typval_T *argvars UNUSED;
16561 typval_T *rettv;
16562{
16563 rettv->vval.v_number = screen_screencol() + 1;
16564}
16565
16566/*
16567 * "screenrow()" function
16568 */
16569 static void
16570f_screenrow(argvars, rettv)
16571 typval_T *argvars UNUSED;
16572 typval_T *rettv;
16573{
16574 rettv->vval.v_number = screen_screenrow() + 1;
16575}
16576
16577/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578 * "search()" function
16579 */
16580 static void
16581f_search(argvars, rettv)
16582 typval_T *argvars;
16583 typval_T *rettv;
16584{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016585 int flags = 0;
16586
16587 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588}
16589
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016591 * "searchdecl()" function
16592 */
16593 static void
16594f_searchdecl(argvars, rettv)
16595 typval_T *argvars;
16596 typval_T *rettv;
16597{
16598 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016599 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016600 int error = FALSE;
16601 char_u *name;
16602
16603 rettv->vval.v_number = 1; /* default: FAIL */
16604
16605 name = get_tv_string_chk(&argvars[0]);
16606 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016607 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016608 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016609 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16610 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16611 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016612 if (!error && name != NULL)
16613 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016614 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016615}
16616
16617/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016618 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016620 static int
16621searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016622 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016623 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624{
16625 char_u *spat, *mpat, *epat;
16626 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 int dir;
16629 int flags = 0;
16630 char_u nbuf1[NUMBUFLEN];
16631 char_u nbuf2[NUMBUFLEN];
16632 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016633 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016634 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016635 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016638 spat = get_tv_string_chk(&argvars[0]);
16639 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16640 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16641 if (spat == NULL || mpat == NULL || epat == NULL)
16642 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 /* Handle the optional fourth argument: flags */
16645 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016646 if (dir == 0)
16647 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016648
16649 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016650 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16651 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016652 if ((flags & (SP_END | SP_SUBPAT)) != 0
16653 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016654 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016655 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016656 goto theend;
16657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016659 /* Using 'r' implies 'W', otherwise it doesn't work. */
16660 if (flags & SP_REPEAT)
16661 p_ws = FALSE;
16662
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016664 if (argvars[3].v_type == VAR_UNKNOWN
16665 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 skip = (char_u *)"";
16667 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016669 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016670 if (argvars[5].v_type != VAR_UNKNOWN)
16671 {
16672 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16673 if (lnum_stop < 0)
16674 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016675#ifdef FEAT_RELTIME
16676 if (argvars[6].v_type != VAR_UNKNOWN)
16677 {
16678 time_limit = get_tv_number_chk(&argvars[6], NULL);
16679 if (time_limit < 0)
16680 goto theend;
16681 }
16682#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016683 }
16684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016685 if (skip == NULL)
16686 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016688 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016689 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016690
16691theend:
16692 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016693
16694 return retval;
16695}
16696
16697/*
16698 * "searchpair()" function
16699 */
16700 static void
16701f_searchpair(argvars, rettv)
16702 typval_T *argvars;
16703 typval_T *rettv;
16704{
16705 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16706}
16707
16708/*
16709 * "searchpairpos()" function
16710 */
16711 static void
16712f_searchpairpos(argvars, rettv)
16713 typval_T *argvars;
16714 typval_T *rettv;
16715{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016716 pos_T match_pos;
16717 int lnum = 0;
16718 int col = 0;
16719
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016720 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016721 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016722
16723 if (searchpair_cmn(argvars, &match_pos) > 0)
16724 {
16725 lnum = match_pos.lnum;
16726 col = match_pos.col;
16727 }
16728
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016729 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16730 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016731}
16732
16733/*
16734 * Search for a start/middle/end thing.
16735 * Used by searchpair(), see its documentation for the details.
16736 * Returns 0 or -1 for no match,
16737 */
16738 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016739do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16740 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016741 char_u *spat; /* start pattern */
16742 char_u *mpat; /* middle pattern */
16743 char_u *epat; /* end pattern */
16744 int dir; /* BACKWARD or FORWARD */
16745 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016746 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016747 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016748 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016749 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016750{
16751 char_u *save_cpo;
16752 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16753 long retval = 0;
16754 pos_T pos;
16755 pos_T firstpos;
16756 pos_T foundpos;
16757 pos_T save_cursor;
16758 pos_T save_pos;
16759 int n;
16760 int r;
16761 int nest = 1;
16762 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016763 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016764 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016765
16766 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16767 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016768 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016769
Bram Moolenaar76929292008-01-06 19:07:36 +000016770#ifdef FEAT_RELTIME
16771 /* Set the time limit, if there is one. */
16772 profile_setlimit(time_limit, &tm);
16773#endif
16774
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016775 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16776 * start/middle/end (pat3, for the top pair). */
16777 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16778 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16779 if (pat2 == NULL || pat3 == NULL)
16780 goto theend;
16781 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16782 if (*mpat == NUL)
16783 STRCPY(pat3, pat2);
16784 else
16785 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16786 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016787 if (flags & SP_START)
16788 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016789
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 save_cursor = curwin->w_cursor;
16791 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016792 clearpos(&firstpos);
16793 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 pat = pat3;
16795 for (;;)
16796 {
16797 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016798 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16800 /* didn't find it or found the first match again: FAIL */
16801 break;
16802
16803 if (firstpos.lnum == 0)
16804 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016805 if (equalpos(pos, foundpos))
16806 {
16807 /* Found the same position again. Can happen with a pattern that
16808 * has "\zs" at the end and searching backwards. Advance one
16809 * character and try again. */
16810 if (dir == BACKWARD)
16811 decl(&pos);
16812 else
16813 incl(&pos);
16814 }
16815 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016817 /* clear the start flag to avoid getting stuck here */
16818 options &= ~SEARCH_START;
16819
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 /* If the skip pattern matches, ignore this match. */
16821 if (*skip != NUL)
16822 {
16823 save_pos = curwin->w_cursor;
16824 curwin->w_cursor = pos;
16825 r = eval_to_bool(skip, &err, NULL, FALSE);
16826 curwin->w_cursor = save_pos;
16827 if (err)
16828 {
16829 /* Evaluating {skip} caused an error, break here. */
16830 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016831 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 break;
16833 }
16834 if (r)
16835 continue;
16836 }
16837
16838 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16839 {
16840 /* Found end when searching backwards or start when searching
16841 * forward: nested pair. */
16842 ++nest;
16843 pat = pat2; /* nested, don't search for middle */
16844 }
16845 else
16846 {
16847 /* Found end when searching forward or start when searching
16848 * backward: end of (nested) pair; or found middle in outer pair. */
16849 if (--nest == 1)
16850 pat = pat3; /* outer level, search for middle */
16851 }
16852
16853 if (nest == 0)
16854 {
16855 /* Found the match: return matchcount or line number. */
16856 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016857 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016859 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016860 if (flags & SP_SETPCMARK)
16861 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 curwin->w_cursor = pos;
16863 if (!(flags & SP_REPEAT))
16864 break;
16865 nest = 1; /* search for next unmatched */
16866 }
16867 }
16868
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016869 if (match_pos != NULL)
16870 {
16871 /* Store the match cursor position */
16872 match_pos->lnum = curwin->w_cursor.lnum;
16873 match_pos->col = curwin->w_cursor.col + 1;
16874 }
16875
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016877 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 curwin->w_cursor = save_cursor;
16879
16880theend:
16881 vim_free(pat2);
16882 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016883 if (p_cpo == empty_option)
16884 p_cpo = save_cpo;
16885 else
16886 /* Darn, evaluating the {skip} expression changed the value. */
16887 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016888
16889 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890}
16891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016892/*
16893 * "searchpos()" function
16894 */
16895 static void
16896f_searchpos(argvars, rettv)
16897 typval_T *argvars;
16898 typval_T *rettv;
16899{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016900 pos_T match_pos;
16901 int lnum = 0;
16902 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016903 int n;
16904 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016905
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016906 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016907 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016908
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016909 n = search_cmn(argvars, &match_pos, &flags);
16910 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016911 {
16912 lnum = match_pos.lnum;
16913 col = match_pos.col;
16914 }
16915
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016916 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16917 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016918 if (flags & SP_SUBPAT)
16919 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016920}
16921
16922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 static void
16924f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016925 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928#ifdef FEAT_CLIENTSERVER
16929 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 char_u *server = get_tv_string_chk(&argvars[0]);
16931 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 if (server == NULL || reply == NULL)
16935 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 if (check_restricted() || check_secure())
16937 return;
16938# ifdef FEAT_X11
16939 if (check_connection() == FAIL)
16940 return;
16941# endif
16942
16943 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 EMSG(_("E258: Unable to send to client"));
16946 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 rettv->vval.v_number = 0;
16949#else
16950 rettv->vval.v_number = -1;
16951#endif
16952}
16953
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 static void
16955f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958{
16959 char_u *r = NULL;
16960
16961#ifdef FEAT_CLIENTSERVER
16962# ifdef WIN32
16963 r = serverGetVimNames();
16964# else
16965 make_connection();
16966 if (X_DISPLAY != NULL)
16967 r = serverGetVimNames(X_DISPLAY);
16968# endif
16969#endif
16970 rettv->v_type = VAR_STRING;
16971 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
16974/*
16975 * "setbufvar()" function
16976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016978f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016980 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981{
16982 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 char_u nbuf[NUMBUFLEN];
16987
16988 if (check_restricted() || check_secure())
16989 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16991 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016992 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 varp = &argvars[2];
16994
16995 if (buf != NULL && varname != NULL && varp != NULL)
16996 {
16997 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
17000 if (*varname == '&')
17001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 long numval;
17003 char_u *strval;
17004 int error = FALSE;
17005
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017007 numval = get_tv_number_chk(varp, &error);
17008 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 if (!error && strval != NULL)
17010 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 }
17012 else
17013 {
17014 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17015 if (bufvarname != NULL)
17016 {
17017 STRCPY(bufvarname, "b:");
17018 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017019 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 vim_free(bufvarname);
17021 }
17022 }
17023
17024 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027}
17028
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017029 static void
17030f_setcharsearch(argvars, rettv)
17031 typval_T *argvars;
17032 typval_T *rettv UNUSED;
17033{
17034 dict_T *d;
17035 dictitem_T *di;
17036 char_u *csearch;
17037
17038 if (argvars[0].v_type != VAR_DICT)
17039 {
17040 EMSG(_(e_dictreq));
17041 return;
17042 }
17043
17044 if ((d = argvars[0].vval.v_dict) != NULL)
17045 {
17046 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17047 if (csearch != NULL)
17048 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017049#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017050 if (enc_utf8)
17051 {
17052 int pcc[MAX_MCO];
17053 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017054
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017055 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17056 }
17057 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017058#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017059 set_last_csearch(mb_ptr2char(csearch),
17060 csearch, mb_ptr2len(csearch));
17061 }
17062
17063 di = dict_find(d, (char_u *)"forward", -1);
17064 if (di != NULL)
17065 set_csearch_direction(get_tv_number(&di->di_tv)
17066 ? FORWARD : BACKWARD);
17067
17068 di = dict_find(d, (char_u *)"until", -1);
17069 if (di != NULL)
17070 set_csearch_until(!!get_tv_number(&di->di_tv));
17071 }
17072}
17073
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074/*
17075 * "setcmdpos()" function
17076 */
17077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017078f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 typval_T *argvars;
17080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 int pos = (int)get_tv_number(&argvars[0]) - 1;
17083
17084 if (pos >= 0)
17085 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086}
17087
17088/*
17089 * "setline()" function
17090 */
17091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017092f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 typval_T *argvars;
17094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095{
17096 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017097 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017098 list_T *l = NULL;
17099 listitem_T *li = NULL;
17100 long added = 0;
17101 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017103 lnum = get_tv_lnum(&argvars[0]);
17104 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017106 l = argvars[1].vval.v_list;
17107 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017109 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017111
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017112 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017113 for (;;)
17114 {
17115 if (l != NULL)
17116 {
17117 /* list argument, get next string */
17118 if (li == NULL)
17119 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017121 li = li->li_next;
17122 }
17123
17124 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017125 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017126 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017127
17128 /* When coming here from Insert mode, sync undo, so that this can be
17129 * undone separately from what was previously inserted. */
17130 if (u_sync_once == 2)
17131 {
17132 u_sync_once = 1; /* notify that u_sync() was called */
17133 u_sync(TRUE);
17134 }
17135
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017136 if (lnum <= curbuf->b_ml.ml_line_count)
17137 {
17138 /* existing line, replace it */
17139 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17140 {
17141 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017142 if (lnum == curwin->w_cursor.lnum)
17143 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017144 rettv->vval.v_number = 0; /* OK */
17145 }
17146 }
17147 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17148 {
17149 /* lnum is one past the last line, append the line */
17150 ++added;
17151 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17152 rettv->vval.v_number = 0; /* OK */
17153 }
17154
17155 if (l == NULL) /* only one string argument */
17156 break;
17157 ++lnum;
17158 }
17159
17160 if (added > 0)
17161 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162}
17163
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017164static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17165
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017167 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017168 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017169 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017170set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017171 win_T *wp UNUSED;
17172 typval_T *list_arg UNUSED;
17173 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017174 typval_T *rettv;
17175{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017176#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017177 char_u *act;
17178 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017179#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017180
Bram Moolenaar2641f772005-03-25 21:58:17 +000017181 rettv->vval.v_number = -1;
17182
17183#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017184 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017185 EMSG(_(e_listreq));
17186 else
17187 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017188 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017189
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017190 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017191 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017192 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017193 if (act == NULL)
17194 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017195 if (*act == 'a' || *act == 'r')
17196 action = *act;
17197 }
17198
Bram Moolenaar81484f42012-12-05 15:16:47 +010017199 if (l != NULL && set_errorlist(wp, l, action,
17200 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017201 rettv->vval.v_number = 0;
17202 }
17203#endif
17204}
17205
17206/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017207 * "setloclist()" function
17208 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017209 static void
17210f_setloclist(argvars, rettv)
17211 typval_T *argvars;
17212 typval_T *rettv;
17213{
17214 win_T *win;
17215
17216 rettv->vval.v_number = -1;
17217
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017218 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017219 if (win != NULL)
17220 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17221}
17222
17223/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017224 * "setmatches()" function
17225 */
17226 static void
17227f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017228 typval_T *argvars UNUSED;
17229 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017230{
17231#ifdef FEAT_SEARCH_EXTRA
17232 list_T *l;
17233 listitem_T *li;
17234 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017235 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017236
17237 rettv->vval.v_number = -1;
17238 if (argvars[0].v_type != VAR_LIST)
17239 {
17240 EMSG(_(e_listreq));
17241 return;
17242 }
17243 if ((l = argvars[0].vval.v_list) != NULL)
17244 {
17245
17246 /* To some extent make sure that we are dealing with a list from
17247 * "getmatches()". */
17248 li = l->lv_first;
17249 while (li != NULL)
17250 {
17251 if (li->li_tv.v_type != VAR_DICT
17252 || (d = li->li_tv.vval.v_dict) == NULL)
17253 {
17254 EMSG(_(e_invarg));
17255 return;
17256 }
17257 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017258 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17259 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017260 && dict_find(d, (char_u *)"priority", -1) != NULL
17261 && dict_find(d, (char_u *)"id", -1) != NULL))
17262 {
17263 EMSG(_(e_invarg));
17264 return;
17265 }
17266 li = li->li_next;
17267 }
17268
17269 clear_matches(curwin);
17270 li = l->lv_first;
17271 while (li != NULL)
17272 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017273 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017274 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017275 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017276 char_u *group;
17277 int priority;
17278 int id;
17279 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017280
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017281 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017282 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17283 {
17284 if (s == NULL)
17285 {
17286 s = list_alloc();
17287 if (s == NULL)
17288 return;
17289 }
17290
17291 /* match from matchaddpos() */
17292 for (i = 1; i < 9; i++)
17293 {
17294 sprintf((char *)buf, (char *)"pos%d", i);
17295 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17296 {
17297 if (di->di_tv.v_type != VAR_LIST)
17298 return;
17299
17300 list_append_tv(s, &di->di_tv);
17301 s->lv_refcount++;
17302 }
17303 else
17304 break;
17305 }
17306 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017307
17308 group = get_dict_string(d, (char_u *)"group", FALSE);
17309 priority = (int)get_dict_number(d, (char_u *)"priority");
17310 id = (int)get_dict_number(d, (char_u *)"id");
17311 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17312 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17313 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017314 if (i == 0)
17315 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017316 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017317 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017318 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017319 }
17320 else
17321 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017322 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017323 list_unref(s);
17324 s = NULL;
17325 }
17326
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017327 li = li->li_next;
17328 }
17329 rettv->vval.v_number = 0;
17330 }
17331#endif
17332}
17333
17334/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017335 * "setpos()" function
17336 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017337 static void
17338f_setpos(argvars, rettv)
17339 typval_T *argvars;
17340 typval_T *rettv;
17341{
17342 pos_T pos;
17343 int fnum;
17344 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017345 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017346
Bram Moolenaar08250432008-02-13 11:42:46 +000017347 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017348 name = get_tv_string_chk(argvars);
17349 if (name != NULL)
17350 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017351 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017352 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017353 if (--pos.col < 0)
17354 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017355 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017356 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017357 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017358 if (fnum == curbuf->b_fnum)
17359 {
17360 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017361 if (curswant >= 0)
17362 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017363 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017364 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017365 }
17366 else
17367 EMSG(_(e_invarg));
17368 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017369 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17370 {
17371 /* set mark */
17372 if (setmark_pos(name[1], &pos, fnum) == OK)
17373 rettv->vval.v_number = 0;
17374 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017375 else
17376 EMSG(_(e_invarg));
17377 }
17378 }
17379}
17380
17381/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017382 * "setqflist()" function
17383 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017384 static void
17385f_setqflist(argvars, rettv)
17386 typval_T *argvars;
17387 typval_T *rettv;
17388{
17389 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17390}
17391
17392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 * "setreg()" function
17394 */
17395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017396f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 typval_T *argvars;
17398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399{
17400 int regname;
17401 char_u *strregname;
17402 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017403 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 int append;
17405 char_u yank_type;
17406 long block_len;
17407
17408 block_len = -1;
17409 yank_type = MAUTO;
17410 append = FALSE;
17411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017412 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017415 if (strregname == NULL)
17416 return; /* type error; errmsg already given */
17417 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418 if (regname == 0 || regname == '@')
17419 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017421 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017423 stropt = get_tv_string_chk(&argvars[2]);
17424 if (stropt == NULL)
17425 return; /* type error */
17426 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 switch (*stropt)
17428 {
17429 case 'a': case 'A': /* append */
17430 append = TRUE;
17431 break;
17432 case 'v': case 'c': /* character-wise selection */
17433 yank_type = MCHAR;
17434 break;
17435 case 'V': case 'l': /* line-wise selection */
17436 yank_type = MLINE;
17437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 case 'b': case Ctrl_V: /* block-wise selection */
17439 yank_type = MBLOCK;
17440 if (VIM_ISDIGIT(stropt[1]))
17441 {
17442 ++stropt;
17443 block_len = getdigits(&stropt) - 1;
17444 --stropt;
17445 }
17446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 }
17448 }
17449
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017450 if (argvars[1].v_type == VAR_LIST)
17451 {
17452 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017453 char_u **allocval;
17454 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017455 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017456 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017457 int len = argvars[1].vval.v_list->lv_len;
17458 listitem_T *li;
17459
Bram Moolenaar7d647822014-04-05 21:28:56 +020017460 /* First half: use for pointers to result lines; second half: use for
17461 * pointers to allocated copies. */
17462 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017463 if (lstval == NULL)
17464 return;
17465 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017466 allocval = lstval + len + 2;
17467 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017468
17469 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17470 li = li->li_next)
17471 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017472 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017473 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017474 goto free_lstval;
17475 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017476 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017477 /* Need to make a copy, next get_tv_string_buf_chk() will
17478 * overwrite the string. */
17479 strval = vim_strsave(buf);
17480 if (strval == NULL)
17481 goto free_lstval;
17482 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017483 }
17484 *curval++ = strval;
17485 }
17486 *curval++ = NULL;
17487
17488 write_reg_contents_lst(regname, lstval, -1,
17489 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017490free_lstval:
17491 while (curallocval > allocval)
17492 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017493 vim_free(lstval);
17494 }
17495 else
17496 {
17497 strval = get_tv_string_chk(&argvars[1]);
17498 if (strval == NULL)
17499 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017500 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504}
17505
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017506/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017507 * "settabvar()" function
17508 */
17509 static void
17510f_settabvar(argvars, rettv)
17511 typval_T *argvars;
17512 typval_T *rettv;
17513{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017514#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017515 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017516 tabpage_T *tp;
17517#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017518 char_u *varname, *tabvarname;
17519 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017520
17521 rettv->vval.v_number = 0;
17522
17523 if (check_restricted() || check_secure())
17524 return;
17525
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017526#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017527 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017528#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017529 varname = get_tv_string_chk(&argvars[1]);
17530 varp = &argvars[2];
17531
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017532 if (varname != NULL && varp != NULL
17533#ifdef FEAT_WINDOWS
17534 && tp != NULL
17535#endif
17536 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017537 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017538#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017539 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017540 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017541#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017542
17543 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17544 if (tabvarname != NULL)
17545 {
17546 STRCPY(tabvarname, "t:");
17547 STRCPY(tabvarname + 2, varname);
17548 set_var(tabvarname, varp, TRUE);
17549 vim_free(tabvarname);
17550 }
17551
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017552#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017553 /* Restore current tabpage */
17554 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017555 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017556#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017557 }
17558}
17559
17560/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017561 * "settabwinvar()" function
17562 */
17563 static void
17564f_settabwinvar(argvars, rettv)
17565 typval_T *argvars;
17566 typval_T *rettv;
17567{
17568 setwinvar(argvars, rettv, 1);
17569}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570
17571/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017572 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017575f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 typval_T *argvars;
17577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017579 setwinvar(argvars, rettv, 0);
17580}
17581
17582/*
17583 * "setwinvar()" and "settabwinvar()" functions
17584 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017586 static void
17587setwinvar(argvars, rettv, off)
17588 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017589 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017590 int off;
17591{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 win_T *win;
17593#ifdef FEAT_WINDOWS
17594 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017595 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596#endif
17597 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017600 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
17602 if (check_restricted() || check_secure())
17603 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017604
17605#ifdef FEAT_WINDOWS
17606 if (off == 1)
17607 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17608 else
17609 tp = curtab;
17610#endif
17611 win = find_win_by_nr(&argvars[off], tp);
17612 varname = get_tv_string_chk(&argvars[off + 1]);
17613 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614
17615 if (win != NULL && varname != NULL && varp != NULL)
17616 {
17617#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017618 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017621 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017623 long numval;
17624 char_u *strval;
17625 int error = FALSE;
17626
17627 ++varname;
17628 numval = get_tv_number_chk(varp, &error);
17629 strval = get_tv_string_buf_chk(varp, nbuf);
17630 if (!error && strval != NULL)
17631 set_option_value(varname, numval, strval, OPT_LOCAL);
17632 }
17633 else
17634 {
17635 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17636 if (winvarname != NULL)
17637 {
17638 STRCPY(winvarname, "w:");
17639 STRCPY(winvarname + 2, varname);
17640 set_var(winvarname, varp, TRUE);
17641 vim_free(winvarname);
17642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 }
17644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017646 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647#endif
17648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649}
17650
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017651#ifdef FEAT_CRYPT
17652/*
17653 * "sha256({string})" function
17654 */
17655 static void
17656f_sha256(argvars, rettv)
17657 typval_T *argvars;
17658 typval_T *rettv;
17659{
17660 char_u *p;
17661
17662 p = get_tv_string(&argvars[0]);
17663 rettv->vval.v_string = vim_strsave(
17664 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17665 rettv->v_type = VAR_STRING;
17666}
17667#endif /* FEAT_CRYPT */
17668
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017670 * "shellescape({string})" function
17671 */
17672 static void
17673f_shellescape(argvars, rettv)
17674 typval_T *argvars;
17675 typval_T *rettv;
17676{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017677 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017678 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017679 rettv->v_type = VAR_STRING;
17680}
17681
17682/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017683 * shiftwidth() function
17684 */
17685 static void
17686f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017687 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017688 typval_T *rettv;
17689{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017690 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017691}
17692
17693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 */
17696 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 typval_T *argvars;
17699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017701 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703 p = get_tv_string(&argvars[0]);
17704 rettv->vval.v_string = vim_strsave(p);
17705 simplify_filename(rettv->vval.v_string); /* simplify in place */
17706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707}
17708
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017709#ifdef FEAT_FLOAT
17710/*
17711 * "sin()" function
17712 */
17713 static void
17714f_sin(argvars, rettv)
17715 typval_T *argvars;
17716 typval_T *rettv;
17717{
17718 float_T f;
17719
17720 rettv->v_type = VAR_FLOAT;
17721 if (get_float_arg(argvars, &f) == OK)
17722 rettv->vval.v_float = sin(f);
17723 else
17724 rettv->vval.v_float = 0.0;
17725}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017726
17727/*
17728 * "sinh()" function
17729 */
17730 static void
17731f_sinh(argvars, rettv)
17732 typval_T *argvars;
17733 typval_T *rettv;
17734{
17735 float_T f;
17736
17737 rettv->v_type = VAR_FLOAT;
17738 if (get_float_arg(argvars, &f) == OK)
17739 rettv->vval.v_float = sinh(f);
17740 else
17741 rettv->vval.v_float = 0.0;
17742}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017743#endif
17744
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745static int
17746#ifdef __BORLANDC__
17747 _RTLENTRYF
17748#endif
17749 item_compare __ARGS((const void *s1, const void *s2));
17750static int
17751#ifdef __BORLANDC__
17752 _RTLENTRYF
17753#endif
17754 item_compare2 __ARGS((const void *s1, const void *s2));
17755
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017756/* struct used in the array that's given to qsort() */
17757typedef struct
17758{
17759 listitem_T *item;
17760 int idx;
17761} sortItem_T;
17762
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017764static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017765static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017766static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017768static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017769static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770#define ITEM_COMPARE_FAIL 999
17771
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017773 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017775 static int
17776#ifdef __BORLANDC__
17777_RTLENTRYF
17778#endif
17779item_compare(s1, s2)
17780 const void *s1;
17781 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017783 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017784 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017785 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017786 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787 int res;
17788 char_u numbuf1[NUMBUFLEN];
17789 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017791 si1 = (sortItem_T *)s1;
17792 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017793 tv1 = &si1->item->li_tv;
17794 tv2 = &si2->item->li_tv;
17795 /* tv2string() puts quotes around a string and allocates memory. Don't do
17796 * that for string variables. Use a single quote when comparing with a
17797 * non-string to do what the docs promise. */
17798 if (tv1->v_type == VAR_STRING)
17799 {
17800 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17801 p1 = (char_u *)"'";
17802 else
17803 p1 = tv1->vval.v_string;
17804 }
17805 else
17806 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17807 if (tv2->v_type == VAR_STRING)
17808 {
17809 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17810 p2 = (char_u *)"'";
17811 else
17812 p2 = tv2->vval.v_string;
17813 }
17814 else
17815 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017816 if (p1 == NULL)
17817 p1 = (char_u *)"";
17818 if (p2 == NULL)
17819 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017820 if (!item_compare_numeric)
17821 {
17822 if (item_compare_ic)
17823 res = STRICMP(p1, p2);
17824 else
17825 res = STRCMP(p1, p2);
17826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017828 {
17829 double n1, n2;
17830 n1 = strtod((char *)p1, (char **)&p1);
17831 n2 = strtod((char *)p2, (char **)&p2);
17832 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17833 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017834
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017835 /* When the result would be zero, compare the item indexes. Makes the
17836 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017837 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017838 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017839
Bram Moolenaar0d660222005-01-07 21:51:51 +000017840 vim_free(tofree1);
17841 vim_free(tofree2);
17842 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843}
17844
17845 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017846#ifdef __BORLANDC__
17847_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017849item_compare2(s1, s2)
17850 const void *s1;
17851 const void *s2;
17852{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017853 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017854 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017855 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017856 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017857 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017859 /* shortcut after failure in previous call; compare all items equal */
17860 if (item_compare_func_err)
17861 return 0;
17862
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017863 si1 = (sortItem_T *)s1;
17864 si2 = (sortItem_T *)s2;
17865
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017866 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017867 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017868 copy_tv(&si1->item->li_tv, &argv[0]);
17869 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017870
17871 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017872 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017873 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17874 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017875 clear_tv(&argv[0]);
17876 clear_tv(&argv[1]);
17877
17878 if (res == FAIL)
17879 res = ITEM_COMPARE_FAIL;
17880 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017881 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17882 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017883 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017884 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017885
17886 /* When the result would be zero, compare the pointers themselves. Makes
17887 * the sort stable. */
17888 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017889 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017890
Bram Moolenaar0d660222005-01-07 21:51:51 +000017891 return res;
17892}
17893
17894/*
17895 * "sort({list})" function
17896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017898do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 typval_T *argvars;
17900 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017901 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902{
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 list_T *l;
17904 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017905 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 long len;
17907 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908
Bram Moolenaar0d660222005-01-07 21:51:51 +000017909 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017910 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 else
17912 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017914 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017915 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17916 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 return;
17918 rettv->vval.v_list = l;
17919 rettv->v_type = VAR_LIST;
17920 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922 len = list_len(l);
17923 if (len <= 1)
17924 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017927 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017929 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930 if (argvars[1].v_type != VAR_UNKNOWN)
17931 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017932 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017933 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935 else
17936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017937 int error = FALSE;
17938
17939 i = get_tv_number_chk(&argvars[1], &error);
17940 if (error)
17941 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942 if (i == 1)
17943 item_compare_ic = TRUE;
17944 else
17945 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017946 if (item_compare_func != NULL)
17947 {
17948 if (STRCMP(item_compare_func, "n") == 0)
17949 {
17950 item_compare_func = NULL;
17951 item_compare_numeric = TRUE;
17952 }
17953 else if (STRCMP(item_compare_func, "i") == 0)
17954 {
17955 item_compare_func = NULL;
17956 item_compare_ic = TRUE;
17957 }
17958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017959 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017960
17961 if (argvars[2].v_type != VAR_UNKNOWN)
17962 {
17963 /* optional third argument: {dict} */
17964 if (argvars[2].v_type != VAR_DICT)
17965 {
17966 EMSG(_(e_dictreq));
17967 return;
17968 }
17969 item_compare_selfdict = argvars[2].vval.v_dict;
17970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972
Bram Moolenaar0d660222005-01-07 21:51:51 +000017973 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017974 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017975 if (ptrs == NULL)
17976 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977
Bram Moolenaar327aa022014-03-25 18:24:23 +010017978 i = 0;
17979 if (sort)
17980 {
17981 /* sort(): ptrs will be the list to sort */
17982 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017983 {
17984 ptrs[i].item = li;
17985 ptrs[i].idx = i;
17986 ++i;
17987 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017988
17989 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017990 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017991 /* test the compare function */
17992 if (item_compare_func != NULL
17993 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017995 EMSG(_("E702: Sort compare function failed"));
17996 else
17997 {
17998 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017999 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018000 item_compare_func == NULL ? item_compare : item_compare2);
18001
18002 if (!item_compare_func_err)
18003 {
18004 /* Clear the List and append the items in sorted order. */
18005 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18006 l->lv_len = 0;
18007 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018008 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018009 }
18010 }
18011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018013 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018014 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18015
18016 /* f_uniq(): ptrs will be a stack of items to remove */
18017 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018018 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018019 item_compare_func_ptr = item_compare_func
18020 ? item_compare2 : item_compare;
18021
18022 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18023 li = li->li_next)
18024 {
18025 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18026 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018027 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018028 if (item_compare_func_err)
18029 {
18030 EMSG(_("E882: Uniq compare function failed"));
18031 break;
18032 }
18033 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018035 if (!item_compare_func_err)
18036 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018037 while (--i >= 0)
18038 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018039 li = ptrs[i].item->li_next;
18040 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018041 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018042 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018043 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018044 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018045 list_fix_watch(l, li);
18046 listitem_free(li);
18047 l->lv_len--;
18048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018049 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018050 }
18051
18052 vim_free(ptrs);
18053 }
18054}
18055
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018056/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018057 * "sort({list})" function
18058 */
18059 static void
18060f_sort(argvars, rettv)
18061 typval_T *argvars;
18062 typval_T *rettv;
18063{
18064 do_sort_uniq(argvars, rettv, TRUE);
18065}
18066
18067/*
18068 * "uniq({list})" function
18069 */
18070 static void
18071f_uniq(argvars, rettv)
18072 typval_T *argvars;
18073 typval_T *rettv;
18074{
18075 do_sort_uniq(argvars, rettv, FALSE);
18076}
18077
18078/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018079 * "soundfold({word})" function
18080 */
18081 static void
18082f_soundfold(argvars, rettv)
18083 typval_T *argvars;
18084 typval_T *rettv;
18085{
18086 char_u *s;
18087
18088 rettv->v_type = VAR_STRING;
18089 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018090#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018091 rettv->vval.v_string = eval_soundfold(s);
18092#else
18093 rettv->vval.v_string = vim_strsave(s);
18094#endif
18095}
18096
18097/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018098 * "spellbadword()" function
18099 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018100 static void
18101f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018102 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018103 typval_T *rettv;
18104{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018105 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018106 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018107 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018109 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018110 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018111
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018112#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018113 if (argvars[0].v_type == VAR_UNKNOWN)
18114 {
18115 /* Find the start and length of the badly spelled word. */
18116 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18117 if (len != 0)
18118 word = ml_get_cursor();
18119 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018120 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018121 {
18122 char_u *str = get_tv_string_chk(&argvars[0]);
18123 int capcol = -1;
18124
18125 if (str != NULL)
18126 {
18127 /* Check the argument for spelling. */
18128 while (*str != NUL)
18129 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018130 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018131 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018132 {
18133 word = str;
18134 break;
18135 }
18136 str += len;
18137 }
18138 }
18139 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018140#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018141
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018142 list_append_string(rettv->vval.v_list, word, len);
18143 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018144 attr == HLF_SPB ? "bad" :
18145 attr == HLF_SPR ? "rare" :
18146 attr == HLF_SPL ? "local" :
18147 attr == HLF_SPC ? "caps" :
18148 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018149}
18150
18151/*
18152 * "spellsuggest()" function
18153 */
18154 static void
18155f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018156 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018157 typval_T *rettv;
18158{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018159#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018160 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018161 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018162 int maxcount;
18163 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018164 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018165 listitem_T *li;
18166 int need_capital = FALSE;
18167#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018168
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018169 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018170 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018171
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018172#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018173 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018174 {
18175 str = get_tv_string(&argvars[0]);
18176 if (argvars[1].v_type != VAR_UNKNOWN)
18177 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018178 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018179 if (maxcount <= 0)
18180 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018181 if (argvars[2].v_type != VAR_UNKNOWN)
18182 {
18183 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18184 if (typeerr)
18185 return;
18186 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018187 }
18188 else
18189 maxcount = 25;
18190
Bram Moolenaar4770d092006-01-12 23:22:24 +000018191 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018192
18193 for (i = 0; i < ga.ga_len; ++i)
18194 {
18195 str = ((char_u **)ga.ga_data)[i];
18196
18197 li = listitem_alloc();
18198 if (li == NULL)
18199 vim_free(str);
18200 else
18201 {
18202 li->li_tv.v_type = VAR_STRING;
18203 li->li_tv.v_lock = 0;
18204 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018205 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018206 }
18207 }
18208 ga_clear(&ga);
18209 }
18210#endif
18211}
18212
Bram Moolenaar0d660222005-01-07 21:51:51 +000018213 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018214f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018215 typval_T *argvars;
18216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217{
18218 char_u *str;
18219 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018220 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 regmatch_T regmatch;
18222 char_u patbuf[NUMBUFLEN];
18223 char_u *save_cpo;
18224 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018226 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018227 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228
18229 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18230 save_cpo = p_cpo;
18231 p_cpo = (char_u *)"";
18232
18233 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018234 if (argvars[1].v_type != VAR_UNKNOWN)
18235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018236 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18237 if (pat == NULL)
18238 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018239 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018240 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018241 }
18242 if (pat == NULL || *pat == NUL)
18243 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018245 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018247 if (typeerr)
18248 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249
Bram Moolenaar0d660222005-01-07 21:51:51 +000018250 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18251 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018254 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018255 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018256 if (*str == NUL)
18257 match = FALSE; /* empty item at the end */
18258 else
18259 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260 if (match)
18261 end = regmatch.startp[0];
18262 else
18263 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018264 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18265 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018266 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018267 if (list_append_string(rettv->vval.v_list, str,
18268 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 }
18271 if (!match)
18272 break;
18273 /* Advance to just after the match. */
18274 if (regmatch.endp[0] > str)
18275 col = 0;
18276 else
18277 {
18278 /* Don't get stuck at the same match. */
18279#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018280 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281#else
18282 col = 1;
18283#endif
18284 }
18285 str = regmatch.endp[0];
18286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287
Bram Moolenaar473de612013-06-08 18:19:48 +020018288 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292}
18293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018294#ifdef FEAT_FLOAT
18295/*
18296 * "sqrt()" function
18297 */
18298 static void
18299f_sqrt(argvars, rettv)
18300 typval_T *argvars;
18301 typval_T *rettv;
18302{
18303 float_T f;
18304
18305 rettv->v_type = VAR_FLOAT;
18306 if (get_float_arg(argvars, &f) == OK)
18307 rettv->vval.v_float = sqrt(f);
18308 else
18309 rettv->vval.v_float = 0.0;
18310}
18311
18312/*
18313 * "str2float()" function
18314 */
18315 static void
18316f_str2float(argvars, rettv)
18317 typval_T *argvars;
18318 typval_T *rettv;
18319{
18320 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18321
18322 if (*p == '+')
18323 p = skipwhite(p + 1);
18324 (void)string2float(p, &rettv->vval.v_float);
18325 rettv->v_type = VAR_FLOAT;
18326}
18327#endif
18328
Bram Moolenaar2c932302006-03-18 21:42:09 +000018329/*
18330 * "str2nr()" function
18331 */
18332 static void
18333f_str2nr(argvars, rettv)
18334 typval_T *argvars;
18335 typval_T *rettv;
18336{
18337 int base = 10;
18338 char_u *p;
18339 long n;
18340
18341 if (argvars[1].v_type != VAR_UNKNOWN)
18342 {
18343 base = get_tv_number(&argvars[1]);
18344 if (base != 8 && base != 10 && base != 16)
18345 {
18346 EMSG(_(e_invarg));
18347 return;
18348 }
18349 }
18350
18351 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018352 if (*p == '+')
18353 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018354 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018355 rettv->vval.v_number = n;
18356}
18357
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358#ifdef HAVE_STRFTIME
18359/*
18360 * "strftime({format}[, {time}])" function
18361 */
18362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018363f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018364 typval_T *argvars;
18365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366{
18367 char_u result_buf[256];
18368 struct tm *curtime;
18369 time_t seconds;
18370 char_u *p;
18371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018374 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018375 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 seconds = time(NULL);
18377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018378 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 curtime = localtime(&seconds);
18380 /* MSVC returns NULL for an invalid value of seconds. */
18381 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018382 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 else
18384 {
18385# ifdef FEAT_MBYTE
18386 vimconv_T conv;
18387 char_u *enc;
18388
18389 conv.vc_type = CONV_NONE;
18390 enc = enc_locale();
18391 convert_setup(&conv, p_enc, enc);
18392 if (conv.vc_type != CONV_NONE)
18393 p = string_convert(&conv, p, NULL);
18394# endif
18395 if (p != NULL)
18396 (void)strftime((char *)result_buf, sizeof(result_buf),
18397 (char *)p, curtime);
18398 else
18399 result_buf[0] = NUL;
18400
18401# ifdef FEAT_MBYTE
18402 if (conv.vc_type != CONV_NONE)
18403 vim_free(p);
18404 convert_setup(&conv, enc, p_enc);
18405 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018406 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 else
18408# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410
18411# ifdef FEAT_MBYTE
18412 /* Release conversion descriptors */
18413 convert_setup(&conv, NULL, NULL);
18414 vim_free(enc);
18415# endif
18416 }
18417}
18418#endif
18419
18420/*
18421 * "stridx()" function
18422 */
18423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018425 typval_T *argvars;
18426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427{
18428 char_u buf[NUMBUFLEN];
18429 char_u *needle;
18430 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018431 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018433 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018435 needle = get_tv_string_chk(&argvars[1]);
18436 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018437 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018438 if (needle == NULL || haystack == NULL)
18439 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 if (argvars[2].v_type != VAR_UNKNOWN)
18442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018443 int error = FALSE;
18444
18445 start_idx = get_tv_number_chk(&argvars[2], &error);
18446 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018448 if (start_idx >= 0)
18449 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 }
18451
18452 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18453 if (pos != NULL)
18454 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455}
18456
18457/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018458 * "string()" function
18459 */
18460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 typval_T *argvars;
18463 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018464{
18465 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018468 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018469 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018470 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018471 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018472 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473}
18474
18475/*
18476 * "strlen()" function
18477 */
18478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T *argvars;
18481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 rettv->vval.v_number = (varnumber_T)(STRLEN(
18484 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485}
18486
18487/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018488 * "strchars()" function
18489 */
18490 static void
18491f_strchars(argvars, rettv)
18492 typval_T *argvars;
18493 typval_T *rettv;
18494{
18495 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018496 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018497#ifdef FEAT_MBYTE
18498 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018499 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018500#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018501
18502 if (argvars[1].v_type != VAR_UNKNOWN)
18503 skipcc = get_tv_number_chk(&argvars[1], NULL);
18504 if (skipcc < 0 || skipcc > 1)
18505 EMSG(_(e_invarg));
18506 else
18507 {
18508#ifdef FEAT_MBYTE
18509 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18510 while (*s != NUL)
18511 {
18512 func_mb_ptr2char_adv(&s);
18513 ++len;
18514 }
18515 rettv->vval.v_number = len;
18516#else
18517 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18518#endif
18519 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018520}
18521
18522/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018523 * "strdisplaywidth()" function
18524 */
18525 static void
18526f_strdisplaywidth(argvars, rettv)
18527 typval_T *argvars;
18528 typval_T *rettv;
18529{
18530 char_u *s = get_tv_string(&argvars[0]);
18531 int col = 0;
18532
18533 if (argvars[1].v_type != VAR_UNKNOWN)
18534 col = get_tv_number(&argvars[1]);
18535
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018536 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018537}
18538
18539/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018540 * "strwidth()" function
18541 */
18542 static void
18543f_strwidth(argvars, rettv)
18544 typval_T *argvars;
18545 typval_T *rettv;
18546{
18547 char_u *s = get_tv_string(&argvars[0]);
18548
18549 rettv->vval.v_number = (varnumber_T)(
18550#ifdef FEAT_MBYTE
18551 mb_string2cells(s, -1)
18552#else
18553 STRLEN(s)
18554#endif
18555 );
18556}
18557
18558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 * "strpart()" function
18560 */
18561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018562f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018563 typval_T *argvars;
18564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565{
18566 char_u *p;
18567 int n;
18568 int len;
18569 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018570 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 slen = (int)STRLEN(p);
18574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018575 n = get_tv_number_chk(&argvars[1], &error);
18576 if (error)
18577 len = 0;
18578 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 else
18581 len = slen - n; /* default len: all bytes that are available. */
18582
18583 /*
18584 * Only return the overlap between the specified part and the actual
18585 * string.
18586 */
18587 if (n < 0)
18588 {
18589 len += n;
18590 n = 0;
18591 }
18592 else if (n > slen)
18593 n = slen;
18594 if (len < 0)
18595 len = 0;
18596 else if (n + len > slen)
18597 len = slen - n;
18598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599 rettv->v_type = VAR_STRING;
18600 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601}
18602
18603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 * "strridx()" function
18605 */
18606 static void
18607f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018608 typval_T *argvars;
18609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610{
18611 char_u buf[NUMBUFLEN];
18612 char_u *needle;
18613 char_u *haystack;
18614 char_u *rest;
18615 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018616 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018618 needle = get_tv_string_chk(&argvars[1]);
18619 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620
18621 rettv->vval.v_number = -1;
18622 if (needle == NULL || haystack == NULL)
18623 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018624
18625 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018626 if (argvars[2].v_type != VAR_UNKNOWN)
18627 {
18628 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018629 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018630 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018632 }
18633 else
18634 end_idx = haystack_len;
18635
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018637 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018639 lastmatch = haystack + end_idx;
18640 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018642 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 for (rest = haystack; *rest != '\0'; ++rest)
18644 {
18645 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018646 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 break;
18648 lastmatch = rest;
18649 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018650 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651
18652 if (lastmatch == NULL)
18653 rettv->vval.v_number = -1;
18654 else
18655 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18656}
18657
18658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 * "strtrans()" function
18660 */
18661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018662f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 typval_T *argvars;
18664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666 rettv->v_type = VAR_STRING;
18667 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668}
18669
18670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018671 * "submatch()" function
18672 */
18673 static void
18674f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018675 typval_T *argvars;
18676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677{
Bram Moolenaar41571762014-04-02 19:00:58 +020018678 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018679 int no;
18680 int retList = 0;
18681
18682 no = (int)get_tv_number_chk(&argvars[0], &error);
18683 if (error)
18684 return;
18685 error = FALSE;
18686 if (argvars[1].v_type != VAR_UNKNOWN)
18687 retList = get_tv_number_chk(&argvars[1], &error);
18688 if (error)
18689 return;
18690
18691 if (retList == 0)
18692 {
18693 rettv->v_type = VAR_STRING;
18694 rettv->vval.v_string = reg_submatch(no);
18695 }
18696 else
18697 {
18698 rettv->v_type = VAR_LIST;
18699 rettv->vval.v_list = reg_submatch_list(no);
18700 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018701}
18702
18703/*
18704 * "substitute()" function
18705 */
18706 static void
18707f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 typval_T *argvars;
18709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710{
18711 char_u patbuf[NUMBUFLEN];
18712 char_u subbuf[NUMBUFLEN];
18713 char_u flagsbuf[NUMBUFLEN];
18714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018715 char_u *str = get_tv_string_chk(&argvars[0]);
18716 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18717 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18718 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18719
Bram Moolenaar0d660222005-01-07 21:51:51 +000018720 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018721 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18722 rettv->vval.v_string = NULL;
18723 else
18724 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018725}
18726
18727/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018728 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
18735 int id = 0;
18736#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018737 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 long col;
18739 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018740 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018742 lnum = get_tv_lnum(argvars); /* -1 on type error */
18743 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18744 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018746 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018747 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018748 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749#endif
18750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752}
18753
18754/*
18755 * "synIDattr(id, what [, mode])" function
18756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018758f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
18762 char_u *p = NULL;
18763#ifdef FEAT_SYN_HL
18764 int id;
18765 char_u *what;
18766 char_u *mode;
18767 char_u modebuf[NUMBUFLEN];
18768 int modec;
18769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018770 id = get_tv_number(&argvars[0]);
18771 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018776 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 modec = 0; /* replace invalid with current */
18778 }
18779 else
18780 {
18781#ifdef FEAT_GUI
18782 if (gui.in_use)
18783 modec = 'g';
18784 else
18785#endif
18786 if (t_colors > 1)
18787 modec = 'c';
18788 else
18789 modec = 't';
18790 }
18791
18792
18793 switch (TOLOWER_ASC(what[0]))
18794 {
18795 case 'b':
18796 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18797 p = highlight_color(id, what, modec);
18798 else /* bold */
18799 p = highlight_has_attr(id, HL_BOLD, modec);
18800 break;
18801
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018802 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 p = highlight_color(id, what, modec);
18804 break;
18805
18806 case 'i':
18807 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18808 p = highlight_has_attr(id, HL_INVERSE, modec);
18809 else /* italic */
18810 p = highlight_has_attr(id, HL_ITALIC, modec);
18811 break;
18812
18813 case 'n': /* name */
18814 p = get_highlight_name(NULL, id - 1);
18815 break;
18816
18817 case 'r': /* reverse */
18818 p = highlight_has_attr(id, HL_INVERSE, modec);
18819 break;
18820
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018821 case 's':
18822 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18823 p = highlight_color(id, what, modec);
18824 else /* standout */
18825 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 break;
18827
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018828 case 'u':
18829 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18830 /* underline */
18831 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18832 else
18833 /* undercurl */
18834 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 break;
18836 }
18837
18838 if (p != NULL)
18839 p = vim_strsave(p);
18840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841 rettv->v_type = VAR_STRING;
18842 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
18845/*
18846 * "synIDtrans(id)" function
18847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018850 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852{
18853 int id;
18854
18855#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018856 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857
18858 if (id > 0)
18859 id = syn_get_final_id(id);
18860 else
18861#endif
18862 id = 0;
18863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865}
18866
18867/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018868 * "synconcealed(lnum, col)" function
18869 */
18870 static void
18871f_synconcealed(argvars, rettv)
18872 typval_T *argvars UNUSED;
18873 typval_T *rettv;
18874{
18875#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18876 long lnum;
18877 long col;
18878 int syntax_flags = 0;
18879 int cchar;
18880 int matchid = 0;
18881 char_u str[NUMBUFLEN];
18882#endif
18883
18884 rettv->v_type = VAR_LIST;
18885 rettv->vval.v_list = NULL;
18886
18887#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18888 lnum = get_tv_lnum(argvars); /* -1 on type error */
18889 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18890
18891 vim_memset(str, NUL, sizeof(str));
18892
18893 if (rettv_list_alloc(rettv) != FAIL)
18894 {
18895 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18896 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18897 && curwin->w_p_cole > 0)
18898 {
18899 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18900 syntax_flags = get_syntax_info(&matchid);
18901
18902 /* get the conceal character */
18903 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18904 {
18905 cchar = syn_get_sub_char();
18906 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18907 cchar = lcs_conceal;
18908 if (cchar != NUL)
18909 {
18910# ifdef FEAT_MBYTE
18911 if (has_mbyte)
18912 (*mb_char2bytes)(cchar, str);
18913 else
18914# endif
18915 str[0] = cchar;
18916 }
18917 }
18918 }
18919
18920 list_append_number(rettv->vval.v_list,
18921 (syntax_flags & HL_CONCEAL) != 0);
18922 /* -1 to auto-determine strlen */
18923 list_append_string(rettv->vval.v_list, str, -1);
18924 list_append_number(rettv->vval.v_list, matchid);
18925 }
18926#endif
18927}
18928
18929/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018930 * "synstack(lnum, col)" function
18931 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018932 static void
18933f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018934 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018935 typval_T *rettv;
18936{
18937#ifdef FEAT_SYN_HL
18938 long lnum;
18939 long col;
18940 int i;
18941 int id;
18942#endif
18943
18944 rettv->v_type = VAR_LIST;
18945 rettv->vval.v_list = NULL;
18946
18947#ifdef FEAT_SYN_HL
18948 lnum = get_tv_lnum(argvars); /* -1 on type error */
18949 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18950
18951 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018952 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018953 && rettv_list_alloc(rettv) != FAIL)
18954 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018955 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018956 for (i = 0; ; ++i)
18957 {
18958 id = syn_get_stack_item(i);
18959 if (id < 0)
18960 break;
18961 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18962 break;
18963 }
18964 }
18965#endif
18966}
18967
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018969get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 typval_T *argvars;
18971 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018972 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018974 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018976 char_u *infile = NULL;
18977 char_u buf[NUMBUFLEN];
18978 int err = FALSE;
18979 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018980 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018981 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018983 rettv->v_type = VAR_STRING;
18984 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018985 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018986 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018987
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018988 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018989 {
18990 /*
18991 * Write the string to a temp file, to be used for input of the shell
18992 * command.
18993 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018994 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018995 {
18996 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018997 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018998 }
18999
19000 fd = mch_fopen((char *)infile, WRITEBIN);
19001 if (fd == NULL)
19002 {
19003 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019004 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019005 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019006 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019007 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019008 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19009 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019010 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019011 else
19012 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019013 size_t len;
19014
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019015 p = get_tv_string_buf_chk(&argvars[1], buf);
19016 if (p == NULL)
19017 {
19018 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019019 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019020 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019021 len = STRLEN(p);
19022 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019023 err = TRUE;
19024 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019025 if (fclose(fd) != 0)
19026 err = TRUE;
19027 if (err)
19028 {
19029 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019030 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019031 }
19032 }
19033
Bram Moolenaar52a72462014-08-29 15:53:52 +020019034 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19035 * echoes typeahead, that messes up the display. */
19036 if (!msg_silent)
19037 flags += SHELL_COOKED;
19038
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019039 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019041 int len;
19042 listitem_T *li;
19043 char_u *s = NULL;
19044 char_u *start;
19045 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019046 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047
Bram Moolenaar52a72462014-08-29 15:53:52 +020019048 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019049 if (res == NULL)
19050 goto errret;
19051
19052 list = list_alloc();
19053 if (list == NULL)
19054 goto errret;
19055
19056 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019058 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019059 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019060 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019061 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019062
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019063 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 if (s == NULL)
19065 goto errret;
19066
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019067 for (p = s; start < end; ++p, ++start)
19068 *p = *start == NUL ? NL : *start;
19069 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019070
19071 li = listitem_alloc();
19072 if (li == NULL)
19073 {
19074 vim_free(s);
19075 goto errret;
19076 }
19077 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019078 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019079 li->li_tv.vval.v_string = s;
19080 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019082
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019083 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019084 rettv->v_type = VAR_LIST;
19085 rettv->vval.v_list = list;
19086 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088 else
19089 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019090 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019091#ifdef USE_CR
19092 /* translate <CR> into <NL> */
19093 if (res != NULL)
19094 {
19095 char_u *s;
19096
19097 for (s = res; *s; ++s)
19098 {
19099 if (*s == CAR)
19100 *s = NL;
19101 }
19102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103#else
19104# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019105 /* translate <CR><NL> into <NL> */
19106 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019108 char_u *s, *d;
19109
19110 d = res;
19111 for (s = res; *s; ++s)
19112 {
19113 if (s[0] == CAR && s[1] == NL)
19114 ++s;
19115 *d++ = *s;
19116 }
19117 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119# endif
19120#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019121 rettv->vval.v_string = res;
19122 res = NULL;
19123 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019124
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019125errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019126 if (infile != NULL)
19127 {
19128 mch_remove(infile);
19129 vim_free(infile);
19130 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019131 if (res != NULL)
19132 vim_free(res);
19133 if (list != NULL)
19134 list_free(list, TRUE);
19135}
19136
19137/*
19138 * "system()" function
19139 */
19140 static void
19141f_system(argvars, rettv)
19142 typval_T *argvars;
19143 typval_T *rettv;
19144{
19145 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19146}
19147
19148/*
19149 * "systemlist()" function
19150 */
19151 static void
19152f_systemlist(argvars, rettv)
19153 typval_T *argvars;
19154 typval_T *rettv;
19155{
19156 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157}
19158
19159/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019160 * "tabpagebuflist()" function
19161 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019162 static void
19163f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019164 typval_T *argvars UNUSED;
19165 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019166{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019167#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019168 tabpage_T *tp;
19169 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019170
19171 if (argvars[0].v_type == VAR_UNKNOWN)
19172 wp = firstwin;
19173 else
19174 {
19175 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19176 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019177 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019178 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019179 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019180 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019181 for (; wp != NULL; wp = wp->w_next)
19182 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019183 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019184 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019185 }
19186#endif
19187}
19188
19189
19190/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019191 * "tabpagenr()" function
19192 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019193 static void
19194f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019195 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019196 typval_T *rettv;
19197{
19198 int nr = 1;
19199#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019200 char_u *arg;
19201
19202 if (argvars[0].v_type != VAR_UNKNOWN)
19203 {
19204 arg = get_tv_string_chk(&argvars[0]);
19205 nr = 0;
19206 if (arg != NULL)
19207 {
19208 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019209 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019210 else
19211 EMSG2(_(e_invexpr2), arg);
19212 }
19213 }
19214 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019215 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019216#endif
19217 rettv->vval.v_number = nr;
19218}
19219
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019220
19221#ifdef FEAT_WINDOWS
19222static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19223
19224/*
19225 * Common code for tabpagewinnr() and winnr().
19226 */
19227 static int
19228get_winnr(tp, argvar)
19229 tabpage_T *tp;
19230 typval_T *argvar;
19231{
19232 win_T *twin;
19233 int nr = 1;
19234 win_T *wp;
19235 char_u *arg;
19236
19237 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19238 if (argvar->v_type != VAR_UNKNOWN)
19239 {
19240 arg = get_tv_string_chk(argvar);
19241 if (arg == NULL)
19242 nr = 0; /* type error; errmsg already given */
19243 else if (STRCMP(arg, "$") == 0)
19244 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19245 else if (STRCMP(arg, "#") == 0)
19246 {
19247 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19248 if (twin == NULL)
19249 nr = 0;
19250 }
19251 else
19252 {
19253 EMSG2(_(e_invexpr2), arg);
19254 nr = 0;
19255 }
19256 }
19257
19258 if (nr > 0)
19259 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19260 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019261 {
19262 if (wp == NULL)
19263 {
19264 /* didn't find it in this tabpage */
19265 nr = 0;
19266 break;
19267 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019268 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019269 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019270 return nr;
19271}
19272#endif
19273
19274/*
19275 * "tabpagewinnr()" function
19276 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019277 static void
19278f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019279 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019280 typval_T *rettv;
19281{
19282 int nr = 1;
19283#ifdef FEAT_WINDOWS
19284 tabpage_T *tp;
19285
19286 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19287 if (tp == NULL)
19288 nr = 0;
19289 else
19290 nr = get_winnr(tp, &argvars[1]);
19291#endif
19292 rettv->vval.v_number = nr;
19293}
19294
19295
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019296/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019297 * "tagfiles()" function
19298 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019299 static void
19300f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019301 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019302 typval_T *rettv;
19303{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019304 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019305 tagname_T tn;
19306 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019308 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019309 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019310 fname = alloc(MAXPATHL);
19311 if (fname == NULL)
19312 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019313
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019314 for (first = TRUE; ; first = FALSE)
19315 if (get_tagfname(&tn, first, fname) == FAIL
19316 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019317 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019318 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019319 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019320}
19321
19322/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019323 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019324 */
19325 static void
19326f_taglist(argvars, rettv)
19327 typval_T *argvars;
19328 typval_T *rettv;
19329{
19330 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019331
19332 tag_pattern = get_tv_string(&argvars[0]);
19333
19334 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019335 if (*tag_pattern == NUL)
19336 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019337
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019338 if (rettv_list_alloc(rettv) == OK)
19339 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019340}
19341
19342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 * "tempname()" function
19344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019346f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019347 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349{
19350 static int x = 'A';
19351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019353 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354
19355 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19356 * names. Skip 'I' and 'O', they are used for shell redirection. */
19357 do
19358 {
19359 if (x == 'Z')
19360 x = '0';
19361 else if (x == '9')
19362 x = 'A';
19363 else
19364 {
19365#ifdef EBCDIC
19366 if (x == 'I')
19367 x = 'J';
19368 else if (x == 'R')
19369 x = 'S';
19370 else
19371#endif
19372 ++x;
19373 }
19374 } while (x == 'I' || x == 'O');
19375}
19376
19377/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019378 * "test(list)" function: Just checking the walls...
19379 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019380 static void
19381f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019382 typval_T *argvars UNUSED;
19383 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019384{
19385 /* Used for unit testing. Change the code below to your liking. */
19386#if 0
19387 listitem_T *li;
19388 list_T *l;
19389 char_u *bad, *good;
19390
19391 if (argvars[0].v_type != VAR_LIST)
19392 return;
19393 l = argvars[0].vval.v_list;
19394 if (l == NULL)
19395 return;
19396 li = l->lv_first;
19397 if (li == NULL)
19398 return;
19399 bad = get_tv_string(&li->li_tv);
19400 li = li->li_next;
19401 if (li == NULL)
19402 return;
19403 good = get_tv_string(&li->li_tv);
19404 rettv->vval.v_number = test_edit_score(bad, good);
19405#endif
19406}
19407
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019408#ifdef FEAT_FLOAT
19409/*
19410 * "tan()" function
19411 */
19412 static void
19413f_tan(argvars, rettv)
19414 typval_T *argvars;
19415 typval_T *rettv;
19416{
19417 float_T f;
19418
19419 rettv->v_type = VAR_FLOAT;
19420 if (get_float_arg(argvars, &f) == OK)
19421 rettv->vval.v_float = tan(f);
19422 else
19423 rettv->vval.v_float = 0.0;
19424}
19425
19426/*
19427 * "tanh()" function
19428 */
19429 static void
19430f_tanh(argvars, rettv)
19431 typval_T *argvars;
19432 typval_T *rettv;
19433{
19434 float_T f;
19435
19436 rettv->v_type = VAR_FLOAT;
19437 if (get_float_arg(argvars, &f) == OK)
19438 rettv->vval.v_float = tanh(f);
19439 else
19440 rettv->vval.v_float = 0.0;
19441}
19442#endif
19443
Bram Moolenaard52d9742005-08-21 22:20:28 +000019444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 * "tolower(string)" function
19446 */
19447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019448f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019449 typval_T *argvars;
19450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451{
19452 char_u *p;
19453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019454 p = vim_strsave(get_tv_string(&argvars[0]));
19455 rettv->v_type = VAR_STRING;
19456 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457
19458 if (p != NULL)
19459 while (*p != NUL)
19460 {
19461#ifdef FEAT_MBYTE
19462 int l;
19463
19464 if (enc_utf8)
19465 {
19466 int c, lc;
19467
19468 c = utf_ptr2char(p);
19469 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019470 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 /* TODO: reallocate string when byte count changes. */
19472 if (utf_char2len(lc) == l)
19473 utf_char2bytes(lc, p);
19474 p += l;
19475 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019476 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 p += l; /* skip multi-byte character */
19478 else
19479#endif
19480 {
19481 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19482 ++p;
19483 }
19484 }
19485}
19486
19487/*
19488 * "toupper(string)" function
19489 */
19490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019491f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019492 typval_T *argvars;
19493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019495 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019496 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497}
19498
19499/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019500 * "tr(string, fromstr, tostr)" function
19501 */
19502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019503f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 typval_T *argvars;
19505 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019506{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019507 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019508 char_u *fromstr;
19509 char_u *tostr;
19510 char_u *p;
19511#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019512 int inlen;
19513 int fromlen;
19514 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019515 int idx;
19516 char_u *cpstr;
19517 int cplen;
19518 int first = TRUE;
19519#endif
19520 char_u buf[NUMBUFLEN];
19521 char_u buf2[NUMBUFLEN];
19522 garray_T ga;
19523
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019524 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019525 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19526 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019527
19528 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019529 rettv->v_type = VAR_STRING;
19530 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019531 if (fromstr == NULL || tostr == NULL)
19532 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019533 ga_init2(&ga, (int)sizeof(char), 80);
19534
19535#ifdef FEAT_MBYTE
19536 if (!has_mbyte)
19537#endif
19538 /* not multi-byte: fromstr and tostr must be the same length */
19539 if (STRLEN(fromstr) != STRLEN(tostr))
19540 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019541#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019542error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019543#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019544 EMSG2(_(e_invarg2), fromstr);
19545 ga_clear(&ga);
19546 return;
19547 }
19548
19549 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019550 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019551 {
19552#ifdef FEAT_MBYTE
19553 if (has_mbyte)
19554 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019555 inlen = (*mb_ptr2len)(in_str);
19556 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019557 cplen = inlen;
19558 idx = 0;
19559 for (p = fromstr; *p != NUL; p += fromlen)
19560 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019561 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019562 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019563 {
19564 for (p = tostr; *p != NUL; p += tolen)
19565 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019566 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019567 if (idx-- == 0)
19568 {
19569 cplen = tolen;
19570 cpstr = p;
19571 break;
19572 }
19573 }
19574 if (*p == NUL) /* tostr is shorter than fromstr */
19575 goto error;
19576 break;
19577 }
19578 ++idx;
19579 }
19580
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019581 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019582 {
19583 /* Check that fromstr and tostr have the same number of
19584 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019585 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019586 first = FALSE;
19587 for (p = tostr; *p != NUL; p += tolen)
19588 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019589 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019590 --idx;
19591 }
19592 if (idx != 0)
19593 goto error;
19594 }
19595
Bram Moolenaarcde88542015-08-11 19:14:00 +020019596 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019597 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019598 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019599
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019600 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019601 }
19602 else
19603#endif
19604 {
19605 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019606 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019607 if (p != NULL)
19608 ga_append(&ga, tostr[p - fromstr]);
19609 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019610 ga_append(&ga, *in_str);
19611 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019612 }
19613 }
19614
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019615 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019616 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019617 ga_append(&ga, NUL);
19618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019619 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019620}
19621
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019622#ifdef FEAT_FLOAT
19623/*
19624 * "trunc({float})" function
19625 */
19626 static void
19627f_trunc(argvars, rettv)
19628 typval_T *argvars;
19629 typval_T *rettv;
19630{
19631 float_T f;
19632
19633 rettv->v_type = VAR_FLOAT;
19634 if (get_float_arg(argvars, &f) == OK)
19635 /* trunc() is not in C90, use floor() or ceil() instead. */
19636 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19637 else
19638 rettv->vval.v_float = 0.0;
19639}
19640#endif
19641
Bram Moolenaar8299df92004-07-10 09:47:34 +000019642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 * "type(expr)" function
19644 */
19645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 typval_T *argvars;
19648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019650 int n;
19651
19652 switch (argvars[0].v_type)
19653 {
19654 case VAR_NUMBER: n = 0; break;
19655 case VAR_STRING: n = 1; break;
19656 case VAR_FUNC: n = 2; break;
19657 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019658 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019659#ifdef FEAT_FLOAT
19660 case VAR_FLOAT: n = 5; break;
19661#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019662 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19663 }
19664 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665}
19666
19667/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019668 * "undofile(name)" function
19669 */
19670 static void
19671f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019672 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019673 typval_T *rettv;
19674{
19675 rettv->v_type = VAR_STRING;
19676#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019677 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019678 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019679
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019680 if (*fname == NUL)
19681 {
19682 /* If there is no file name there will be no undo file. */
19683 rettv->vval.v_string = NULL;
19684 }
19685 else
19686 {
19687 char_u *ffname = FullName_save(fname, FALSE);
19688
19689 if (ffname != NULL)
19690 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19691 vim_free(ffname);
19692 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019693 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019694#else
19695 rettv->vval.v_string = NULL;
19696#endif
19697}
19698
19699/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019700 * "undotree()" function
19701 */
19702 static void
19703f_undotree(argvars, rettv)
19704 typval_T *argvars UNUSED;
19705 typval_T *rettv;
19706{
19707 if (rettv_dict_alloc(rettv) == OK)
19708 {
19709 dict_T *dict = rettv->vval.v_dict;
19710 list_T *list;
19711
Bram Moolenaar730cde92010-06-27 05:18:54 +020019712 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019713 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019714 dict_add_nr_str(dict, "save_last",
19715 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019716 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19717 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019718 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019719
19720 list = list_alloc();
19721 if (list != NULL)
19722 {
19723 u_eval_tree(curbuf->b_u_oldhead, list);
19724 dict_add_list(dict, "entries", list);
19725 }
19726 }
19727}
19728
19729/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019730 * "values(dict)" function
19731 */
19732 static void
19733f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 typval_T *argvars;
19735 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019736{
19737 dict_list(argvars, rettv, 1);
19738}
19739
19740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 * "virtcol(string)" function
19742 */
19743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 typval_T *argvars;
19746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747{
19748 colnr_T vcol = 0;
19749 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019750 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019752 fp = var2fpos(&argvars[0], FALSE, &fnum);
19753 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19754 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 {
19756 getvvcol(curwin, fp, NULL, NULL, &vcol);
19757 ++vcol;
19758 }
19759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761}
19762
19763/*
19764 * "visualmode()" function
19765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019768 typval_T *argvars UNUSED;
19769 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 char_u str[2];
19772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 str[0] = curbuf->b_visual_mode_eval;
19775 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777
19778 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019779 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781}
19782
19783/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019784 * "wildmenumode()" function
19785 */
19786 static void
19787f_wildmenumode(argvars, rettv)
19788 typval_T *argvars UNUSED;
19789 typval_T *rettv UNUSED;
19790{
19791#ifdef FEAT_WILDMENU
19792 if (wild_menu_showing)
19793 rettv->vval.v_number = 1;
19794#endif
19795}
19796
19797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 * "winbufnr(nr)" function
19799 */
19800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 typval_T *argvars;
19803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804{
19805 win_T *wp;
19806
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019807 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019811 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812}
19813
19814/*
19815 * "wincol()" function
19816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019818f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821{
19822 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824}
19825
19826/*
19827 * "winheight(nr)" function
19828 */
19829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019831 typval_T *argvars;
19832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833{
19834 win_T *wp;
19835
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019836 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841}
19842
19843/*
19844 * "winline()" function
19845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019848 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850{
19851 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853}
19854
19855/*
19856 * "winnr()" function
19857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019859f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019860 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862{
19863 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019864
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019866 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869}
19870
19871/*
19872 * "winrestcmd()" function
19873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019876 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878{
19879#ifdef FEAT_WINDOWS
19880 win_T *wp;
19881 int winnr = 1;
19882 garray_T ga;
19883 char_u buf[50];
19884
19885 ga_init2(&ga, (int)sizeof(char), 70);
19886 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19887 {
19888 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19889 ga_concat(&ga, buf);
19890# ifdef FEAT_VERTSPLIT
19891 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19892 ga_concat(&ga, buf);
19893# endif
19894 ++winnr;
19895 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019896 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019898 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019900 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019902 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903}
19904
19905/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019906 * "winrestview()" function
19907 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019908 static void
19909f_winrestview(argvars, rettv)
19910 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019911 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019912{
19913 dict_T *dict;
19914
19915 if (argvars[0].v_type != VAR_DICT
19916 || (dict = argvars[0].vval.v_dict) == NULL)
19917 EMSG(_(e_invarg));
19918 else
19919 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019920 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19921 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19922 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19923 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019924#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019925 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19926 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019927#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019928 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19929 {
19930 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19931 curwin->w_set_curswant = FALSE;
19932 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019933
Bram Moolenaar82c25852014-05-28 16:47:16 +020019934 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19935 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019936#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019937 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19938 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019939#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019940 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19941 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19942 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19943 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019944
19945 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019946 win_new_height(curwin, curwin->w_height);
19947# ifdef FEAT_VERTSPLIT
19948 win_new_width(curwin, W_WIDTH(curwin));
19949# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019950 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019951
Bram Moolenaarb851a962014-10-31 15:45:52 +010019952 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019953 curwin->w_topline = 1;
19954 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19955 curwin->w_topline = curbuf->b_ml.ml_line_count;
19956#ifdef FEAT_DIFF
19957 check_topfill(curwin, TRUE);
19958#endif
19959 }
19960}
19961
19962/*
19963 * "winsaveview()" function
19964 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019965 static void
19966f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019967 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019968 typval_T *rettv;
19969{
19970 dict_T *dict;
19971
Bram Moolenaara800b422010-06-27 01:15:55 +020019972 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019973 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019974 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019975
19976 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19977 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19978#ifdef FEAT_VIRTUALEDIT
19979 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19980#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019981 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019982 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19983
19984 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19985#ifdef FEAT_DIFF
19986 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19987#endif
19988 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19989 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19990}
19991
19992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 * "winwidth(nr)" function
19994 */
19995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 typval_T *argvars;
19998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999{
20000 win_T *wp;
20001
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020002 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 else
20006#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020009 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010#endif
20011}
20012
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020014 * Write list of strings to file
20015 */
20016 static int
20017write_list(fd, list, binary)
20018 FILE *fd;
20019 list_T *list;
20020 int binary;
20021{
20022 listitem_T *li;
20023 int c;
20024 int ret = OK;
20025 char_u *s;
20026
20027 for (li = list->lv_first; li != NULL; li = li->li_next)
20028 {
20029 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20030 {
20031 if (*s == '\n')
20032 c = putc(NUL, fd);
20033 else
20034 c = putc(*s, fd);
20035 if (c == EOF)
20036 {
20037 ret = FAIL;
20038 break;
20039 }
20040 }
20041 if (!binary || li->li_next != NULL)
20042 if (putc('\n', fd) == EOF)
20043 {
20044 ret = FAIL;
20045 break;
20046 }
20047 if (ret == FAIL)
20048 {
20049 EMSG(_(e_write));
20050 break;
20051 }
20052 }
20053 return ret;
20054}
20055
20056/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020057 * "writefile()" function
20058 */
20059 static void
20060f_writefile(argvars, rettv)
20061 typval_T *argvars;
20062 typval_T *rettv;
20063{
20064 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020065 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020066 char_u *fname;
20067 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020068 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020069
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020070 if (check_restricted() || check_secure())
20071 return;
20072
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020073 if (argvars[0].v_type != VAR_LIST)
20074 {
20075 EMSG2(_(e_listarg), "writefile()");
20076 return;
20077 }
20078 if (argvars[0].vval.v_list == NULL)
20079 return;
20080
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020081 if (argvars[2].v_type != VAR_UNKNOWN)
20082 {
20083 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20084 binary = TRUE;
20085 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20086 append = TRUE;
20087 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020088
20089 /* Always open the file in binary mode, library functions have a mind of
20090 * their own about CR-LF conversion. */
20091 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020092 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20093 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020094 {
20095 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20096 ret = -1;
20097 }
20098 else
20099 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020100 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20101 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020102 fclose(fd);
20103 }
20104
20105 rettv->vval.v_number = ret;
20106}
20107
20108/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020109 * "xor(expr, expr)" function
20110 */
20111 static void
20112f_xor(argvars, rettv)
20113 typval_T *argvars;
20114 typval_T *rettv;
20115{
20116 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20117 ^ get_tv_number_chk(&argvars[1], NULL);
20118}
20119
20120
20121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020123 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 */
20125 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020126var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020128 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020129 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020131 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020133 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134
Bram Moolenaara5525202006-03-02 22:52:09 +000020135 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020136 if (varp->v_type == VAR_LIST)
20137 {
20138 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020139 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020140 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020141 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020142
20143 l = varp->vval.v_list;
20144 if (l == NULL)
20145 return NULL;
20146
20147 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020148 pos.lnum = list_find_nr(l, 0L, &error);
20149 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020150 return NULL; /* invalid line number */
20151
20152 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020153 pos.col = list_find_nr(l, 1L, &error);
20154 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020155 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020156 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020157
20158 /* We accept "$" for the column number: last column. */
20159 li = list_find(l, 1L);
20160 if (li != NULL && li->li_tv.v_type == VAR_STRING
20161 && li->li_tv.vval.v_string != NULL
20162 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20163 pos.col = len + 1;
20164
Bram Moolenaara5525202006-03-02 22:52:09 +000020165 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020166 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020167 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020168 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020169
Bram Moolenaara5525202006-03-02 22:52:09 +000020170#ifdef FEAT_VIRTUALEDIT
20171 /* Get the virtual offset. Defaults to zero. */
20172 pos.coladd = list_find_nr(l, 2L, &error);
20173 if (error)
20174 pos.coladd = 0;
20175#endif
20176
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020177 return &pos;
20178 }
20179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020180 name = get_tv_string_chk(varp);
20181 if (name == NULL)
20182 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020183 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020185 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20186 {
20187 if (VIsual_active)
20188 return &VIsual;
20189 return &curwin->w_cursor;
20190 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020191 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020193 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20195 return NULL;
20196 return pp;
20197 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020198
20199#ifdef FEAT_VIRTUALEDIT
20200 pos.coladd = 0;
20201#endif
20202
Bram Moolenaar477933c2007-07-17 14:32:23 +000020203 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020204 {
20205 pos.col = 0;
20206 if (name[1] == '0') /* "w0": first visible line */
20207 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020208 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020209 pos.lnum = curwin->w_topline;
20210 return &pos;
20211 }
20212 else if (name[1] == '$') /* "w$": last visible line */
20213 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020214 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020215 pos.lnum = curwin->w_botline - 1;
20216 return &pos;
20217 }
20218 }
20219 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020221 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 {
20223 pos.lnum = curbuf->b_ml.ml_line_count;
20224 pos.col = 0;
20225 }
20226 else
20227 {
20228 pos.lnum = curwin->w_cursor.lnum;
20229 pos.col = (colnr_T)STRLEN(ml_get_curline());
20230 }
20231 return &pos;
20232 }
20233 return NULL;
20234}
20235
20236/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020237 * Convert list in "arg" into a position and optional file number.
20238 * When "fnump" is NULL there is no file number, only 3 items.
20239 * Note that the column is passed on as-is, the caller may want to decrement
20240 * it to use 1 for the first column.
20241 * Return FAIL when conversion is not possible, doesn't check the position for
20242 * validity.
20243 */
20244 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020245list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020246 typval_T *arg;
20247 pos_T *posp;
20248 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020249 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020250{
20251 list_T *l = arg->vval.v_list;
20252 long i = 0;
20253 long n;
20254
Bram Moolenaar493c1782014-05-28 14:34:46 +020020255 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20256 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020257 if (arg->v_type != VAR_LIST
20258 || l == NULL
20259 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020260 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020261 return FAIL;
20262
20263 if (fnump != NULL)
20264 {
20265 n = list_find_nr(l, i++, NULL); /* fnum */
20266 if (n < 0)
20267 return FAIL;
20268 if (n == 0)
20269 n = curbuf->b_fnum; /* current buffer */
20270 *fnump = n;
20271 }
20272
20273 n = list_find_nr(l, i++, NULL); /* lnum */
20274 if (n < 0)
20275 return FAIL;
20276 posp->lnum = n;
20277
20278 n = list_find_nr(l, i++, NULL); /* col */
20279 if (n < 0)
20280 return FAIL;
20281 posp->col = n;
20282
20283#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020284 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020285 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020286 posp->coladd = 0;
20287 else
20288 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020289#endif
20290
Bram Moolenaar493c1782014-05-28 14:34:46 +020020291 if (curswantp != NULL)
20292 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20293
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020294 return OK;
20295}
20296
20297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 * Get the length of an environment variable name.
20299 * Advance "arg" to the first character after the name.
20300 * Return 0 for error.
20301 */
20302 static int
20303get_env_len(arg)
20304 char_u **arg;
20305{
20306 char_u *p;
20307 int len;
20308
20309 for (p = *arg; vim_isIDc(*p); ++p)
20310 ;
20311 if (p == *arg) /* no name found */
20312 return 0;
20313
20314 len = (int)(p - *arg);
20315 *arg = p;
20316 return len;
20317}
20318
20319/*
20320 * Get the length of the name of a function or internal variable.
20321 * "arg" is advanced to the first non-white character after the name.
20322 * Return 0 if something is wrong.
20323 */
20324 static int
20325get_id_len(arg)
20326 char_u **arg;
20327{
20328 char_u *p;
20329 int len;
20330
20331 /* Find the end of the name. */
20332 for (p = *arg; eval_isnamec(*p); ++p)
20333 ;
20334 if (p == *arg) /* no name found */
20335 return 0;
20336
20337 len = (int)(p - *arg);
20338 *arg = skipwhite(p);
20339
20340 return len;
20341}
20342
20343/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020344 * Get the length of the name of a variable or function.
20345 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020347 * Return -1 if curly braces expansion failed.
20348 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 * If the name contains 'magic' {}'s, expand them and return the
20350 * expanded name in an allocated string via 'alias' - caller must free.
20351 */
20352 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020353get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 char_u **arg;
20355 char_u **alias;
20356 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020357 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358{
20359 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 char_u *p;
20361 char_u *expr_start;
20362 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
20364 *alias = NULL; /* default to no alias */
20365
20366 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20367 && (*arg)[2] == (int)KE_SNR)
20368 {
20369 /* hard coded <SNR>, already translated */
20370 *arg += 3;
20371 return get_id_len(arg) + 3;
20372 }
20373 len = eval_fname_script(*arg);
20374 if (len > 0)
20375 {
20376 /* literal "<SID>", "s:" or "<SNR>" */
20377 *arg += len;
20378 }
20379
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020383 p = find_name_end(*arg, &expr_start, &expr_end,
20384 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 if (expr_start != NULL)
20386 {
20387 char_u *temp_string;
20388
20389 if (!evaluate)
20390 {
20391 len += (int)(p - *arg);
20392 *arg = skipwhite(p);
20393 return len;
20394 }
20395
20396 /*
20397 * Include any <SID> etc in the expanded string:
20398 * Thus the -len here.
20399 */
20400 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20401 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020402 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 *alias = temp_string;
20404 *arg = skipwhite(p);
20405 return (int)STRLEN(temp_string);
20406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407
20408 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020409 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 EMSG2(_(e_invexpr2), *arg);
20411
20412 return len;
20413}
20414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020415/*
20416 * Find the end of a variable or function name, taking care of magic braces.
20417 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20418 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020419 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020420 * Return a pointer to just after the name. Equal to "arg" if there is no
20421 * valid name.
20422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020424find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 char_u *arg;
20426 char_u **expr_start;
20427 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020428 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020430 int mb_nest = 0;
20431 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 char_u *p;
20433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020436 *expr_start = NULL;
20437 *expr_end = NULL;
20438 }
20439
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020440 /* Quick check for valid starting character. */
20441 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20442 return arg;
20443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 for (p = arg; *p != NUL
20445 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020446 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020447 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020448 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020449 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020451 if (*p == '\'')
20452 {
20453 /* skip over 'string' to avoid counting [ and ] inside it. */
20454 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20455 ;
20456 if (*p == NUL)
20457 break;
20458 }
20459 else if (*p == '"')
20460 {
20461 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20462 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20463 if (*p == '\\' && p[1] != NUL)
20464 ++p;
20465 if (*p == NUL)
20466 break;
20467 }
20468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020469 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020471 if (*p == '[')
20472 ++br_nest;
20473 else if (*p == ']')
20474 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020477 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020479 if (*p == '{')
20480 {
20481 mb_nest++;
20482 if (expr_start != NULL && *expr_start == NULL)
20483 *expr_start = p;
20484 }
20485 else if (*p == '}')
20486 {
20487 mb_nest--;
20488 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20489 *expr_end = p;
20490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 }
20493
20494 return p;
20495}
20496
20497/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020498 * Expands out the 'magic' {}'s in a variable/function name.
20499 * Note that this can call itself recursively, to deal with
20500 * constructs like foo{bar}{baz}{bam}
20501 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20502 * "in_start" ^
20503 * "expr_start" ^
20504 * "expr_end" ^
20505 * "in_end" ^
20506 *
20507 * Returns a new allocated string, which the caller must free.
20508 * Returns NULL for failure.
20509 */
20510 static char_u *
20511make_expanded_name(in_start, expr_start, expr_end, in_end)
20512 char_u *in_start;
20513 char_u *expr_start;
20514 char_u *expr_end;
20515 char_u *in_end;
20516{
20517 char_u c1;
20518 char_u *retval = NULL;
20519 char_u *temp_result;
20520 char_u *nextcmd = NULL;
20521
20522 if (expr_end == NULL || in_end == NULL)
20523 return NULL;
20524 *expr_start = NUL;
20525 *expr_end = NUL;
20526 c1 = *in_end;
20527 *in_end = NUL;
20528
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020529 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020530 if (temp_result != NULL && nextcmd == NULL)
20531 {
20532 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20533 + (in_end - expr_end) + 1));
20534 if (retval != NULL)
20535 {
20536 STRCPY(retval, in_start);
20537 STRCAT(retval, temp_result);
20538 STRCAT(retval, expr_end + 1);
20539 }
20540 }
20541 vim_free(temp_result);
20542
20543 *in_end = c1; /* put char back for error messages */
20544 *expr_start = '{';
20545 *expr_end = '}';
20546
20547 if (retval != NULL)
20548 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020549 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020550 if (expr_start != NULL)
20551 {
20552 /* Further expansion! */
20553 temp_result = make_expanded_name(retval, expr_start,
20554 expr_end, temp_result);
20555 vim_free(retval);
20556 retval = temp_result;
20557 }
20558 }
20559
20560 return retval;
20561}
20562
20563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020565 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 */
20567 static int
20568eval_isnamec(c)
20569 int c;
20570{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020571 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20572}
20573
20574/*
20575 * Return TRUE if character "c" can be used as the first character in a
20576 * variable or function name (excluding '{' and '}').
20577 */
20578 static int
20579eval_isnamec1(c)
20580 int c;
20581{
20582 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583}
20584
20585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 * Set number v: variable to "val".
20587 */
20588 void
20589set_vim_var_nr(idx, val)
20590 int idx;
20591 long val;
20592{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020593 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594}
20595
20596/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020597 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 */
20599 long
20600get_vim_var_nr(idx)
20601 int idx;
20602{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020603 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604}
20605
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020606/*
20607 * Get string v: variable value. Uses a static buffer, can only be used once.
20608 */
20609 char_u *
20610get_vim_var_str(idx)
20611 int idx;
20612{
20613 return get_tv_string(&vimvars[idx].vv_tv);
20614}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020615
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020617 * Get List v: variable value. Caller must take care of reference count when
20618 * needed.
20619 */
20620 list_T *
20621get_vim_var_list(idx)
20622 int idx;
20623{
20624 return vimvars[idx].vv_list;
20625}
20626
20627/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020628 * Set v:char to character "c".
20629 */
20630 void
20631set_vim_var_char(c)
20632 int c;
20633{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020634 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020635
20636#ifdef FEAT_MBYTE
20637 if (has_mbyte)
20638 buf[(*mb_char2bytes)(c, buf)] = NUL;
20639 else
20640#endif
20641 {
20642 buf[0] = c;
20643 buf[1] = NUL;
20644 }
20645 set_vim_var_string(VV_CHAR, buf, -1);
20646}
20647
20648/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020649 * Set v:count to "count" and v:count1 to "count1".
20650 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 */
20652 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020653set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 long count;
20655 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020656 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020658 if (set_prevcount)
20659 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020660 vimvars[VV_COUNT].vv_nr = count;
20661 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662}
20663
20664/*
20665 * Set string v: variable to a copy of "val".
20666 */
20667 void
20668set_vim_var_string(idx, val, len)
20669 int idx;
20670 char_u *val;
20671 int len; /* length of "val" to use or -1 (whole string) */
20672{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020673 /* Need to do this (at least) once, since we can't initialize a union.
20674 * Will always be invoked when "v:progname" is set. */
20675 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20676
Bram Moolenaare9a41262005-01-15 22:18:47 +000020677 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020679 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020681 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020683 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684}
20685
20686/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020687 * Set List v: variable to "val".
20688 */
20689 void
20690set_vim_var_list(idx, val)
20691 int idx;
20692 list_T *val;
20693{
20694 list_unref(vimvars[idx].vv_list);
20695 vimvars[idx].vv_list = val;
20696 if (val != NULL)
20697 ++val->lv_refcount;
20698}
20699
20700/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020701 * Set Dictionary v: variable to "val".
20702 */
20703 void
20704set_vim_var_dict(idx, val)
20705 int idx;
20706 dict_T *val;
20707{
20708 int todo;
20709 hashitem_T *hi;
20710
20711 dict_unref(vimvars[idx].vv_dict);
20712 vimvars[idx].vv_dict = val;
20713 if (val != NULL)
20714 {
20715 ++val->dv_refcount;
20716
20717 /* Set readonly */
20718 todo = (int)val->dv_hashtab.ht_used;
20719 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20720 {
20721 if (HASHITEM_EMPTY(hi))
20722 continue;
20723 --todo;
20724 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20725 }
20726 }
20727}
20728
20729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 * Set v:register if needed.
20731 */
20732 void
20733set_reg_var(c)
20734 int c;
20735{
20736 char_u regname;
20737
20738 if (c == 0 || c == ' ')
20739 regname = '"';
20740 else
20741 regname = c;
20742 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020743 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 set_vim_var_string(VV_REG, &regname, 1);
20745}
20746
20747/*
20748 * Get or set v:exception. If "oldval" == NULL, return the current value.
20749 * Otherwise, restore the value to "oldval" and return NULL.
20750 * Must always be called in pairs to save and restore v:exception! Does not
20751 * take care of memory allocations.
20752 */
20753 char_u *
20754v_exception(oldval)
20755 char_u *oldval;
20756{
20757 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020758 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759
Bram Moolenaare9a41262005-01-15 22:18:47 +000020760 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 return NULL;
20762}
20763
20764/*
20765 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20766 * Otherwise, restore the value to "oldval" and return NULL.
20767 * Must always be called in pairs to save and restore v:throwpoint! Does not
20768 * take care of memory allocations.
20769 */
20770 char_u *
20771v_throwpoint(oldval)
20772 char_u *oldval;
20773{
20774 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020775 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776
Bram Moolenaare9a41262005-01-15 22:18:47 +000020777 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 return NULL;
20779}
20780
20781#if defined(FEAT_AUTOCMD) || defined(PROTO)
20782/*
20783 * Set v:cmdarg.
20784 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20785 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20786 * Must always be called in pairs!
20787 */
20788 char_u *
20789set_cmdarg(eap, oldarg)
20790 exarg_T *eap;
20791 char_u *oldarg;
20792{
20793 char_u *oldval;
20794 char_u *newval;
20795 unsigned len;
20796
Bram Moolenaare9a41262005-01-15 22:18:47 +000020797 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020798 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020800 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020801 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020802 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 }
20804
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020805 if (eap->force_bin == FORCE_BIN)
20806 len = 6;
20807 else if (eap->force_bin == FORCE_NOBIN)
20808 len = 8;
20809 else
20810 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020811
20812 if (eap->read_edit)
20813 len += 7;
20814
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020815 if (eap->force_ff != 0)
20816 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20817# ifdef FEAT_MBYTE
20818 if (eap->force_enc != 0)
20819 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020820 if (eap->bad_char != 0)
20821 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020822# endif
20823
20824 newval = alloc(len + 1);
20825 if (newval == NULL)
20826 return NULL;
20827
20828 if (eap->force_bin == FORCE_BIN)
20829 sprintf((char *)newval, " ++bin");
20830 else if (eap->force_bin == FORCE_NOBIN)
20831 sprintf((char *)newval, " ++nobin");
20832 else
20833 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020834
20835 if (eap->read_edit)
20836 STRCAT(newval, " ++edit");
20837
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020838 if (eap->force_ff != 0)
20839 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20840 eap->cmd + eap->force_ff);
20841# ifdef FEAT_MBYTE
20842 if (eap->force_enc != 0)
20843 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20844 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020845 if (eap->bad_char == BAD_KEEP)
20846 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20847 else if (eap->bad_char == BAD_DROP)
20848 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20849 else if (eap->bad_char != 0)
20850 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020851# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020852 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020853 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854}
20855#endif
20856
20857/*
20858 * Get the value of internal variable "name".
20859 * Return OK or FAIL.
20860 */
20861 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020862get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 char_u *name;
20864 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020865 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020866 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020867 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020868 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869{
20870 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020871 typval_T *tv = NULL;
20872 typval_T atv;
20873 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875
20876 /* truncate the name, so that we can use strcmp() */
20877 cc = name[len];
20878 name[len] = NUL;
20879
20880 /*
20881 * Check for "b:changedtick".
20882 */
20883 if (STRCMP(name, "b:changedtick") == 0)
20884 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020885 atv.v_type = VAR_NUMBER;
20886 atv.vval.v_number = curbuf->b_changedtick;
20887 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 }
20889
20890 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 * Check for user-defined variables.
20892 */
20893 else
20894 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020895 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020897 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020898 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020899 if (dip != NULL)
20900 *dip = v;
20901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 }
20903
Bram Moolenaare9a41262005-01-15 22:18:47 +000020904 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020906 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 ret = FAIL;
20909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020911 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912
20913 name[len] = cc;
20914
20915 return ret;
20916}
20917
20918/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020919 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20920 * Also handle function call with Funcref variable: func(expr)
20921 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20922 */
20923 static int
20924handle_subscript(arg, rettv, evaluate, verbose)
20925 char_u **arg;
20926 typval_T *rettv;
20927 int evaluate; /* do more than finding the end */
20928 int verbose; /* give error messages */
20929{
20930 int ret = OK;
20931 dict_T *selfdict = NULL;
20932 char_u *s;
20933 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020934 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020935
20936 while (ret == OK
20937 && (**arg == '['
20938 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020939 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020940 && !vim_iswhite(*(*arg - 1)))
20941 {
20942 if (**arg == '(')
20943 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020944 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020945 if (evaluate)
20946 {
20947 functv = *rettv;
20948 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020949
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020950 /* Invoke the function. Recursive! */
20951 s = functv.vval.v_string;
20952 }
20953 else
20954 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020955 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020956 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20957 &len, evaluate, selfdict);
20958
20959 /* Clear the funcref afterwards, so that deleting it while
20960 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020961 if (evaluate)
20962 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020963
20964 /* Stop the expression evaluation when immediately aborting on
20965 * error, or when an interrupt occurred or an exception was thrown
20966 * but not caught. */
20967 if (aborting())
20968 {
20969 if (ret == OK)
20970 clear_tv(rettv);
20971 ret = FAIL;
20972 }
20973 dict_unref(selfdict);
20974 selfdict = NULL;
20975 }
20976 else /* **arg == '[' || **arg == '.' */
20977 {
20978 dict_unref(selfdict);
20979 if (rettv->v_type == VAR_DICT)
20980 {
20981 selfdict = rettv->vval.v_dict;
20982 if (selfdict != NULL)
20983 ++selfdict->dv_refcount;
20984 }
20985 else
20986 selfdict = NULL;
20987 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20988 {
20989 clear_tv(rettv);
20990 ret = FAIL;
20991 }
20992 }
20993 }
20994 dict_unref(selfdict);
20995 return ret;
20996}
20997
20998/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020999 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021000 * value).
21001 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021003alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021004{
Bram Moolenaar33570922005-01-25 22:26:29 +000021005 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006}
21007
21008/*
21009 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 * The string "s" must have been allocated, it is consumed.
21011 * Return NULL for out of memory, the variable otherwise.
21012 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021014alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 char_u *s;
21016{
Bram Moolenaar33570922005-01-25 22:26:29 +000021017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021019 rettv = alloc_tv();
21020 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 rettv->v_type = VAR_STRING;
21023 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 }
21025 else
21026 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021027 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028}
21029
21030/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021031 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021033 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021035 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036{
21037 if (varp != NULL)
21038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021039 switch (varp->v_type)
21040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021041 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021042 func_unref(varp->vval.v_string);
21043 /*FALLTHROUGH*/
21044 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021045 vim_free(varp->vval.v_string);
21046 break;
21047 case VAR_LIST:
21048 list_unref(varp->vval.v_list);
21049 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 case VAR_DICT:
21051 dict_unref(varp->vval.v_dict);
21052 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021053 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021054#ifdef FEAT_FLOAT
21055 case VAR_FLOAT:
21056#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021057 case VAR_UNKNOWN:
21058 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021059 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021060 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021061 break;
21062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 vim_free(varp);
21064 }
21065}
21066
21067/*
21068 * Free the memory for a variable value and set the value to NULL or 0.
21069 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021070 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021071clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021072 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073{
21074 if (varp != NULL)
21075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021078 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021079 func_unref(varp->vval.v_string);
21080 /*FALLTHROUGH*/
21081 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021082 vim_free(varp->vval.v_string);
21083 varp->vval.v_string = NULL;
21084 break;
21085 case VAR_LIST:
21086 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021087 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021088 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021089 case VAR_DICT:
21090 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021091 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021092 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021093 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021094 varp->vval.v_number = 0;
21095 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021096#ifdef FEAT_FLOAT
21097 case VAR_FLOAT:
21098 varp->vval.v_float = 0.0;
21099 break;
21100#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 case VAR_UNKNOWN:
21102 break;
21103 default:
21104 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021106 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 }
21108}
21109
21110/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111 * Set the value of a variable to NULL without freeing items.
21112 */
21113 static void
21114init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021115 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021116{
21117 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021118 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021119}
21120
21121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 * Get the number value of a variable.
21123 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021124 * For incompatible types, return 0.
21125 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21126 * caller of incompatible types: it sets *denote to TRUE if "denote"
21127 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 */
21129 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021130get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021133 int error = FALSE;
21134
21135 return get_tv_number_chk(varp, &error); /* return 0L on error */
21136}
21137
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021138 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021139get_tv_number_chk(varp, denote)
21140 typval_T *varp;
21141 int *denote;
21142{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021143 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021145 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021147 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021148 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021149#ifdef FEAT_FLOAT
21150 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021151 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021152 break;
21153#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021154 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021155 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021156 break;
21157 case VAR_STRING:
21158 if (varp->vval.v_string != NULL)
21159 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021160 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021161 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021162 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021163 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021164 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021165 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021166 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021167 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021168 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021169 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021172 if (denote == NULL) /* useful for values that must be unsigned */
21173 n = -1;
21174 else
21175 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021176 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177}
21178
21179/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021180 * Get the lnum from the first argument.
21181 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021182 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 */
21184 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021186 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187{
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 linenr_T lnum;
21190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021191 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 if (lnum == 0) /* no valid number, try using line() */
21193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021194 rettv.v_type = VAR_NUMBER;
21195 f_line(argvars, &rettv);
21196 lnum = rettv.vval.v_number;
21197 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 }
21199 return lnum;
21200}
21201
21202/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021203 * Get the lnum from the first argument.
21204 * Also accepts "$", then "buf" is used.
21205 * Returns 0 on error.
21206 */
21207 static linenr_T
21208get_tv_lnum_buf(argvars, buf)
21209 typval_T *argvars;
21210 buf_T *buf;
21211{
21212 if (argvars[0].v_type == VAR_STRING
21213 && argvars[0].vval.v_string != NULL
21214 && argvars[0].vval.v_string[0] == '$'
21215 && buf != NULL)
21216 return buf->b_ml.ml_line_count;
21217 return get_tv_number_chk(&argvars[0], NULL);
21218}
21219
21220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 * Get the string value of a variable.
21222 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021223 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21224 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 * If the String variable has never been set, return an empty string.
21226 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021227 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21228 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 */
21230 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021232 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233{
21234 static char_u mybuf[NUMBUFLEN];
21235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021236 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237}
21238
21239 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021240get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021241 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021242 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021244 char_u *res = get_tv_string_buf_chk(varp, buf);
21245
21246 return res != NULL ? res : (char_u *)"";
21247}
21248
Bram Moolenaar7d647822014-04-05 21:28:56 +020021249/*
21250 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21251 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021252 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021253get_tv_string_chk(varp)
21254 typval_T *varp;
21255{
21256 static char_u mybuf[NUMBUFLEN];
21257
21258 return get_tv_string_buf_chk(varp, mybuf);
21259}
21260
21261 static char_u *
21262get_tv_string_buf_chk(varp, buf)
21263 typval_T *varp;
21264 char_u *buf;
21265{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021266 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 case VAR_NUMBER:
21269 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21270 return buf;
21271 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021272 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021273 break;
21274 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021275 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021276 break;
21277 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021278 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021279 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021280#ifdef FEAT_FLOAT
21281 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021282 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021283 break;
21284#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021285 case VAR_STRING:
21286 if (varp->vval.v_string != NULL)
21287 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021288 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021290 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021293 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294}
21295
21296/*
21297 * Find variable "name" in the list of variables.
21298 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021299 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021300 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021301 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021304find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021306 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021307 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311
Bram Moolenaara7043832005-01-21 11:56:39 +000021312 ht = find_var_ht(name, &varname);
21313 if (htp != NULL)
21314 *htp = ht;
21315 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021317 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318}
21319
21320/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021321 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021322 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021324 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021325find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021327 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021328 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021329 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021330{
Bram Moolenaar33570922005-01-25 22:26:29 +000021331 hashitem_T *hi;
21332
21333 if (*varname == NUL)
21334 {
21335 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021336 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021337 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021338 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021339 case 'g': return &globvars_var;
21340 case 'v': return &vimvars_var;
21341 case 'b': return &curbuf->b_bufvar;
21342 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021343#ifdef FEAT_WINDOWS
21344 case 't': return &curtab->tp_winvar;
21345#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021346 case 'l': return current_funccal == NULL
21347 ? NULL : &current_funccal->l_vars_var;
21348 case 'a': return current_funccal == NULL
21349 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021350 }
21351 return NULL;
21352 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021353
21354 hi = hash_find(ht, varname);
21355 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021356 {
21357 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021358 * worked find the variable again. Don't auto-load a script if it was
21359 * loaded already, otherwise it would be loaded every time when
21360 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021361 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021362 {
21363 /* Note: script_autoload() may make "hi" invalid. It must either
21364 * be obtained again or not used. */
21365 if (!script_autoload(varname, FALSE) || aborting())
21366 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021367 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021368 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021369 if (HASHITEM_EMPTY(hi))
21370 return NULL;
21371 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021372 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021373}
21374
21375/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021376 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021377 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021378 * Set "varname" to the start of name without ':'.
21379 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021381find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 char_u *name;
21383 char_u **varname;
21384{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021385 hashitem_T *hi;
21386
Bram Moolenaar73627d02015-08-11 15:46:09 +020021387 if (name[0] == NUL)
21388 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 if (name[1] != ':')
21390 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021391 /* The name must not start with a colon or #. */
21392 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 return NULL;
21394 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021395
21396 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021397 hi = hash_find(&compat_hashtab, name);
21398 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021399 return &compat_hashtab;
21400
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 return &globvarht; /* global variable */
21403 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 }
21405 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021406 if (*name == 'g') /* global variable */
21407 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021408 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21409 */
21410 if (vim_strchr(name + 2, ':') != NULL
21411 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021412 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021414 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021416 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021417#ifdef FEAT_WINDOWS
21418 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021419 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021420#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021421 if (*name == 'v') /* v: variable */
21422 return &vimvarht;
21423 if (*name == 'a' && current_funccal != NULL) /* function argument */
21424 return &current_funccal->l_avars.dv_hashtab;
21425 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21426 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 if (*name == 's' /* script variable */
21428 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21429 return &SCRIPT_VARS(current_SID);
21430 return NULL;
21431}
21432
21433/*
21434 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021435 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436 * Returns NULL when it doesn't exist.
21437 */
21438 char_u *
21439get_var_value(name)
21440 char_u *name;
21441{
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021444 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 if (v == NULL)
21446 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021447 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448}
21449
21450/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021451 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 * sourcing this script and when executing functions defined in the script.
21453 */
21454 void
21455new_script_vars(id)
21456 scid_T id;
21457{
Bram Moolenaara7043832005-01-21 11:56:39 +000021458 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 hashtab_T *ht;
21460 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021461
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21463 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021464 /* Re-allocating ga_data means that an ht_array pointing to
21465 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021467 for (i = 1; i <= ga_scripts.ga_len; ++i)
21468 {
21469 ht = &SCRIPT_VARS(i);
21470 if (ht->ht_mask == HT_INIT_SIZE - 1)
21471 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021472 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021474 }
21475
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 while (ga_scripts.ga_len < id)
21477 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021478 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021479 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021480 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 }
21483 }
21484}
21485
21486/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21488 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 */
21490 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021491init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021492 dict_T *dict;
21493 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021494 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495{
Bram Moolenaar33570922005-01-25 22:26:29 +000021496 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021497 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021498 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021499 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021500 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021501 dict_var->di_tv.vval.v_dict = dict;
21502 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021503 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021504 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21505 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506}
21507
21508/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021509 * Unreference a dictionary initialized by init_var_dict().
21510 */
21511 void
21512unref_var_dict(dict)
21513 dict_T *dict;
21514{
21515 /* Now the dict needs to be freed if no one else is using it, go back to
21516 * normal reference counting. */
21517 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21518 dict_unref(dict);
21519}
21520
21521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021523 * Frees all allocated variables and the value they contain.
21524 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 */
21526 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021527vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 hashtab_T *ht;
21529{
21530 vars_clear_ext(ht, TRUE);
21531}
21532
21533/*
21534 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21535 */
21536 static void
21537vars_clear_ext(ht, free_val)
21538 hashtab_T *ht;
21539 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
Bram Moolenaara7043832005-01-21 11:56:39 +000021541 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 hashitem_T *hi;
21543 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544
Bram Moolenaar33570922005-01-25 22:26:29 +000021545 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021546 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021547 for (hi = ht->ht_array; todo > 0; ++hi)
21548 {
21549 if (!HASHITEM_EMPTY(hi))
21550 {
21551 --todo;
21552
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021554 * ht_array might change then. hash_clear() takes care of it
21555 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 v = HI2DI(hi);
21557 if (free_val)
21558 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021559 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021561 }
21562 }
21563 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021564 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565}
21566
Bram Moolenaara7043832005-01-21 11:56:39 +000021567/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 * Delete a variable from hashtab "ht" at item "hi".
21569 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021572delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 hashtab_T *ht;
21574 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575{
Bram Moolenaar33570922005-01-25 22:26:29 +000021576 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021577
21578 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021579 clear_tv(&di->di_tv);
21580 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581}
21582
21583/*
21584 * List the value of one internal variable.
21585 */
21586 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021587list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021590 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592 char_u *tofree;
21593 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021594 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021595
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021596 current_copyID += COPYID_INC;
21597 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021598 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021599 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601}
21602
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021604list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 char_u *prefix;
21606 char_u *name;
21607 int type;
21608 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021609 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610{
Bram Moolenaar31859182007-08-14 20:41:13 +000021611 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21612 msg_start();
21613 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 if (name != NULL) /* "a:" vars don't have a name stored */
21615 msg_puts(name);
21616 msg_putchar(' ');
21617 msg_advance(22);
21618 if (type == VAR_NUMBER)
21619 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 else if (type == VAR_FUNC)
21621 msg_putchar('*');
21622 else if (type == VAR_LIST)
21623 {
21624 msg_putchar('[');
21625 if (*string == '[')
21626 ++string;
21627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021628 else if (type == VAR_DICT)
21629 {
21630 msg_putchar('{');
21631 if (*string == '{')
21632 ++string;
21633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 else
21635 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021636
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021638
21639 if (type == VAR_FUNC)
21640 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021641 if (*first)
21642 {
21643 msg_clr_eos();
21644 *first = FALSE;
21645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646}
21647
21648/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021649 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 * If the variable already exists, the value is updated.
21651 * Otherwise the variable is created.
21652 */
21653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021654set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021657 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658{
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021663 ht = find_var_ht(name, &varname);
21664 if (ht == NULL || *varname == NUL)
21665 {
21666 EMSG2(_(e_illvar), name);
21667 return;
21668 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021669 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021670
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021671 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21672 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021673
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021676 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021677 if (var_check_ro(v->di_flags, name, FALSE)
21678 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021679 return;
21680 if (v->di_tv.v_type != tv->v_type
21681 && !((v->di_tv.v_type == VAR_STRING
21682 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021683 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021684 || tv->v_type == VAR_NUMBER))
21685#ifdef FEAT_FLOAT
21686 && !((v->di_tv.v_type == VAR_NUMBER
21687 || v->di_tv.v_type == VAR_FLOAT)
21688 && (tv->v_type == VAR_NUMBER
21689 || tv->v_type == VAR_FLOAT))
21690#endif
21691 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021692 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021693 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021694 return;
21695 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021696
21697 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021698 * Handle setting internal v: variables separately where needed to
21699 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021700 */
21701 if (ht == &vimvarht)
21702 {
21703 if (v->di_tv.v_type == VAR_STRING)
21704 {
21705 vim_free(v->di_tv.vval.v_string);
21706 if (copy || tv->v_type != VAR_STRING)
21707 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21708 else
21709 {
21710 /* Take over the string to avoid an extra alloc/free. */
21711 v->di_tv.vval.v_string = tv->vval.v_string;
21712 tv->vval.v_string = NULL;
21713 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021714 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021716 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021718 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021719 if (STRCMP(varname, "searchforward") == 0)
21720 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021721#ifdef FEAT_SEARCH_EXTRA
21722 else if (STRCMP(varname, "hlsearch") == 0)
21723 {
21724 no_hlsearch = !v->di_tv.vval.v_number;
21725 redraw_all_later(SOME_VALID);
21726 }
21727#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021728 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021729 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021730 else if (v->di_tv.v_type != tv->v_type)
21731 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021732 }
21733
21734 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 }
21736 else /* add a new variable */
21737 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021738 /* Can't add "v:" variable. */
21739 if (ht == &vimvarht)
21740 {
21741 EMSG2(_(e_illvar), name);
21742 return;
21743 }
21744
Bram Moolenaar92124a32005-06-17 22:03:40 +000021745 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021746 if (!valid_varname(varname))
21747 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021749 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21750 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021751 if (v == NULL)
21752 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021756 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 return;
21758 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021759 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021762 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021763 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021764 else
21765 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021766 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021767 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770}
21771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021772/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021773 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 * Also give an error message.
21775 */
21776 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021777var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 int flags;
21779 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021780 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021781{
21782 if (flags & DI_FLAGS_RO)
21783 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021784 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021785 return TRUE;
21786 }
21787 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21788 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021789 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 return TRUE;
21791 }
21792 return FALSE;
21793}
21794
21795/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021796 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21797 * Also give an error message.
21798 */
21799 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021800var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021801 int flags;
21802 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021803 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021804{
21805 if (flags & DI_FLAGS_FIX)
21806 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021807 EMSG2(_("E795: Cannot delete variable %s"),
21808 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021809 return TRUE;
21810 }
21811 return FALSE;
21812}
21813
21814/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021815 * Check if a funcref is assigned to a valid variable name.
21816 * Return TRUE and give an error if not.
21817 */
21818 static int
21819var_check_func_name(name, new_var)
21820 char_u *name; /* points to start of variable name */
21821 int new_var; /* TRUE when creating the variable */
21822{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021823 /* Allow for w: b: s: and t:. */
21824 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021825 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21826 ? name[2] : name[0]))
21827 {
21828 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21829 name);
21830 return TRUE;
21831 }
21832 /* Don't allow hiding a function. When "v" is not NULL we might be
21833 * assigning another function to the same var, the type is checked
21834 * below. */
21835 if (new_var && function_exists(name))
21836 {
21837 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21838 name);
21839 return TRUE;
21840 }
21841 return FALSE;
21842}
21843
21844/*
21845 * Check if a variable name is valid.
21846 * Return FALSE and give an error if not.
21847 */
21848 static int
21849valid_varname(varname)
21850 char_u *varname;
21851{
21852 char_u *p;
21853
21854 for (p = varname; *p != NUL; ++p)
21855 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21856 && *p != AUTOLOAD_CHAR)
21857 {
21858 EMSG2(_(e_illvar), varname);
21859 return FALSE;
21860 }
21861 return TRUE;
21862}
21863
21864/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021865 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021866 * Also give an error message, using "name" or _("name") when use_gettext is
21867 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021868 */
21869 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021870tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021871 int lock;
21872 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021873 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021874{
21875 if (lock & VAR_LOCKED)
21876 {
21877 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021878 name == NULL ? (char_u *)_("Unknown")
21879 : use_gettext ? (char_u *)_(name)
21880 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021881 return TRUE;
21882 }
21883 if (lock & VAR_FIXED)
21884 {
21885 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021886 name == NULL ? (char_u *)_("Unknown")
21887 : use_gettext ? (char_u *)_(name)
21888 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021889 return TRUE;
21890 }
21891 return FALSE;
21892}
21893
21894/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021896 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021897 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021898 * It is OK for "from" and "to" to point to the same item. This is used to
21899 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021900 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021901 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021902copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 typval_T *from;
21904 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021906 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021907 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021908 switch (from->v_type)
21909 {
21910 case VAR_NUMBER:
21911 to->vval.v_number = from->vval.v_number;
21912 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021913#ifdef FEAT_FLOAT
21914 case VAR_FLOAT:
21915 to->vval.v_float = from->vval.v_float;
21916 break;
21917#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918 case VAR_STRING:
21919 case VAR_FUNC:
21920 if (from->vval.v_string == NULL)
21921 to->vval.v_string = NULL;
21922 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021923 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021924 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021925 if (from->v_type == VAR_FUNC)
21926 func_ref(to->vval.v_string);
21927 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928 break;
21929 case VAR_LIST:
21930 if (from->vval.v_list == NULL)
21931 to->vval.v_list = NULL;
21932 else
21933 {
21934 to->vval.v_list = from->vval.v_list;
21935 ++to->vval.v_list->lv_refcount;
21936 }
21937 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021938 case VAR_DICT:
21939 if (from->vval.v_dict == NULL)
21940 to->vval.v_dict = NULL;
21941 else
21942 {
21943 to->vval.v_dict = from->vval.v_dict;
21944 ++to->vval.v_dict->dv_refcount;
21945 }
21946 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021947 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021949 break;
21950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951}
21952
21953/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021954 * Make a copy of an item.
21955 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021956 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21957 * reference to an already copied list/dict can be used.
21958 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021960 static int
21961item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021962 typval_T *from;
21963 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021964 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021965 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966{
21967 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021968 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021969
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 {
21972 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021973 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021974 }
21975 ++recurse;
21976
21977 switch (from->v_type)
21978 {
21979 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021980#ifdef FEAT_FLOAT
21981 case VAR_FLOAT:
21982#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021983 case VAR_STRING:
21984 case VAR_FUNC:
21985 copy_tv(from, to);
21986 break;
21987 case VAR_LIST:
21988 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021989 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021990 if (from->vval.v_list == NULL)
21991 to->vval.v_list = NULL;
21992 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21993 {
21994 /* use the copy made earlier */
21995 to->vval.v_list = from->vval.v_list->lv_copylist;
21996 ++to->vval.v_list->lv_refcount;
21997 }
21998 else
21999 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22000 if (to->vval.v_list == NULL)
22001 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022002 break;
22003 case VAR_DICT:
22004 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022005 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022006 if (from->vval.v_dict == NULL)
22007 to->vval.v_dict = NULL;
22008 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22009 {
22010 /* use the copy made earlier */
22011 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22012 ++to->vval.v_dict->dv_refcount;
22013 }
22014 else
22015 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22016 if (to->vval.v_dict == NULL)
22017 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022018 break;
22019 default:
22020 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022021 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022022 }
22023 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022024 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022025}
22026
22027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 * ":echo expr1 ..." print each argument separated with a space, add a
22029 * newline at the end.
22030 * ":echon expr1 ..." print each argument plain.
22031 */
22032 void
22033ex_echo(eap)
22034 exarg_T *eap;
22035{
22036 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022037 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022038 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 char_u *p;
22040 int needclr = TRUE;
22041 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022042 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043
22044 if (eap->skip)
22045 ++emsg_skip;
22046 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22047 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022048 /* If eval1() causes an error message the text from the command may
22049 * still need to be cleared. E.g., "echo 22,44". */
22050 need_clr_eos = needclr;
22051
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022053 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 {
22055 /*
22056 * Report the invalid expression unless the expression evaluation
22057 * has been cancelled due to an aborting error, an interrupt, or an
22058 * exception.
22059 */
22060 if (!aborting())
22061 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022062 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 break;
22064 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022065 need_clr_eos = FALSE;
22066
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 if (!eap->skip)
22068 {
22069 if (atstart)
22070 {
22071 atstart = FALSE;
22072 /* Call msg_start() after eval1(), evaluating the expression
22073 * may cause a message to appear. */
22074 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022075 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022076 /* Mark the saved text as finishing the line, so that what
22077 * follows is displayed on a new line when scrolling back
22078 * at the more prompt. */
22079 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 }
22083 else if (eap->cmdidx == CMD_echo)
22084 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022085 current_copyID += COPYID_INC;
22086 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022087 if (p != NULL)
22088 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022090 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022092 if (*p != TAB && needclr)
22093 {
22094 /* remove any text still there from the command */
22095 msg_clr_eos();
22096 needclr = FALSE;
22097 }
22098 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 }
22100 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022101 {
22102#ifdef FEAT_MBYTE
22103 if (has_mbyte)
22104 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022105 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022106
22107 (void)msg_outtrans_len_attr(p, i, echo_attr);
22108 p += i - 1;
22109 }
22110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022112 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022115 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022117 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 arg = skipwhite(arg);
22119 }
22120 eap->nextcmd = check_nextcmd(arg);
22121
22122 if (eap->skip)
22123 --emsg_skip;
22124 else
22125 {
22126 /* remove text that may still be there from the command */
22127 if (needclr)
22128 msg_clr_eos();
22129 if (eap->cmdidx == CMD_echo)
22130 msg_end();
22131 }
22132}
22133
22134/*
22135 * ":echohl {name}".
22136 */
22137 void
22138ex_echohl(eap)
22139 exarg_T *eap;
22140{
22141 int id;
22142
22143 id = syn_name2id(eap->arg);
22144 if (id == 0)
22145 echo_attr = 0;
22146 else
22147 echo_attr = syn_id2attr(id);
22148}
22149
22150/*
22151 * ":execute expr1 ..." execute the result of an expression.
22152 * ":echomsg expr1 ..." Print a message
22153 * ":echoerr expr1 ..." Print an error
22154 * Each gets spaces around each argument and a newline at the end for
22155 * echo commands
22156 */
22157 void
22158ex_execute(eap)
22159 exarg_T *eap;
22160{
22161 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 int ret = OK;
22164 char_u *p;
22165 garray_T ga;
22166 int len;
22167 int save_did_emsg;
22168
22169 ga_init2(&ga, 1, 80);
22170
22171 if (eap->skip)
22172 ++emsg_skip;
22173 while (*arg != NUL && *arg != '|' && *arg != '\n')
22174 {
22175 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 {
22178 /*
22179 * Report the invalid expression unless the expression evaluation
22180 * has been cancelled due to an aborting error, an interrupt, or an
22181 * exception.
22182 */
22183 if (!aborting())
22184 EMSG2(_(e_invexpr2), p);
22185 ret = FAIL;
22186 break;
22187 }
22188
22189 if (!eap->skip)
22190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022191 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 len = (int)STRLEN(p);
22193 if (ga_grow(&ga, len + 2) == FAIL)
22194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 ret = FAIL;
22197 break;
22198 }
22199 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 ga.ga_len += len;
22203 }
22204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 arg = skipwhite(arg);
22207 }
22208
22209 if (ret != FAIL && ga.ga_data != NULL)
22210 {
22211 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022214 out_flush();
22215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 else if (eap->cmdidx == CMD_echoerr)
22217 {
22218 /* We don't want to abort following commands, restore did_emsg. */
22219 save_did_emsg = did_emsg;
22220 EMSG((char_u *)ga.ga_data);
22221 if (!force_abort)
22222 did_emsg = save_did_emsg;
22223 }
22224 else if (eap->cmdidx == CMD_execute)
22225 do_cmdline((char_u *)ga.ga_data,
22226 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22227 }
22228
22229 ga_clear(&ga);
22230
22231 if (eap->skip)
22232 --emsg_skip;
22233
22234 eap->nextcmd = check_nextcmd(arg);
22235}
22236
22237/*
22238 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22239 * "arg" points to the "&" or '+' when called, to "option" when returning.
22240 * Returns NULL when no option name found. Otherwise pointer to the char
22241 * after the option name.
22242 */
22243 static char_u *
22244find_option_end(arg, opt_flags)
22245 char_u **arg;
22246 int *opt_flags;
22247{
22248 char_u *p = *arg;
22249
22250 ++p;
22251 if (*p == 'g' && p[1] == ':')
22252 {
22253 *opt_flags = OPT_GLOBAL;
22254 p += 2;
22255 }
22256 else if (*p == 'l' && p[1] == ':')
22257 {
22258 *opt_flags = OPT_LOCAL;
22259 p += 2;
22260 }
22261 else
22262 *opt_flags = 0;
22263
22264 if (!ASCII_ISALPHA(*p))
22265 return NULL;
22266 *arg = p;
22267
22268 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22269 p += 4; /* termcap option */
22270 else
22271 while (ASCII_ISALPHA(*p))
22272 ++p;
22273 return p;
22274}
22275
22276/*
22277 * ":function"
22278 */
22279 void
22280ex_function(eap)
22281 exarg_T *eap;
22282{
22283 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 int j;
22286 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022288 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 char_u *name = NULL;
22290 char_u *p;
22291 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022292 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 garray_T newargs;
22294 garray_T newlines;
22295 int varargs = FALSE;
22296 int mustend = FALSE;
22297 int flags = 0;
22298 ufunc_T *fp;
22299 int indent;
22300 int nesting;
22301 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022302 dictitem_T *v;
22303 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022304 static int func_nr = 0; /* number for nameless function */
22305 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022306 hashtab_T *ht;
22307 int todo;
22308 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022309 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310
22311 /*
22312 * ":function" without argument: list functions.
22313 */
22314 if (ends_excmd(*eap->arg))
22315 {
22316 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022317 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022318 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022319 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022320 {
22321 if (!HASHITEM_EMPTY(hi))
22322 {
22323 --todo;
22324 fp = HI2UF(hi);
22325 if (!isdigit(*fp->uf_name))
22326 list_func_head(fp, FALSE);
22327 }
22328 }
22329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 eap->nextcmd = check_nextcmd(eap->arg);
22331 return;
22332 }
22333
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022334 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022335 * ":function /pat": list functions matching pattern.
22336 */
22337 if (*eap->arg == '/')
22338 {
22339 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22340 if (!eap->skip)
22341 {
22342 regmatch_T regmatch;
22343
22344 c = *p;
22345 *p = NUL;
22346 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22347 *p = c;
22348 if (regmatch.regprog != NULL)
22349 {
22350 regmatch.rm_ic = p_ic;
22351
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022352 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022353 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22354 {
22355 if (!HASHITEM_EMPTY(hi))
22356 {
22357 --todo;
22358 fp = HI2UF(hi);
22359 if (!isdigit(*fp->uf_name)
22360 && vim_regexec(&regmatch, fp->uf_name, 0))
22361 list_func_head(fp, FALSE);
22362 }
22363 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022364 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022365 }
22366 }
22367 if (*p == '/')
22368 ++p;
22369 eap->nextcmd = check_nextcmd(p);
22370 return;
22371 }
22372
22373 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022374 * Get the function name. There are these situations:
22375 * func normal function name
22376 * "name" == func, "fudi.fd_dict" == NULL
22377 * dict.func new dictionary entry
22378 * "name" == NULL, "fudi.fd_dict" set,
22379 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22380 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022381 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022382 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22383 * dict.func existing dict entry that's not a Funcref
22384 * "name" == NULL, "fudi.fd_dict" set,
22385 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022386 * s:func script-local function name
22387 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022390 name = trans_function_name(&p, eap->skip, 0, &fudi);
22391 paren = (vim_strchr(p, '(') != NULL);
22392 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 {
22394 /*
22395 * Return on an invalid expression in braces, unless the expression
22396 * evaluation has been cancelled due to an aborting error, an
22397 * interrupt, or an exception.
22398 */
22399 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022401 if (!eap->skip && fudi.fd_newkey != NULL)
22402 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022403 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 else
22407 eap->skip = TRUE;
22408 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022409
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 /* An error in a function call during evaluation of an expression in magic
22411 * braces should not cause the function not to be defined. */
22412 saved_did_emsg = did_emsg;
22413 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414
22415 /*
22416 * ":function func" with only function name: list function.
22417 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022418 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 {
22420 if (!ends_excmd(*skipwhite(p)))
22421 {
22422 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 }
22425 eap->nextcmd = check_nextcmd(p);
22426 if (eap->nextcmd != NULL)
22427 *p = NUL;
22428 if (!eap->skip && !got_int)
22429 {
22430 fp = find_func(name);
22431 if (fp != NULL)
22432 {
22433 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022434 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022436 if (FUNCLINE(fp, j) == NULL)
22437 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 msg_putchar('\n');
22439 msg_outnum((long)(j + 1));
22440 if (j < 9)
22441 msg_putchar(' ');
22442 if (j < 99)
22443 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022444 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 out_flush(); /* show a line at a time */
22446 ui_breakcheck();
22447 }
22448 if (!got_int)
22449 {
22450 msg_putchar('\n');
22451 msg_puts((char_u *)" endfunction");
22452 }
22453 }
22454 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022455 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022457 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 }
22459
22460 /*
22461 * ":function name(arg1, arg2)" Define function.
22462 */
22463 p = skipwhite(p);
22464 if (*p != '(')
22465 {
22466 if (!eap->skip)
22467 {
22468 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022469 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 }
22471 /* attempt to continue by skipping some text */
22472 if (vim_strchr(p, '(') != NULL)
22473 p = vim_strchr(p, '(');
22474 }
22475 p = skipwhite(p + 1);
22476
22477 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22478 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22479
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022480 if (!eap->skip)
22481 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022482 /* Check the name of the function. Unless it's a dictionary function
22483 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022484 if (name != NULL)
22485 arg = name;
22486 else
22487 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022488 if (arg != NULL && (fudi.fd_di == NULL
22489 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022490 {
22491 if (*arg == K_SPECIAL)
22492 j = 3;
22493 else
22494 j = 0;
22495 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22496 : eval_isnamec(arg[j])))
22497 ++j;
22498 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022499 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022500 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022501 /* Disallow using the g: dict. */
22502 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22503 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022504 }
22505
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 /*
22507 * Isolate the arguments: "arg1, arg2, ...)"
22508 */
22509 while (*p != ')')
22510 {
22511 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22512 {
22513 varargs = TRUE;
22514 p += 3;
22515 mustend = TRUE;
22516 }
22517 else
22518 {
22519 arg = p;
22520 while (ASCII_ISALNUM(*p) || *p == '_')
22521 ++p;
22522 if (arg == p || isdigit(*arg)
22523 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22524 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22525 {
22526 if (!eap->skip)
22527 EMSG2(_("E125: Illegal argument: %s"), arg);
22528 break;
22529 }
22530 if (ga_grow(&newargs, 1) == FAIL)
22531 goto erret;
22532 c = *p;
22533 *p = NUL;
22534 arg = vim_strsave(arg);
22535 if (arg == NULL)
22536 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022537
22538 /* Check for duplicate argument name. */
22539 for (i = 0; i < newargs.ga_len; ++i)
22540 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22541 {
22542 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022543 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022544 goto erret;
22545 }
22546
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22548 *p = c;
22549 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550 if (*p == ',')
22551 ++p;
22552 else
22553 mustend = TRUE;
22554 }
22555 p = skipwhite(p);
22556 if (mustend && *p != ')')
22557 {
22558 if (!eap->skip)
22559 EMSG2(_(e_invarg2), eap->arg);
22560 break;
22561 }
22562 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022563 if (*p != ')')
22564 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 ++p; /* skip the ')' */
22566
Bram Moolenaare9a41262005-01-15 22:18:47 +000022567 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 for (;;)
22569 {
22570 p = skipwhite(p);
22571 if (STRNCMP(p, "range", 5) == 0)
22572 {
22573 flags |= FC_RANGE;
22574 p += 5;
22575 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022576 else if (STRNCMP(p, "dict", 4) == 0)
22577 {
22578 flags |= FC_DICT;
22579 p += 4;
22580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 else if (STRNCMP(p, "abort", 5) == 0)
22582 {
22583 flags |= FC_ABORT;
22584 p += 5;
22585 }
22586 else
22587 break;
22588 }
22589
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022590 /* When there is a line break use what follows for the function body.
22591 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22592 if (*p == '\n')
22593 line_arg = p + 1;
22594 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 EMSG(_(e_trailing));
22596
22597 /*
22598 * Read the body of the function, until ":endfunction" is found.
22599 */
22600 if (KeyTyped)
22601 {
22602 /* Check if the function already exists, don't let the user type the
22603 * whole function before telling him it doesn't work! For a script we
22604 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022605 if (!eap->skip && !eap->forceit)
22606 {
22607 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22608 EMSG(_(e_funcdict));
22609 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022610 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022613 if (!eap->skip && did_emsg)
22614 goto erret;
22615
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 msg_putchar('\n'); /* don't overwrite the function name */
22617 cmdline_row = msg_row;
22618 }
22619
22620 indent = 2;
22621 nesting = 0;
22622 for (;;)
22623 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022624 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022625 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022626 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022627 saved_wait_return = FALSE;
22628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022630 sourcing_lnum_off = sourcing_lnum;
22631
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022632 if (line_arg != NULL)
22633 {
22634 /* Use eap->arg, split up in parts by line breaks. */
22635 theline = line_arg;
22636 p = vim_strchr(theline, '\n');
22637 if (p == NULL)
22638 line_arg += STRLEN(line_arg);
22639 else
22640 {
22641 *p = NUL;
22642 line_arg = p + 1;
22643 }
22644 }
22645 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 theline = getcmdline(':', 0L, indent);
22647 else
22648 theline = eap->getline(':', eap->cookie, indent);
22649 if (KeyTyped)
22650 lines_left = Rows - 1;
22651 if (theline == NULL)
22652 {
22653 EMSG(_("E126: Missing :endfunction"));
22654 goto erret;
22655 }
22656
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022657 /* Detect line continuation: sourcing_lnum increased more than one. */
22658 if (sourcing_lnum > sourcing_lnum_off + 1)
22659 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22660 else
22661 sourcing_lnum_off = 0;
22662
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 if (skip_until != NULL)
22664 {
22665 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22666 * don't check for ":endfunc". */
22667 if (STRCMP(theline, skip_until) == 0)
22668 {
22669 vim_free(skip_until);
22670 skip_until = NULL;
22671 }
22672 }
22673 else
22674 {
22675 /* skip ':' and blanks*/
22676 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22677 ;
22678
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022679 /* Check for "endfunction". */
22680 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022682 if (line_arg == NULL)
22683 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 break;
22685 }
22686
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022687 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 * at "end". */
22689 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22690 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022691 else if (STRNCMP(p, "if", 2) == 0
22692 || STRNCMP(p, "wh", 2) == 0
22693 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 || STRNCMP(p, "try", 3) == 0)
22695 indent += 2;
22696
22697 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022698 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022700 if (*p == '!')
22701 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022703 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22704 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022706 ++nesting;
22707 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 }
22709 }
22710
22711 /* Check for ":append" or ":insert". */
22712 p = skip_range(p, NULL);
22713 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22714 || (p[0] == 'i'
22715 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22716 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22717 skip_until = vim_strsave((char_u *)".");
22718
22719 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22720 arg = skipwhite(skiptowhite(p));
22721 if (arg[0] == '<' && arg[1] =='<'
22722 && ((p[0] == 'p' && p[1] == 'y'
22723 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22724 || (p[0] == 'p' && p[1] == 'e'
22725 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22726 || (p[0] == 't' && p[1] == 'c'
22727 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022728 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22729 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22731 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022732 || (p[0] == 'm' && p[1] == 'z'
22733 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 ))
22735 {
22736 /* ":python <<" continues until a dot, like ":append" */
22737 p = skipwhite(arg + 2);
22738 if (*p == NUL)
22739 skip_until = vim_strsave((char_u *)".");
22740 else
22741 skip_until = vim_strsave(p);
22742 }
22743 }
22744
22745 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022746 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022747 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022748 if (line_arg == NULL)
22749 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022751 }
22752
22753 /* Copy the line to newly allocated memory. get_one_sourceline()
22754 * allocates 250 bytes per line, this saves 80% on average. The cost
22755 * is an extra alloc/free. */
22756 p = vim_strsave(theline);
22757 if (p != NULL)
22758 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022759 if (line_arg == NULL)
22760 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022761 theline = p;
22762 }
22763
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022764 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22765
22766 /* Add NULL lines for continuation lines, so that the line count is
22767 * equal to the index in the growarray. */
22768 while (sourcing_lnum_off-- > 0)
22769 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022770
22771 /* Check for end of eap->arg. */
22772 if (line_arg != NULL && *line_arg == NUL)
22773 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 }
22775
22776 /* Don't define the function when skipping commands or when an error was
22777 * detected. */
22778 if (eap->skip || did_emsg)
22779 goto erret;
22780
22781 /*
22782 * If there are no errors, add the function
22783 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022785 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022786 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022788 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022789 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022790 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022791 goto erret;
22792 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022793
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022794 fp = find_func(name);
22795 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022797 if (!eap->forceit)
22798 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022799 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022800 goto erret;
22801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022802 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022803 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022804 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022805 name);
22806 goto erret;
22807 }
22808 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022809 ga_clear_strings(&(fp->uf_args));
22810 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022811 vim_free(name);
22812 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 }
22815 else
22816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022817 char numbuf[20];
22818
22819 fp = NULL;
22820 if (fudi.fd_newkey == NULL && !eap->forceit)
22821 {
22822 EMSG(_(e_funcdict));
22823 goto erret;
22824 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022825 if (fudi.fd_di == NULL)
22826 {
22827 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022828 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022829 goto erret;
22830 }
22831 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022832 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022833 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834
22835 /* Give the function a sequential number. Can only be used with a
22836 * Funcref! */
22837 vim_free(name);
22838 sprintf(numbuf, "%d", ++func_nr);
22839 name = vim_strsave((char_u *)numbuf);
22840 if (name == NULL)
22841 goto erret;
22842 }
22843
22844 if (fp == NULL)
22845 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022846 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022847 {
22848 int slen, plen;
22849 char_u *scriptname;
22850
22851 /* Check that the autoload name matches the script name. */
22852 j = FAIL;
22853 if (sourcing_name != NULL)
22854 {
22855 scriptname = autoload_name(name);
22856 if (scriptname != NULL)
22857 {
22858 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022859 plen = (int)STRLEN(p);
22860 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022861 if (slen > plen && fnamecmp(p,
22862 sourcing_name + slen - plen) == 0)
22863 j = OK;
22864 vim_free(scriptname);
22865 }
22866 }
22867 if (j == FAIL)
22868 {
22869 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22870 goto erret;
22871 }
22872 }
22873
22874 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 if (fp == NULL)
22876 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022877
22878 if (fudi.fd_dict != NULL)
22879 {
22880 if (fudi.fd_di == NULL)
22881 {
22882 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022883 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022884 if (fudi.fd_di == NULL)
22885 {
22886 vim_free(fp);
22887 goto erret;
22888 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022889 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22890 {
22891 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022892 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022893 goto erret;
22894 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022895 }
22896 else
22897 /* overwrite existing dict entry */
22898 clear_tv(&fudi.fd_di->di_tv);
22899 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022900 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022901 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022902 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022903
22904 /* behave like "dict" was used */
22905 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022906 }
22907
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022909 STRCPY(fp->uf_name, name);
22910 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022912 fp->uf_args = newargs;
22913 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022914#ifdef FEAT_PROFILE
22915 fp->uf_tml_count = NULL;
22916 fp->uf_tml_total = NULL;
22917 fp->uf_tml_self = NULL;
22918 fp->uf_profiling = FALSE;
22919 if (prof_def_func())
22920 func_do_profile(fp);
22921#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022922 fp->uf_varargs = varargs;
22923 fp->uf_flags = flags;
22924 fp->uf_calls = 0;
22925 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022926 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927
22928erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 ga_clear_strings(&newargs);
22930 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931ret_free:
22932 vim_free(skip_until);
22933 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022936 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937}
22938
22939/*
22940 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022941 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022943 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022944 * TFN_INT: internal function name OK
22945 * TFN_QUIET: be quiet
22946 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 * Advances "pp" to just after the function name (if no error).
22948 */
22949 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022950trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 char_u **pp;
22952 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022953 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022954 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022956 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 char_u *start;
22958 char_u *end;
22959 int lead;
22960 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022962 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022963
22964 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022965 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022967
22968 /* Check for hard coded <SNR>: already translated function ID (from a user
22969 * command). */
22970 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22971 && (*pp)[2] == (int)KE_SNR)
22972 {
22973 *pp += 3;
22974 len = get_id_len(pp) + 3;
22975 return vim_strnsave(start, len);
22976 }
22977
22978 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22979 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022981 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022983
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022984 /* Note that TFN_ flags use the same values as GLV_ flags. */
22985 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022986 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022987 if (end == start)
22988 {
22989 if (!skip)
22990 EMSG(_("E129: Function name required"));
22991 goto theend;
22992 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022993 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022994 {
22995 /*
22996 * Report an invalid expression in braces, unless the expression
22997 * evaluation has been cancelled due to an aborting error, an
22998 * interrupt, or an exception.
22999 */
23000 if (!aborting())
23001 {
23002 if (end != NULL)
23003 EMSG2(_(e_invarg2), start);
23004 }
23005 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023006 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023007 goto theend;
23008 }
23009
23010 if (lv.ll_tv != NULL)
23011 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023012 if (fdp != NULL)
23013 {
23014 fdp->fd_dict = lv.ll_dict;
23015 fdp->fd_newkey = lv.ll_newkey;
23016 lv.ll_newkey = NULL;
23017 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023019 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23020 {
23021 name = vim_strsave(lv.ll_tv->vval.v_string);
23022 *pp = end;
23023 }
23024 else
23025 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023026 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23027 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023028 EMSG(_(e_funcref));
23029 else
23030 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023031 name = NULL;
23032 }
23033 goto theend;
23034 }
23035
23036 if (lv.ll_name == NULL)
23037 {
23038 /* Error found, but continue after the function name. */
23039 *pp = end;
23040 goto theend;
23041 }
23042
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023043 /* Check if the name is a Funcref. If so, use the value. */
23044 if (lv.ll_exp_name != NULL)
23045 {
23046 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023047 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023048 if (name == lv.ll_exp_name)
23049 name = NULL;
23050 }
23051 else
23052 {
23053 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023054 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023055 if (name == *pp)
23056 name = NULL;
23057 }
23058 if (name != NULL)
23059 {
23060 name = vim_strsave(name);
23061 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023062 if (STRNCMP(name, "<SNR>", 5) == 0)
23063 {
23064 /* Change "<SNR>" to the byte sequence. */
23065 name[0] = K_SPECIAL;
23066 name[1] = KS_EXTRA;
23067 name[2] = (int)KE_SNR;
23068 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23069 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023070 goto theend;
23071 }
23072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023073 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023074 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023075 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023076 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23077 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23078 {
23079 /* When there was "s:" already or the name expanded to get a
23080 * leading "s:" then remove it. */
23081 lv.ll_name += 2;
23082 len -= 2;
23083 lead = 2;
23084 }
23085 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023086 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023087 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023088 /* skip over "s:" and "g:" */
23089 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023090 lv.ll_name += 2;
23091 len = (int)(end - lv.ll_name);
23092 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023093
23094 /*
23095 * Copy the function name to allocated memory.
23096 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23097 * Accept <SNR>123_name() outside a script.
23098 */
23099 if (skip)
23100 lead = 0; /* do nothing */
23101 else if (lead > 0)
23102 {
23103 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023104 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23105 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023106 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023107 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023108 if (current_SID <= 0)
23109 {
23110 EMSG(_(e_usingsid));
23111 goto theend;
23112 }
23113 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23114 lead += (int)STRLEN(sid_buf);
23115 }
23116 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023117 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023118 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023119 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023120 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023121 goto theend;
23122 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023123 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023124 {
23125 char_u *cp = vim_strchr(lv.ll_name, ':');
23126
23127 if (cp != NULL && cp < end)
23128 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023129 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023130 goto theend;
23131 }
23132 }
23133
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023134 name = alloc((unsigned)(len + lead + 1));
23135 if (name != NULL)
23136 {
23137 if (lead > 0)
23138 {
23139 name[0] = K_SPECIAL;
23140 name[1] = KS_EXTRA;
23141 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023142 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023143 STRCPY(name + 3, sid_buf);
23144 }
23145 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023146 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023147 }
23148 *pp = end;
23149
23150theend:
23151 clear_lval(&lv);
23152 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153}
23154
23155/*
23156 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23157 * Return 2 if "p" starts with "s:".
23158 * Return 0 otherwise.
23159 */
23160 static int
23161eval_fname_script(p)
23162 char_u *p;
23163{
23164 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23165 || STRNICMP(p + 1, "SNR>", 4) == 0))
23166 return 5;
23167 if (p[0] == 's' && p[1] == ':')
23168 return 2;
23169 return 0;
23170}
23171
23172/*
23173 * Return TRUE if "p" starts with "<SID>" or "s:".
23174 * Only works if eval_fname_script() returned non-zero for "p"!
23175 */
23176 static int
23177eval_fname_sid(p)
23178 char_u *p;
23179{
23180 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23181}
23182
23183/*
23184 * List the head of the function: "name(arg1, arg2)".
23185 */
23186 static void
23187list_func_head(fp, indent)
23188 ufunc_T *fp;
23189 int indent;
23190{
23191 int j;
23192
23193 msg_start();
23194 if (indent)
23195 MSG_PUTS(" ");
23196 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023197 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 {
23199 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 }
23202 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023203 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023205 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 {
23207 if (j)
23208 MSG_PUTS(", ");
23209 msg_puts(FUNCARG(fp, j));
23210 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023211 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 {
23213 if (j)
23214 MSG_PUTS(", ");
23215 MSG_PUTS("...");
23216 }
23217 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023218 if (fp->uf_flags & FC_ABORT)
23219 MSG_PUTS(" abort");
23220 if (fp->uf_flags & FC_RANGE)
23221 MSG_PUTS(" range");
23222 if (fp->uf_flags & FC_DICT)
23223 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023224 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023225 if (p_verbose > 0)
23226 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227}
23228
23229/*
23230 * Find a function by name, return pointer to it in ufuncs.
23231 * Return NULL for unknown function.
23232 */
23233 static ufunc_T *
23234find_func(name)
23235 char_u *name;
23236{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023237 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023239 hi = hash_find(&func_hashtab, name);
23240 if (!HASHITEM_EMPTY(hi))
23241 return HI2UF(hi);
23242 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243}
23244
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023245#if defined(EXITFREE) || defined(PROTO)
23246 void
23247free_all_functions()
23248{
23249 hashitem_T *hi;
23250
23251 /* Need to start all over every time, because func_free() may change the
23252 * hash table. */
23253 while (func_hashtab.ht_used > 0)
23254 for (hi = func_hashtab.ht_array; ; ++hi)
23255 if (!HASHITEM_EMPTY(hi))
23256 {
23257 func_free(HI2UF(hi));
23258 break;
23259 }
23260}
23261#endif
23262
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023263 int
23264translated_function_exists(name)
23265 char_u *name;
23266{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023267 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023268 return find_internal_func(name) >= 0;
23269 return find_func(name) != NULL;
23270}
23271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023272/*
23273 * Return TRUE if a function "name" exists.
23274 */
23275 static int
23276function_exists(name)
23277 char_u *name;
23278{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023279 char_u *nm = name;
23280 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023281 int n = FALSE;
23282
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023283 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23284 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023285 nm = skipwhite(nm);
23286
23287 /* Only accept "funcname", "funcname ", "funcname (..." and
23288 * "funcname(...", not "funcname!...". */
23289 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023290 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023291 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023292 return n;
23293}
23294
Bram Moolenaara1544c02013-05-30 12:35:52 +020023295 char_u *
23296get_expanded_name(name, check)
23297 char_u *name;
23298 int check;
23299{
23300 char_u *nm = name;
23301 char_u *p;
23302
23303 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23304
23305 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023306 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023307 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023308
Bram Moolenaara1544c02013-05-30 12:35:52 +020023309 vim_free(p);
23310 return NULL;
23311}
23312
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023313/*
23314 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023315 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23316 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023317 */
23318 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023319builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023320 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023321 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023322{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023323 char_u *p;
23324
23325 if (!ASCII_ISLOWER(name[0]))
23326 return FALSE;
23327 p = vim_strchr(name, AUTOLOAD_CHAR);
23328 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023329}
23330
Bram Moolenaar05159a02005-02-26 23:04:13 +000023331#if defined(FEAT_PROFILE) || defined(PROTO)
23332/*
23333 * Start profiling function "fp".
23334 */
23335 static void
23336func_do_profile(fp)
23337 ufunc_T *fp;
23338{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023339 int len = fp->uf_lines.ga_len;
23340
23341 if (len == 0)
23342 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023343 fp->uf_tm_count = 0;
23344 profile_zero(&fp->uf_tm_self);
23345 profile_zero(&fp->uf_tm_total);
23346 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023347 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023348 if (fp->uf_tml_total == NULL)
23349 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023350 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023351 if (fp->uf_tml_self == NULL)
23352 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023353 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023354 fp->uf_tml_idx = -1;
23355 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23356 || fp->uf_tml_self == NULL)
23357 return; /* out of memory */
23358
23359 fp->uf_profiling = TRUE;
23360}
23361
23362/*
23363 * Dump the profiling results for all functions in file "fd".
23364 */
23365 void
23366func_dump_profile(fd)
23367 FILE *fd;
23368{
23369 hashitem_T *hi;
23370 int todo;
23371 ufunc_T *fp;
23372 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023373 ufunc_T **sorttab;
23374 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023375
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023376 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023377 if (todo == 0)
23378 return; /* nothing to dump */
23379
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023380 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023381
Bram Moolenaar05159a02005-02-26 23:04:13 +000023382 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23383 {
23384 if (!HASHITEM_EMPTY(hi))
23385 {
23386 --todo;
23387 fp = HI2UF(hi);
23388 if (fp->uf_profiling)
23389 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023390 if (sorttab != NULL)
23391 sorttab[st_len++] = fp;
23392
Bram Moolenaar05159a02005-02-26 23:04:13 +000023393 if (fp->uf_name[0] == K_SPECIAL)
23394 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23395 else
23396 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23397 if (fp->uf_tm_count == 1)
23398 fprintf(fd, "Called 1 time\n");
23399 else
23400 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23401 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23402 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23403 fprintf(fd, "\n");
23404 fprintf(fd, "count total (s) self (s)\n");
23405
23406 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23407 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023408 if (FUNCLINE(fp, i) == NULL)
23409 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023410 prof_func_line(fd, fp->uf_tml_count[i],
23411 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023412 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23413 }
23414 fprintf(fd, "\n");
23415 }
23416 }
23417 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023418
23419 if (sorttab != NULL && st_len > 0)
23420 {
23421 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23422 prof_total_cmp);
23423 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23424 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23425 prof_self_cmp);
23426 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23427 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023428
23429 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023430}
Bram Moolenaar73830342005-02-28 22:48:19 +000023431
23432 static void
23433prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23434 FILE *fd;
23435 ufunc_T **sorttab;
23436 int st_len;
23437 char *title;
23438 int prefer_self; /* when equal print only self time */
23439{
23440 int i;
23441 ufunc_T *fp;
23442
23443 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23444 fprintf(fd, "count total (s) self (s) function\n");
23445 for (i = 0; i < 20 && i < st_len; ++i)
23446 {
23447 fp = sorttab[i];
23448 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23449 prefer_self);
23450 if (fp->uf_name[0] == K_SPECIAL)
23451 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23452 else
23453 fprintf(fd, " %s()\n", fp->uf_name);
23454 }
23455 fprintf(fd, "\n");
23456}
23457
23458/*
23459 * Print the count and times for one function or function line.
23460 */
23461 static void
23462prof_func_line(fd, count, total, self, prefer_self)
23463 FILE *fd;
23464 int count;
23465 proftime_T *total;
23466 proftime_T *self;
23467 int prefer_self; /* when equal print only self time */
23468{
23469 if (count > 0)
23470 {
23471 fprintf(fd, "%5d ", count);
23472 if (prefer_self && profile_equal(total, self))
23473 fprintf(fd, " ");
23474 else
23475 fprintf(fd, "%s ", profile_msg(total));
23476 if (!prefer_self && profile_equal(total, self))
23477 fprintf(fd, " ");
23478 else
23479 fprintf(fd, "%s ", profile_msg(self));
23480 }
23481 else
23482 fprintf(fd, " ");
23483}
23484
23485/*
23486 * Compare function for total time sorting.
23487 */
23488 static int
23489#ifdef __BORLANDC__
23490_RTLENTRYF
23491#endif
23492prof_total_cmp(s1, s2)
23493 const void *s1;
23494 const void *s2;
23495{
23496 ufunc_T *p1, *p2;
23497
23498 p1 = *(ufunc_T **)s1;
23499 p2 = *(ufunc_T **)s2;
23500 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23501}
23502
23503/*
23504 * Compare function for self time sorting.
23505 */
23506 static int
23507#ifdef __BORLANDC__
23508_RTLENTRYF
23509#endif
23510prof_self_cmp(s1, s2)
23511 const void *s1;
23512 const void *s2;
23513{
23514 ufunc_T *p1, *p2;
23515
23516 p1 = *(ufunc_T **)s1;
23517 p2 = *(ufunc_T **)s2;
23518 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23519}
23520
Bram Moolenaar05159a02005-02-26 23:04:13 +000023521#endif
23522
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023523/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023524 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023525 * Return TRUE if a package was loaded.
23526 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023527 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023528script_autoload(name, reload)
23529 char_u *name;
23530 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023531{
23532 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023533 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023534 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023535 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023536
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023537 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023538 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023539 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023540 return FALSE;
23541
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023542 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023544 /* Find the name in the list of previously loaded package names. Skip
23545 * "autoload/", it's always the same. */
23546 for (i = 0; i < ga_loaded.ga_len; ++i)
23547 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23548 break;
23549 if (!reload && i < ga_loaded.ga_len)
23550 ret = FALSE; /* was loaded already */
23551 else
23552 {
23553 /* Remember the name if it wasn't loaded already. */
23554 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23555 {
23556 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23557 tofree = NULL;
23558 }
23559
23560 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023561 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023562 ret = TRUE;
23563 }
23564
23565 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023566 return ret;
23567}
23568
23569/*
23570 * Return the autoload script name for a function or variable name.
23571 * Returns NULL when out of memory.
23572 */
23573 static char_u *
23574autoload_name(name)
23575 char_u *name;
23576{
23577 char_u *p;
23578 char_u *scriptname;
23579
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023580 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023581 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23582 if (scriptname == NULL)
23583 return FALSE;
23584 STRCPY(scriptname, "autoload/");
23585 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023586 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023587 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023588 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023589 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023590 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023591}
23592
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23594
23595/*
23596 * Function given to ExpandGeneric() to obtain the list of user defined
23597 * function names.
23598 */
23599 char_u *
23600get_user_func_name(xp, idx)
23601 expand_T *xp;
23602 int idx;
23603{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023604 static long_u done;
23605 static hashitem_T *hi;
23606 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607
23608 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023610 done = 0;
23611 hi = func_hashtab.ht_array;
23612 }
23613 if (done < func_hashtab.ht_used)
23614 {
23615 if (done++ > 0)
23616 ++hi;
23617 while (HASHITEM_EMPTY(hi))
23618 ++hi;
23619 fp = HI2UF(hi);
23620
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023621 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023622 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023623
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023624 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23625 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626
23627 cat_func_name(IObuff, fp);
23628 if (xp->xp_context != EXPAND_USER_FUNC)
23629 {
23630 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023631 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 STRCAT(IObuff, ")");
23633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 return IObuff;
23635 }
23636 return NULL;
23637}
23638
23639#endif /* FEAT_CMDL_COMPL */
23640
23641/*
23642 * Copy the function name of "fp" to buffer "buf".
23643 * "buf" must be able to hold the function name plus three bytes.
23644 * Takes care of script-local function names.
23645 */
23646 static void
23647cat_func_name(buf, fp)
23648 char_u *buf;
23649 ufunc_T *fp;
23650{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023651 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 {
23653 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023654 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 }
23656 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023657 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658}
23659
23660/*
23661 * ":delfunction {name}"
23662 */
23663 void
23664ex_delfunction(eap)
23665 exarg_T *eap;
23666{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023667 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 char_u *p;
23669 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023670 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671
23672 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023673 name = trans_function_name(&p, eap->skip, 0, &fudi);
23674 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023676 {
23677 if (fudi.fd_dict != NULL && !eap->skip)
23678 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 if (!ends_excmd(*skipwhite(p)))
23682 {
23683 vim_free(name);
23684 EMSG(_(e_trailing));
23685 return;
23686 }
23687 eap->nextcmd = check_nextcmd(p);
23688 if (eap->nextcmd != NULL)
23689 *p = NUL;
23690
23691 if (!eap->skip)
23692 fp = find_func(name);
23693 vim_free(name);
23694
23695 if (!eap->skip)
23696 {
23697 if (fp == NULL)
23698 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023699 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700 return;
23701 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023702 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 {
23704 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23705 return;
23706 }
23707
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023708 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 /* Delete the dict item that refers to the function, it will
23711 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023712 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023714 else
23715 func_free(fp);
23716 }
23717}
23718
23719/*
23720 * Free a function and remove it from the list of functions.
23721 */
23722 static void
23723func_free(fp)
23724 ufunc_T *fp;
23725{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023726 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023727
23728 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023729 ga_clear_strings(&(fp->uf_args));
23730 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023731#ifdef FEAT_PROFILE
23732 vim_free(fp->uf_tml_count);
23733 vim_free(fp->uf_tml_total);
23734 vim_free(fp->uf_tml_self);
23735#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023736
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023737 /* remove the function from the function hashtable */
23738 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23739 if (HASHITEM_EMPTY(hi))
23740 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023741 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023742 hash_remove(&func_hashtab, hi);
23743
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023744 vim_free(fp);
23745}
23746
23747/*
23748 * Unreference a Function: decrement the reference count and free it when it
23749 * becomes zero. Only for numbered functions.
23750 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023751 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023752func_unref(name)
23753 char_u *name;
23754{
23755 ufunc_T *fp;
23756
23757 if (name != NULL && isdigit(*name))
23758 {
23759 fp = find_func(name);
23760 if (fp == NULL)
23761 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023762 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023763 {
23764 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023765 * when "uf_calls" becomes zero. */
23766 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023767 func_free(fp);
23768 }
23769 }
23770}
23771
23772/*
23773 * Count a reference to a Function.
23774 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023775 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023776func_ref(name)
23777 char_u *name;
23778{
23779 ufunc_T *fp;
23780
23781 if (name != NULL && isdigit(*name))
23782 {
23783 fp = find_func(name);
23784 if (fp == NULL)
23785 EMSG2(_(e_intern2), "func_ref()");
23786 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023787 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 }
23789}
23790
23791/*
23792 * Call a user function.
23793 */
23794 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023795call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 ufunc_T *fp; /* pointer to function */
23797 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023798 typval_T *argvars; /* arguments */
23799 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 linenr_T firstline; /* first line of range */
23801 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023802 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803{
Bram Moolenaar33570922005-01-25 22:26:29 +000023804 char_u *save_sourcing_name;
23805 linenr_T save_sourcing_lnum;
23806 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023807 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023808 int save_did_emsg;
23809 static int depth = 0;
23810 dictitem_T *v;
23811 int fixvar_idx = 0; /* index in fixvar[] */
23812 int i;
23813 int ai;
23814 char_u numbuf[NUMBUFLEN];
23815 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023816#ifdef FEAT_PROFILE
23817 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023818 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820
23821 /* If depth of calling is getting too high, don't execute the function */
23822 if (depth >= p_mfd)
23823 {
23824 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023825 rettv->v_type = VAR_NUMBER;
23826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 return;
23828 }
23829 ++depth;
23830
23831 line_breakcheck(); /* check for CTRL-C hit */
23832
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023833 fc = (funccall_T *)alloc(sizeof(funccall_T));
23834 fc->caller = current_funccal;
23835 current_funccal = fc;
23836 fc->func = fp;
23837 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023838 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023839 fc->linenr = 0;
23840 fc->returned = FALSE;
23841 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023843 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23844 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845
Bram Moolenaar33570922005-01-25 22:26:29 +000023846 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023847 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023848 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23849 * each argument variable and saves a lot of time.
23850 */
23851 /*
23852 * Init l: variables.
23853 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023854 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023855 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023856 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023857 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23858 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023859 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023860 name = v->di_key;
23861 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023862 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023863 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023864 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023865 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023866 v->di_tv.vval.v_dict = selfdict;
23867 ++selfdict->dv_refcount;
23868 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023869
Bram Moolenaar33570922005-01-25 22:26:29 +000023870 /*
23871 * Init a: variables.
23872 * Set a:0 to "argcount".
23873 * Set a:000 to a list with room for the "..." arguments.
23874 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023875 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023876 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023877 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023878 /* Use "name" to avoid a warning from some compiler that checks the
23879 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023880 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023881 name = v->di_key;
23882 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023883 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023884 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023885 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023886 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023887 v->di_tv.vval.v_list = &fc->l_varlist;
23888 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23889 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23890 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023891
23892 /*
23893 * Set a:firstline to "firstline" and a:lastline to "lastline".
23894 * Set a:name to named arguments.
23895 * Set a:N to the "..." arguments.
23896 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023897 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023898 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023899 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023900 (varnumber_T)lastline);
23901 for (i = 0; i < argcount; ++i)
23902 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023903 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023904 if (ai < 0)
23905 /* named argument a:name */
23906 name = FUNCARG(fp, i);
23907 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023908 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023909 /* "..." argument a:1, a:2, etc. */
23910 sprintf((char *)numbuf, "%d", ai + 1);
23911 name = numbuf;
23912 }
23913 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23914 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023915 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023916 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23917 }
23918 else
23919 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023920 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23921 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023922 if (v == NULL)
23923 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023924 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023925 }
23926 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023927 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023928
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023929 /* Note: the values are copied directly to avoid alloc/free.
23930 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023931 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023932 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023933
23934 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23935 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023936 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23937 fc->l_listitems[ai].li_tv = argvars[i];
23938 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023939 }
23940 }
23941
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 /* Don't redraw while executing the function. */
23943 ++RedrawingDisabled;
23944 save_sourcing_name = sourcing_name;
23945 save_sourcing_lnum = sourcing_lnum;
23946 sourcing_lnum = 1;
23947 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023948 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 if (sourcing_name != NULL)
23950 {
23951 if (save_sourcing_name != NULL
23952 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23953 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23954 else
23955 STRCPY(sourcing_name, "function ");
23956 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23957
23958 if (p_verbose >= 12)
23959 {
23960 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023961 verbose_enter_scroll();
23962
Bram Moolenaar555b2802005-05-19 21:08:39 +000023963 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 if (p_verbose >= 14)
23965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023967 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023968 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023969 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970
23971 msg_puts((char_u *)"(");
23972 for (i = 0; i < argcount; ++i)
23973 {
23974 if (i > 0)
23975 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023976 if (argvars[i].v_type == VAR_NUMBER)
23977 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 else
23979 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023980 /* Do not want errors such as E724 here. */
23981 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023982 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023983 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023984 if (s != NULL)
23985 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023986 if (vim_strsize(s) > MSG_BUF_CLEN)
23987 {
23988 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23989 s = buf;
23990 }
23991 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023992 vim_free(tofree);
23993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 }
23995 }
23996 msg_puts((char_u *)")");
23997 }
23998 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023999
24000 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 --no_wait_return;
24002 }
24003 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024005 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024006 {
24007 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24008 func_do_profile(fp);
24009 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024010 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024011 {
24012 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024013 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024014 profile_zero(&fp->uf_tm_children);
24015 }
24016 script_prof_save(&wait_start);
24017 }
24018#endif
24019
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024021 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 save_did_emsg = did_emsg;
24023 did_emsg = FALSE;
24024
24025 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024026 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24028
24029 --RedrawingDisabled;
24030
24031 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024032 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024034 clear_tv(rettv);
24035 rettv->v_type = VAR_NUMBER;
24036 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 }
24038
Bram Moolenaar05159a02005-02-26 23:04:13 +000024039#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024040 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024041 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024042 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024043 profile_end(&call_start);
24044 profile_sub_wait(&wait_start, &call_start);
24045 profile_add(&fp->uf_tm_total, &call_start);
24046 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024047 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024049 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24050 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024051 }
24052 }
24053#endif
24054
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 /* when being verbose, mention the return value */
24056 if (p_verbose >= 12)
24057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024059 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024062 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024063 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024064 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024065 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024068 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024069 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024070 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024071 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024072
Bram Moolenaar555b2802005-05-19 21:08:39 +000024073 /* The value may be very long. Skip the middle part, so that we
24074 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024075 * truncate it at the end. Don't want errors such as E724 here. */
24076 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024077 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024078 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024079 if (s != NULL)
24080 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024081 if (vim_strsize(s) > MSG_BUF_CLEN)
24082 {
24083 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24084 s = buf;
24085 }
24086 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024087 vim_free(tofree);
24088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089 }
24090 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024091
24092 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 --no_wait_return;
24094 }
24095
24096 vim_free(sourcing_name);
24097 sourcing_name = save_sourcing_name;
24098 sourcing_lnum = save_sourcing_lnum;
24099 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024100#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024101 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024102 script_prof_restore(&wait_start);
24103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104
24105 if (p_verbose >= 12 && sourcing_name != NULL)
24106 {
24107 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024108 verbose_enter_scroll();
24109
Bram Moolenaar555b2802005-05-19 21:08:39 +000024110 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024112
24113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114 --no_wait_return;
24115 }
24116
24117 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024118 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024120
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024121 /* If the a:000 list and the l: and a: dicts are not referenced we can
24122 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024123 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24124 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24125 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24126 {
24127 free_funccal(fc, FALSE);
24128 }
24129 else
24130 {
24131 hashitem_T *hi;
24132 listitem_T *li;
24133 int todo;
24134
24135 /* "fc" is still in use. This can happen when returning "a:000" or
24136 * assigning "l:" to a global variable.
24137 * Link "fc" in the list for garbage collection later. */
24138 fc->caller = previous_funccal;
24139 previous_funccal = fc;
24140
24141 /* Make a copy of the a: variables, since we didn't do that above. */
24142 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24143 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24144 {
24145 if (!HASHITEM_EMPTY(hi))
24146 {
24147 --todo;
24148 v = HI2DI(hi);
24149 copy_tv(&v->di_tv, &v->di_tv);
24150 }
24151 }
24152
24153 /* Make a copy of the a:000 items, since we didn't do that above. */
24154 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24155 copy_tv(&li->li_tv, &li->li_tv);
24156 }
24157}
24158
24159/*
24160 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024161 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024162 */
24163 static int
24164can_free_funccal(fc, copyID)
24165 funccall_T *fc;
24166 int copyID;
24167{
24168 return (fc->l_varlist.lv_copyID != copyID
24169 && fc->l_vars.dv_copyID != copyID
24170 && fc->l_avars.dv_copyID != copyID);
24171}
24172
24173/*
24174 * Free "fc" and what it contains.
24175 */
24176 static void
24177free_funccal(fc, free_val)
24178 funccall_T *fc;
24179 int free_val; /* a: vars were allocated */
24180{
24181 listitem_T *li;
24182
24183 /* The a: variables typevals may not have been allocated, only free the
24184 * allocated variables. */
24185 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24186
24187 /* free all l: variables */
24188 vars_clear(&fc->l_vars.dv_hashtab);
24189
24190 /* Free the a:000 variables if they were allocated. */
24191 if (free_val)
24192 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24193 clear_tv(&li->li_tv);
24194
24195 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196}
24197
24198/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024199 * Add a number variable "name" to dict "dp" with value "nr".
24200 */
24201 static void
24202add_nr_var(dp, v, name, nr)
24203 dict_T *dp;
24204 dictitem_T *v;
24205 char *name;
24206 varnumber_T nr;
24207{
24208 STRCPY(v->di_key, name);
24209 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24210 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24211 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024212 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024213 v->di_tv.vval.v_number = nr;
24214}
24215
24216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 * ":return [expr]"
24218 */
24219 void
24220ex_return(eap)
24221 exarg_T *eap;
24222{
24223 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024224 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 int returning = FALSE;
24226
24227 if (current_funccal == NULL)
24228 {
24229 EMSG(_("E133: :return not inside a function"));
24230 return;
24231 }
24232
24233 if (eap->skip)
24234 ++emsg_skip;
24235
24236 eap->nextcmd = NULL;
24237 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024238 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 {
24240 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024241 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 }
24245 /* It's safer to return also on error. */
24246 else if (!eap->skip)
24247 {
24248 /*
24249 * Return unless the expression evaluation has been cancelled due to an
24250 * aborting error, an interrupt, or an exception.
24251 */
24252 if (!aborting())
24253 returning = do_return(eap, FALSE, TRUE, NULL);
24254 }
24255
24256 /* When skipping or the return gets pending, advance to the next command
24257 * in this line (!returning). Otherwise, ignore the rest of the line.
24258 * Following lines will be ignored by get_func_line(). */
24259 if (returning)
24260 eap->nextcmd = NULL;
24261 else if (eap->nextcmd == NULL) /* no argument */
24262 eap->nextcmd = check_nextcmd(arg);
24263
24264 if (eap->skip)
24265 --emsg_skip;
24266}
24267
24268/*
24269 * Return from a function. Possibly makes the return pending. Also called
24270 * for a pending return at the ":endtry" or after returning from an extra
24271 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024272 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024273 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 * FALSE when the return gets pending.
24275 */
24276 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024277do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 exarg_T *eap;
24279 int reanimate;
24280 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024281 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282{
24283 int idx;
24284 struct condstack *cstack = eap->cstack;
24285
24286 if (reanimate)
24287 /* Undo the return. */
24288 current_funccal->returned = FALSE;
24289
24290 /*
24291 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24292 * not in its finally clause (which then is to be executed next) is found.
24293 * In this case, make the ":return" pending for execution at the ":endtry".
24294 * Otherwise, return normally.
24295 */
24296 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24297 if (idx >= 0)
24298 {
24299 cstack->cs_pending[idx] = CSTP_RETURN;
24300
24301 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024302 /* A pending return again gets pending. "rettv" points to an
24303 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024305 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 else
24307 {
24308 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024309 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024311 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024313 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 {
24315 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024316 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024317 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 else
24319 EMSG(_(e_outofmem));
24320 }
24321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024322 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323
24324 if (reanimate)
24325 {
24326 /* The pending return value could be overwritten by a ":return"
24327 * without argument in a finally clause; reset the default
24328 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024329 current_funccal->rettv->v_type = VAR_NUMBER;
24330 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 }
24332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024333 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 }
24335 else
24336 {
24337 current_funccal->returned = TRUE;
24338
24339 /* If the return is carried out now, store the return value. For
24340 * a return immediately after reanimation, the value is already
24341 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024342 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024344 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024345 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024347 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 }
24349 }
24350
24351 return idx < 0;
24352}
24353
24354/*
24355 * Free the variable with a pending return value.
24356 */
24357 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024358discard_pending_return(rettv)
24359 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360{
Bram Moolenaar33570922005-01-25 22:26:29 +000024361 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362}
24363
24364/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024365 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 * is an allocated string. Used by report_pending() for verbose messages.
24367 */
24368 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024369get_return_cmd(rettv)
24370 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024372 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024373 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024374 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024376 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024377 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024378 if (s == NULL)
24379 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024380
24381 STRCPY(IObuff, ":return ");
24382 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24383 if (STRLEN(s) + 8 >= IOSIZE)
24384 STRCPY(IObuff + IOSIZE - 4, "...");
24385 vim_free(tofree);
24386 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387}
24388
24389/*
24390 * Get next function line.
24391 * Called by do_cmdline() to get the next line.
24392 * Returns allocated string, or NULL for end of function.
24393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 char_u *
24395get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024396 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024398 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399{
Bram Moolenaar33570922005-01-25 22:26:29 +000024400 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024401 ufunc_T *fp = fcp->func;
24402 char_u *retval;
24403 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404
24405 /* If breakpoints have been added/deleted need to check for it. */
24406 if (fcp->dbg_tick != debug_tick)
24407 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024408 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 sourcing_lnum);
24410 fcp->dbg_tick = debug_tick;
24411 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024412#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024413 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024414 func_line_end(cookie);
24415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416
Bram Moolenaar05159a02005-02-26 23:04:13 +000024417 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024418 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24419 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 retval = NULL;
24421 else
24422 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024423 /* Skip NULL lines (continuation lines). */
24424 while (fcp->linenr < gap->ga_len
24425 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24426 ++fcp->linenr;
24427 if (fcp->linenr >= gap->ga_len)
24428 retval = NULL;
24429 else
24430 {
24431 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24432 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024433#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024434 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024435 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024436#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438 }
24439
24440 /* Did we encounter a breakpoint? */
24441 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24442 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024445 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 sourcing_lnum);
24447 fcp->dbg_tick = debug_tick;
24448 }
24449
24450 return retval;
24451}
24452
Bram Moolenaar05159a02005-02-26 23:04:13 +000024453#if defined(FEAT_PROFILE) || defined(PROTO)
24454/*
24455 * Called when starting to read a function line.
24456 * "sourcing_lnum" must be correct!
24457 * When skipping lines it may not actually be executed, but we won't find out
24458 * until later and we need to store the time now.
24459 */
24460 void
24461func_line_start(cookie)
24462 void *cookie;
24463{
24464 funccall_T *fcp = (funccall_T *)cookie;
24465 ufunc_T *fp = fcp->func;
24466
24467 if (fp->uf_profiling && sourcing_lnum >= 1
24468 && sourcing_lnum <= fp->uf_lines.ga_len)
24469 {
24470 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024471 /* Skip continuation lines. */
24472 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24473 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024474 fp->uf_tml_execed = FALSE;
24475 profile_start(&fp->uf_tml_start);
24476 profile_zero(&fp->uf_tml_children);
24477 profile_get_wait(&fp->uf_tml_wait);
24478 }
24479}
24480
24481/*
24482 * Called when actually executing a function line.
24483 */
24484 void
24485func_line_exec(cookie)
24486 void *cookie;
24487{
24488 funccall_T *fcp = (funccall_T *)cookie;
24489 ufunc_T *fp = fcp->func;
24490
24491 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24492 fp->uf_tml_execed = TRUE;
24493}
24494
24495/*
24496 * Called when done with a function line.
24497 */
24498 void
24499func_line_end(cookie)
24500 void *cookie;
24501{
24502 funccall_T *fcp = (funccall_T *)cookie;
24503 ufunc_T *fp = fcp->func;
24504
24505 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24506 {
24507 if (fp->uf_tml_execed)
24508 {
24509 ++fp->uf_tml_count[fp->uf_tml_idx];
24510 profile_end(&fp->uf_tml_start);
24511 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024512 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024513 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24514 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024515 }
24516 fp->uf_tml_idx = -1;
24517 }
24518}
24519#endif
24520
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521/*
24522 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024523 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 */
24525 int
24526func_has_ended(cookie)
24527 void *cookie;
24528{
Bram Moolenaar33570922005-01-25 22:26:29 +000024529 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530
24531 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24532 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024533 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 || fcp->returned);
24535}
24536
24537/*
24538 * return TRUE if cookie indicates a function which "abort"s on errors.
24539 */
24540 int
24541func_has_abort(cookie)
24542 void *cookie;
24543{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024544 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545}
24546
24547#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24548typedef enum
24549{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024550 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24551 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24552 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553} var_flavour_T;
24554
24555static var_flavour_T var_flavour __ARGS((char_u *varname));
24556
24557 static var_flavour_T
24558var_flavour(varname)
24559 char_u *varname;
24560{
24561 char_u *p = varname;
24562
24563 if (ASCII_ISUPPER(*p))
24564 {
24565 while (*(++p))
24566 if (ASCII_ISLOWER(*p))
24567 return VAR_FLAVOUR_SESSION;
24568 return VAR_FLAVOUR_VIMINFO;
24569 }
24570 else
24571 return VAR_FLAVOUR_DEFAULT;
24572}
24573#endif
24574
24575#if defined(FEAT_VIMINFO) || defined(PROTO)
24576/*
24577 * Restore global vars that start with a capital from the viminfo file
24578 */
24579 int
24580read_viminfo_varlist(virp, writing)
24581 vir_T *virp;
24582 int writing;
24583{
24584 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024585 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024586 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587
24588 if (!writing && (find_viminfo_parameter('!') != NULL))
24589 {
24590 tab = vim_strchr(virp->vir_line + 1, '\t');
24591 if (tab != NULL)
24592 {
24593 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024594 switch (*tab)
24595 {
24596 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024597#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024598 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024599#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024600 case 'D': type = VAR_DICT; break;
24601 case 'L': type = VAR_LIST; break;
24602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603
24604 tab = vim_strchr(tab, '\t');
24605 if (tab != NULL)
24606 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024607 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024608 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024609 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024611#ifdef FEAT_FLOAT
24612 else if (type == VAR_FLOAT)
24613 (void)string2float(tab + 1, &tv.vval.v_float);
24614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024616 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024617 if (type == VAR_DICT || type == VAR_LIST)
24618 {
24619 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24620
24621 if (etv == NULL)
24622 /* Failed to parse back the dict or list, use it as a
24623 * string. */
24624 tv.v_type = VAR_STRING;
24625 else
24626 {
24627 vim_free(tv.vval.v_string);
24628 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024629 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024630 }
24631 }
24632
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024633 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024634
24635 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024636 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024637 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24638 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 }
24640 }
24641 }
24642
24643 return viminfo_readline(virp);
24644}
24645
24646/*
24647 * Write global vars that start with a capital to the viminfo file
24648 */
24649 void
24650write_viminfo_varlist(fp)
24651 FILE *fp;
24652{
Bram Moolenaar33570922005-01-25 22:26:29 +000024653 hashitem_T *hi;
24654 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024655 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024656 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024657 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024658 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024659 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024660
24661 if (find_viminfo_parameter('!') == NULL)
24662 return;
24663
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024664 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024665
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024666 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024667 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024671 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024672 this_var = HI2DI(hi);
24673 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024674 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024675 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024676 {
24677 case VAR_STRING: s = "STR"; break;
24678 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024679#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024680 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024681#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024682 case VAR_DICT: s = "DIC"; break;
24683 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024684 default: continue;
24685 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024686 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024687 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024688 if (p != NULL)
24689 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024690 vim_free(tofree);
24691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 }
24693 }
24694}
24695#endif
24696
24697#if defined(FEAT_SESSION) || defined(PROTO)
24698 int
24699store_session_globals(fd)
24700 FILE *fd;
24701{
Bram Moolenaar33570922005-01-25 22:26:29 +000024702 hashitem_T *hi;
24703 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024704 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 char_u *p, *t;
24706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024707 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024708 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024710 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024712 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024713 this_var = HI2DI(hi);
24714 if ((this_var->di_tv.v_type == VAR_NUMBER
24715 || this_var->di_tv.v_type == VAR_STRING)
24716 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024717 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024718 /* Escape special characters with a backslash. Turn a LF and
24719 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024720 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024721 (char_u *)"\\\"\n\r");
24722 if (p == NULL) /* out of memory */
24723 break;
24724 for (t = p; *t != NUL; ++t)
24725 if (*t == '\n')
24726 *t = 'n';
24727 else if (*t == '\r')
24728 *t = 'r';
24729 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024730 this_var->di_key,
24731 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24732 : ' ',
24733 p,
24734 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24735 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024736 || put_eol(fd) == FAIL)
24737 {
24738 vim_free(p);
24739 return FAIL;
24740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 vim_free(p);
24742 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024743#ifdef FEAT_FLOAT
24744 else if (this_var->di_tv.v_type == VAR_FLOAT
24745 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24746 {
24747 float_T f = this_var->di_tv.vval.v_float;
24748 int sign = ' ';
24749
24750 if (f < 0)
24751 {
24752 f = -f;
24753 sign = '-';
24754 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024755 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024756 this_var->di_key, sign, f) < 0)
24757 || put_eol(fd) == FAIL)
24758 return FAIL;
24759 }
24760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024761 }
24762 }
24763 return OK;
24764}
24765#endif
24766
Bram Moolenaar661b1822005-07-28 22:36:45 +000024767/*
24768 * Display script name where an item was last set.
24769 * Should only be invoked when 'verbose' is non-zero.
24770 */
24771 void
24772last_set_msg(scriptID)
24773 scid_T scriptID;
24774{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024775 char_u *p;
24776
Bram Moolenaar661b1822005-07-28 22:36:45 +000024777 if (scriptID != 0)
24778 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024779 p = home_replace_save(NULL, get_scriptname(scriptID));
24780 if (p != NULL)
24781 {
24782 verbose_enter();
24783 MSG_PUTS(_("\n\tLast set from "));
24784 MSG_PUTS(p);
24785 vim_free(p);
24786 verbose_leave();
24787 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024788 }
24789}
24790
Bram Moolenaard812df62008-11-09 12:46:09 +000024791/*
24792 * List v:oldfiles in a nice way.
24793 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024794 void
24795ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024796 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024797{
24798 list_T *l = vimvars[VV_OLDFILES].vv_list;
24799 listitem_T *li;
24800 int nr = 0;
24801
24802 if (l == NULL)
24803 msg((char_u *)_("No old files"));
24804 else
24805 {
24806 msg_start();
24807 msg_scroll = TRUE;
24808 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24809 {
24810 msg_outnum((long)++nr);
24811 MSG_PUTS(": ");
24812 msg_outtrans(get_tv_string(&li->li_tv));
24813 msg_putchar('\n');
24814 out_flush(); /* output one line at a time */
24815 ui_breakcheck();
24816 }
24817 /* Assume "got_int" was set to truncate the listing. */
24818 got_int = FALSE;
24819
24820#ifdef FEAT_BROWSE_CMD
24821 if (cmdmod.browse)
24822 {
24823 quit_more = FALSE;
24824 nr = prompt_for_number(FALSE);
24825 msg_starthere();
24826 if (nr > 0)
24827 {
24828 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24829 (long)nr);
24830
24831 if (p != NULL)
24832 {
24833 p = expand_env_save(p);
24834 eap->arg = p;
24835 eap->cmdidx = CMD_edit;
24836 cmdmod.browse = FALSE;
24837 do_exedit(eap, NULL);
24838 vim_free(p);
24839 }
24840 }
24841 }
24842#endif
24843 }
24844}
24845
Bram Moolenaar53744302015-07-17 17:38:22 +020024846/* reset v:option_new, v:option_old and v:option_type */
24847 void
24848reset_v_option_vars()
24849{
24850 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24851 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24852 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24853}
24854
24855
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856#endif /* FEAT_EVAL */
24857
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024859#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860
24861#ifdef WIN3264
24862/*
24863 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24864 */
24865static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24866static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24867static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24868
24869/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024870 * Get the short path (8.3) for the filename in "fnamep".
24871 * Only works for a valid file name.
24872 * When the path gets longer "fnamep" is changed and the allocated buffer
24873 * is put in "bufp".
24874 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24875 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 */
24877 static int
24878get_short_pathname(fnamep, bufp, fnamelen)
24879 char_u **fnamep;
24880 char_u **bufp;
24881 int *fnamelen;
24882{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024883 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 char_u *newbuf;
24885
24886 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 l = GetShortPathName(*fnamep, *fnamep, len);
24888 if (l > len - 1)
24889 {
24890 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024891 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 newbuf = vim_strnsave(*fnamep, l);
24893 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024894 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895
24896 vim_free(*bufp);
24897 *fnamep = *bufp = newbuf;
24898
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024899 /* Really should always succeed, as the buffer is big enough. */
24900 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 }
24902
24903 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024904 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905}
24906
24907/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024908 * Get the short path (8.3) for the filename in "fname". The converted
24909 * path is returned in "bufp".
24910 *
24911 * Some of the directories specified in "fname" may not exist. This function
24912 * will shorten the existing directories at the beginning of the path and then
24913 * append the remaining non-existing path.
24914 *
24915 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024916 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024917 * bufp - Pointer to an allocated buffer for the filename.
24918 * fnamelen - Length of the filename pointed to by fname
24919 *
24920 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 */
24922 static int
24923shortpath_for_invalid_fname(fname, bufp, fnamelen)
24924 char_u **fname;
24925 char_u **bufp;
24926 int *fnamelen;
24927{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024928 char_u *short_fname, *save_fname, *pbuf_unused;
24929 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024931 int old_len, len;
24932 int new_len, sfx_len;
24933 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934
24935 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024936 old_len = *fnamelen;
24937 save_fname = vim_strnsave(*fname, old_len);
24938 pbuf_unused = NULL;
24939 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024941 endp = save_fname + old_len - 1; /* Find the end of the copy */
24942 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024944 /*
24945 * Try shortening the supplied path till it succeeds by removing one
24946 * directory at a time from the tail of the path.
24947 */
24948 len = 0;
24949 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024951 /* go back one path-separator */
24952 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24953 --endp;
24954 if (endp <= save_fname)
24955 break; /* processed the complete path */
24956
24957 /*
24958 * Replace the path separator with a NUL and try to shorten the
24959 * resulting path.
24960 */
24961 ch = *endp;
24962 *endp = 0;
24963 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024964 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024965 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24966 {
24967 retval = FAIL;
24968 goto theend;
24969 }
24970 *endp = ch; /* preserve the string */
24971
24972 if (len > 0)
24973 break; /* successfully shortened the path */
24974
24975 /* failed to shorten the path. Skip the path separator */
24976 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977 }
24978
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024979 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024981 /*
24982 * Succeeded in shortening the path. Now concatenate the shortened
24983 * path with the remaining path at the tail.
24984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024986 /* Compute the length of the new path. */
24987 sfx_len = (int)(save_endp - endp) + 1;
24988 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024990 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024992 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024994 /* There is not enough space in the currently allocated string,
24995 * copy it to a buffer big enough. */
24996 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024998 {
24999 retval = FAIL;
25000 goto theend;
25001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002 }
25003 else
25004 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025005 /* Transfer short_fname to the main buffer (it's big enough),
25006 * unless get_short_pathname() did its work in-place. */
25007 *fname = *bufp = save_fname;
25008 if (short_fname != save_fname)
25009 vim_strncpy(save_fname, short_fname, len);
25010 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025012
25013 /* concat the not-shortened part of the path */
25014 vim_strncpy(*fname + len, endp, sfx_len);
25015 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025017
25018theend:
25019 vim_free(pbuf_unused);
25020 vim_free(save_fname);
25021
25022 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023}
25024
25025/*
25026 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025027 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028 */
25029 static int
25030shortpath_for_partial(fnamep, bufp, fnamelen)
25031 char_u **fnamep;
25032 char_u **bufp;
25033 int *fnamelen;
25034{
25035 int sepcount, len, tflen;
25036 char_u *p;
25037 char_u *pbuf, *tfname;
25038 int hasTilde;
25039
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025040 /* Count up the path separators from the RHS.. so we know which part
25041 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025043 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 if (vim_ispathsep(*p))
25045 ++sepcount;
25046
25047 /* Need full path first (use expand_env() to remove a "~/") */
25048 hasTilde = (**fnamep == '~');
25049 if (hasTilde)
25050 pbuf = tfname = expand_env_save(*fnamep);
25051 else
25052 pbuf = tfname = FullName_save(*fnamep, FALSE);
25053
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025054 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025056 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25057 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058
25059 if (len == 0)
25060 {
25061 /* Don't have a valid filename, so shorten the rest of the
25062 * path if we can. This CAN give us invalid 8.3 filenames, but
25063 * there's not a lot of point in guessing what it might be.
25064 */
25065 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025066 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25067 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 }
25069
25070 /* Count the paths backward to find the beginning of the desired string. */
25071 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025072 {
25073#ifdef FEAT_MBYTE
25074 if (has_mbyte)
25075 p -= mb_head_off(tfname, p);
25076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 if (vim_ispathsep(*p))
25078 {
25079 if (sepcount == 0 || (hasTilde && sepcount == 1))
25080 break;
25081 else
25082 sepcount --;
25083 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 if (hasTilde)
25086 {
25087 --p;
25088 if (p >= tfname)
25089 *p = '~';
25090 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025091 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 }
25093 else
25094 ++p;
25095
25096 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25097 vim_free(*bufp);
25098 *fnamelen = (int)STRLEN(p);
25099 *bufp = pbuf;
25100 *fnamep = p;
25101
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025102 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103}
25104#endif /* WIN3264 */
25105
25106/*
25107 * Adjust a filename, according to a string of modifiers.
25108 * *fnamep must be NUL terminated when called. When returning, the length is
25109 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025110 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 * When there is an error, *fnamep is set to NULL.
25112 */
25113 int
25114modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25115 char_u *src; /* string with modifiers */
25116 int *usedlen; /* characters after src that are used */
25117 char_u **fnamep; /* file name so far */
25118 char_u **bufp; /* buffer for allocated file name or NULL */
25119 int *fnamelen; /* length of fnamep */
25120{
25121 int valid = 0;
25122 char_u *tail;
25123 char_u *s, *p, *pbuf;
25124 char_u dirname[MAXPATHL];
25125 int c;
25126 int has_fullname = 0;
25127#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025128 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025129 int has_shortname = 0;
25130#endif
25131
25132repeat:
25133 /* ":p" - full path/file_name */
25134 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25135 {
25136 has_fullname = 1;
25137
25138 valid |= VALID_PATH;
25139 *usedlen += 2;
25140
25141 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25142 if ((*fnamep)[0] == '~'
25143#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25144 && ((*fnamep)[1] == '/'
25145# ifdef BACKSLASH_IN_FILENAME
25146 || (*fnamep)[1] == '\\'
25147# endif
25148 || (*fnamep)[1] == NUL)
25149
25150#endif
25151 )
25152 {
25153 *fnamep = expand_env_save(*fnamep);
25154 vim_free(*bufp); /* free any allocated file name */
25155 *bufp = *fnamep;
25156 if (*fnamep == NULL)
25157 return -1;
25158 }
25159
25160 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025161 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 {
25163 if (vim_ispathsep(*p)
25164 && p[1] == '.'
25165 && (p[2] == NUL
25166 || vim_ispathsep(p[2])
25167 || (p[2] == '.'
25168 && (p[3] == NUL || vim_ispathsep(p[3])))))
25169 break;
25170 }
25171
25172 /* FullName_save() is slow, don't use it when not needed. */
25173 if (*p != NUL || !vim_isAbsName(*fnamep))
25174 {
25175 *fnamep = FullName_save(*fnamep, *p != NUL);
25176 vim_free(*bufp); /* free any allocated file name */
25177 *bufp = *fnamep;
25178 if (*fnamep == NULL)
25179 return -1;
25180 }
25181
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025182#ifdef WIN3264
25183# if _WIN32_WINNT >= 0x0500
25184 if (vim_strchr(*fnamep, '~') != NULL)
25185 {
25186 /* Expand 8.3 filename to full path. Needed to make sure the same
25187 * file does not have two different names.
25188 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25189 p = alloc(_MAX_PATH + 1);
25190 if (p != NULL)
25191 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025192 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025193 {
25194 vim_free(*bufp);
25195 *bufp = *fnamep = p;
25196 }
25197 else
25198 vim_free(p);
25199 }
25200 }
25201# endif
25202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 /* Append a path separator to a directory. */
25204 if (mch_isdir(*fnamep))
25205 {
25206 /* Make room for one or two extra characters. */
25207 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25208 vim_free(*bufp); /* free any allocated file name */
25209 *bufp = *fnamep;
25210 if (*fnamep == NULL)
25211 return -1;
25212 add_pathsep(*fnamep);
25213 }
25214 }
25215
25216 /* ":." - path relative to the current directory */
25217 /* ":~" - path relative to the home directory */
25218 /* ":8" - shortname path - postponed till after */
25219 while (src[*usedlen] == ':'
25220 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25221 {
25222 *usedlen += 2;
25223 if (c == '8')
25224 {
25225#ifdef WIN3264
25226 has_shortname = 1; /* Postpone this. */
25227#endif
25228 continue;
25229 }
25230 pbuf = NULL;
25231 /* Need full path first (use expand_env() to remove a "~/") */
25232 if (!has_fullname)
25233 {
25234 if (c == '.' && **fnamep == '~')
25235 p = pbuf = expand_env_save(*fnamep);
25236 else
25237 p = pbuf = FullName_save(*fnamep, FALSE);
25238 }
25239 else
25240 p = *fnamep;
25241
25242 has_fullname = 0;
25243
25244 if (p != NULL)
25245 {
25246 if (c == '.')
25247 {
25248 mch_dirname(dirname, MAXPATHL);
25249 s = shorten_fname(p, dirname);
25250 if (s != NULL)
25251 {
25252 *fnamep = s;
25253 if (pbuf != NULL)
25254 {
25255 vim_free(*bufp); /* free any allocated file name */
25256 *bufp = pbuf;
25257 pbuf = NULL;
25258 }
25259 }
25260 }
25261 else
25262 {
25263 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25264 /* Only replace it when it starts with '~' */
25265 if (*dirname == '~')
25266 {
25267 s = vim_strsave(dirname);
25268 if (s != NULL)
25269 {
25270 *fnamep = s;
25271 vim_free(*bufp);
25272 *bufp = s;
25273 }
25274 }
25275 }
25276 vim_free(pbuf);
25277 }
25278 }
25279
25280 tail = gettail(*fnamep);
25281 *fnamelen = (int)STRLEN(*fnamep);
25282
25283 /* ":h" - head, remove "/file_name", can be repeated */
25284 /* Don't remove the first "/" or "c:\" */
25285 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25286 {
25287 valid |= VALID_HEAD;
25288 *usedlen += 2;
25289 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025290 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025291 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 *fnamelen = (int)(tail - *fnamep);
25293#ifdef VMS
25294 if (*fnamelen > 0)
25295 *fnamelen += 1; /* the path separator is part of the path */
25296#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025297 if (*fnamelen == 0)
25298 {
25299 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25300 p = vim_strsave((char_u *)".");
25301 if (p == NULL)
25302 return -1;
25303 vim_free(*bufp);
25304 *bufp = *fnamep = tail = p;
25305 *fnamelen = 1;
25306 }
25307 else
25308 {
25309 while (tail > s && !after_pathsep(s, tail))
25310 mb_ptr_back(*fnamep, tail);
25311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 }
25313
25314 /* ":8" - shortname */
25315 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25316 {
25317 *usedlen += 2;
25318#ifdef WIN3264
25319 has_shortname = 1;
25320#endif
25321 }
25322
25323#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025324 /*
25325 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326 */
25327 if (has_shortname)
25328 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025329 /* Copy the string if it is shortened by :h and when it wasn't copied
25330 * yet, because we are going to change it in place. Avoids changing
25331 * the buffer name for "%:8". */
25332 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333 {
25334 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025335 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 return -1;
25337 vim_free(*bufp);
25338 *bufp = *fnamep = p;
25339 }
25340
25341 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025342 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 if (!has_fullname && !vim_isAbsName(*fnamep))
25344 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025345 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 return -1;
25347 }
25348 else
25349 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025350 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351
Bram Moolenaardc935552011-08-17 15:23:23 +020025352 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025354 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 return -1;
25356
25357 if (l == 0)
25358 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025359 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025361 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 return -1;
25363 }
25364 *fnamelen = l;
25365 }
25366 }
25367#endif /* WIN3264 */
25368
25369 /* ":t" - tail, just the basename */
25370 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25371 {
25372 *usedlen += 2;
25373 *fnamelen -= (int)(tail - *fnamep);
25374 *fnamep = tail;
25375 }
25376
25377 /* ":e" - extension, can be repeated */
25378 /* ":r" - root, without extension, can be repeated */
25379 while (src[*usedlen] == ':'
25380 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25381 {
25382 /* find a '.' in the tail:
25383 * - for second :e: before the current fname
25384 * - otherwise: The last '.'
25385 */
25386 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25387 s = *fnamep - 2;
25388 else
25389 s = *fnamep + *fnamelen - 1;
25390 for ( ; s > tail; --s)
25391 if (s[0] == '.')
25392 break;
25393 if (src[*usedlen + 1] == 'e') /* :e */
25394 {
25395 if (s > tail)
25396 {
25397 *fnamelen += (int)(*fnamep - (s + 1));
25398 *fnamep = s + 1;
25399#ifdef VMS
25400 /* cut version from the extension */
25401 s = *fnamep + *fnamelen - 1;
25402 for ( ; s > *fnamep; --s)
25403 if (s[0] == ';')
25404 break;
25405 if (s > *fnamep)
25406 *fnamelen = s - *fnamep;
25407#endif
25408 }
25409 else if (*fnamep <= tail)
25410 *fnamelen = 0;
25411 }
25412 else /* :r */
25413 {
25414 if (s > tail) /* remove one extension */
25415 *fnamelen = (int)(s - *fnamep);
25416 }
25417 *usedlen += 2;
25418 }
25419
25420 /* ":s?pat?foo?" - substitute */
25421 /* ":gs?pat?foo?" - global substitute */
25422 if (src[*usedlen] == ':'
25423 && (src[*usedlen + 1] == 's'
25424 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25425 {
25426 char_u *str;
25427 char_u *pat;
25428 char_u *sub;
25429 int sep;
25430 char_u *flags;
25431 int didit = FALSE;
25432
25433 flags = (char_u *)"";
25434 s = src + *usedlen + 2;
25435 if (src[*usedlen + 1] == 'g')
25436 {
25437 flags = (char_u *)"g";
25438 ++s;
25439 }
25440
25441 sep = *s++;
25442 if (sep)
25443 {
25444 /* find end of pattern */
25445 p = vim_strchr(s, sep);
25446 if (p != NULL)
25447 {
25448 pat = vim_strnsave(s, (int)(p - s));
25449 if (pat != NULL)
25450 {
25451 s = p + 1;
25452 /* find end of substitution */
25453 p = vim_strchr(s, sep);
25454 if (p != NULL)
25455 {
25456 sub = vim_strnsave(s, (int)(p - s));
25457 str = vim_strnsave(*fnamep, *fnamelen);
25458 if (sub != NULL && str != NULL)
25459 {
25460 *usedlen = (int)(p + 1 - src);
25461 s = do_string_sub(str, pat, sub, flags);
25462 if (s != NULL)
25463 {
25464 *fnamep = s;
25465 *fnamelen = (int)STRLEN(s);
25466 vim_free(*bufp);
25467 *bufp = s;
25468 didit = TRUE;
25469 }
25470 }
25471 vim_free(sub);
25472 vim_free(str);
25473 }
25474 vim_free(pat);
25475 }
25476 }
25477 /* after using ":s", repeat all the modifiers */
25478 if (didit)
25479 goto repeat;
25480 }
25481 }
25482
Bram Moolenaar26df0922014-02-23 23:39:13 +010025483 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25484 {
25485 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25486 if (p == NULL)
25487 return -1;
25488 vim_free(*bufp);
25489 *bufp = *fnamep = p;
25490 *fnamelen = (int)STRLEN(p);
25491 *usedlen += 2;
25492 }
25493
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 return valid;
25495}
25496
25497/*
25498 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25499 * "flags" can be "g" to do a global substitute.
25500 * Returns an allocated string, NULL for error.
25501 */
25502 char_u *
25503do_string_sub(str, pat, sub, flags)
25504 char_u *str;
25505 char_u *pat;
25506 char_u *sub;
25507 char_u *flags;
25508{
25509 int sublen;
25510 regmatch_T regmatch;
25511 int i;
25512 int do_all;
25513 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025514 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515 garray_T ga;
25516 char_u *ret;
25517 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025518 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519
25520 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25521 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025522 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
25524 ga_init2(&ga, 1, 200);
25525
25526 do_all = (flags[0] == 'g');
25527
25528 regmatch.rm_ic = p_ic;
25529 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25530 if (regmatch.regprog != NULL)
25531 {
25532 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025533 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25535 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025536 /* Skip empty match except for first match. */
25537 if (regmatch.startp[0] == regmatch.endp[0])
25538 {
25539 if (zero_width == regmatch.startp[0])
25540 {
25541 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025542 i = MB_PTR2LEN(tail);
25543 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25544 (size_t)i);
25545 ga.ga_len += i;
25546 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025547 continue;
25548 }
25549 zero_width = regmatch.startp[0];
25550 }
25551
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 /*
25553 * Get some space for a temporary buffer to do the substitution
25554 * into. It will contain:
25555 * - The text up to where the match is.
25556 * - The substituted text.
25557 * - The text after the match.
25558 */
25559 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025560 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25562 {
25563 ga_clear(&ga);
25564 break;
25565 }
25566
25567 /* copy the text up to where the match is */
25568 i = (int)(regmatch.startp[0] - tail);
25569 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25570 /* add the substituted text */
25571 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25572 + ga.ga_len + i, TRUE, TRUE, FALSE);
25573 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025574 tail = regmatch.endp[0];
25575 if (*tail == NUL)
25576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 if (!do_all)
25578 break;
25579 }
25580
25581 if (ga.ga_data != NULL)
25582 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25583
Bram Moolenaar473de612013-06-08 18:19:48 +020025584 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 }
25586
25587 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25588 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025589 if (p_cpo == empty_option)
25590 p_cpo = save_cpo;
25591 else
25592 /* Darn, evaluating {sub} expression changed the value. */
25593 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594
25595 return ret;
25596}
25597
25598#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */