blob: e2f40f0bce9e5a1e7cf580159ac5d330ab6e46f9 [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 Moolenaar7e47d1a2015-08-25 16:19:05 +020012294 if (switch_win(&oldcurwin, &oldtabpage,
12295 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012296 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012297 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012298 /* look up the variable */
12299 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12300 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12301 if (v != NULL)
12302 {
12303 copy_tv(&v->di_tv, rettv);
12304 done = TRUE;
12305 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012306 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012307
12308 /* restore previous notion of curwin */
12309 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012310 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012311
12312 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012313 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012314}
12315
12316/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012317 * "gettabwinvar()" function
12318 */
12319 static void
12320f_gettabwinvar(argvars, rettv)
12321 typval_T *argvars;
12322 typval_T *rettv;
12323{
12324 getwinvar(argvars, rettv, 1);
12325}
12326
12327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 * "getwinposx()" function
12329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336#ifdef FEAT_GUI
12337 if (gui.in_use)
12338 {
12339 int x, y;
12340
12341 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 }
12344#endif
12345}
12346
12347/*
12348 * "getwinposy()" function
12349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012351f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012352 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356#ifdef FEAT_GUI
12357 if (gui.in_use)
12358 {
12359 int x, y;
12360
12361 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 }
12364#endif
12365}
12366
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012367/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012368 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012369 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012370 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012371find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012372 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012373 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012374{
12375#ifdef FEAT_WINDOWS
12376 win_T *wp;
12377#endif
12378 int nr;
12379
12380 nr = get_tv_number_chk(vp, NULL);
12381
12382#ifdef FEAT_WINDOWS
12383 if (nr < 0)
12384 return NULL;
12385 if (nr == 0)
12386 return curwin;
12387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012388 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12389 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012390 if (--nr <= 0)
12391 break;
12392 return wp;
12393#else
12394 if (nr == 0 || nr == 1)
12395 return curwin;
12396 return NULL;
12397#endif
12398}
12399
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400/*
12401 * "getwinvar()" function
12402 */
12403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012405 typval_T *argvars;
12406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012408 getwinvar(argvars, rettv, 0);
12409}
12410
12411/*
12412 * getwinvar() and gettabwinvar()
12413 */
12414 static void
12415getwinvar(argvars, rettv, off)
12416 typval_T *argvars;
12417 typval_T *rettv;
12418 int off; /* 1 for gettabwinvar() */
12419{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 win_T *win, *oldcurwin;
12421 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012423 tabpage_T *tp = NULL;
12424 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012425 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012427#ifdef FEAT_WINDOWS
12428 if (off == 1)
12429 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12430 else
12431 tp = curtab;
12432#endif
12433 win = find_win_by_nr(&argvars[off], tp);
12434 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012437 rettv->v_type = VAR_STRING;
12438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439
12440 if (win != NULL && varname != NULL)
12441 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012442 /* Set curwin to be our win, temporarily. Also set the tabpage,
12443 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012444 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012445 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012446 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012447 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012448 if (get_option_tv(&varname, rettv, 1) == OK)
12449 done = TRUE;
12450 }
12451 else
12452 {
12453 /* Look up the variable. */
12454 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12455 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12456 varname, FALSE);
12457 if (v != NULL)
12458 {
12459 copy_tv(&v->di_tv, rettv);
12460 done = TRUE;
12461 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012464
12465 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012466 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 }
12468
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012469 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12470 /* use the default return value */
12471 copy_tv(&argvars[off + 2], rettv);
12472
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 --emsg_off;
12474}
12475
12476/*
12477 * "glob()" function
12478 */
12479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012481 typval_T *argvars;
12482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012484 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012486 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012488 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012489 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012491 if (argvars[1].v_type != VAR_UNKNOWN)
12492 {
12493 if (get_tv_number_chk(&argvars[1], &error))
12494 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012496 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012497 if (get_tv_number_chk(&argvars[2], &error))
12498 {
12499 rettv->v_type = VAR_LIST;
12500 rettv->vval.v_list = NULL;
12501 }
12502 if (argvars[3].v_type != VAR_UNKNOWN
12503 && get_tv_number_chk(&argvars[3], &error))
12504 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012505 }
12506 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012507 if (!error)
12508 {
12509 ExpandInit(&xpc);
12510 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012511 if (p_wic)
12512 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012513 if (rettv->v_type == VAR_STRING)
12514 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012515 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012516 else if (rettv_list_alloc(rettv) != FAIL)
12517 {
12518 int i;
12519
12520 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12521 NULL, options, WILD_ALL_KEEP);
12522 for (i = 0; i < xpc.xp_numfiles; i++)
12523 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12524
12525 ExpandCleanup(&xpc);
12526 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012527 }
12528 else
12529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530}
12531
12532/*
12533 * "globpath()" function
12534 */
12535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 typval_T *argvars;
12538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012540 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012543 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012544 garray_T ga;
12545 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012547 /* When the optional second argument is non-zero, don't remove matches
12548 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012550 if (argvars[2].v_type != VAR_UNKNOWN)
12551 {
12552 if (get_tv_number_chk(&argvars[2], &error))
12553 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012554 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012555 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012556 if (get_tv_number_chk(&argvars[3], &error))
12557 {
12558 rettv->v_type = VAR_LIST;
12559 rettv->vval.v_list = NULL;
12560 }
12561 if (argvars[4].v_type != VAR_UNKNOWN
12562 && get_tv_number_chk(&argvars[4], &error))
12563 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012564 }
12565 }
12566 if (file != NULL && !error)
12567 {
12568 ga_init2(&ga, (int)sizeof(char_u *), 10);
12569 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12570 if (rettv->v_type == VAR_STRING)
12571 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12572 else if (rettv_list_alloc(rettv) != FAIL)
12573 for (i = 0; i < ga.ga_len; ++i)
12574 list_append_string(rettv->vval.v_list,
12575 ((char_u **)(ga.ga_data))[i], -1);
12576 ga_clear_strings(&ga);
12577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012578 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580}
12581
12582/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012583 * "glob2regpat()" function
12584 */
12585 static void
12586f_glob2regpat(argvars, rettv)
12587 typval_T *argvars;
12588 typval_T *rettv;
12589{
12590 char_u *pat = get_tv_string_chk(&argvars[0]);
12591
12592 rettv->v_type = VAR_STRING;
12593 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12594}
12595
12596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 * "has()" function
12598 */
12599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012600f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012601 typval_T *argvars;
12602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603{
12604 int i;
12605 char_u *name;
12606 int n = FALSE;
12607 static char *(has_list[]) =
12608 {
12609#ifdef AMIGA
12610 "amiga",
12611# ifdef FEAT_ARP
12612 "arp",
12613# endif
12614#endif
12615#ifdef __BEOS__
12616 "beos",
12617#endif
12618#ifdef MSDOS
12619# ifdef DJGPP
12620 "dos32",
12621# else
12622 "dos16",
12623# endif
12624#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012625#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 "mac",
12627#endif
12628#if defined(MACOS_X_UNIX)
12629 "macunix",
12630#endif
12631#ifdef OS2
12632 "os2",
12633#endif
12634#ifdef __QNX__
12635 "qnx",
12636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637#ifdef UNIX
12638 "unix",
12639#endif
12640#ifdef VMS
12641 "vms",
12642#endif
12643#ifdef WIN16
12644 "win16",
12645#endif
12646#ifdef WIN32
12647 "win32",
12648#endif
12649#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12650 "win32unix",
12651#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012652#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 "win64",
12654#endif
12655#ifdef EBCDIC
12656 "ebcdic",
12657#endif
12658#ifndef CASE_INSENSITIVE_FILENAME
12659 "fname_case",
12660#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012661#ifdef HAVE_ACL
12662 "acl",
12663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664#ifdef FEAT_ARABIC
12665 "arabic",
12666#endif
12667#ifdef FEAT_AUTOCMD
12668 "autocmd",
12669#endif
12670#ifdef FEAT_BEVAL
12671 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012672# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12673 "balloon_multiline",
12674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675#endif
12676#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12677 "builtin_terms",
12678# ifdef ALL_BUILTIN_TCAPS
12679 "all_builtin_terms",
12680# endif
12681#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012682#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12683 || defined(FEAT_GUI_W32) \
12684 || defined(FEAT_GUI_MOTIF))
12685 "browsefilter",
12686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687#ifdef FEAT_BYTEOFF
12688 "byte_offset",
12689#endif
12690#ifdef FEAT_CINDENT
12691 "cindent",
12692#endif
12693#ifdef FEAT_CLIENTSERVER
12694 "clientserver",
12695#endif
12696#ifdef FEAT_CLIPBOARD
12697 "clipboard",
12698#endif
12699#ifdef FEAT_CMDL_COMPL
12700 "cmdline_compl",
12701#endif
12702#ifdef FEAT_CMDHIST
12703 "cmdline_hist",
12704#endif
12705#ifdef FEAT_COMMENTS
12706 "comments",
12707#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012708#ifdef FEAT_CONCEAL
12709 "conceal",
12710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711#ifdef FEAT_CRYPT
12712 "cryptv",
12713#endif
12714#ifdef FEAT_CSCOPE
12715 "cscope",
12716#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012717#ifdef FEAT_CURSORBIND
12718 "cursorbind",
12719#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012720#ifdef CURSOR_SHAPE
12721 "cursorshape",
12722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723#ifdef DEBUG
12724 "debug",
12725#endif
12726#ifdef FEAT_CON_DIALOG
12727 "dialog_con",
12728#endif
12729#ifdef FEAT_GUI_DIALOG
12730 "dialog_gui",
12731#endif
12732#ifdef FEAT_DIFF
12733 "diff",
12734#endif
12735#ifdef FEAT_DIGRAPHS
12736 "digraphs",
12737#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012738#ifdef FEAT_DIRECTX
12739 "directx",
12740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#ifdef FEAT_DND
12742 "dnd",
12743#endif
12744#ifdef FEAT_EMACS_TAGS
12745 "emacs_tags",
12746#endif
12747 "eval", /* always present, of course! */
12748#ifdef FEAT_EX_EXTRA
12749 "ex_extra",
12750#endif
12751#ifdef FEAT_SEARCH_EXTRA
12752 "extra_search",
12753#endif
12754#ifdef FEAT_FKMAP
12755 "farsi",
12756#endif
12757#ifdef FEAT_SEARCHPATH
12758 "file_in_path",
12759#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012760#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012761 "filterpipe",
12762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763#ifdef FEAT_FIND_ID
12764 "find_in_path",
12765#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012766#ifdef FEAT_FLOAT
12767 "float",
12768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#ifdef FEAT_FOLDING
12770 "folding",
12771#endif
12772#ifdef FEAT_FOOTER
12773 "footer",
12774#endif
12775#if !defined(USE_SYSTEM) && defined(UNIX)
12776 "fork",
12777#endif
12778#ifdef FEAT_GETTEXT
12779 "gettext",
12780#endif
12781#ifdef FEAT_GUI
12782 "gui",
12783#endif
12784#ifdef FEAT_GUI_ATHENA
12785# ifdef FEAT_GUI_NEXTAW
12786 "gui_neXtaw",
12787# else
12788 "gui_athena",
12789# endif
12790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791#ifdef FEAT_GUI_GTK
12792 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012795#ifdef FEAT_GUI_GNOME
12796 "gui_gnome",
12797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#ifdef FEAT_GUI_MAC
12799 "gui_mac",
12800#endif
12801#ifdef FEAT_GUI_MOTIF
12802 "gui_motif",
12803#endif
12804#ifdef FEAT_GUI_PHOTON
12805 "gui_photon",
12806#endif
12807#ifdef FEAT_GUI_W16
12808 "gui_win16",
12809#endif
12810#ifdef FEAT_GUI_W32
12811 "gui_win32",
12812#endif
12813#ifdef FEAT_HANGULIN
12814 "hangul_input",
12815#endif
12816#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12817 "iconv",
12818#endif
12819#ifdef FEAT_INS_EXPAND
12820 "insert_expand",
12821#endif
12822#ifdef FEAT_JUMPLIST
12823 "jumplist",
12824#endif
12825#ifdef FEAT_KEYMAP
12826 "keymap",
12827#endif
12828#ifdef FEAT_LANGMAP
12829 "langmap",
12830#endif
12831#ifdef FEAT_LIBCALL
12832 "libcall",
12833#endif
12834#ifdef FEAT_LINEBREAK
12835 "linebreak",
12836#endif
12837#ifdef FEAT_LISP
12838 "lispindent",
12839#endif
12840#ifdef FEAT_LISTCMDS
12841 "listcmds",
12842#endif
12843#ifdef FEAT_LOCALMAP
12844 "localmap",
12845#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012846#ifdef FEAT_LUA
12847# ifndef DYNAMIC_LUA
12848 "lua",
12849# endif
12850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#ifdef FEAT_MENU
12852 "menu",
12853#endif
12854#ifdef FEAT_SESSION
12855 "mksession",
12856#endif
12857#ifdef FEAT_MODIFY_FNAME
12858 "modify_fname",
12859#endif
12860#ifdef FEAT_MOUSE
12861 "mouse",
12862#endif
12863#ifdef FEAT_MOUSESHAPE
12864 "mouseshape",
12865#endif
12866#if defined(UNIX) || defined(VMS)
12867# ifdef FEAT_MOUSE_DEC
12868 "mouse_dec",
12869# endif
12870# ifdef FEAT_MOUSE_GPM
12871 "mouse_gpm",
12872# endif
12873# ifdef FEAT_MOUSE_JSB
12874 "mouse_jsbterm",
12875# endif
12876# ifdef FEAT_MOUSE_NET
12877 "mouse_netterm",
12878# endif
12879# ifdef FEAT_MOUSE_PTERM
12880 "mouse_pterm",
12881# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012882# ifdef FEAT_MOUSE_SGR
12883 "mouse_sgr",
12884# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012885# ifdef FEAT_SYSMOUSE
12886 "mouse_sysmouse",
12887# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012888# ifdef FEAT_MOUSE_URXVT
12889 "mouse_urxvt",
12890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891# ifdef FEAT_MOUSE_XTERM
12892 "mouse_xterm",
12893# endif
12894#endif
12895#ifdef FEAT_MBYTE
12896 "multi_byte",
12897#endif
12898#ifdef FEAT_MBYTE_IME
12899 "multi_byte_ime",
12900#endif
12901#ifdef FEAT_MULTI_LANG
12902 "multi_lang",
12903#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012904#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012905#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012906 "mzscheme",
12907#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909#ifdef FEAT_OLE
12910 "ole",
12911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#ifdef FEAT_PATH_EXTRA
12913 "path_extra",
12914#endif
12915#ifdef FEAT_PERL
12916#ifndef DYNAMIC_PERL
12917 "perl",
12918#endif
12919#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012920#ifdef FEAT_PERSISTENT_UNDO
12921 "persistent_undo",
12922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923#ifdef FEAT_PYTHON
12924#ifndef DYNAMIC_PYTHON
12925 "python",
12926#endif
12927#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012928#ifdef FEAT_PYTHON3
12929#ifndef DYNAMIC_PYTHON3
12930 "python3",
12931#endif
12932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933#ifdef FEAT_POSTSCRIPT
12934 "postscript",
12935#endif
12936#ifdef FEAT_PRINTER
12937 "printer",
12938#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012939#ifdef FEAT_PROFILE
12940 "profile",
12941#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012942#ifdef FEAT_RELTIME
12943 "reltime",
12944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945#ifdef FEAT_QUICKFIX
12946 "quickfix",
12947#endif
12948#ifdef FEAT_RIGHTLEFT
12949 "rightleft",
12950#endif
12951#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12952 "ruby",
12953#endif
12954#ifdef FEAT_SCROLLBIND
12955 "scrollbind",
12956#endif
12957#ifdef FEAT_CMDL_INFO
12958 "showcmd",
12959 "cmdline_info",
12960#endif
12961#ifdef FEAT_SIGNS
12962 "signs",
12963#endif
12964#ifdef FEAT_SMARTINDENT
12965 "smartindent",
12966#endif
12967#ifdef FEAT_SNIFF
12968 "sniff",
12969#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012970#ifdef STARTUPTIME
12971 "startuptime",
12972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973#ifdef FEAT_STL_OPT
12974 "statusline",
12975#endif
12976#ifdef FEAT_SUN_WORKSHOP
12977 "sun_workshop",
12978#endif
12979#ifdef FEAT_NETBEANS_INTG
12980 "netbeans_intg",
12981#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012982#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012983 "spell",
12984#endif
12985#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 "syntax",
12987#endif
12988#if defined(USE_SYSTEM) || !defined(UNIX)
12989 "system",
12990#endif
12991#ifdef FEAT_TAG_BINS
12992 "tag_binary",
12993#endif
12994#ifdef FEAT_TAG_OLDSTATIC
12995 "tag_old_static",
12996#endif
12997#ifdef FEAT_TAG_ANYWHITE
12998 "tag_any_white",
12999#endif
13000#ifdef FEAT_TCL
13001# ifndef DYNAMIC_TCL
13002 "tcl",
13003# endif
13004#endif
13005#ifdef TERMINFO
13006 "terminfo",
13007#endif
13008#ifdef FEAT_TERMRESPONSE
13009 "termresponse",
13010#endif
13011#ifdef FEAT_TEXTOBJ
13012 "textobjects",
13013#endif
13014#ifdef HAVE_TGETENT
13015 "tgetent",
13016#endif
13017#ifdef FEAT_TITLE
13018 "title",
13019#endif
13020#ifdef FEAT_TOOLBAR
13021 "toolbar",
13022#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013023#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13024 "unnamedplus",
13025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026#ifdef FEAT_USR_CMDS
13027 "user-commands", /* was accidentally included in 5.4 */
13028 "user_commands",
13029#endif
13030#ifdef FEAT_VIMINFO
13031 "viminfo",
13032#endif
13033#ifdef FEAT_VERTSPLIT
13034 "vertsplit",
13035#endif
13036#ifdef FEAT_VIRTUALEDIT
13037 "virtualedit",
13038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040#ifdef FEAT_VISUALEXTRA
13041 "visualextra",
13042#endif
13043#ifdef FEAT_VREPLACE
13044 "vreplace",
13045#endif
13046#ifdef FEAT_WILDIGN
13047 "wildignore",
13048#endif
13049#ifdef FEAT_WILDMENU
13050 "wildmenu",
13051#endif
13052#ifdef FEAT_WINDOWS
13053 "windows",
13054#endif
13055#ifdef FEAT_WAK
13056 "winaltkeys",
13057#endif
13058#ifdef FEAT_WRITEBACKUP
13059 "writebackup",
13060#endif
13061#ifdef FEAT_XIM
13062 "xim",
13063#endif
13064#ifdef FEAT_XFONTSET
13065 "xfontset",
13066#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013067#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013068 "xpm",
13069 "xpm_w32", /* for backward compatibility */
13070#else
13071# if defined(HAVE_XPM)
13072 "xpm",
13073# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075#ifdef USE_XSMP
13076 "xsmp",
13077#endif
13078#ifdef USE_XSMP_INTERACT
13079 "xsmp_interact",
13080#endif
13081#ifdef FEAT_XCLIPBOARD
13082 "xterm_clipboard",
13083#endif
13084#ifdef FEAT_XTERM_SAVE
13085 "xterm_save",
13086#endif
13087#if defined(UNIX) && defined(FEAT_X11)
13088 "X11",
13089#endif
13090 NULL
13091 };
13092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013093 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 for (i = 0; has_list[i] != NULL; ++i)
13095 if (STRICMP(name, has_list[i]) == 0)
13096 {
13097 n = TRUE;
13098 break;
13099 }
13100
13101 if (n == FALSE)
13102 {
13103 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013104 {
13105 if (name[5] == '-'
13106 && STRLEN(name) > 11
13107 && vim_isdigit(name[6])
13108 && vim_isdigit(name[8])
13109 && vim_isdigit(name[10]))
13110 {
13111 int major = atoi((char *)name + 6);
13112 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013113
13114 /* Expect "patch-9.9.01234". */
13115 n = (major < VIM_VERSION_MAJOR
13116 || (major == VIM_VERSION_MAJOR
13117 && (minor < VIM_VERSION_MINOR
13118 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013119 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013120 }
13121 else
13122 n = has_patch(atoi((char *)name + 5));
13123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 else if (STRICMP(name, "vim_starting") == 0)
13125 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013126#ifdef FEAT_MBYTE
13127 else if (STRICMP(name, "multi_byte_encoding") == 0)
13128 n = has_mbyte;
13129#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013130#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13131 else if (STRICMP(name, "balloon_multiline") == 0)
13132 n = multiline_balloon_available();
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef DYNAMIC_TCL
13135 else if (STRICMP(name, "tcl") == 0)
13136 n = tcl_enabled(FALSE);
13137#endif
13138#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13139 else if (STRICMP(name, "iconv") == 0)
13140 n = iconv_enabled(FALSE);
13141#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013142#ifdef DYNAMIC_LUA
13143 else if (STRICMP(name, "lua") == 0)
13144 n = lua_enabled(FALSE);
13145#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013146#ifdef DYNAMIC_MZSCHEME
13147 else if (STRICMP(name, "mzscheme") == 0)
13148 n = mzscheme_enabled(FALSE);
13149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef DYNAMIC_RUBY
13151 else if (STRICMP(name, "ruby") == 0)
13152 n = ruby_enabled(FALSE);
13153#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013154#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155#ifdef DYNAMIC_PYTHON
13156 else if (STRICMP(name, "python") == 0)
13157 n = python_enabled(FALSE);
13158#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013159#endif
13160#ifdef FEAT_PYTHON3
13161#ifdef DYNAMIC_PYTHON3
13162 else if (STRICMP(name, "python3") == 0)
13163 n = python3_enabled(FALSE);
13164#endif
13165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166#ifdef DYNAMIC_PERL
13167 else if (STRICMP(name, "perl") == 0)
13168 n = perl_enabled(FALSE);
13169#endif
13170#ifdef FEAT_GUI
13171 else if (STRICMP(name, "gui_running") == 0)
13172 n = (gui.in_use || gui.starting);
13173# ifdef FEAT_GUI_W32
13174 else if (STRICMP(name, "gui_win32s") == 0)
13175 n = gui_is_win32s();
13176# endif
13177# ifdef FEAT_BROWSE
13178 else if (STRICMP(name, "browse") == 0)
13179 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13180# endif
13181#endif
13182#ifdef FEAT_SYN_HL
13183 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013184 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185#endif
13186#if defined(WIN3264)
13187 else if (STRICMP(name, "win95") == 0)
13188 n = mch_windows95();
13189#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013190#ifdef FEAT_NETBEANS_INTG
13191 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013192 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 }
13195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197}
13198
13199/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013200 * "has_key()" function
13201 */
13202 static void
13203f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 typval_T *argvars;
13205 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013206{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013207 if (argvars[0].v_type != VAR_DICT)
13208 {
13209 EMSG(_(e_dictreq));
13210 return;
13211 }
13212 if (argvars[0].vval.v_dict == NULL)
13213 return;
13214
13215 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013216 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013217}
13218
13219/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013220 * "haslocaldir()" function
13221 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013222 static void
13223f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013224 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013225 typval_T *rettv;
13226{
13227 rettv->vval.v_number = (curwin->w_localdir != NULL);
13228}
13229
13230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 * "hasmapto()" function
13232 */
13233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013235 typval_T *argvars;
13236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237{
13238 char_u *name;
13239 char_u *mode;
13240 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013241 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 mode = (char_u *)"nvo";
13246 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013249 if (argvars[2].v_type != VAR_UNKNOWN)
13250 abbr = get_tv_number(&argvars[2]);
13251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252
Bram Moolenaar2c932302006-03-18 21:42:09 +000013253 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257}
13258
13259/*
13260 * "histadd()" function
13261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013264 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266{
13267#ifdef FEAT_CMDHIST
13268 int histype;
13269 char_u *str;
13270 char_u buf[NUMBUFLEN];
13271#endif
13272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 if (check_restricted() || check_secure())
13275 return;
13276#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13278 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 if (histype >= 0)
13280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 if (*str != NUL)
13283 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013284 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 return;
13288 }
13289 }
13290#endif
13291}
13292
13293/*
13294 * "histdel()" function
13295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013298 typval_T *argvars UNUSED;
13299 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300{
13301#ifdef FEAT_CMDHIST
13302 int n;
13303 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13307 if (str == NULL)
13308 n = 0;
13309 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013312 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 else
13317 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013318 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319 get_tv_string_buf(&argvars[1], buf));
13320 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321#endif
13322}
13323
13324/*
13325 * "histget()" function
13326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
13332#ifdef FEAT_CMDHIST
13333 int type;
13334 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013335 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013337 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13338 if (str == NULL)
13339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013341 {
13342 type = get_histtype(str);
13343 if (argvars[1].v_type == VAR_UNKNOWN)
13344 idx = get_history_idx(type);
13345 else
13346 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13347 /* -1 on type error */
13348 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013351 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013353 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354}
13355
13356/*
13357 * "histnr()" function
13358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013361 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363{
13364 int i;
13365
13366#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013367 char_u *history = get_tv_string_chk(&argvars[0]);
13368
13369 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 if (i >= HIST_CMD && i < HIST_COUNT)
13371 i = get_history_idx(i);
13372 else
13373#endif
13374 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376}
13377
13378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 * "highlightID(name)" function
13380 */
13381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *argvars;
13384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387}
13388
13389/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 * "highlight_exists()" function
13391 */
13392 static void
13393f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013394 typval_T *argvars;
13395 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013396{
13397 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13398}
13399
13400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 * "hostname()" function
13402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013405 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407{
13408 char_u hostname[256];
13409
13410 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 rettv->v_type = VAR_STRING;
13412 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413}
13414
13415/*
13416 * iconv() function
13417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013420 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422{
13423#ifdef FEAT_MBYTE
13424 char_u buf1[NUMBUFLEN];
13425 char_u buf2[NUMBUFLEN];
13426 char_u *from, *to, *str;
13427 vimconv_T vimconv;
13428#endif
13429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430 rettv->v_type = VAR_STRING;
13431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432
13433#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 str = get_tv_string(&argvars[0]);
13435 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13436 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 vimconv.vc_type = CONV_NONE;
13438 convert_setup(&vimconv, from, to);
13439
13440 /* If the encodings are equal, no conversion needed. */
13441 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445
13446 convert_setup(&vimconv, NULL, NULL);
13447 vim_free(from);
13448 vim_free(to);
13449#endif
13450}
13451
13452/*
13453 * "indent()" function
13454 */
13455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013457 typval_T *argvars;
13458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459{
13460 linenr_T lnum;
13461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467}
13468
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013469/*
13470 * "index()" function
13471 */
13472 static void
13473f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *argvars;
13475 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013476{
Bram Moolenaar33570922005-01-25 22:26:29 +000013477 list_T *l;
13478 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013479 long idx = 0;
13480 int ic = FALSE;
13481
13482 rettv->vval.v_number = -1;
13483 if (argvars[0].v_type != VAR_LIST)
13484 {
13485 EMSG(_(e_listreq));
13486 return;
13487 }
13488 l = argvars[0].vval.v_list;
13489 if (l != NULL)
13490 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013491 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 int error = FALSE;
13495
Bram Moolenaar758711c2005-02-02 23:11:38 +000013496 /* Start at specified item. Use the cached index that list_find()
13497 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013498 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013499 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013500 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 ic = get_tv_number_chk(&argvars[3], &error);
13502 if (error)
13503 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013505
Bram Moolenaar758711c2005-02-02 23:11:38 +000013506 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013507 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013508 {
13509 rettv->vval.v_number = idx;
13510 break;
13511 }
13512 }
13513}
13514
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515static int inputsecret_flag = 0;
13516
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013517static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13518
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013520 * This function is used by f_input() and f_inputdialog() functions. The third
13521 * argument to f_input() specifies the type of completion to use at the
13522 * prompt. The third argument to f_inputdialog() specifies the value to return
13523 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 */
13525 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013526get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013527 typval_T *argvars;
13528 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013529 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 char_u *p = NULL;
13533 int c;
13534 char_u buf[NUMBUFLEN];
13535 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013537 int xp_type = EXPAND_NOTHING;
13538 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542
13543#ifdef NO_CONSOLE_INPUT
13544 /* While starting up, there is no place to enter text. */
13545 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547#endif
13548
13549 cmd_silent = FALSE; /* Want to see the prompt. */
13550 if (prompt != NULL)
13551 {
13552 /* Only the part of the message after the last NL is considered as
13553 * prompt for the command line */
13554 p = vim_strrchr(prompt, '\n');
13555 if (p == NULL)
13556 p = prompt;
13557 else
13558 {
13559 ++p;
13560 c = *p;
13561 *p = NUL;
13562 msg_start();
13563 msg_clr_eos();
13564 msg_puts_attr(prompt, echo_attr);
13565 msg_didout = FALSE;
13566 msg_starthere();
13567 *p = c;
13568 }
13569 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013571 if (argvars[1].v_type != VAR_UNKNOWN)
13572 {
13573 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13574 if (defstr != NULL)
13575 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013577 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013578 {
13579 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013580 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013581 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013582
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013583 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013584 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013585
Bram Moolenaar4463f292005-09-25 22:20:24 +000013586 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13587 if (xp_name == NULL)
13588 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013589
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013590 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013591
Bram Moolenaar4463f292005-09-25 22:20:24 +000013592 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13593 &xp_arg) == FAIL)
13594 return;
13595 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013596 }
13597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013599 {
13600# ifdef FEAT_EX_EXTRA
13601 int save_ex_normal_busy = ex_normal_busy;
13602 ex_normal_busy = 0;
13603# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013605 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13606 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013607# ifdef FEAT_EX_EXTRA
13608 ex_normal_busy = save_ex_normal_busy;
13609# endif
13610 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013611 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013612 && argvars[1].v_type != VAR_UNKNOWN
13613 && argvars[2].v_type != VAR_UNKNOWN)
13614 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13615 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013616
13617 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 /* since the user typed this, no need to wait for return */
13620 need_wait_return = FALSE;
13621 msg_didout = FALSE;
13622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 cmd_silent = cmd_silent_save;
13624}
13625
13626/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013627 * "input()" function
13628 * Also handles inputsecret() when inputsecret is set.
13629 */
13630 static void
13631f_input(argvars, rettv)
13632 typval_T *argvars;
13633 typval_T *rettv;
13634{
13635 get_user_input(argvars, rettv, FALSE);
13636}
13637
13638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 * "inputdialog()" function
13640 */
13641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *argvars;
13644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
13646#if defined(FEAT_GUI_TEXTDIALOG)
13647 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13648 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13649 {
13650 char_u *message;
13651 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013652 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013654 message = get_tv_string_chk(&argvars[0]);
13655 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013656 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013657 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 else
13659 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013660 if (message != NULL && defstr != NULL
13661 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013662 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 else
13665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013666 if (message != NULL && defstr != NULL
13667 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013668 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669 rettv->vval.v_string = vim_strsave(
13670 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 }
13676 else
13677#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013678 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679}
13680
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013681/*
13682 * "inputlist()" function
13683 */
13684 static void
13685f_inputlist(argvars, rettv)
13686 typval_T *argvars;
13687 typval_T *rettv;
13688{
13689 listitem_T *li;
13690 int selected;
13691 int mouse_used;
13692
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013693#ifdef NO_CONSOLE_INPUT
13694 /* While starting up, there is no place to enter text. */
13695 if (no_console_input())
13696 return;
13697#endif
13698 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13699 {
13700 EMSG2(_(e_listarg), "inputlist()");
13701 return;
13702 }
13703
13704 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013705 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013706 lines_left = Rows; /* avoid more prompt */
13707 msg_scroll = TRUE;
13708 msg_clr_eos();
13709
13710 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13711 {
13712 msg_puts(get_tv_string(&li->li_tv));
13713 msg_putchar('\n');
13714 }
13715
13716 /* Ask for choice. */
13717 selected = prompt_for_number(&mouse_used);
13718 if (mouse_used)
13719 selected -= lines_left;
13720
13721 rettv->vval.v_number = selected;
13722}
13723
13724
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13726
13727/*
13728 * "inputrestore()" function
13729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
13735 if (ga_userinput.ga_len > 0)
13736 {
13737 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13739 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013740 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 }
13742 else if (p_verbose > 1)
13743 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013744 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746 }
13747}
13748
13749/*
13750 * "inputsave()" function
13751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013754 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013757 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 if (ga_grow(&ga_userinput, 1) == OK)
13759 {
13760 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13761 + ga_userinput.ga_len);
13762 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013763 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 }
13765 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767}
13768
13769/*
13770 * "inputsecret()" function
13771 */
13772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013774 typval_T *argvars;
13775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776{
13777 ++cmdline_star;
13778 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 --cmdline_star;
13781 --inputsecret_flag;
13782}
13783
13784/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013785 * "insert()" function
13786 */
13787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013789 typval_T *argvars;
13790 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013791{
13792 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013793 listitem_T *item;
13794 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013796
13797 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013799 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013800 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013801 {
13802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 before = get_tv_number_chk(&argvars[2], &error);
13804 if (error)
13805 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013806
Bram Moolenaar758711c2005-02-02 23:11:38 +000013807 if (before == l->lv_len)
13808 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013809 else
13810 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013811 item = list_find(l, before);
13812 if (item == NULL)
13813 {
13814 EMSGN(_(e_listidx), before);
13815 l = NULL;
13816 }
13817 }
13818 if (l != NULL)
13819 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013820 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013821 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013822 }
13823 }
13824}
13825
13826/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013827 * "invert(expr)" function
13828 */
13829 static void
13830f_invert(argvars, rettv)
13831 typval_T *argvars;
13832 typval_T *rettv;
13833{
13834 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13835}
13836
13837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 * "isdirectory()" function
13839 */
13840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013842 typval_T *argvars;
13843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846}
13847
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013848/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013849 * "islocked()" function
13850 */
13851 static void
13852f_islocked(argvars, rettv)
13853 typval_T *argvars;
13854 typval_T *rettv;
13855{
13856 lval_T lv;
13857 char_u *end;
13858 dictitem_T *di;
13859
13860 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013861 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13862 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013863 if (end != NULL && lv.ll_name != NULL)
13864 {
13865 if (*end != NUL)
13866 EMSG(_(e_trailing));
13867 else
13868 {
13869 if (lv.ll_tv == NULL)
13870 {
13871 if (check_changedtick(lv.ll_name))
13872 rettv->vval.v_number = 1; /* always locked */
13873 else
13874 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013875 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013876 if (di != NULL)
13877 {
13878 /* Consider a variable locked when:
13879 * 1. the variable itself is locked
13880 * 2. the value of the variable is locked.
13881 * 3. the List or Dict value is locked.
13882 */
13883 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13884 || tv_islocked(&di->di_tv));
13885 }
13886 }
13887 }
13888 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013889 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013890 else if (lv.ll_newkey != NULL)
13891 EMSG2(_(e_dictkey), lv.ll_newkey);
13892 else if (lv.ll_list != NULL)
13893 /* List item. */
13894 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13895 else
13896 /* Dictionary item. */
13897 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13898 }
13899 }
13900
13901 clear_lval(&lv);
13902}
13903
Bram Moolenaar33570922005-01-25 22:26:29 +000013904static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013905
13906/*
13907 * Turn a dict into a list:
13908 * "what" == 0: list of keys
13909 * "what" == 1: list of values
13910 * "what" == 2: list of items
13911 */
13912 static void
13913dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013914 typval_T *argvars;
13915 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013916 int what;
13917{
Bram Moolenaar33570922005-01-25 22:26:29 +000013918 list_T *l2;
13919 dictitem_T *di;
13920 hashitem_T *hi;
13921 listitem_T *li;
13922 listitem_T *li2;
13923 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013924 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013925
Bram Moolenaar8c711452005-01-14 21:53:12 +000013926 if (argvars[0].v_type != VAR_DICT)
13927 {
13928 EMSG(_(e_dictreq));
13929 return;
13930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013931 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013932 return;
13933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013935 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013936
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013937 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013938 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013939 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013940 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013941 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013942 --todo;
13943 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013944
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013945 li = listitem_alloc();
13946 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013947 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013948 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013949
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013950 if (what == 0)
13951 {
13952 /* keys() */
13953 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013954 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013955 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13956 }
13957 else if (what == 1)
13958 {
13959 /* values() */
13960 copy_tv(&di->di_tv, &li->li_tv);
13961 }
13962 else
13963 {
13964 /* items() */
13965 l2 = list_alloc();
13966 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013967 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013968 li->li_tv.vval.v_list = l2;
13969 if (l2 == NULL)
13970 break;
13971 ++l2->lv_refcount;
13972
13973 li2 = listitem_alloc();
13974 if (li2 == NULL)
13975 break;
13976 list_append(l2, li2);
13977 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013978 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013979 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13980
13981 li2 = listitem_alloc();
13982 if (li2 == NULL)
13983 break;
13984 list_append(l2, li2);
13985 copy_tv(&di->di_tv, &li2->li_tv);
13986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013987 }
13988 }
13989}
13990
13991/*
13992 * "items(dict)" function
13993 */
13994 static void
13995f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013996 typval_T *argvars;
13997 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013998{
13999 dict_list(argvars, rettv, 2);
14000}
14001
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014003 * "join()" function
14004 */
14005 static void
14006f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014007 typval_T *argvars;
14008 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014009{
14010 garray_T ga;
14011 char_u *sep;
14012
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013 if (argvars[0].v_type != VAR_LIST)
14014 {
14015 EMSG(_(e_listreq));
14016 return;
14017 }
14018 if (argvars[0].vval.v_list == NULL)
14019 return;
14020 if (argvars[1].v_type == VAR_UNKNOWN)
14021 sep = (char_u *)" ";
14022 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014023 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014024
14025 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026
14027 if (sep != NULL)
14028 {
14029 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014030 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 ga_append(&ga, NUL);
14032 rettv->vval.v_string = (char_u *)ga.ga_data;
14033 }
14034 else
14035 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014036}
14037
14038/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014039 * "keys()" function
14040 */
14041 static void
14042f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014043 typval_T *argvars;
14044 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014045{
14046 dict_list(argvars, rettv, 0);
14047}
14048
14049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 * "last_buffer_nr()" function.
14051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056{
14057 int n = 0;
14058 buf_T *buf;
14059
14060 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14061 if (n < buf->b_fnum)
14062 n = buf->b_fnum;
14063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014064 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014065}
14066
14067/*
14068 * "len()" function
14069 */
14070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014072 typval_T *argvars;
14073 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014074{
14075 switch (argvars[0].v_type)
14076 {
14077 case VAR_STRING:
14078 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 rettv->vval.v_number = (varnumber_T)STRLEN(
14080 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014081 break;
14082 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014084 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014085 case VAR_DICT:
14086 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14087 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014088 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014089 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014090 break;
14091 }
14092}
14093
Bram Moolenaar33570922005-01-25 22:26:29 +000014094static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014095
14096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014100 int type;
14101{
14102#ifdef FEAT_LIBCALL
14103 char_u *string_in;
14104 char_u **string_result;
14105 int nr_result;
14106#endif
14107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014108 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014109 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014110 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014111
14112 if (check_restricted() || check_secure())
14113 return;
14114
14115#ifdef FEAT_LIBCALL
14116 /* The first two args must be strings, otherwise its meaningless */
14117 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14118 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014119 string_in = NULL;
14120 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014121 string_in = argvars[2].vval.v_string;
14122 if (type == VAR_NUMBER)
14123 string_result = NULL;
14124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014126 if (mch_libcall(argvars[0].vval.v_string,
14127 argvars[1].vval.v_string,
14128 string_in,
14129 argvars[2].vval.v_number,
14130 string_result,
14131 &nr_result) == OK
14132 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014134 }
14135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136}
14137
14138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139 * "libcall()" function
14140 */
14141 static void
14142f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014145{
14146 libcall_common(argvars, rettv, VAR_STRING);
14147}
14148
14149/*
14150 * "libcallnr()" function
14151 */
14152 static void
14153f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014154 typval_T *argvars;
14155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156{
14157 libcall_common(argvars, rettv, VAR_NUMBER);
14158}
14159
14160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 * "line(string)" function
14162 */
14163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *argvars;
14166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167{
14168 linenr_T lnum = 0;
14169 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014170 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014172 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 if (fp != NULL)
14174 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176}
14177
14178/*
14179 * "line2byte(lnum)" function
14180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185{
14186#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#else
14189 linenr_T lnum;
14190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014193 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14196 if (rettv->vval.v_number >= 0)
14197 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198#endif
14199}
14200
14201/*
14202 * "lispindent(lnum)" function
14203 */
14204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014205f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014206 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208{
14209#ifdef FEAT_LISP
14210 pos_T pos;
14211 linenr_T lnum;
14212
14213 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14216 {
14217 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014218 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219 curwin->w_cursor = pos;
14220 }
14221 else
14222#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014223 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
14226/*
14227 * "localtime()" function
14228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014231 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235}
14236
Bram Moolenaar33570922005-01-25 22:26:29 +000014237static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238
14239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014241 typval_T *argvars;
14242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 int exact;
14244{
14245 char_u *keys;
14246 char_u *which;
14247 char_u buf[NUMBUFLEN];
14248 char_u *keys_buf = NULL;
14249 char_u *rhs;
14250 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014251 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014252 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014253 mapblock_T *mp;
14254 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255
14256 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014257 rettv->v_type = VAR_STRING;
14258 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 if (*keys == NUL)
14262 return;
14263
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014264 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014267 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014268 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014269 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014270 if (argvars[3].v_type != VAR_UNKNOWN)
14271 get_dict = get_tv_number(&argvars[3]);
14272 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 else
14275 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276 if (which == NULL)
14277 return;
14278
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 mode = get_map_mode(&which, 0);
14280
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014281 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014282 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014284
14285 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014287 /* Return a string. */
14288 if (rhs != NULL)
14289 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290
Bram Moolenaarbd743252010-10-20 21:23:33 +020014291 }
14292 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14293 {
14294 /* Return a dictionary. */
14295 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14296 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14297 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
Bram Moolenaarbd743252010-10-20 21:23:33 +020014299 dict_add_nr_str(dict, "lhs", 0L, lhs);
14300 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14301 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14302 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14303 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14304 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14305 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014306 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014307 dict_add_nr_str(dict, "mode", 0L, mapmode);
14308
14309 vim_free(lhs);
14310 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 }
14312}
14313
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014314#ifdef FEAT_FLOAT
14315/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014316 * "log()" function
14317 */
14318 static void
14319f_log(argvars, rettv)
14320 typval_T *argvars;
14321 typval_T *rettv;
14322{
14323 float_T f;
14324
14325 rettv->v_type = VAR_FLOAT;
14326 if (get_float_arg(argvars, &f) == OK)
14327 rettv->vval.v_float = log(f);
14328 else
14329 rettv->vval.v_float = 0.0;
14330}
14331
14332/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014333 * "log10()" function
14334 */
14335 static void
14336f_log10(argvars, rettv)
14337 typval_T *argvars;
14338 typval_T *rettv;
14339{
14340 float_T f;
14341
14342 rettv->v_type = VAR_FLOAT;
14343 if (get_float_arg(argvars, &f) == OK)
14344 rettv->vval.v_float = log10(f);
14345 else
14346 rettv->vval.v_float = 0.0;
14347}
14348#endif
14349
Bram Moolenaar1dced572012-04-05 16:54:08 +020014350#ifdef FEAT_LUA
14351/*
14352 * "luaeval()" function
14353 */
14354 static void
14355f_luaeval(argvars, rettv)
14356 typval_T *argvars;
14357 typval_T *rettv;
14358{
14359 char_u *str;
14360 char_u buf[NUMBUFLEN];
14361
14362 str = get_tv_string_buf(&argvars[0], buf);
14363 do_luaeval(str, argvars + 1, rettv);
14364}
14365#endif
14366
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014368 * "map()" function
14369 */
14370 static void
14371f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 typval_T *argvars;
14373 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014374{
14375 filter_map(argvars, rettv, TRUE);
14376}
14377
14378/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 */
14381 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014382f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T *argvars;
14384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014386 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387}
14388
14389/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014390 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 */
14392 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014394 typval_T *argvars;
14395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398}
14399
Bram Moolenaar33570922005-01-25 22:26:29 +000014400static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
14402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 typval_T *argvars;
14405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 int type;
14407{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014408 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014409 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 char_u *pat;
14412 regmatch_T regmatch;
14413 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014414 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 char_u *save_cpo;
14416 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014417 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014418 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014419 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014420 list_T *l = NULL;
14421 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014422 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014423 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424
14425 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14426 save_cpo = p_cpo;
14427 p_cpo = (char_u *)"";
14428
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014429 rettv->vval.v_number = -1;
14430 if (type == 3)
14431 {
14432 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014433 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014434 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014435 }
14436 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014438 rettv->v_type = VAR_STRING;
14439 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014442 if (argvars[0].v_type == VAR_LIST)
14443 {
14444 if ((l = argvars[0].vval.v_list) == NULL)
14445 goto theend;
14446 li = l->lv_first;
14447 }
14448 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014449 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014450 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014451 len = (long)STRLEN(str);
14452 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14455 if (pat == NULL)
14456 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014457
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014460 int error = FALSE;
14461
14462 start = get_tv_number_chk(&argvars[2], &error);
14463 if (error)
14464 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014465 if (l != NULL)
14466 {
14467 li = list_find(l, start);
14468 if (li == NULL)
14469 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014470 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014471 }
14472 else
14473 {
14474 if (start < 0)
14475 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014476 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014477 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014478 /* When "count" argument is there ignore matches before "start",
14479 * otherwise skip part of the string. Differs when pattern is "^"
14480 * or "\<". */
14481 if (argvars[3].v_type != VAR_UNKNOWN)
14482 startcol = start;
14483 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014484 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014485 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014486 len -= start;
14487 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014488 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014489
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014490 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 nth = get_tv_number_chk(&argvars[3], &error);
14492 if (error)
14493 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 }
14495
14496 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14497 if (regmatch.regprog != NULL)
14498 {
14499 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014500
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014501 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014502 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014503 if (l != NULL)
14504 {
14505 if (li == NULL)
14506 {
14507 match = FALSE;
14508 break;
14509 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014510 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014511 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014512 if (str == NULL)
14513 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014514 }
14515
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014516 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014517
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014518 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014519 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014520 if (l == NULL && !match)
14521 break;
14522
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014523 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014524 if (l != NULL)
14525 {
14526 li = li->li_next;
14527 ++idx;
14528 }
14529 else
14530 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014531#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014532 startcol = (colnr_T)(regmatch.startp[0]
14533 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014534#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014535 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014536#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014537 if (startcol > (colnr_T)len
14538 || str + startcol <= regmatch.startp[0])
14539 {
14540 match = FALSE;
14541 break;
14542 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014543 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014544 }
14545
14546 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014548 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014549 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014550 int i;
14551
14552 /* return list with matched string and submatches */
14553 for (i = 0; i < NSUBEXP; ++i)
14554 {
14555 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014556 {
14557 if (list_append_string(rettv->vval.v_list,
14558 (char_u *)"", 0) == FAIL)
14559 break;
14560 }
14561 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014562 regmatch.startp[i],
14563 (int)(regmatch.endp[i] - regmatch.startp[i]))
14564 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014566 }
14567 }
14568 else if (type == 2)
14569 {
14570 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014571 if (l != NULL)
14572 copy_tv(&li->li_tv, rettv);
14573 else
14574 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014576 }
14577 else if (l != NULL)
14578 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 else
14580 {
14581 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 (varnumber_T)(regmatch.startp[0] - str);
14584 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014587 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 }
14589 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014590 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 }
14592
14593theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014594 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 p_cpo = save_cpo;
14596}
14597
14598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 * "match()" function
14600 */
14601 static void
14602f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014603 typval_T *argvars;
14604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605{
14606 find_some_match(argvars, rettv, 1);
14607}
14608
14609/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014610 * "matchadd()" function
14611 */
14612 static void
14613f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014614 typval_T *argvars UNUSED;
14615 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014616{
14617#ifdef FEAT_SEARCH_EXTRA
14618 char_u buf[NUMBUFLEN];
14619 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14620 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14621 int prio = 10; /* default priority */
14622 int id = -1;
14623 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014624 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014625
14626 rettv->vval.v_number = -1;
14627
14628 if (grp == NULL || pat == NULL)
14629 return;
14630 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014631 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014632 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014633 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014634 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014635 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014636 if (argvars[4].v_type != VAR_UNKNOWN)
14637 {
14638 if (argvars[4].v_type != VAR_DICT)
14639 {
14640 EMSG(_(e_dictreq));
14641 return;
14642 }
14643 if (dict_find(argvars[4].vval.v_dict,
14644 (char_u *)"conceal", -1) != NULL)
14645 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14646 (char_u *)"conceal", FALSE);
14647 }
14648 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014649 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014650 if (error == TRUE)
14651 return;
14652 if (id >= 1 && id <= 3)
14653 {
14654 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14655 return;
14656 }
14657
Bram Moolenaar6561d522015-07-21 15:48:27 +020014658 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14659 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014660#endif
14661}
14662
14663/*
14664 * "matchaddpos()" function
14665 */
14666 static void
14667f_matchaddpos(argvars, rettv)
14668 typval_T *argvars UNUSED;
14669 typval_T *rettv UNUSED;
14670{
14671#ifdef FEAT_SEARCH_EXTRA
14672 char_u buf[NUMBUFLEN];
14673 char_u *group;
14674 int prio = 10;
14675 int id = -1;
14676 int error = FALSE;
14677 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014678 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014679
14680 rettv->vval.v_number = -1;
14681
14682 group = get_tv_string_buf_chk(&argvars[0], buf);
14683 if (group == NULL)
14684 return;
14685
14686 if (argvars[1].v_type != VAR_LIST)
14687 {
14688 EMSG2(_(e_listarg), "matchaddpos()");
14689 return;
14690 }
14691 l = argvars[1].vval.v_list;
14692 if (l == NULL)
14693 return;
14694
14695 if (argvars[2].v_type != VAR_UNKNOWN)
14696 {
14697 prio = get_tv_number_chk(&argvars[2], &error);
14698 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014699 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014700 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014701 if (argvars[4].v_type != VAR_UNKNOWN)
14702 {
14703 if (argvars[4].v_type != VAR_DICT)
14704 {
14705 EMSG(_(e_dictreq));
14706 return;
14707 }
14708 if (dict_find(argvars[4].vval.v_dict,
14709 (char_u *)"conceal", -1) != NULL)
14710 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14711 (char_u *)"conceal", FALSE);
14712 }
14713 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014714 }
14715 if (error == TRUE)
14716 return;
14717
14718 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14719 if (id == 1 || id == 2)
14720 {
14721 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14722 return;
14723 }
14724
Bram Moolenaar6561d522015-07-21 15:48:27 +020014725 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14726 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014727#endif
14728}
14729
14730/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014731 * "matcharg()" function
14732 */
14733 static void
14734f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014735 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014736 typval_T *rettv;
14737{
14738 if (rettv_list_alloc(rettv) == OK)
14739 {
14740#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014741 int id = get_tv_number(&argvars[0]);
14742 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014743
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014744 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014745 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014746 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14747 {
14748 list_append_string(rettv->vval.v_list,
14749 syn_id2name(m->hlg_id), -1);
14750 list_append_string(rettv->vval.v_list, m->pattern, -1);
14751 }
14752 else
14753 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014754 list_append_string(rettv->vval.v_list, NULL, -1);
14755 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014756 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014757 }
14758#endif
14759 }
14760}
14761
14762/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014763 * "matchdelete()" function
14764 */
14765 static void
14766f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014767 typval_T *argvars UNUSED;
14768 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014769{
14770#ifdef FEAT_SEARCH_EXTRA
14771 rettv->vval.v_number = match_delete(curwin,
14772 (int)get_tv_number(&argvars[0]), TRUE);
14773#endif
14774}
14775
14776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014777 * "matchend()" function
14778 */
14779 static void
14780f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014781 typval_T *argvars;
14782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014783{
14784 find_some_match(argvars, rettv, 0);
14785}
14786
14787/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014788 * "matchlist()" function
14789 */
14790 static void
14791f_matchlist(argvars, rettv)
14792 typval_T *argvars;
14793 typval_T *rettv;
14794{
14795 find_some_match(argvars, rettv, 3);
14796}
14797
14798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014799 * "matchstr()" function
14800 */
14801 static void
14802f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014803 typval_T *argvars;
14804 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014805{
14806 find_some_match(argvars, rettv, 2);
14807}
14808
Bram Moolenaar33570922005-01-25 22:26:29 +000014809static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014810
14811 static void
14812max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014813 typval_T *argvars;
14814 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014815 int domax;
14816{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014817 long n = 0;
14818 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014820
14821 if (argvars[0].v_type == VAR_LIST)
14822 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014823 list_T *l;
14824 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014825
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014826 l = argvars[0].vval.v_list;
14827 if (l != NULL)
14828 {
14829 li = l->lv_first;
14830 if (li != NULL)
14831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014832 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014833 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014834 {
14835 li = li->li_next;
14836 if (li == NULL)
14837 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014838 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014839 if (domax ? i > n : i < n)
14840 n = i;
14841 }
14842 }
14843 }
14844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014845 else if (argvars[0].v_type == VAR_DICT)
14846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014848 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014850 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014851
14852 d = argvars[0].vval.v_dict;
14853 if (d != NULL)
14854 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014855 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014856 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014858 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014860 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014861 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014862 if (first)
14863 {
14864 n = i;
14865 first = FALSE;
14866 }
14867 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014868 n = i;
14869 }
14870 }
14871 }
14872 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014873 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014874 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014875 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014876}
14877
14878/*
14879 * "max()" function
14880 */
14881 static void
14882f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 typval_T *argvars;
14884 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014885{
14886 max_min(argvars, rettv, TRUE);
14887}
14888
14889/*
14890 * "min()" function
14891 */
14892 static void
14893f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014894 typval_T *argvars;
14895 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014896{
14897 max_min(argvars, rettv, FALSE);
14898}
14899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014900static int mkdir_recurse __ARGS((char_u *dir, int prot));
14901
14902/*
14903 * Create the directory in which "dir" is located, and higher levels when
14904 * needed.
14905 */
14906 static int
14907mkdir_recurse(dir, prot)
14908 char_u *dir;
14909 int prot;
14910{
14911 char_u *p;
14912 char_u *updir;
14913 int r = FAIL;
14914
14915 /* Get end of directory name in "dir".
14916 * We're done when it's "/" or "c:/". */
14917 p = gettail_sep(dir);
14918 if (p <= get_past_head(dir))
14919 return OK;
14920
14921 /* If the directory exists we're done. Otherwise: create it.*/
14922 updir = vim_strnsave(dir, (int)(p - dir));
14923 if (updir == NULL)
14924 return FAIL;
14925 if (mch_isdir(updir))
14926 r = OK;
14927 else if (mkdir_recurse(updir, prot) == OK)
14928 r = vim_mkdir_emsg(updir, prot);
14929 vim_free(updir);
14930 return r;
14931}
14932
14933#ifdef vim_mkdir
14934/*
14935 * "mkdir()" function
14936 */
14937 static void
14938f_mkdir(argvars, rettv)
14939 typval_T *argvars;
14940 typval_T *rettv;
14941{
14942 char_u *dir;
14943 char_u buf[NUMBUFLEN];
14944 int prot = 0755;
14945
14946 rettv->vval.v_number = FAIL;
14947 if (check_restricted() || check_secure())
14948 return;
14949
14950 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014951 if (*dir == NUL)
14952 rettv->vval.v_number = FAIL;
14953 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014954 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014955 if (*gettail(dir) == NUL)
14956 /* remove trailing slashes */
14957 *gettail_sep(dir) = NUL;
14958
14959 if (argvars[1].v_type != VAR_UNKNOWN)
14960 {
14961 if (argvars[2].v_type != VAR_UNKNOWN)
14962 prot = get_tv_number_chk(&argvars[2], NULL);
14963 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14964 mkdir_recurse(dir, prot);
14965 }
14966 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014967 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014968}
14969#endif
14970
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 * "mode()" function
14973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014979 char_u buf[3];
14980
14981 buf[1] = NUL;
14982 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 if (VIsual_active)
14985 {
14986 if (VIsual_select)
14987 buf[0] = VIsual_mode + 's' - 'v';
14988 else
14989 buf[0] = VIsual_mode;
14990 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014991 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014992 || State == CONFIRM)
14993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014995 if (State == ASKMORE)
14996 buf[1] = 'm';
14997 else if (State == CONFIRM)
14998 buf[1] = '?';
14999 }
15000 else if (State == EXTERNCMD)
15001 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 else if (State & INSERT)
15003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015004#ifdef FEAT_VREPLACE
15005 if (State & VREPLACE_FLAG)
15006 {
15007 buf[0] = 'R';
15008 buf[1] = 'v';
15009 }
15010 else
15011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 if (State & REPLACE_FLAG)
15013 buf[0] = 'R';
15014 else
15015 buf[0] = 'i';
15016 }
15017 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015020 if (exmode_active)
15021 buf[1] = 'v';
15022 }
15023 else if (exmode_active)
15024 {
15025 buf[0] = 'c';
15026 buf[1] = 'e';
15027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015031 if (finish_op)
15032 buf[1] = 'o';
15033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015035 /* Clear out the minor mode when the argument is not a non-zero number or
15036 * non-empty string. */
15037 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015038 buf[1] = NUL;
15039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 rettv->vval.v_string = vim_strsave(buf);
15041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042}
15043
Bram Moolenaar429fa852013-04-15 12:27:36 +020015044#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015045/*
15046 * "mzeval()" function
15047 */
15048 static void
15049f_mzeval(argvars, rettv)
15050 typval_T *argvars;
15051 typval_T *rettv;
15052{
15053 char_u *str;
15054 char_u buf[NUMBUFLEN];
15055
15056 str = get_tv_string_buf(&argvars[0], buf);
15057 do_mzeval(str, rettv);
15058}
Bram Moolenaar75676462013-01-30 14:55:42 +010015059
15060 void
15061mzscheme_call_vim(name, args, rettv)
15062 char_u *name;
15063 typval_T *args;
15064 typval_T *rettv;
15065{
15066 typval_T argvars[3];
15067
15068 argvars[0].v_type = VAR_STRING;
15069 argvars[0].vval.v_string = name;
15070 copy_tv(args, &argvars[1]);
15071 argvars[2].v_type = VAR_UNKNOWN;
15072 f_call(argvars, rettv);
15073 clear_tv(&argvars[1]);
15074}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015075#endif
15076
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 * "nextnonblank()" function
15079 */
15080 static void
15081f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 typval_T *argvars;
15083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084{
15085 linenr_T lnum;
15086
15087 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090 {
15091 lnum = 0;
15092 break;
15093 }
15094 if (*skipwhite(ml_get(lnum)) != NUL)
15095 break;
15096 }
15097 rettv->vval.v_number = lnum;
15098}
15099
15100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 * "nr2char()" function
15102 */
15103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015104f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 typval_T *argvars;
15106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107{
15108 char_u buf[NUMBUFLEN];
15109
15110#ifdef FEAT_MBYTE
15111 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015112 {
15113 int utf8 = 0;
15114
15115 if (argvars[1].v_type != VAR_UNKNOWN)
15116 utf8 = get_tv_number_chk(&argvars[1], NULL);
15117 if (utf8)
15118 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15119 else
15120 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 else
15123#endif
15124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 buf[1] = NUL;
15127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 rettv->v_type = VAR_STRING;
15129 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015130}
15131
15132/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015133 * "or(expr, expr)" function
15134 */
15135 static void
15136f_or(argvars, rettv)
15137 typval_T *argvars;
15138 typval_T *rettv;
15139{
15140 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15141 | get_tv_number_chk(&argvars[1], NULL);
15142}
15143
15144/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015145 * "pathshorten()" function
15146 */
15147 static void
15148f_pathshorten(argvars, rettv)
15149 typval_T *argvars;
15150 typval_T *rettv;
15151{
15152 char_u *p;
15153
15154 rettv->v_type = VAR_STRING;
15155 p = get_tv_string_chk(&argvars[0]);
15156 if (p == NULL)
15157 rettv->vval.v_string = NULL;
15158 else
15159 {
15160 p = vim_strsave(p);
15161 rettv->vval.v_string = p;
15162 if (p != NULL)
15163 shorten_dir(p);
15164 }
15165}
15166
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015167#ifdef FEAT_FLOAT
15168/*
15169 * "pow()" function
15170 */
15171 static void
15172f_pow(argvars, rettv)
15173 typval_T *argvars;
15174 typval_T *rettv;
15175{
15176 float_T fx, fy;
15177
15178 rettv->v_type = VAR_FLOAT;
15179 if (get_float_arg(argvars, &fx) == OK
15180 && get_float_arg(&argvars[1], &fy) == OK)
15181 rettv->vval.v_float = pow(fx, fy);
15182 else
15183 rettv->vval.v_float = 0.0;
15184}
15185#endif
15186
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015187/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188 * "prevnonblank()" function
15189 */
15190 static void
15191f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *argvars;
15193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194{
15195 linenr_T lnum;
15196
15197 lnum = get_tv_lnum(argvars);
15198 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15199 lnum = 0;
15200 else
15201 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15202 --lnum;
15203 rettv->vval.v_number = lnum;
15204}
15205
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015206#ifdef HAVE_STDARG_H
15207/* This dummy va_list is here because:
15208 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15209 * - locally in the function results in a "used before set" warning
15210 * - using va_start() to initialize it gives "function with fixed args" error */
15211static va_list ap;
15212#endif
15213
Bram Moolenaar8c711452005-01-14 21:53:12 +000015214/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015215 * "printf()" function
15216 */
15217 static void
15218f_printf(argvars, rettv)
15219 typval_T *argvars;
15220 typval_T *rettv;
15221{
15222 rettv->v_type = VAR_STRING;
15223 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015224#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015225 {
15226 char_u buf[NUMBUFLEN];
15227 int len;
15228 char_u *s;
15229 int saved_did_emsg = did_emsg;
15230 char *fmt;
15231
15232 /* Get the required length, allocate the buffer and do it for real. */
15233 did_emsg = FALSE;
15234 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015235 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015236 if (!did_emsg)
15237 {
15238 s = alloc(len + 1);
15239 if (s != NULL)
15240 {
15241 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015242 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015243 }
15244 }
15245 did_emsg |= saved_did_emsg;
15246 }
15247#endif
15248}
15249
15250/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015251 * "pumvisible()" function
15252 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015253 static void
15254f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015255 typval_T *argvars UNUSED;
15256 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015258#ifdef FEAT_INS_EXPAND
15259 if (pum_visible())
15260 rettv->vval.v_number = 1;
15261#endif
15262}
15263
Bram Moolenaardb913952012-06-29 12:54:53 +020015264#ifdef FEAT_PYTHON3
15265/*
15266 * "py3eval()" function
15267 */
15268 static void
15269f_py3eval(argvars, rettv)
15270 typval_T *argvars;
15271 typval_T *rettv;
15272{
15273 char_u *str;
15274 char_u buf[NUMBUFLEN];
15275
15276 str = get_tv_string_buf(&argvars[0], buf);
15277 do_py3eval(str, rettv);
15278}
15279#endif
15280
15281#ifdef FEAT_PYTHON
15282/*
15283 * "pyeval()" function
15284 */
15285 static void
15286f_pyeval(argvars, rettv)
15287 typval_T *argvars;
15288 typval_T *rettv;
15289{
15290 char_u *str;
15291 char_u buf[NUMBUFLEN];
15292
15293 str = get_tv_string_buf(&argvars[0], buf);
15294 do_pyeval(str, rettv);
15295}
15296#endif
15297
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015298/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015299 * "range()" function
15300 */
15301 static void
15302f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015303 typval_T *argvars;
15304 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015305{
15306 long start;
15307 long end;
15308 long stride = 1;
15309 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015312 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015313 if (argvars[1].v_type == VAR_UNKNOWN)
15314 {
15315 end = start - 1;
15316 start = 0;
15317 }
15318 else
15319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015322 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015323 }
15324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 if (error)
15326 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015328 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015329 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015330 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015331 else
15332 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015333 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015334 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015335 if (list_append_number(rettv->vval.v_list,
15336 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015337 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015338 }
15339}
15340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015341/*
15342 * "readfile()" function
15343 */
15344 static void
15345f_readfile(argvars, rettv)
15346 typval_T *argvars;
15347 typval_T *rettv;
15348{
15349 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015350 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 char_u *fname;
15352 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015353 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15354 int io_size = sizeof(buf);
15355 int readlen; /* size of last fread() */
15356 char_u *prev = NULL; /* previously read bytes, if any */
15357 long prevlen = 0; /* length of data in prev */
15358 long prevsize = 0; /* size of prev buffer */
15359 long maxline = MAXLNUM;
15360 long cnt = 0;
15361 char_u *p; /* position in buf */
15362 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015363
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015364 if (argvars[1].v_type != VAR_UNKNOWN)
15365 {
15366 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15367 binary = TRUE;
15368 if (argvars[2].v_type != VAR_UNKNOWN)
15369 maxline = get_tv_number(&argvars[2]);
15370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015372 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015374
15375 /* Always open the file in binary mode, library functions have a mind of
15376 * their own about CR-LF conversion. */
15377 fname = get_tv_string(&argvars[0]);
15378 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15379 {
15380 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15381 return;
15382 }
15383
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015384 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015385 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015386 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015387
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015388 /* This for loop processes what was read, but is also entered at end
15389 * of file so that either:
15390 * - an incomplete line gets written
15391 * - a "binary" file gets an empty line at the end if it ends in a
15392 * newline. */
15393 for (p = buf, start = buf;
15394 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15395 ++p)
15396 {
15397 if (*p == '\n' || readlen <= 0)
15398 {
15399 listitem_T *li;
15400 char_u *s = NULL;
15401 long_u len = p - start;
15402
15403 /* Finished a line. Remove CRs before NL. */
15404 if (readlen > 0 && !binary)
15405 {
15406 while (len > 0 && start[len - 1] == '\r')
15407 --len;
15408 /* removal may cross back to the "prev" string */
15409 if (len == 0)
15410 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15411 --prevlen;
15412 }
15413 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015414 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015415 else
15416 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015417 /* Change "prev" buffer to be the right size. This way
15418 * the bytes are only copied once, and very long lines are
15419 * allocated only once. */
15420 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015421 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015422 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015423 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015424 prev = NULL; /* the list will own the string */
15425 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015426 }
15427 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015428 if (s == NULL)
15429 {
15430 do_outofmem_msg((long_u) prevlen + len + 1);
15431 failed = TRUE;
15432 break;
15433 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015434
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015435 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015436 {
15437 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015438 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015439 break;
15440 }
15441 li->li_tv.v_type = VAR_STRING;
15442 li->li_tv.v_lock = 0;
15443 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015444 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015445
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015446 start = p + 1; /* step over newline */
15447 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015448 break;
15449 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015450 else if (*p == NUL)
15451 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015452#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015453 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15454 * when finding the BF and check the previous two bytes. */
15455 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015456 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015457 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15458 * + 1, these may be in the "prev" string. */
15459 char_u back1 = p >= buf + 1 ? p[-1]
15460 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15461 char_u back2 = p >= buf + 2 ? p[-2]
15462 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15463 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015464
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015465 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015466 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 char_u *dest = p - 2;
15468
15469 /* Usually a BOM is at the beginning of a file, and so at
15470 * the beginning of a line; then we can just step over it.
15471 */
15472 if (start == dest)
15473 start = p + 1;
15474 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015475 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015476 /* have to shuffle buf to close gap */
15477 int adjust_prevlen = 0;
15478
15479 if (dest < buf)
15480 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015481 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015482 dest = buf;
15483 }
15484 if (readlen > p - buf + 1)
15485 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15486 readlen -= 3 - adjust_prevlen;
15487 prevlen -= adjust_prevlen;
15488 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015489 }
15490 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015491 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015492#endif
15493 } /* for */
15494
15495 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15496 break;
15497 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015498 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015499 /* There's part of a line in buf, store it in "prev". */
15500 if (p - start + prevlen >= prevsize)
15501 {
15502 /* need bigger "prev" buffer */
15503 char_u *newprev;
15504
15505 /* A common use case is ordinary text files and "prev" gets a
15506 * fragment of a line, so the first allocation is made
15507 * small, to avoid repeatedly 'allocing' large and
15508 * 'reallocing' small. */
15509 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015510 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015511 else
15512 {
15513 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015514 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015515 prevsize = grow50pc > growmin ? grow50pc : growmin;
15516 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015517 newprev = prev == NULL ? alloc(prevsize)
15518 : vim_realloc(prev, prevsize);
15519 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015520 {
15521 do_outofmem_msg((long_u)prevsize);
15522 failed = TRUE;
15523 break;
15524 }
15525 prev = newprev;
15526 }
15527 /* Add the line part to end of "prev". */
15528 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015529 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015530 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015531 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015533 /*
15534 * For a negative line count use only the lines at the end of the file,
15535 * free the rest.
15536 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015537 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015538 while (cnt > -maxline)
15539 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015540 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015541 --cnt;
15542 }
15543
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015544 if (failed)
15545 {
15546 list_free(rettv->vval.v_list, TRUE);
15547 /* readfile doc says an empty list is returned on error */
15548 rettv->vval.v_list = list_alloc();
15549 }
15550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015551 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015552 fclose(fd);
15553}
15554
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015555#if defined(FEAT_RELTIME)
15556static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15557
15558/*
15559 * Convert a List to proftime_T.
15560 * Return FAIL when there is something wrong.
15561 */
15562 static int
15563list2proftime(arg, tm)
15564 typval_T *arg;
15565 proftime_T *tm;
15566{
15567 long n1, n2;
15568 int error = FALSE;
15569
15570 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15571 || arg->vval.v_list->lv_len != 2)
15572 return FAIL;
15573 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15574 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15575# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015576 tm->HighPart = n1;
15577 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015578# else
15579 tm->tv_sec = n1;
15580 tm->tv_usec = n2;
15581# endif
15582 return error ? FAIL : OK;
15583}
15584#endif /* FEAT_RELTIME */
15585
15586/*
15587 * "reltime()" function
15588 */
15589 static void
15590f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015591 typval_T *argvars UNUSED;
15592 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015593{
15594#ifdef FEAT_RELTIME
15595 proftime_T res;
15596 proftime_T start;
15597
15598 if (argvars[0].v_type == VAR_UNKNOWN)
15599 {
15600 /* No arguments: get current time. */
15601 profile_start(&res);
15602 }
15603 else if (argvars[1].v_type == VAR_UNKNOWN)
15604 {
15605 if (list2proftime(&argvars[0], &res) == FAIL)
15606 return;
15607 profile_end(&res);
15608 }
15609 else
15610 {
15611 /* Two arguments: compute the difference. */
15612 if (list2proftime(&argvars[0], &start) == FAIL
15613 || list2proftime(&argvars[1], &res) == FAIL)
15614 return;
15615 profile_sub(&res, &start);
15616 }
15617
15618 if (rettv_list_alloc(rettv) == OK)
15619 {
15620 long n1, n2;
15621
15622# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015623 n1 = res.HighPart;
15624 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015625# else
15626 n1 = res.tv_sec;
15627 n2 = res.tv_usec;
15628# endif
15629 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15630 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15631 }
15632#endif
15633}
15634
15635/*
15636 * "reltimestr()" function
15637 */
15638 static void
15639f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015640 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015641 typval_T *rettv;
15642{
15643#ifdef FEAT_RELTIME
15644 proftime_T tm;
15645#endif
15646
15647 rettv->v_type = VAR_STRING;
15648 rettv->vval.v_string = NULL;
15649#ifdef FEAT_RELTIME
15650 if (list2proftime(&argvars[0], &tm) == OK)
15651 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15652#endif
15653}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015654
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15656static void make_connection __ARGS((void));
15657static int check_connection __ARGS((void));
15658
15659 static void
15660make_connection()
15661{
15662 if (X_DISPLAY == NULL
15663# ifdef FEAT_GUI
15664 && !gui.in_use
15665# endif
15666 )
15667 {
15668 x_force_connect = TRUE;
15669 setup_term_clip();
15670 x_force_connect = FALSE;
15671 }
15672}
15673
15674 static int
15675check_connection()
15676{
15677 make_connection();
15678 if (X_DISPLAY == NULL)
15679 {
15680 EMSG(_("E240: No connection to Vim server"));
15681 return FAIL;
15682 }
15683 return OK;
15684}
15685#endif
15686
15687#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015688static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689
15690 static void
15691remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 typval_T *argvars;
15693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694 int expr;
15695{
15696 char_u *server_name;
15697 char_u *keys;
15698 char_u *r = NULL;
15699 char_u buf[NUMBUFLEN];
15700# ifdef WIN32
15701 HWND w;
15702# else
15703 Window w;
15704# endif
15705
15706 if (check_restricted() || check_secure())
15707 return;
15708
15709# ifdef FEAT_X11
15710 if (check_connection() == FAIL)
15711 return;
15712# endif
15713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015714 server_name = get_tv_string_chk(&argvars[0]);
15715 if (server_name == NULL)
15716 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717 keys = get_tv_string_buf(&argvars[1], buf);
15718# ifdef WIN32
15719 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15720# else
15721 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15722 < 0)
15723# endif
15724 {
15725 if (r != NULL)
15726 EMSG(r); /* sending worked but evaluation failed */
15727 else
15728 EMSG2(_("E241: Unable to send to %s"), server_name);
15729 return;
15730 }
15731
15732 rettv->vval.v_string = r;
15733
15734 if (argvars[2].v_type != VAR_UNKNOWN)
15735 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015736 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015737 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015738 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015740 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015741 v.di_tv.v_type = VAR_STRING;
15742 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015743 idvar = get_tv_string_chk(&argvars[2]);
15744 if (idvar != NULL)
15745 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015746 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747 }
15748}
15749#endif
15750
15751/*
15752 * "remote_expr()" function
15753 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754 static void
15755f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758{
15759 rettv->v_type = VAR_STRING;
15760 rettv->vval.v_string = NULL;
15761#ifdef FEAT_CLIENTSERVER
15762 remote_common(argvars, rettv, TRUE);
15763#endif
15764}
15765
15766/*
15767 * "remote_foreground()" function
15768 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769 static void
15770f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015771 typval_T *argvars UNUSED;
15772 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774#ifdef FEAT_CLIENTSERVER
15775# ifdef WIN32
15776 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015777 {
15778 char_u *server_name = get_tv_string_chk(&argvars[0]);
15779
15780 if (server_name != NULL)
15781 serverForeground(server_name);
15782 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783# else
15784 /* Send a foreground() expression to the server. */
15785 argvars[1].v_type = VAR_STRING;
15786 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15787 argvars[2].v_type = VAR_UNKNOWN;
15788 remote_common(argvars, rettv, TRUE);
15789 vim_free(argvars[1].vval.v_string);
15790# endif
15791#endif
15792}
15793
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794 static void
15795f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015796 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798{
15799#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015800 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801 char_u *s = NULL;
15802# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015803 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015805 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806
15807 if (check_restricted() || check_secure())
15808 {
15809 rettv->vval.v_number = -1;
15810 return;
15811 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015812 serverid = get_tv_string_chk(&argvars[0]);
15813 if (serverid == NULL)
15814 {
15815 rettv->vval.v_number = -1;
15816 return; /* type error; errmsg already given */
15817 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015819 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015820 if (n == 0)
15821 rettv->vval.v_number = -1;
15822 else
15823 {
15824 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15825 rettv->vval.v_number = (s != NULL);
15826 }
15827# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015828 if (check_connection() == FAIL)
15829 return;
15830
15831 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015832 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833# endif
15834
15835 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015837 char_u *retvar;
15838
Bram Moolenaar33570922005-01-25 22:26:29 +000015839 v.di_tv.v_type = VAR_STRING;
15840 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 retvar = get_tv_string_chk(&argvars[1]);
15842 if (retvar != NULL)
15843 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015844 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845 }
15846#else
15847 rettv->vval.v_number = -1;
15848#endif
15849}
15850
Bram Moolenaar0d660222005-01-07 21:51:51 +000015851 static void
15852f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015854 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855{
15856 char_u *r = NULL;
15857
15858#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 char_u *serverid = get_tv_string_chk(&argvars[0]);
15860
15861 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862 {
15863# ifdef WIN32
15864 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015865 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015867 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868 if (n != 0)
15869 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15870 if (r == NULL)
15871# else
15872 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015873 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874# endif
15875 EMSG(_("E277: Unable to read a server reply"));
15876 }
15877#endif
15878 rettv->v_type = VAR_STRING;
15879 rettv->vval.v_string = r;
15880}
15881
15882/*
15883 * "remote_send()" function
15884 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015885 static void
15886f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889{
15890 rettv->v_type = VAR_STRING;
15891 rettv->vval.v_string = NULL;
15892#ifdef FEAT_CLIENTSERVER
15893 remote_common(argvars, rettv, FALSE);
15894#endif
15895}
15896
15897/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015898 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015899 */
15900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015902 typval_T *argvars;
15903 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015904{
Bram Moolenaar33570922005-01-25 22:26:29 +000015905 list_T *l;
15906 listitem_T *item, *item2;
15907 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015908 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015909 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015910 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015911 dict_T *d;
15912 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015913 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015914
Bram Moolenaar8c711452005-01-14 21:53:12 +000015915 if (argvars[0].v_type == VAR_DICT)
15916 {
15917 if (argvars[2].v_type != VAR_UNKNOWN)
15918 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015919 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015920 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 key = get_tv_string_chk(&argvars[1]);
15923 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 di = dict_find(d, key, -1);
15926 if (di == NULL)
15927 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015928 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15929 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015930 {
15931 *rettv = di->di_tv;
15932 init_tv(&di->di_tv);
15933 dictitem_remove(d, di);
15934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015936 }
15937 }
15938 else if (argvars[0].v_type != VAR_LIST)
15939 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015940 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015941 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015943 int error = FALSE;
15944
15945 idx = get_tv_number_chk(&argvars[1], &error);
15946 if (error)
15947 ; /* type error: do nothing, errmsg already given */
15948 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015949 EMSGN(_(e_listidx), idx);
15950 else
15951 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015952 if (argvars[2].v_type == VAR_UNKNOWN)
15953 {
15954 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015955 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015956 *rettv = item->li_tv;
15957 vim_free(item);
15958 }
15959 else
15960 {
15961 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015962 end = get_tv_number_chk(&argvars[2], &error);
15963 if (error)
15964 ; /* type error: do nothing */
15965 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015966 EMSGN(_(e_listidx), end);
15967 else
15968 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015969 int cnt = 0;
15970
15971 for (li = item; li != NULL; li = li->li_next)
15972 {
15973 ++cnt;
15974 if (li == item2)
15975 break;
15976 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015977 if (li == NULL) /* didn't find "item2" after "item" */
15978 EMSG(_(e_invrange));
15979 else
15980 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015981 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015982 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015983 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015984 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015985 l->lv_first = item;
15986 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015987 item->li_prev = NULL;
15988 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015989 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015990 }
15991 }
15992 }
15993 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015994 }
15995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996}
15997
15998/*
15999 * "rename({from}, {to})" function
16000 */
16001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016003 typval_T *argvars;
16004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005{
16006 char_u buf[NUMBUFLEN];
16007
16008 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016011 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16012 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013}
16014
16015/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016016 * "repeat()" function
16017 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016018 static void
16019f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 typval_T *argvars;
16021 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016022{
16023 char_u *p;
16024 int n;
16025 int slen;
16026 int len;
16027 char_u *r;
16028 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016029
16030 n = get_tv_number(&argvars[1]);
16031 if (argvars[0].v_type == VAR_LIST)
16032 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016033 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016034 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016035 if (list_extend(rettv->vval.v_list,
16036 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016037 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016038 }
16039 else
16040 {
16041 p = get_tv_string(&argvars[0]);
16042 rettv->v_type = VAR_STRING;
16043 rettv->vval.v_string = NULL;
16044
16045 slen = (int)STRLEN(p);
16046 len = slen * n;
16047 if (len <= 0)
16048 return;
16049
16050 r = alloc(len + 1);
16051 if (r != NULL)
16052 {
16053 for (i = 0; i < n; i++)
16054 mch_memmove(r + i * slen, p, (size_t)slen);
16055 r[len] = NUL;
16056 }
16057
16058 rettv->vval.v_string = r;
16059 }
16060}
16061
16062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 * "resolve()" function
16064 */
16065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016066f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016067 typval_T *argvars;
16068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069{
16070 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016071#ifdef HAVE_READLINK
16072 char_u *buf = NULL;
16073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016075 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076#ifdef FEAT_SHORTCUT
16077 {
16078 char_u *v = NULL;
16079
16080 v = mch_resolve_shortcut(p);
16081 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016082 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 }
16086#else
16087# ifdef HAVE_READLINK
16088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 char_u *cpy;
16090 int len;
16091 char_u *remain = NULL;
16092 char_u *q;
16093 int is_relative_to_current = FALSE;
16094 int has_trailing_pathsep = FALSE;
16095 int limit = 100;
16096
16097 p = vim_strsave(p);
16098
16099 if (p[0] == '.' && (vim_ispathsep(p[1])
16100 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16101 is_relative_to_current = TRUE;
16102
16103 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016104 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016107 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109
16110 q = getnextcomp(p);
16111 if (*q != NUL)
16112 {
16113 /* Separate the first path component in "p", and keep the
16114 * remainder (beginning with the path separator). */
16115 remain = vim_strsave(q - 1);
16116 q[-1] = NUL;
16117 }
16118
Bram Moolenaard9462e32011-04-11 21:35:11 +020016119 buf = alloc(MAXPATHL + 1);
16120 if (buf == NULL)
16121 goto fail;
16122
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 for (;;)
16124 {
16125 for (;;)
16126 {
16127 len = readlink((char *)p, (char *)buf, MAXPATHL);
16128 if (len <= 0)
16129 break;
16130 buf[len] = NUL;
16131
16132 if (limit-- == 0)
16133 {
16134 vim_free(p);
16135 vim_free(remain);
16136 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016137 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 goto fail;
16139 }
16140
16141 /* Ensure that the result will have a trailing path separator
16142 * if the argument has one. */
16143 if (remain == NULL && has_trailing_pathsep)
16144 add_pathsep(buf);
16145
16146 /* Separate the first path component in the link value and
16147 * concatenate the remainders. */
16148 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16149 if (*q != NUL)
16150 {
16151 if (remain == NULL)
16152 remain = vim_strsave(q - 1);
16153 else
16154 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016155 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 if (cpy != NULL)
16157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 vim_free(remain);
16159 remain = cpy;
16160 }
16161 }
16162 q[-1] = NUL;
16163 }
16164
16165 q = gettail(p);
16166 if (q > p && *q == NUL)
16167 {
16168 /* Ignore trailing path separator. */
16169 q[-1] = NUL;
16170 q = gettail(p);
16171 }
16172 if (q > p && !mch_isFullName(buf))
16173 {
16174 /* symlink is relative to directory of argument */
16175 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16176 if (cpy != NULL)
16177 {
16178 STRCPY(cpy, p);
16179 STRCPY(gettail(cpy), buf);
16180 vim_free(p);
16181 p = cpy;
16182 }
16183 }
16184 else
16185 {
16186 vim_free(p);
16187 p = vim_strsave(buf);
16188 }
16189 }
16190
16191 if (remain == NULL)
16192 break;
16193
16194 /* Append the first path component of "remain" to "p". */
16195 q = getnextcomp(remain + 1);
16196 len = q - remain - (*q != NUL);
16197 cpy = vim_strnsave(p, STRLEN(p) + len);
16198 if (cpy != NULL)
16199 {
16200 STRNCAT(cpy, remain, len);
16201 vim_free(p);
16202 p = cpy;
16203 }
16204 /* Shorten "remain". */
16205 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 else
16208 {
16209 vim_free(remain);
16210 remain = NULL;
16211 }
16212 }
16213
16214 /* If the result is a relative path name, make it explicitly relative to
16215 * the current directory if and only if the argument had this form. */
16216 if (!vim_ispathsep(*p))
16217 {
16218 if (is_relative_to_current
16219 && *p != NUL
16220 && !(p[0] == '.'
16221 && (p[1] == NUL
16222 || vim_ispathsep(p[1])
16223 || (p[1] == '.'
16224 && (p[2] == NUL
16225 || vim_ispathsep(p[2]))))))
16226 {
16227 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016228 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 if (cpy != NULL)
16230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 vim_free(p);
16232 p = cpy;
16233 }
16234 }
16235 else if (!is_relative_to_current)
16236 {
16237 /* Strip leading "./". */
16238 q = p;
16239 while (q[0] == '.' && vim_ispathsep(q[1]))
16240 q += 2;
16241 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016242 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 }
16244 }
16245
16246 /* Ensure that the result will have no trailing path separator
16247 * if the argument had none. But keep "/" or "//". */
16248 if (!has_trailing_pathsep)
16249 {
16250 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016251 if (after_pathsep(p, q))
16252 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 }
16254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016255 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 }
16257# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016258 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259# endif
16260#endif
16261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263
16264#ifdef HAVE_READLINK
16265fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016266 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269}
16270
16271/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016272 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 */
16274 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 typval_T *argvars;
16277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278{
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 list_T *l;
16280 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282 if (argvars[0].v_type != VAR_LIST)
16283 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016284 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016285 && !tv_check_lock(l->lv_lock,
16286 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 {
16288 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016289 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016290 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016291 while (li != NULL)
16292 {
16293 ni = li->li_prev;
16294 list_append(l, li);
16295 li = ni;
16296 }
16297 rettv->vval.v_list = l;
16298 rettv->v_type = VAR_LIST;
16299 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016300 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302}
16303
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016304#define SP_NOMOVE 0x01 /* don't move cursor */
16305#define SP_REPEAT 0x02 /* repeat to find outer pair */
16306#define SP_RETCOUNT 0x04 /* return matchcount */
16307#define SP_SETPCMARK 0x08 /* set previous context mark */
16308#define SP_START 0x10 /* accept match at start position */
16309#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16310#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016311
Bram Moolenaar33570922005-01-25 22:26:29 +000016312static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313
16314/*
16315 * Get flags for a search function.
16316 * Possibly sets "p_ws".
16317 * Returns BACKWARD, FORWARD or zero (for an error).
16318 */
16319 static int
16320get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016322 int *flagsp;
16323{
16324 int dir = FORWARD;
16325 char_u *flags;
16326 char_u nbuf[NUMBUFLEN];
16327 int mask;
16328
16329 if (varp->v_type != VAR_UNKNOWN)
16330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016331 flags = get_tv_string_buf_chk(varp, nbuf);
16332 if (flags == NULL)
16333 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016334 while (*flags != NUL)
16335 {
16336 switch (*flags)
16337 {
16338 case 'b': dir = BACKWARD; break;
16339 case 'w': p_ws = TRUE; break;
16340 case 'W': p_ws = FALSE; break;
16341 default: mask = 0;
16342 if (flagsp != NULL)
16343 switch (*flags)
16344 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016345 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016346 case 'e': mask = SP_END; break;
16347 case 'm': mask = SP_RETCOUNT; break;
16348 case 'n': mask = SP_NOMOVE; break;
16349 case 'p': mask = SP_SUBPAT; break;
16350 case 'r': mask = SP_REPEAT; break;
16351 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 }
16353 if (mask == 0)
16354 {
16355 EMSG2(_(e_invarg2), flags);
16356 dir = 0;
16357 }
16358 else
16359 *flagsp |= mask;
16360 }
16361 if (dir == 0)
16362 break;
16363 ++flags;
16364 }
16365 }
16366 return dir;
16367}
16368
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016370 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016372 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016373search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016375 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016376 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016378 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 char_u *pat;
16380 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016381 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 int save_p_ws = p_ws;
16383 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016384 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016385 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016386 proftime_T tm;
16387#ifdef FEAT_RELTIME
16388 long time_limit = 0;
16389#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016390 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016391 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016393 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016394 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016395 if (dir == 0)
16396 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016397 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016398 if (flags & SP_START)
16399 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016400 if (flags & SP_END)
16401 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016402
Bram Moolenaar76929292008-01-06 19:07:36 +000016403 /* Optional arguments: line number to stop searching and timeout. */
16404 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016405 {
16406 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16407 if (lnum_stop < 0)
16408 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016409#ifdef FEAT_RELTIME
16410 if (argvars[3].v_type != VAR_UNKNOWN)
16411 {
16412 time_limit = get_tv_number_chk(&argvars[3], NULL);
16413 if (time_limit < 0)
16414 goto theend;
16415 }
16416#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016417 }
16418
Bram Moolenaar76929292008-01-06 19:07:36 +000016419#ifdef FEAT_RELTIME
16420 /* Set the time limit, if there is one. */
16421 profile_setlimit(time_limit, &tm);
16422#endif
16423
Bram Moolenaar231334e2005-07-25 20:46:57 +000016424 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016425 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016426 * Check to make sure only those flags are set.
16427 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16428 * flags cannot be set. Check for that condition also.
16429 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016430 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016431 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016433 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016434 goto theend;
16435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016437 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016438 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016439 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016440 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016442 if (flags & SP_SUBPAT)
16443 retval = subpatnum;
16444 else
16445 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016446 if (flags & SP_SETPCMARK)
16447 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016449 if (match_pos != NULL)
16450 {
16451 /* Store the match cursor position */
16452 match_pos->lnum = pos.lnum;
16453 match_pos->col = pos.col + 1;
16454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 /* "/$" will put the cursor after the end of the line, may need to
16456 * correct that here */
16457 check_cursor();
16458 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016459
16460 /* If 'n' flag is used: restore cursor position. */
16461 if (flags & SP_NOMOVE)
16462 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016463 else
16464 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016465theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016467
16468 return retval;
16469}
16470
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016471#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016472
16473/*
16474 * round() is not in C90, use ceil() or floor() instead.
16475 */
16476 float_T
16477vim_round(f)
16478 float_T f;
16479{
16480 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16481}
16482
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016483/*
16484 * "round({float})" function
16485 */
16486 static void
16487f_round(argvars, rettv)
16488 typval_T *argvars;
16489 typval_T *rettv;
16490{
16491 float_T f;
16492
16493 rettv->v_type = VAR_FLOAT;
16494 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016495 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016496 else
16497 rettv->vval.v_float = 0.0;
16498}
16499#endif
16500
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016501/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016502 * "screenattr()" function
16503 */
16504 static void
16505f_screenattr(argvars, rettv)
16506 typval_T *argvars UNUSED;
16507 typval_T *rettv;
16508{
16509 int row;
16510 int col;
16511 int c;
16512
16513 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16514 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16515 if (row < 0 || row >= screen_Rows
16516 || col < 0 || col >= screen_Columns)
16517 c = -1;
16518 else
16519 c = ScreenAttrs[LineOffset[row] + col];
16520 rettv->vval.v_number = c;
16521}
16522
16523/*
16524 * "screenchar()" function
16525 */
16526 static void
16527f_screenchar(argvars, rettv)
16528 typval_T *argvars UNUSED;
16529 typval_T *rettv;
16530{
16531 int row;
16532 int col;
16533 int off;
16534 int c;
16535
16536 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16537 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16538 if (row < 0 || row >= screen_Rows
16539 || col < 0 || col >= screen_Columns)
16540 c = -1;
16541 else
16542 {
16543 off = LineOffset[row] + col;
16544#ifdef FEAT_MBYTE
16545 if (enc_utf8 && ScreenLinesUC[off] != 0)
16546 c = ScreenLinesUC[off];
16547 else
16548#endif
16549 c = ScreenLines[off];
16550 }
16551 rettv->vval.v_number = c;
16552}
16553
16554/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016555 * "screencol()" function
16556 *
16557 * First column is 1 to be consistent with virtcol().
16558 */
16559 static void
16560f_screencol(argvars, rettv)
16561 typval_T *argvars UNUSED;
16562 typval_T *rettv;
16563{
16564 rettv->vval.v_number = screen_screencol() + 1;
16565}
16566
16567/*
16568 * "screenrow()" function
16569 */
16570 static void
16571f_screenrow(argvars, rettv)
16572 typval_T *argvars UNUSED;
16573 typval_T *rettv;
16574{
16575 rettv->vval.v_number = screen_screenrow() + 1;
16576}
16577
16578/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016579 * "search()" function
16580 */
16581 static void
16582f_search(argvars, rettv)
16583 typval_T *argvars;
16584 typval_T *rettv;
16585{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016586 int flags = 0;
16587
16588 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589}
16590
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016592 * "searchdecl()" function
16593 */
16594 static void
16595f_searchdecl(argvars, rettv)
16596 typval_T *argvars;
16597 typval_T *rettv;
16598{
16599 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016600 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016601 int error = FALSE;
16602 char_u *name;
16603
16604 rettv->vval.v_number = 1; /* default: FAIL */
16605
16606 name = get_tv_string_chk(&argvars[0]);
16607 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016608 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016609 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016610 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16611 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16612 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016613 if (!error && name != NULL)
16614 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016615 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016616}
16617
16618/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016619 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016621 static int
16622searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016623 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016624 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625{
16626 char_u *spat, *mpat, *epat;
16627 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 int dir;
16630 int flags = 0;
16631 char_u nbuf1[NUMBUFLEN];
16632 char_u nbuf2[NUMBUFLEN];
16633 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016634 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016635 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016636 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016639 spat = get_tv_string_chk(&argvars[0]);
16640 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16641 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16642 if (spat == NULL || mpat == NULL || epat == NULL)
16643 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 /* Handle the optional fourth argument: flags */
16646 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016647 if (dir == 0)
16648 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016649
16650 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016651 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16652 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016653 if ((flags & (SP_END | SP_SUBPAT)) != 0
16654 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016655 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016656 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016657 goto theend;
16658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016660 /* Using 'r' implies 'W', otherwise it doesn't work. */
16661 if (flags & SP_REPEAT)
16662 p_ws = FALSE;
16663
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016664 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016665 if (argvars[3].v_type == VAR_UNKNOWN
16666 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 skip = (char_u *)"";
16668 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016670 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016671 if (argvars[5].v_type != VAR_UNKNOWN)
16672 {
16673 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16674 if (lnum_stop < 0)
16675 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016676#ifdef FEAT_RELTIME
16677 if (argvars[6].v_type != VAR_UNKNOWN)
16678 {
16679 time_limit = get_tv_number_chk(&argvars[6], NULL);
16680 if (time_limit < 0)
16681 goto theend;
16682 }
16683#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016684 }
16685 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 if (skip == NULL)
16687 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016689 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016690 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016691
16692theend:
16693 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016694
16695 return retval;
16696}
16697
16698/*
16699 * "searchpair()" function
16700 */
16701 static void
16702f_searchpair(argvars, rettv)
16703 typval_T *argvars;
16704 typval_T *rettv;
16705{
16706 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16707}
16708
16709/*
16710 * "searchpairpos()" function
16711 */
16712 static void
16713f_searchpairpos(argvars, rettv)
16714 typval_T *argvars;
16715 typval_T *rettv;
16716{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016717 pos_T match_pos;
16718 int lnum = 0;
16719 int col = 0;
16720
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016721 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016722 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016723
16724 if (searchpair_cmn(argvars, &match_pos) > 0)
16725 {
16726 lnum = match_pos.lnum;
16727 col = match_pos.col;
16728 }
16729
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016730 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16731 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016732}
16733
16734/*
16735 * Search for a start/middle/end thing.
16736 * Used by searchpair(), see its documentation for the details.
16737 * Returns 0 or -1 for no match,
16738 */
16739 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016740do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16741 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016742 char_u *spat; /* start pattern */
16743 char_u *mpat; /* middle pattern */
16744 char_u *epat; /* end pattern */
16745 int dir; /* BACKWARD or FORWARD */
16746 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016747 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016748 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016749 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016750 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016751{
16752 char_u *save_cpo;
16753 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16754 long retval = 0;
16755 pos_T pos;
16756 pos_T firstpos;
16757 pos_T foundpos;
16758 pos_T save_cursor;
16759 pos_T save_pos;
16760 int n;
16761 int r;
16762 int nest = 1;
16763 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016764 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016765 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016766
16767 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16768 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016769 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016770
Bram Moolenaar76929292008-01-06 19:07:36 +000016771#ifdef FEAT_RELTIME
16772 /* Set the time limit, if there is one. */
16773 profile_setlimit(time_limit, &tm);
16774#endif
16775
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016776 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16777 * start/middle/end (pat3, for the top pair). */
16778 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16779 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16780 if (pat2 == NULL || pat3 == NULL)
16781 goto theend;
16782 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16783 if (*mpat == NUL)
16784 STRCPY(pat3, pat2);
16785 else
16786 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16787 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016788 if (flags & SP_START)
16789 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016790
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 save_cursor = curwin->w_cursor;
16792 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016793 clearpos(&firstpos);
16794 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 pat = pat3;
16796 for (;;)
16797 {
16798 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016799 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16801 /* didn't find it or found the first match again: FAIL */
16802 break;
16803
16804 if (firstpos.lnum == 0)
16805 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016806 if (equalpos(pos, foundpos))
16807 {
16808 /* Found the same position again. Can happen with a pattern that
16809 * has "\zs" at the end and searching backwards. Advance one
16810 * character and try again. */
16811 if (dir == BACKWARD)
16812 decl(&pos);
16813 else
16814 incl(&pos);
16815 }
16816 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016818 /* clear the start flag to avoid getting stuck here */
16819 options &= ~SEARCH_START;
16820
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 /* If the skip pattern matches, ignore this match. */
16822 if (*skip != NUL)
16823 {
16824 save_pos = curwin->w_cursor;
16825 curwin->w_cursor = pos;
16826 r = eval_to_bool(skip, &err, NULL, FALSE);
16827 curwin->w_cursor = save_pos;
16828 if (err)
16829 {
16830 /* Evaluating {skip} caused an error, break here. */
16831 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016832 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 break;
16834 }
16835 if (r)
16836 continue;
16837 }
16838
16839 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16840 {
16841 /* Found end when searching backwards or start when searching
16842 * forward: nested pair. */
16843 ++nest;
16844 pat = pat2; /* nested, don't search for middle */
16845 }
16846 else
16847 {
16848 /* Found end when searching forward or start when searching
16849 * backward: end of (nested) pair; or found middle in outer pair. */
16850 if (--nest == 1)
16851 pat = pat3; /* outer level, search for middle */
16852 }
16853
16854 if (nest == 0)
16855 {
16856 /* Found the match: return matchcount or line number. */
16857 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016858 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016860 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016861 if (flags & SP_SETPCMARK)
16862 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 curwin->w_cursor = pos;
16864 if (!(flags & SP_REPEAT))
16865 break;
16866 nest = 1; /* search for next unmatched */
16867 }
16868 }
16869
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016870 if (match_pos != NULL)
16871 {
16872 /* Store the match cursor position */
16873 match_pos->lnum = curwin->w_cursor.lnum;
16874 match_pos->col = curwin->w_cursor.col + 1;
16875 }
16876
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016878 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 curwin->w_cursor = save_cursor;
16880
16881theend:
16882 vim_free(pat2);
16883 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016884 if (p_cpo == empty_option)
16885 p_cpo = save_cpo;
16886 else
16887 /* Darn, evaluating the {skip} expression changed the value. */
16888 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016889
16890 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891}
16892
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016893/*
16894 * "searchpos()" function
16895 */
16896 static void
16897f_searchpos(argvars, rettv)
16898 typval_T *argvars;
16899 typval_T *rettv;
16900{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016901 pos_T match_pos;
16902 int lnum = 0;
16903 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016904 int n;
16905 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016906
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016907 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016908 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016909
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016910 n = search_cmn(argvars, &match_pos, &flags);
16911 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016912 {
16913 lnum = match_pos.lnum;
16914 col = match_pos.col;
16915 }
16916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016917 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16918 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016919 if (flags & SP_SUBPAT)
16920 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016921}
16922
16923
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 static void
16925f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929#ifdef FEAT_CLIENTSERVER
16930 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 char_u *server = get_tv_string_chk(&argvars[0]);
16932 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935 if (server == NULL || reply == NULL)
16936 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 if (check_restricted() || check_secure())
16938 return;
16939# ifdef FEAT_X11
16940 if (check_connection() == FAIL)
16941 return;
16942# endif
16943
16944 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 EMSG(_("E258: Unable to send to client"));
16947 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 rettv->vval.v_number = 0;
16950#else
16951 rettv->vval.v_number = -1;
16952#endif
16953}
16954
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 static void
16956f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959{
16960 char_u *r = NULL;
16961
16962#ifdef FEAT_CLIENTSERVER
16963# ifdef WIN32
16964 r = serverGetVimNames();
16965# else
16966 make_connection();
16967 if (X_DISPLAY != NULL)
16968 r = serverGetVimNames(X_DISPLAY);
16969# endif
16970#endif
16971 rettv->v_type = VAR_STRING;
16972 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973}
16974
16975/*
16976 * "setbufvar()" function
16977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016979f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016981 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982{
16983 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 char_u nbuf[NUMBUFLEN];
16988
16989 if (check_restricted() || check_secure())
16990 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016991 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16992 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016993 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 varp = &argvars[2];
16995
16996 if (buf != NULL && varname != NULL && varp != NULL)
16997 {
16998 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000
17001 if (*varname == '&')
17002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 long numval;
17004 char_u *strval;
17005 int error = FALSE;
17006
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 numval = get_tv_number_chk(varp, &error);
17009 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017010 if (!error && strval != NULL)
17011 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 }
17013 else
17014 {
17015 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17016 if (bufvarname != NULL)
17017 {
17018 STRCPY(bufvarname, "b:");
17019 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017020 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 vim_free(bufvarname);
17022 }
17023 }
17024
17025 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028}
17029
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017030 static void
17031f_setcharsearch(argvars, rettv)
17032 typval_T *argvars;
17033 typval_T *rettv UNUSED;
17034{
17035 dict_T *d;
17036 dictitem_T *di;
17037 char_u *csearch;
17038
17039 if (argvars[0].v_type != VAR_DICT)
17040 {
17041 EMSG(_(e_dictreq));
17042 return;
17043 }
17044
17045 if ((d = argvars[0].vval.v_dict) != NULL)
17046 {
17047 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17048 if (csearch != NULL)
17049 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017050#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017051 if (enc_utf8)
17052 {
17053 int pcc[MAX_MCO];
17054 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017055
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017056 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17057 }
17058 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017059#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017060 set_last_csearch(PTR2CHAR(csearch),
17061 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017062 }
17063
17064 di = dict_find(d, (char_u *)"forward", -1);
17065 if (di != NULL)
17066 set_csearch_direction(get_tv_number(&di->di_tv)
17067 ? FORWARD : BACKWARD);
17068
17069 di = dict_find(d, (char_u *)"until", -1);
17070 if (di != NULL)
17071 set_csearch_until(!!get_tv_number(&di->di_tv));
17072 }
17073}
17074
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075/*
17076 * "setcmdpos()" function
17077 */
17078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 typval_T *argvars;
17081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017083 int pos = (int)get_tv_number(&argvars[0]) - 1;
17084
17085 if (pos >= 0)
17086 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087}
17088
17089/*
17090 * "setline()" function
17091 */
17092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017093f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 typval_T *argvars;
17095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096{
17097 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017098 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017099 list_T *l = NULL;
17100 listitem_T *li = NULL;
17101 long added = 0;
17102 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017104 lnum = get_tv_lnum(&argvars[0]);
17105 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017107 l = argvars[1].vval.v_list;
17108 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017110 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017112
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017113 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017114 for (;;)
17115 {
17116 if (l != NULL)
17117 {
17118 /* list argument, get next string */
17119 if (li == NULL)
17120 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017121 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122 li = li->li_next;
17123 }
17124
17125 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017127 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017128
17129 /* When coming here from Insert mode, sync undo, so that this can be
17130 * undone separately from what was previously inserted. */
17131 if (u_sync_once == 2)
17132 {
17133 u_sync_once = 1; /* notify that u_sync() was called */
17134 u_sync(TRUE);
17135 }
17136
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 if (lnum <= curbuf->b_ml.ml_line_count)
17138 {
17139 /* existing line, replace it */
17140 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17141 {
17142 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017143 if (lnum == curwin->w_cursor.lnum)
17144 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017145 rettv->vval.v_number = 0; /* OK */
17146 }
17147 }
17148 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17149 {
17150 /* lnum is one past the last line, append the line */
17151 ++added;
17152 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17153 rettv->vval.v_number = 0; /* OK */
17154 }
17155
17156 if (l == NULL) /* only one string argument */
17157 break;
17158 ++lnum;
17159 }
17160
17161 if (added > 0)
17162 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163}
17164
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017165static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17166
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017168 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017169 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017170 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017171set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017172 win_T *wp UNUSED;
17173 typval_T *list_arg UNUSED;
17174 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017175 typval_T *rettv;
17176{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017177#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017178 char_u *act;
17179 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017180#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017181
Bram Moolenaar2641f772005-03-25 21:58:17 +000017182 rettv->vval.v_number = -1;
17183
17184#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017185 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017186 EMSG(_(e_listreq));
17187 else
17188 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017189 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017190
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017191 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017192 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017193 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017194 if (act == NULL)
17195 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017196 if (*act == 'a' || *act == 'r')
17197 action = *act;
17198 }
17199
Bram Moolenaar81484f42012-12-05 15:16:47 +010017200 if (l != NULL && set_errorlist(wp, l, action,
17201 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017202 rettv->vval.v_number = 0;
17203 }
17204#endif
17205}
17206
17207/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017208 * "setloclist()" function
17209 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017210 static void
17211f_setloclist(argvars, rettv)
17212 typval_T *argvars;
17213 typval_T *rettv;
17214{
17215 win_T *win;
17216
17217 rettv->vval.v_number = -1;
17218
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017219 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017220 if (win != NULL)
17221 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17222}
17223
17224/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017225 * "setmatches()" function
17226 */
17227 static void
17228f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017229 typval_T *argvars UNUSED;
17230 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017231{
17232#ifdef FEAT_SEARCH_EXTRA
17233 list_T *l;
17234 listitem_T *li;
17235 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017236 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017237
17238 rettv->vval.v_number = -1;
17239 if (argvars[0].v_type != VAR_LIST)
17240 {
17241 EMSG(_(e_listreq));
17242 return;
17243 }
17244 if ((l = argvars[0].vval.v_list) != NULL)
17245 {
17246
17247 /* To some extent make sure that we are dealing with a list from
17248 * "getmatches()". */
17249 li = l->lv_first;
17250 while (li != NULL)
17251 {
17252 if (li->li_tv.v_type != VAR_DICT
17253 || (d = li->li_tv.vval.v_dict) == NULL)
17254 {
17255 EMSG(_(e_invarg));
17256 return;
17257 }
17258 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017259 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17260 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017261 && dict_find(d, (char_u *)"priority", -1) != NULL
17262 && dict_find(d, (char_u *)"id", -1) != NULL))
17263 {
17264 EMSG(_(e_invarg));
17265 return;
17266 }
17267 li = li->li_next;
17268 }
17269
17270 clear_matches(curwin);
17271 li = l->lv_first;
17272 while (li != NULL)
17273 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017274 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017275 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017276 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017277 char_u *group;
17278 int priority;
17279 int id;
17280 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017281
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017282 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017283 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17284 {
17285 if (s == NULL)
17286 {
17287 s = list_alloc();
17288 if (s == NULL)
17289 return;
17290 }
17291
17292 /* match from matchaddpos() */
17293 for (i = 1; i < 9; i++)
17294 {
17295 sprintf((char *)buf, (char *)"pos%d", i);
17296 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17297 {
17298 if (di->di_tv.v_type != VAR_LIST)
17299 return;
17300
17301 list_append_tv(s, &di->di_tv);
17302 s->lv_refcount++;
17303 }
17304 else
17305 break;
17306 }
17307 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017308
17309 group = get_dict_string(d, (char_u *)"group", FALSE);
17310 priority = (int)get_dict_number(d, (char_u *)"priority");
17311 id = (int)get_dict_number(d, (char_u *)"id");
17312 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17313 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17314 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017315 if (i == 0)
17316 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017317 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017318 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017319 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017320 }
17321 else
17322 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017323 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017324 list_unref(s);
17325 s = NULL;
17326 }
17327
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017328 li = li->li_next;
17329 }
17330 rettv->vval.v_number = 0;
17331 }
17332#endif
17333}
17334
17335/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017336 * "setpos()" function
17337 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017338 static void
17339f_setpos(argvars, rettv)
17340 typval_T *argvars;
17341 typval_T *rettv;
17342{
17343 pos_T pos;
17344 int fnum;
17345 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017346 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017347
Bram Moolenaar08250432008-02-13 11:42:46 +000017348 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017349 name = get_tv_string_chk(argvars);
17350 if (name != NULL)
17351 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017352 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017353 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017354 if (--pos.col < 0)
17355 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017356 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017357 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017358 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017359 if (fnum == curbuf->b_fnum)
17360 {
17361 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017362 if (curswant >= 0)
17363 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017364 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017365 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017366 }
17367 else
17368 EMSG(_(e_invarg));
17369 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017370 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17371 {
17372 /* set mark */
17373 if (setmark_pos(name[1], &pos, fnum) == OK)
17374 rettv->vval.v_number = 0;
17375 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017376 else
17377 EMSG(_(e_invarg));
17378 }
17379 }
17380}
17381
17382/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017383 * "setqflist()" function
17384 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017385 static void
17386f_setqflist(argvars, rettv)
17387 typval_T *argvars;
17388 typval_T *rettv;
17389{
17390 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17391}
17392
17393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 * "setreg()" function
17395 */
17396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017397f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 typval_T *argvars;
17399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400{
17401 int regname;
17402 char_u *strregname;
17403 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017404 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 int append;
17406 char_u yank_type;
17407 long block_len;
17408
17409 block_len = -1;
17410 yank_type = MAUTO;
17411 append = FALSE;
17412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017413 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017416 if (strregname == NULL)
17417 return; /* type error; errmsg already given */
17418 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 if (regname == 0 || regname == '@')
17420 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017424 stropt = get_tv_string_chk(&argvars[2]);
17425 if (stropt == NULL)
17426 return; /* type error */
17427 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 switch (*stropt)
17429 {
17430 case 'a': case 'A': /* append */
17431 append = TRUE;
17432 break;
17433 case 'v': case 'c': /* character-wise selection */
17434 yank_type = MCHAR;
17435 break;
17436 case 'V': case 'l': /* line-wise selection */
17437 yank_type = MLINE;
17438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 case 'b': case Ctrl_V: /* block-wise selection */
17440 yank_type = MBLOCK;
17441 if (VIM_ISDIGIT(stropt[1]))
17442 {
17443 ++stropt;
17444 block_len = getdigits(&stropt) - 1;
17445 --stropt;
17446 }
17447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 }
17449 }
17450
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017451 if (argvars[1].v_type == VAR_LIST)
17452 {
17453 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017454 char_u **allocval;
17455 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017456 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017457 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017458 int len = argvars[1].vval.v_list->lv_len;
17459 listitem_T *li;
17460
Bram Moolenaar7d647822014-04-05 21:28:56 +020017461 /* First half: use for pointers to result lines; second half: use for
17462 * pointers to allocated copies. */
17463 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017464 if (lstval == NULL)
17465 return;
17466 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017467 allocval = lstval + len + 2;
17468 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017469
17470 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17471 li = li->li_next)
17472 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017473 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017474 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017475 goto free_lstval;
17476 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017477 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017478 /* Need to make a copy, next get_tv_string_buf_chk() will
17479 * overwrite the string. */
17480 strval = vim_strsave(buf);
17481 if (strval == NULL)
17482 goto free_lstval;
17483 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017484 }
17485 *curval++ = strval;
17486 }
17487 *curval++ = NULL;
17488
17489 write_reg_contents_lst(regname, lstval, -1,
17490 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017491free_lstval:
17492 while (curallocval > allocval)
17493 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017494 vim_free(lstval);
17495 }
17496 else
17497 {
17498 strval = get_tv_string_chk(&argvars[1]);
17499 if (strval == NULL)
17500 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017501 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017504 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505}
17506
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017507/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017508 * "settabvar()" function
17509 */
17510 static void
17511f_settabvar(argvars, rettv)
17512 typval_T *argvars;
17513 typval_T *rettv;
17514{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017515#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017516 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017517 tabpage_T *tp;
17518#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017519 char_u *varname, *tabvarname;
17520 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017521
17522 rettv->vval.v_number = 0;
17523
17524 if (check_restricted() || check_secure())
17525 return;
17526
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017527#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017528 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017529#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017530 varname = get_tv_string_chk(&argvars[1]);
17531 varp = &argvars[2];
17532
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017533 if (varname != NULL && varp != NULL
17534#ifdef FEAT_WINDOWS
17535 && tp != NULL
17536#endif
17537 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017538 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017539#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017540 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017541 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017542#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017543
17544 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17545 if (tabvarname != NULL)
17546 {
17547 STRCPY(tabvarname, "t:");
17548 STRCPY(tabvarname + 2, varname);
17549 set_var(tabvarname, varp, TRUE);
17550 vim_free(tabvarname);
17551 }
17552
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017553#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017554 /* Restore current tabpage */
17555 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017556 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017557#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017558 }
17559}
17560
17561/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017562 * "settabwinvar()" function
17563 */
17564 static void
17565f_settabwinvar(argvars, rettv)
17566 typval_T *argvars;
17567 typval_T *rettv;
17568{
17569 setwinvar(argvars, rettv, 1);
17570}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571
17572/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017573 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017576f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 typval_T *argvars;
17578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017580 setwinvar(argvars, rettv, 0);
17581}
17582
17583/*
17584 * "setwinvar()" and "settabwinvar()" functions
17585 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017586
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017587 static void
17588setwinvar(argvars, rettv, off)
17589 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017590 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017591 int off;
17592{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 win_T *win;
17594#ifdef FEAT_WINDOWS
17595 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017596 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597#endif
17598 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017601 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602
17603 if (check_restricted() || check_secure())
17604 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017605
17606#ifdef FEAT_WINDOWS
17607 if (off == 1)
17608 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17609 else
17610 tp = curtab;
17611#endif
17612 win = find_win_by_nr(&argvars[off], tp);
17613 varname = get_tv_string_chk(&argvars[off + 1]);
17614 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615
17616 if (win != NULL && varname != NULL && varp != NULL)
17617 {
17618#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017619 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017622 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017624 long numval;
17625 char_u *strval;
17626 int error = FALSE;
17627
17628 ++varname;
17629 numval = get_tv_number_chk(varp, &error);
17630 strval = get_tv_string_buf_chk(varp, nbuf);
17631 if (!error && strval != NULL)
17632 set_option_value(varname, numval, strval, OPT_LOCAL);
17633 }
17634 else
17635 {
17636 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17637 if (winvarname != NULL)
17638 {
17639 STRCPY(winvarname, "w:");
17640 STRCPY(winvarname + 2, varname);
17641 set_var(winvarname, varp, TRUE);
17642 vim_free(winvarname);
17643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 }
17645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017647 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648#endif
17649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650}
17651
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017652#ifdef FEAT_CRYPT
17653/*
17654 * "sha256({string})" function
17655 */
17656 static void
17657f_sha256(argvars, rettv)
17658 typval_T *argvars;
17659 typval_T *rettv;
17660{
17661 char_u *p;
17662
17663 p = get_tv_string(&argvars[0]);
17664 rettv->vval.v_string = vim_strsave(
17665 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17666 rettv->v_type = VAR_STRING;
17667}
17668#endif /* FEAT_CRYPT */
17669
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017671 * "shellescape({string})" function
17672 */
17673 static void
17674f_shellescape(argvars, rettv)
17675 typval_T *argvars;
17676 typval_T *rettv;
17677{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017678 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017679 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017680 rettv->v_type = VAR_STRING;
17681}
17682
17683/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017684 * shiftwidth() function
17685 */
17686 static void
17687f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017688 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017689 typval_T *rettv;
17690{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017691 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017692}
17693
17694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017695 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 */
17697 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 typval_T *argvars;
17700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703
Bram Moolenaar0d660222005-01-07 21:51:51 +000017704 p = get_tv_string(&argvars[0]);
17705 rettv->vval.v_string = vim_strsave(p);
17706 simplify_filename(rettv->vval.v_string); /* simplify in place */
17707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708}
17709
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017710#ifdef FEAT_FLOAT
17711/*
17712 * "sin()" function
17713 */
17714 static void
17715f_sin(argvars, rettv)
17716 typval_T *argvars;
17717 typval_T *rettv;
17718{
17719 float_T f;
17720
17721 rettv->v_type = VAR_FLOAT;
17722 if (get_float_arg(argvars, &f) == OK)
17723 rettv->vval.v_float = sin(f);
17724 else
17725 rettv->vval.v_float = 0.0;
17726}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017727
17728/*
17729 * "sinh()" function
17730 */
17731 static void
17732f_sinh(argvars, rettv)
17733 typval_T *argvars;
17734 typval_T *rettv;
17735{
17736 float_T f;
17737
17738 rettv->v_type = VAR_FLOAT;
17739 if (get_float_arg(argvars, &f) == OK)
17740 rettv->vval.v_float = sinh(f);
17741 else
17742 rettv->vval.v_float = 0.0;
17743}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017744#endif
17745
Bram Moolenaar0d660222005-01-07 21:51:51 +000017746static int
17747#ifdef __BORLANDC__
17748 _RTLENTRYF
17749#endif
17750 item_compare __ARGS((const void *s1, const void *s2));
17751static int
17752#ifdef __BORLANDC__
17753 _RTLENTRYF
17754#endif
17755 item_compare2 __ARGS((const void *s1, const void *s2));
17756
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017757/* struct used in the array that's given to qsort() */
17758typedef struct
17759{
17760 listitem_T *item;
17761 int idx;
17762} sortItem_T;
17763
Bram Moolenaar0d660222005-01-07 21:51:51 +000017764static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017765static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017767static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017769static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017770static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771#define ITEM_COMPARE_FAIL 999
17772
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017774 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017776 static int
17777#ifdef __BORLANDC__
17778_RTLENTRYF
17779#endif
17780item_compare(s1, s2)
17781 const void *s1;
17782 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017784 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017785 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017786 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017787 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788 int res;
17789 char_u numbuf1[NUMBUFLEN];
17790 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017792 si1 = (sortItem_T *)s1;
17793 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017794 tv1 = &si1->item->li_tv;
17795 tv2 = &si2->item->li_tv;
17796 /* tv2string() puts quotes around a string and allocates memory. Don't do
17797 * that for string variables. Use a single quote when comparing with a
17798 * non-string to do what the docs promise. */
17799 if (tv1->v_type == VAR_STRING)
17800 {
17801 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17802 p1 = (char_u *)"'";
17803 else
17804 p1 = tv1->vval.v_string;
17805 }
17806 else
17807 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17808 if (tv2->v_type == VAR_STRING)
17809 {
17810 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17811 p2 = (char_u *)"'";
17812 else
17813 p2 = tv2->vval.v_string;
17814 }
17815 else
17816 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017817 if (p1 == NULL)
17818 p1 = (char_u *)"";
17819 if (p2 == NULL)
17820 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017821 if (!item_compare_numeric)
17822 {
17823 if (item_compare_ic)
17824 res = STRICMP(p1, p2);
17825 else
17826 res = STRCMP(p1, p2);
17827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017829 {
17830 double n1, n2;
17831 n1 = strtod((char *)p1, (char **)&p1);
17832 n2 = strtod((char *)p2, (char **)&p2);
17833 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17834 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017835
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017836 /* When the result would be zero, compare the item indexes. Makes the
17837 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017838 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017839 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017840
Bram Moolenaar0d660222005-01-07 21:51:51 +000017841 vim_free(tofree1);
17842 vim_free(tofree2);
17843 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844}
17845
17846 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017847#ifdef __BORLANDC__
17848_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850item_compare2(s1, s2)
17851 const void *s1;
17852 const void *s2;
17853{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017854 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017855 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017856 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017857 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017858 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017860 /* shortcut after failure in previous call; compare all items equal */
17861 if (item_compare_func_err)
17862 return 0;
17863
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017864 si1 = (sortItem_T *)s1;
17865 si2 = (sortItem_T *)s2;
17866
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017867 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017868 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017869 copy_tv(&si1->item->li_tv, &argv[0]);
17870 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017871
17872 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017873 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017874 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17875 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017876 clear_tv(&argv[0]);
17877 clear_tv(&argv[1]);
17878
17879 if (res == FAIL)
17880 res = ITEM_COMPARE_FAIL;
17881 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017882 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17883 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017884 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017885 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017886
17887 /* When the result would be zero, compare the pointers themselves. Makes
17888 * the sort stable. */
17889 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017890 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017891
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892 return res;
17893}
17894
17895/*
17896 * "sort({list})" function
17897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017899do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 typval_T *argvars;
17901 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017902 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903{
Bram Moolenaar33570922005-01-25 22:26:29 +000017904 list_T *l;
17905 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017906 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017907 long len;
17908 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017911 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 else
17913 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017914 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017915 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017916 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17917 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017918 return;
17919 rettv->vval.v_list = l;
17920 rettv->v_type = VAR_LIST;
17921 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922
Bram Moolenaar0d660222005-01-07 21:51:51 +000017923 len = list_len(l);
17924 if (len <= 1)
17925 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926
Bram Moolenaar0d660222005-01-07 21:51:51 +000017927 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017928 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017930 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017931 if (argvars[1].v_type != VAR_UNKNOWN)
17932 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017933 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017935 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017936 else
17937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017938 int error = FALSE;
17939
17940 i = get_tv_number_chk(&argvars[1], &error);
17941 if (error)
17942 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017943 if (i == 1)
17944 item_compare_ic = TRUE;
17945 else
17946 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017947 if (item_compare_func != NULL)
17948 {
17949 if (STRCMP(item_compare_func, "n") == 0)
17950 {
17951 item_compare_func = NULL;
17952 item_compare_numeric = TRUE;
17953 }
17954 else if (STRCMP(item_compare_func, "i") == 0)
17955 {
17956 item_compare_func = NULL;
17957 item_compare_ic = TRUE;
17958 }
17959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017961
17962 if (argvars[2].v_type != VAR_UNKNOWN)
17963 {
17964 /* optional third argument: {dict} */
17965 if (argvars[2].v_type != VAR_DICT)
17966 {
17967 EMSG(_(e_dictreq));
17968 return;
17969 }
17970 item_compare_selfdict = argvars[2].vval.v_dict;
17971 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973
Bram Moolenaar0d660222005-01-07 21:51:51 +000017974 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017975 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017976 if (ptrs == NULL)
17977 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978
Bram Moolenaar327aa022014-03-25 18:24:23 +010017979 i = 0;
17980 if (sort)
17981 {
17982 /* sort(): ptrs will be the list to sort */
17983 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017984 {
17985 ptrs[i].item = li;
17986 ptrs[i].idx = i;
17987 ++i;
17988 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017989
17990 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017991 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017992 /* test the compare function */
17993 if (item_compare_func != NULL
17994 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017996 EMSG(_("E702: Sort compare function failed"));
17997 else
17998 {
17999 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018000 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018001 item_compare_func == NULL ? item_compare : item_compare2);
18002
18003 if (!item_compare_func_err)
18004 {
18005 /* Clear the List and append the items in sorted order. */
18006 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18007 l->lv_len = 0;
18008 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018009 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018010 }
18011 }
18012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018015 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18016
18017 /* f_uniq(): ptrs will be a stack of items to remove */
18018 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018019 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018020 item_compare_func_ptr = item_compare_func
18021 ? item_compare2 : item_compare;
18022
18023 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18024 li = li->li_next)
18025 {
18026 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18027 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018028 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018029 if (item_compare_func_err)
18030 {
18031 EMSG(_("E882: Uniq compare function failed"));
18032 break;
18033 }
18034 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018036 if (!item_compare_func_err)
18037 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018038 while (--i >= 0)
18039 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018040 li = ptrs[i].item->li_next;
18041 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018042 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018043 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018044 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018045 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018046 list_fix_watch(l, li);
18047 listitem_free(li);
18048 l->lv_len--;
18049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018050 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051 }
18052
18053 vim_free(ptrs);
18054 }
18055}
18056
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018057/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018058 * "sort({list})" function
18059 */
18060 static void
18061f_sort(argvars, rettv)
18062 typval_T *argvars;
18063 typval_T *rettv;
18064{
18065 do_sort_uniq(argvars, rettv, TRUE);
18066}
18067
18068/*
18069 * "uniq({list})" function
18070 */
18071 static void
18072f_uniq(argvars, rettv)
18073 typval_T *argvars;
18074 typval_T *rettv;
18075{
18076 do_sort_uniq(argvars, rettv, FALSE);
18077}
18078
18079/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018080 * "soundfold({word})" function
18081 */
18082 static void
18083f_soundfold(argvars, rettv)
18084 typval_T *argvars;
18085 typval_T *rettv;
18086{
18087 char_u *s;
18088
18089 rettv->v_type = VAR_STRING;
18090 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018091#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018092 rettv->vval.v_string = eval_soundfold(s);
18093#else
18094 rettv->vval.v_string = vim_strsave(s);
18095#endif
18096}
18097
18098/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018099 * "spellbadword()" function
18100 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018101 static void
18102f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018103 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018104 typval_T *rettv;
18105{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018106 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018107 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018108 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018111 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018112
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018113#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018114 if (argvars[0].v_type == VAR_UNKNOWN)
18115 {
18116 /* Find the start and length of the badly spelled word. */
18117 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18118 if (len != 0)
18119 word = ml_get_cursor();
18120 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018121 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018122 {
18123 char_u *str = get_tv_string_chk(&argvars[0]);
18124 int capcol = -1;
18125
18126 if (str != NULL)
18127 {
18128 /* Check the argument for spelling. */
18129 while (*str != NUL)
18130 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018131 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018132 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018133 {
18134 word = str;
18135 break;
18136 }
18137 str += len;
18138 }
18139 }
18140 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018141#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018143 list_append_string(rettv->vval.v_list, word, len);
18144 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018145 attr == HLF_SPB ? "bad" :
18146 attr == HLF_SPR ? "rare" :
18147 attr == HLF_SPL ? "local" :
18148 attr == HLF_SPC ? "caps" :
18149 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018150}
18151
18152/*
18153 * "spellsuggest()" function
18154 */
18155 static void
18156f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018157 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018158 typval_T *rettv;
18159{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018160#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018161 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018162 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018163 int maxcount;
18164 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018165 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018166 listitem_T *li;
18167 int need_capital = FALSE;
18168#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018169
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018170 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018171 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018172
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018173#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018174 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018175 {
18176 str = get_tv_string(&argvars[0]);
18177 if (argvars[1].v_type != VAR_UNKNOWN)
18178 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018179 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018180 if (maxcount <= 0)
18181 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018182 if (argvars[2].v_type != VAR_UNKNOWN)
18183 {
18184 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18185 if (typeerr)
18186 return;
18187 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018188 }
18189 else
18190 maxcount = 25;
18191
Bram Moolenaar4770d092006-01-12 23:22:24 +000018192 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018193
18194 for (i = 0; i < ga.ga_len; ++i)
18195 {
18196 str = ((char_u **)ga.ga_data)[i];
18197
18198 li = listitem_alloc();
18199 if (li == NULL)
18200 vim_free(str);
18201 else
18202 {
18203 li->li_tv.v_type = VAR_STRING;
18204 li->li_tv.v_lock = 0;
18205 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018206 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018207 }
18208 }
18209 ga_clear(&ga);
18210 }
18211#endif
18212}
18213
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018215f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 typval_T *argvars;
18217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018218{
18219 char_u *str;
18220 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018221 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018222 regmatch_T regmatch;
18223 char_u patbuf[NUMBUFLEN];
18224 char_u *save_cpo;
18225 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018226 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018227 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018228 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229
18230 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18231 save_cpo = p_cpo;
18232 p_cpo = (char_u *)"";
18233
18234 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018235 if (argvars[1].v_type != VAR_UNKNOWN)
18236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18238 if (pat == NULL)
18239 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018240 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018242 }
18243 if (pat == NULL || *pat == NUL)
18244 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018246 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018248 if (typeerr)
18249 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18252 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018255 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018256 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018257 if (*str == NUL)
18258 match = FALSE; /* empty item at the end */
18259 else
18260 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018261 if (match)
18262 end = regmatch.startp[0];
18263 else
18264 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018265 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18266 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018268 if (list_append_string(rettv->vval.v_list, str,
18269 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271 }
18272 if (!match)
18273 break;
18274 /* Advance to just after the match. */
18275 if (regmatch.endp[0] > str)
18276 col = 0;
18277 else
18278 {
18279 /* Don't get stuck at the same match. */
18280#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018281 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282#else
18283 col = 1;
18284#endif
18285 }
18286 str = regmatch.endp[0];
18287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288
Bram Moolenaar473de612013-06-08 18:19:48 +020018289 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293}
18294
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018295#ifdef FEAT_FLOAT
18296/*
18297 * "sqrt()" function
18298 */
18299 static void
18300f_sqrt(argvars, rettv)
18301 typval_T *argvars;
18302 typval_T *rettv;
18303{
18304 float_T f;
18305
18306 rettv->v_type = VAR_FLOAT;
18307 if (get_float_arg(argvars, &f) == OK)
18308 rettv->vval.v_float = sqrt(f);
18309 else
18310 rettv->vval.v_float = 0.0;
18311}
18312
18313/*
18314 * "str2float()" function
18315 */
18316 static void
18317f_str2float(argvars, rettv)
18318 typval_T *argvars;
18319 typval_T *rettv;
18320{
18321 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18322
18323 if (*p == '+')
18324 p = skipwhite(p + 1);
18325 (void)string2float(p, &rettv->vval.v_float);
18326 rettv->v_type = VAR_FLOAT;
18327}
18328#endif
18329
Bram Moolenaar2c932302006-03-18 21:42:09 +000018330/*
18331 * "str2nr()" function
18332 */
18333 static void
18334f_str2nr(argvars, rettv)
18335 typval_T *argvars;
18336 typval_T *rettv;
18337{
18338 int base = 10;
18339 char_u *p;
18340 long n;
18341
18342 if (argvars[1].v_type != VAR_UNKNOWN)
18343 {
18344 base = get_tv_number(&argvars[1]);
18345 if (base != 8 && base != 10 && base != 16)
18346 {
18347 EMSG(_(e_invarg));
18348 return;
18349 }
18350 }
18351
18352 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018353 if (*p == '+')
18354 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018355 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018356 rettv->vval.v_number = n;
18357}
18358
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359#ifdef HAVE_STRFTIME
18360/*
18361 * "strftime({format}[, {time}])" function
18362 */
18363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018365 typval_T *argvars;
18366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367{
18368 char_u result_buf[256];
18369 struct tm *curtime;
18370 time_t seconds;
18371 char_u *p;
18372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018376 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 seconds = time(NULL);
18378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018379 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 curtime = localtime(&seconds);
18381 /* MSVC returns NULL for an invalid value of seconds. */
18382 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 else
18385 {
18386# ifdef FEAT_MBYTE
18387 vimconv_T conv;
18388 char_u *enc;
18389
18390 conv.vc_type = CONV_NONE;
18391 enc = enc_locale();
18392 convert_setup(&conv, p_enc, enc);
18393 if (conv.vc_type != CONV_NONE)
18394 p = string_convert(&conv, p, NULL);
18395# endif
18396 if (p != NULL)
18397 (void)strftime((char *)result_buf, sizeof(result_buf),
18398 (char *)p, curtime);
18399 else
18400 result_buf[0] = NUL;
18401
18402# ifdef FEAT_MBYTE
18403 if (conv.vc_type != CONV_NONE)
18404 vim_free(p);
18405 convert_setup(&conv, enc, p_enc);
18406 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018407 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 else
18409# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018410 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411
18412# ifdef FEAT_MBYTE
18413 /* Release conversion descriptors */
18414 convert_setup(&conv, NULL, NULL);
18415 vim_free(enc);
18416# endif
18417 }
18418}
18419#endif
18420
18421/*
18422 * "stridx()" function
18423 */
18424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018425f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018426 typval_T *argvars;
18427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428{
18429 char_u buf[NUMBUFLEN];
18430 char_u *needle;
18431 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018432 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018434 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018436 needle = get_tv_string_chk(&argvars[1]);
18437 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018439 if (needle == NULL || haystack == NULL)
18440 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441
Bram Moolenaar33570922005-01-25 22:26:29 +000018442 if (argvars[2].v_type != VAR_UNKNOWN)
18443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018444 int error = FALSE;
18445
18446 start_idx = get_tv_number_chk(&argvars[2], &error);
18447 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018448 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018449 if (start_idx >= 0)
18450 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 }
18452
18453 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18454 if (pos != NULL)
18455 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456}
18457
18458/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018459 * "string()" function
18460 */
18461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018462f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018463 typval_T *argvars;
18464 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018465{
18466 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018467 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018470 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018471 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018472 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474}
18475
18476/*
18477 * "strlen()" function
18478 */
18479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018480f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018481 typval_T *argvars;
18482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 rettv->vval.v_number = (varnumber_T)(STRLEN(
18485 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486}
18487
18488/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018489 * "strchars()" function
18490 */
18491 static void
18492f_strchars(argvars, rettv)
18493 typval_T *argvars;
18494 typval_T *rettv;
18495{
18496 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018497 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018498#ifdef FEAT_MBYTE
18499 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018500 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018501#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018502
18503 if (argvars[1].v_type != VAR_UNKNOWN)
18504 skipcc = get_tv_number_chk(&argvars[1], NULL);
18505 if (skipcc < 0 || skipcc > 1)
18506 EMSG(_(e_invarg));
18507 else
18508 {
18509#ifdef FEAT_MBYTE
18510 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18511 while (*s != NUL)
18512 {
18513 func_mb_ptr2char_adv(&s);
18514 ++len;
18515 }
18516 rettv->vval.v_number = len;
18517#else
18518 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18519#endif
18520 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018521}
18522
18523/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018524 * "strdisplaywidth()" function
18525 */
18526 static void
18527f_strdisplaywidth(argvars, rettv)
18528 typval_T *argvars;
18529 typval_T *rettv;
18530{
18531 char_u *s = get_tv_string(&argvars[0]);
18532 int col = 0;
18533
18534 if (argvars[1].v_type != VAR_UNKNOWN)
18535 col = get_tv_number(&argvars[1]);
18536
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018537 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018538}
18539
18540/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018541 * "strwidth()" function
18542 */
18543 static void
18544f_strwidth(argvars, rettv)
18545 typval_T *argvars;
18546 typval_T *rettv;
18547{
18548 char_u *s = get_tv_string(&argvars[0]);
18549
18550 rettv->vval.v_number = (varnumber_T)(
18551#ifdef FEAT_MBYTE
18552 mb_string2cells(s, -1)
18553#else
18554 STRLEN(s)
18555#endif
18556 );
18557}
18558
18559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 * "strpart()" function
18561 */
18562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018564 typval_T *argvars;
18565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566{
18567 char_u *p;
18568 int n;
18569 int len;
18570 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 slen = (int)STRLEN(p);
18575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018576 n = get_tv_number_chk(&argvars[1], &error);
18577 if (error)
18578 len = 0;
18579 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018580 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 else
18582 len = slen - n; /* default len: all bytes that are available. */
18583
18584 /*
18585 * Only return the overlap between the specified part and the actual
18586 * string.
18587 */
18588 if (n < 0)
18589 {
18590 len += n;
18591 n = 0;
18592 }
18593 else if (n > slen)
18594 n = slen;
18595 if (len < 0)
18596 len = 0;
18597 else if (n + len > slen)
18598 len = slen - n;
18599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018600 rettv->v_type = VAR_STRING;
18601 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602}
18603
18604/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605 * "strridx()" function
18606 */
18607 static void
18608f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018609 typval_T *argvars;
18610 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611{
18612 char_u buf[NUMBUFLEN];
18613 char_u *needle;
18614 char_u *haystack;
18615 char_u *rest;
18616 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018617 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018619 needle = get_tv_string_chk(&argvars[1]);
18620 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018621
18622 rettv->vval.v_number = -1;
18623 if (needle == NULL || haystack == NULL)
18624 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018625
18626 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018627 if (argvars[2].v_type != VAR_UNKNOWN)
18628 {
18629 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018630 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018631 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018632 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018633 }
18634 else
18635 end_idx = haystack_len;
18636
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018638 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018640 lastmatch = haystack + end_idx;
18641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018642 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018643 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018644 for (rest = haystack; *rest != '\0'; ++rest)
18645 {
18646 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018647 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 break;
18649 lastmatch = rest;
18650 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018651 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652
18653 if (lastmatch == NULL)
18654 rettv->vval.v_number = -1;
18655 else
18656 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18657}
18658
18659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 * "strtrans()" function
18661 */
18662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018664 typval_T *argvars;
18665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667 rettv->v_type = VAR_STRING;
18668 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669}
18670
18671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018672 * "submatch()" function
18673 */
18674 static void
18675f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 typval_T *argvars;
18677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018678{
Bram Moolenaar41571762014-04-02 19:00:58 +020018679 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018680 int no;
18681 int retList = 0;
18682
18683 no = (int)get_tv_number_chk(&argvars[0], &error);
18684 if (error)
18685 return;
18686 error = FALSE;
18687 if (argvars[1].v_type != VAR_UNKNOWN)
18688 retList = get_tv_number_chk(&argvars[1], &error);
18689 if (error)
18690 return;
18691
18692 if (retList == 0)
18693 {
18694 rettv->v_type = VAR_STRING;
18695 rettv->vval.v_string = reg_submatch(no);
18696 }
18697 else
18698 {
18699 rettv->v_type = VAR_LIST;
18700 rettv->vval.v_list = reg_submatch_list(no);
18701 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018702}
18703
18704/*
18705 * "substitute()" function
18706 */
18707 static void
18708f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018709 typval_T *argvars;
18710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711{
18712 char_u patbuf[NUMBUFLEN];
18713 char_u subbuf[NUMBUFLEN];
18714 char_u flagsbuf[NUMBUFLEN];
18715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018716 char_u *str = get_tv_string_chk(&argvars[0]);
18717 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18718 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18719 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18720
Bram Moolenaar0d660222005-01-07 21:51:51 +000018721 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018722 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18723 rettv->vval.v_string = NULL;
18724 else
18725 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018726}
18727
18728/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018729 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735{
18736 int id = 0;
18737#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018738 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 long col;
18740 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018741 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018743 lnum = get_tv_lnum(argvars); /* -1 on type error */
18744 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18745 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018747 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018748 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018749 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750#endif
18751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753}
18754
18755/*
18756 * "synIDattr(id, what [, mode])" function
18757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018760 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762{
18763 char_u *p = NULL;
18764#ifdef FEAT_SYN_HL
18765 int id;
18766 char_u *what;
18767 char_u *mode;
18768 char_u modebuf[NUMBUFLEN];
18769 int modec;
18770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 id = get_tv_number(&argvars[0]);
18772 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018773 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018777 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 modec = 0; /* replace invalid with current */
18779 }
18780 else
18781 {
18782#ifdef FEAT_GUI
18783 if (gui.in_use)
18784 modec = 'g';
18785 else
18786#endif
18787 if (t_colors > 1)
18788 modec = 'c';
18789 else
18790 modec = 't';
18791 }
18792
18793
18794 switch (TOLOWER_ASC(what[0]))
18795 {
18796 case 'b':
18797 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18798 p = highlight_color(id, what, modec);
18799 else /* bold */
18800 p = highlight_has_attr(id, HL_BOLD, modec);
18801 break;
18802
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018803 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 p = highlight_color(id, what, modec);
18805 break;
18806
18807 case 'i':
18808 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18809 p = highlight_has_attr(id, HL_INVERSE, modec);
18810 else /* italic */
18811 p = highlight_has_attr(id, HL_ITALIC, modec);
18812 break;
18813
18814 case 'n': /* name */
18815 p = get_highlight_name(NULL, id - 1);
18816 break;
18817
18818 case 'r': /* reverse */
18819 p = highlight_has_attr(id, HL_INVERSE, modec);
18820 break;
18821
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018822 case 's':
18823 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18824 p = highlight_color(id, what, modec);
18825 else /* standout */
18826 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 break;
18828
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018829 case 'u':
18830 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18831 /* underline */
18832 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18833 else
18834 /* undercurl */
18835 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 break;
18837 }
18838
18839 if (p != NULL)
18840 p = vim_strsave(p);
18841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->v_type = VAR_STRING;
18843 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844}
18845
18846/*
18847 * "synIDtrans(id)" function
18848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018850f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853{
18854 int id;
18855
18856#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018857 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858
18859 if (id > 0)
18860 id = syn_get_final_id(id);
18861 else
18862#endif
18863 id = 0;
18864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018865 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866}
18867
18868/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018869 * "synconcealed(lnum, col)" function
18870 */
18871 static void
18872f_synconcealed(argvars, rettv)
18873 typval_T *argvars UNUSED;
18874 typval_T *rettv;
18875{
18876#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18877 long lnum;
18878 long col;
18879 int syntax_flags = 0;
18880 int cchar;
18881 int matchid = 0;
18882 char_u str[NUMBUFLEN];
18883#endif
18884
18885 rettv->v_type = VAR_LIST;
18886 rettv->vval.v_list = NULL;
18887
18888#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18889 lnum = get_tv_lnum(argvars); /* -1 on type error */
18890 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18891
18892 vim_memset(str, NUL, sizeof(str));
18893
18894 if (rettv_list_alloc(rettv) != FAIL)
18895 {
18896 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18897 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18898 && curwin->w_p_cole > 0)
18899 {
18900 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18901 syntax_flags = get_syntax_info(&matchid);
18902
18903 /* get the conceal character */
18904 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18905 {
18906 cchar = syn_get_sub_char();
18907 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18908 cchar = lcs_conceal;
18909 if (cchar != NUL)
18910 {
18911# ifdef FEAT_MBYTE
18912 if (has_mbyte)
18913 (*mb_char2bytes)(cchar, str);
18914 else
18915# endif
18916 str[0] = cchar;
18917 }
18918 }
18919 }
18920
18921 list_append_number(rettv->vval.v_list,
18922 (syntax_flags & HL_CONCEAL) != 0);
18923 /* -1 to auto-determine strlen */
18924 list_append_string(rettv->vval.v_list, str, -1);
18925 list_append_number(rettv->vval.v_list, matchid);
18926 }
18927#endif
18928}
18929
18930/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018931 * "synstack(lnum, col)" function
18932 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018933 static void
18934f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018935 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018936 typval_T *rettv;
18937{
18938#ifdef FEAT_SYN_HL
18939 long lnum;
18940 long col;
18941 int i;
18942 int id;
18943#endif
18944
18945 rettv->v_type = VAR_LIST;
18946 rettv->vval.v_list = NULL;
18947
18948#ifdef FEAT_SYN_HL
18949 lnum = get_tv_lnum(argvars); /* -1 on type error */
18950 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18951
18952 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018953 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018954 && rettv_list_alloc(rettv) != FAIL)
18955 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018956 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018957 for (i = 0; ; ++i)
18958 {
18959 id = syn_get_stack_item(i);
18960 if (id < 0)
18961 break;
18962 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18963 break;
18964 }
18965 }
18966#endif
18967}
18968
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018970get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 typval_T *argvars;
18972 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018973 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018975 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018977 char_u *infile = NULL;
18978 char_u buf[NUMBUFLEN];
18979 int err = FALSE;
18980 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018981 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018982 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018984 rettv->v_type = VAR_STRING;
18985 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018986 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018987 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018988
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018989 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018990 {
18991 /*
18992 * Write the string to a temp file, to be used for input of the shell
18993 * command.
18994 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018995 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018996 {
18997 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018998 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018999 }
19000
19001 fd = mch_fopen((char *)infile, WRITEBIN);
19002 if (fd == NULL)
19003 {
19004 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019005 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019006 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019007 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019008 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019009 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19010 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019011 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019012 else
19013 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019014 size_t len;
19015
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019016 p = get_tv_string_buf_chk(&argvars[1], buf);
19017 if (p == NULL)
19018 {
19019 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019020 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019021 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019022 len = STRLEN(p);
19023 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019024 err = TRUE;
19025 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019026 if (fclose(fd) != 0)
19027 err = TRUE;
19028 if (err)
19029 {
19030 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019031 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019032 }
19033 }
19034
Bram Moolenaar52a72462014-08-29 15:53:52 +020019035 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19036 * echoes typeahead, that messes up the display. */
19037 if (!msg_silent)
19038 flags += SHELL_COOKED;
19039
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019040 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019042 int len;
19043 listitem_T *li;
19044 char_u *s = NULL;
19045 char_u *start;
19046 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019047 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048
Bram Moolenaar52a72462014-08-29 15:53:52 +020019049 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019050 if (res == NULL)
19051 goto errret;
19052
19053 list = list_alloc();
19054 if (list == NULL)
19055 goto errret;
19056
19057 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019060 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019061 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019062 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019063
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019064 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019065 if (s == NULL)
19066 goto errret;
19067
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019068 for (p = s; start < end; ++p, ++start)
19069 *p = *start == NUL ? NL : *start;
19070 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019071
19072 li = listitem_alloc();
19073 if (li == NULL)
19074 {
19075 vim_free(s);
19076 goto errret;
19077 }
19078 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019079 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019080 li->li_tv.vval.v_string = s;
19081 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019083
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019084 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019085 rettv->v_type = VAR_LIST;
19086 rettv->vval.v_list = list;
19087 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019089 else
19090 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019091 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019092#ifdef USE_CR
19093 /* translate <CR> into <NL> */
19094 if (res != NULL)
19095 {
19096 char_u *s;
19097
19098 for (s = res; *s; ++s)
19099 {
19100 if (*s == CAR)
19101 *s = NL;
19102 }
19103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104#else
19105# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019106 /* translate <CR><NL> into <NL> */
19107 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019109 char_u *s, *d;
19110
19111 d = res;
19112 for (s = res; *s; ++s)
19113 {
19114 if (s[0] == CAR && s[1] == NL)
19115 ++s;
19116 *d++ = *s;
19117 }
19118 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120# endif
19121#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019122 rettv->vval.v_string = res;
19123 res = NULL;
19124 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019125
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019126errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019127 if (infile != NULL)
19128 {
19129 mch_remove(infile);
19130 vim_free(infile);
19131 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019132 if (res != NULL)
19133 vim_free(res);
19134 if (list != NULL)
19135 list_free(list, TRUE);
19136}
19137
19138/*
19139 * "system()" function
19140 */
19141 static void
19142f_system(argvars, rettv)
19143 typval_T *argvars;
19144 typval_T *rettv;
19145{
19146 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19147}
19148
19149/*
19150 * "systemlist()" function
19151 */
19152 static void
19153f_systemlist(argvars, rettv)
19154 typval_T *argvars;
19155 typval_T *rettv;
19156{
19157 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158}
19159
19160/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019161 * "tabpagebuflist()" function
19162 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019163 static void
19164f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019165 typval_T *argvars UNUSED;
19166 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019167{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019168#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019169 tabpage_T *tp;
19170 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019171
19172 if (argvars[0].v_type == VAR_UNKNOWN)
19173 wp = firstwin;
19174 else
19175 {
19176 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19177 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019178 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019179 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019180 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019181 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019182 for (; wp != NULL; wp = wp->w_next)
19183 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019184 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019185 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019186 }
19187#endif
19188}
19189
19190
19191/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019192 * "tabpagenr()" function
19193 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019194 static void
19195f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019196 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019197 typval_T *rettv;
19198{
19199 int nr = 1;
19200#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019201 char_u *arg;
19202
19203 if (argvars[0].v_type != VAR_UNKNOWN)
19204 {
19205 arg = get_tv_string_chk(&argvars[0]);
19206 nr = 0;
19207 if (arg != NULL)
19208 {
19209 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019210 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019211 else
19212 EMSG2(_(e_invexpr2), arg);
19213 }
19214 }
19215 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019216 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019217#endif
19218 rettv->vval.v_number = nr;
19219}
19220
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019221
19222#ifdef FEAT_WINDOWS
19223static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19224
19225/*
19226 * Common code for tabpagewinnr() and winnr().
19227 */
19228 static int
19229get_winnr(tp, argvar)
19230 tabpage_T *tp;
19231 typval_T *argvar;
19232{
19233 win_T *twin;
19234 int nr = 1;
19235 win_T *wp;
19236 char_u *arg;
19237
19238 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19239 if (argvar->v_type != VAR_UNKNOWN)
19240 {
19241 arg = get_tv_string_chk(argvar);
19242 if (arg == NULL)
19243 nr = 0; /* type error; errmsg already given */
19244 else if (STRCMP(arg, "$") == 0)
19245 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19246 else if (STRCMP(arg, "#") == 0)
19247 {
19248 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19249 if (twin == NULL)
19250 nr = 0;
19251 }
19252 else
19253 {
19254 EMSG2(_(e_invexpr2), arg);
19255 nr = 0;
19256 }
19257 }
19258
19259 if (nr > 0)
19260 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19261 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019262 {
19263 if (wp == NULL)
19264 {
19265 /* didn't find it in this tabpage */
19266 nr = 0;
19267 break;
19268 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019269 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019270 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019271 return nr;
19272}
19273#endif
19274
19275/*
19276 * "tabpagewinnr()" function
19277 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019278 static void
19279f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019280 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019281 typval_T *rettv;
19282{
19283 int nr = 1;
19284#ifdef FEAT_WINDOWS
19285 tabpage_T *tp;
19286
19287 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19288 if (tp == NULL)
19289 nr = 0;
19290 else
19291 nr = get_winnr(tp, &argvars[1]);
19292#endif
19293 rettv->vval.v_number = nr;
19294}
19295
19296
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019297/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019298 * "tagfiles()" function
19299 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019300 static void
19301f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019302 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019303 typval_T *rettv;
19304{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019305 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019306 tagname_T tn;
19307 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019308
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019309 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019310 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019311 fname = alloc(MAXPATHL);
19312 if (fname == NULL)
19313 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019314
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019315 for (first = TRUE; ; first = FALSE)
19316 if (get_tagfname(&tn, first, fname) == FAIL
19317 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019318 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019319 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019320 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019321}
19322
19323/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019324 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019325 */
19326 static void
19327f_taglist(argvars, rettv)
19328 typval_T *argvars;
19329 typval_T *rettv;
19330{
19331 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019332
19333 tag_pattern = get_tv_string(&argvars[0]);
19334
19335 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019336 if (*tag_pattern == NUL)
19337 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019338
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019339 if (rettv_list_alloc(rettv) == OK)
19340 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019341}
19342
19343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 * "tempname()" function
19345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350{
19351 static int x = 'A';
19352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019353 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019354 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355
19356 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19357 * names. Skip 'I' and 'O', they are used for shell redirection. */
19358 do
19359 {
19360 if (x == 'Z')
19361 x = '0';
19362 else if (x == '9')
19363 x = 'A';
19364 else
19365 {
19366#ifdef EBCDIC
19367 if (x == 'I')
19368 x = 'J';
19369 else if (x == 'R')
19370 x = 'S';
19371 else
19372#endif
19373 ++x;
19374 }
19375 } while (x == 'I' || x == 'O');
19376}
19377
19378/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019379 * "test(list)" function: Just checking the walls...
19380 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019381 static void
19382f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019383 typval_T *argvars UNUSED;
19384 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019385{
19386 /* Used for unit testing. Change the code below to your liking. */
19387#if 0
19388 listitem_T *li;
19389 list_T *l;
19390 char_u *bad, *good;
19391
19392 if (argvars[0].v_type != VAR_LIST)
19393 return;
19394 l = argvars[0].vval.v_list;
19395 if (l == NULL)
19396 return;
19397 li = l->lv_first;
19398 if (li == NULL)
19399 return;
19400 bad = get_tv_string(&li->li_tv);
19401 li = li->li_next;
19402 if (li == NULL)
19403 return;
19404 good = get_tv_string(&li->li_tv);
19405 rettv->vval.v_number = test_edit_score(bad, good);
19406#endif
19407}
19408
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019409#ifdef FEAT_FLOAT
19410/*
19411 * "tan()" function
19412 */
19413 static void
19414f_tan(argvars, rettv)
19415 typval_T *argvars;
19416 typval_T *rettv;
19417{
19418 float_T f;
19419
19420 rettv->v_type = VAR_FLOAT;
19421 if (get_float_arg(argvars, &f) == OK)
19422 rettv->vval.v_float = tan(f);
19423 else
19424 rettv->vval.v_float = 0.0;
19425}
19426
19427/*
19428 * "tanh()" function
19429 */
19430 static void
19431f_tanh(argvars, rettv)
19432 typval_T *argvars;
19433 typval_T *rettv;
19434{
19435 float_T f;
19436
19437 rettv->v_type = VAR_FLOAT;
19438 if (get_float_arg(argvars, &f) == OK)
19439 rettv->vval.v_float = tanh(f);
19440 else
19441 rettv->vval.v_float = 0.0;
19442}
19443#endif
19444
Bram Moolenaard52d9742005-08-21 22:20:28 +000019445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 * "tolower(string)" function
19447 */
19448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019449f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019450 typval_T *argvars;
19451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452{
19453 char_u *p;
19454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019455 p = vim_strsave(get_tv_string(&argvars[0]));
19456 rettv->v_type = VAR_STRING;
19457 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458
19459 if (p != NULL)
19460 while (*p != NUL)
19461 {
19462#ifdef FEAT_MBYTE
19463 int l;
19464
19465 if (enc_utf8)
19466 {
19467 int c, lc;
19468
19469 c = utf_ptr2char(p);
19470 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019471 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 /* TODO: reallocate string when byte count changes. */
19473 if (utf_char2len(lc) == l)
19474 utf_char2bytes(lc, p);
19475 p += l;
19476 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019477 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 p += l; /* skip multi-byte character */
19479 else
19480#endif
19481 {
19482 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19483 ++p;
19484 }
19485 }
19486}
19487
19488/*
19489 * "toupper(string)" function
19490 */
19491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019492f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019493 typval_T *argvars;
19494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019496 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019497 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498}
19499
19500/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019501 * "tr(string, fromstr, tostr)" function
19502 */
19503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019505 typval_T *argvars;
19506 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019507{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019508 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019509 char_u *fromstr;
19510 char_u *tostr;
19511 char_u *p;
19512#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019513 int inlen;
19514 int fromlen;
19515 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019516 int idx;
19517 char_u *cpstr;
19518 int cplen;
19519 int first = TRUE;
19520#endif
19521 char_u buf[NUMBUFLEN];
19522 char_u buf2[NUMBUFLEN];
19523 garray_T ga;
19524
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019525 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019526 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19527 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019528
19529 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019530 rettv->v_type = VAR_STRING;
19531 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019532 if (fromstr == NULL || tostr == NULL)
19533 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019534 ga_init2(&ga, (int)sizeof(char), 80);
19535
19536#ifdef FEAT_MBYTE
19537 if (!has_mbyte)
19538#endif
19539 /* not multi-byte: fromstr and tostr must be the same length */
19540 if (STRLEN(fromstr) != STRLEN(tostr))
19541 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019542#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019543error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019544#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019545 EMSG2(_(e_invarg2), fromstr);
19546 ga_clear(&ga);
19547 return;
19548 }
19549
19550 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019551 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019552 {
19553#ifdef FEAT_MBYTE
19554 if (has_mbyte)
19555 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019556 inlen = (*mb_ptr2len)(in_str);
19557 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019558 cplen = inlen;
19559 idx = 0;
19560 for (p = fromstr; *p != NUL; p += fromlen)
19561 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019562 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019563 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019564 {
19565 for (p = tostr; *p != NUL; p += tolen)
19566 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019567 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019568 if (idx-- == 0)
19569 {
19570 cplen = tolen;
19571 cpstr = p;
19572 break;
19573 }
19574 }
19575 if (*p == NUL) /* tostr is shorter than fromstr */
19576 goto error;
19577 break;
19578 }
19579 ++idx;
19580 }
19581
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019582 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019583 {
19584 /* Check that fromstr and tostr have the same number of
19585 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019586 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019587 first = FALSE;
19588 for (p = tostr; *p != NUL; p += tolen)
19589 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019590 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019591 --idx;
19592 }
19593 if (idx != 0)
19594 goto error;
19595 }
19596
Bram Moolenaarcde88542015-08-11 19:14:00 +020019597 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019598 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019599 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019600
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019601 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019602 }
19603 else
19604#endif
19605 {
19606 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019607 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019608 if (p != NULL)
19609 ga_append(&ga, tostr[p - fromstr]);
19610 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019611 ga_append(&ga, *in_str);
19612 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019613 }
19614 }
19615
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019616 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019617 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019618 ga_append(&ga, NUL);
19619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019621}
19622
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019623#ifdef FEAT_FLOAT
19624/*
19625 * "trunc({float})" function
19626 */
19627 static void
19628f_trunc(argvars, rettv)
19629 typval_T *argvars;
19630 typval_T *rettv;
19631{
19632 float_T f;
19633
19634 rettv->v_type = VAR_FLOAT;
19635 if (get_float_arg(argvars, &f) == OK)
19636 /* trunc() is not in C90, use floor() or ceil() instead. */
19637 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19638 else
19639 rettv->vval.v_float = 0.0;
19640}
19641#endif
19642
Bram Moolenaar8299df92004-07-10 09:47:34 +000019643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 * "type(expr)" function
19645 */
19646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 typval_T *argvars;
19649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019651 int n;
19652
19653 switch (argvars[0].v_type)
19654 {
19655 case VAR_NUMBER: n = 0; break;
19656 case VAR_STRING: n = 1; break;
19657 case VAR_FUNC: n = 2; break;
19658 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019659 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019660#ifdef FEAT_FLOAT
19661 case VAR_FLOAT: n = 5; break;
19662#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019663 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19664 }
19665 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666}
19667
19668/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019669 * "undofile(name)" function
19670 */
19671 static void
19672f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019673 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019674 typval_T *rettv;
19675{
19676 rettv->v_type = VAR_STRING;
19677#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019678 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019679 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019680
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019681 if (*fname == NUL)
19682 {
19683 /* If there is no file name there will be no undo file. */
19684 rettv->vval.v_string = NULL;
19685 }
19686 else
19687 {
19688 char_u *ffname = FullName_save(fname, FALSE);
19689
19690 if (ffname != NULL)
19691 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19692 vim_free(ffname);
19693 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019694 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019695#else
19696 rettv->vval.v_string = NULL;
19697#endif
19698}
19699
19700/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019701 * "undotree()" function
19702 */
19703 static void
19704f_undotree(argvars, rettv)
19705 typval_T *argvars UNUSED;
19706 typval_T *rettv;
19707{
19708 if (rettv_dict_alloc(rettv) == OK)
19709 {
19710 dict_T *dict = rettv->vval.v_dict;
19711 list_T *list;
19712
Bram Moolenaar730cde92010-06-27 05:18:54 +020019713 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019714 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019715 dict_add_nr_str(dict, "save_last",
19716 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019717 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19718 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019719 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019720
19721 list = list_alloc();
19722 if (list != NULL)
19723 {
19724 u_eval_tree(curbuf->b_u_oldhead, list);
19725 dict_add_list(dict, "entries", list);
19726 }
19727 }
19728}
19729
19730/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019731 * "values(dict)" function
19732 */
19733 static void
19734f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 typval_T *argvars;
19736 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019737{
19738 dict_list(argvars, rettv, 1);
19739}
19740
19741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 * "virtcol(string)" function
19743 */
19744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 typval_T *argvars;
19747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748{
19749 colnr_T vcol = 0;
19750 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019751 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019753 fp = var2fpos(&argvars[0], FALSE, &fnum);
19754 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19755 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 {
19757 getvvcol(curwin, fp, NULL, NULL, &vcol);
19758 ++vcol;
19759 }
19760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762}
19763
19764/*
19765 * "visualmode()" function
19766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019769 typval_T *argvars UNUSED;
19770 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 char_u str[2];
19773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 str[0] = curbuf->b_visual_mode_eval;
19776 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778
19779 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019780 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782}
19783
19784/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019785 * "wildmenumode()" function
19786 */
19787 static void
19788f_wildmenumode(argvars, rettv)
19789 typval_T *argvars UNUSED;
19790 typval_T *rettv UNUSED;
19791{
19792#ifdef FEAT_WILDMENU
19793 if (wild_menu_showing)
19794 rettv->vval.v_number = 1;
19795#endif
19796}
19797
19798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 * "winbufnr(nr)" function
19800 */
19801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019802f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019803 typval_T *argvars;
19804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805{
19806 win_T *wp;
19807
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019808 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813}
19814
19815/*
19816 * "wincol()" function
19817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822{
19823 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825}
19826
19827/*
19828 * "winheight(nr)" function
19829 */
19830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 typval_T *argvars;
19833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834{
19835 win_T *wp;
19836
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019837 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842}
19843
19844/*
19845 * "winline()" function
19846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851{
19852 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854}
19855
19856/*
19857 * "winnr()" function
19858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019860f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019861 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863{
19864 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019865
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019867 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870}
19871
19872/*
19873 * "winrestcmd()" function
19874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019876f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019877 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879{
19880#ifdef FEAT_WINDOWS
19881 win_T *wp;
19882 int winnr = 1;
19883 garray_T ga;
19884 char_u buf[50];
19885
19886 ga_init2(&ga, (int)sizeof(char), 70);
19887 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19888 {
19889 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19890 ga_concat(&ga, buf);
19891# ifdef FEAT_VERTSPLIT
19892 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19893 ga_concat(&ga, buf);
19894# endif
19895 ++winnr;
19896 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019897 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904}
19905
19906/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019907 * "winrestview()" function
19908 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909 static void
19910f_winrestview(argvars, rettv)
19911 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019912 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019913{
19914 dict_T *dict;
19915
19916 if (argvars[0].v_type != VAR_DICT
19917 || (dict = argvars[0].vval.v_dict) == NULL)
19918 EMSG(_(e_invarg));
19919 else
19920 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019921 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19922 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19923 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19924 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019925#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019926 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19927 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019928#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019929 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19930 {
19931 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19932 curwin->w_set_curswant = FALSE;
19933 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019934
Bram Moolenaar82c25852014-05-28 16:47:16 +020019935 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19936 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019937#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019938 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19939 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019940#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019941 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19942 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19943 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19944 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019945
19946 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019947 win_new_height(curwin, curwin->w_height);
19948# ifdef FEAT_VERTSPLIT
19949 win_new_width(curwin, W_WIDTH(curwin));
19950# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019951 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019952
Bram Moolenaarb851a962014-10-31 15:45:52 +010019953 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019954 curwin->w_topline = 1;
19955 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19956 curwin->w_topline = curbuf->b_ml.ml_line_count;
19957#ifdef FEAT_DIFF
19958 check_topfill(curwin, TRUE);
19959#endif
19960 }
19961}
19962
19963/*
19964 * "winsaveview()" function
19965 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019966 static void
19967f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019968 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019969 typval_T *rettv;
19970{
19971 dict_T *dict;
19972
Bram Moolenaara800b422010-06-27 01:15:55 +020019973 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019974 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019975 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019976
19977 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19978 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19979#ifdef FEAT_VIRTUALEDIT
19980 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19981#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019982 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019983 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19984
19985 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19986#ifdef FEAT_DIFF
19987 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19988#endif
19989 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19990 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19991}
19992
19993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 * "winwidth(nr)" function
19995 */
19996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019998 typval_T *argvars;
19999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000{
20001 win_T *wp;
20002
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020003 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 else
20007#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011#endif
20012}
20013
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020015 * Write list of strings to file
20016 */
20017 static int
20018write_list(fd, list, binary)
20019 FILE *fd;
20020 list_T *list;
20021 int binary;
20022{
20023 listitem_T *li;
20024 int c;
20025 int ret = OK;
20026 char_u *s;
20027
20028 for (li = list->lv_first; li != NULL; li = li->li_next)
20029 {
20030 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20031 {
20032 if (*s == '\n')
20033 c = putc(NUL, fd);
20034 else
20035 c = putc(*s, fd);
20036 if (c == EOF)
20037 {
20038 ret = FAIL;
20039 break;
20040 }
20041 }
20042 if (!binary || li->li_next != NULL)
20043 if (putc('\n', fd) == EOF)
20044 {
20045 ret = FAIL;
20046 break;
20047 }
20048 if (ret == FAIL)
20049 {
20050 EMSG(_(e_write));
20051 break;
20052 }
20053 }
20054 return ret;
20055}
20056
20057/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020058 * "writefile()" function
20059 */
20060 static void
20061f_writefile(argvars, rettv)
20062 typval_T *argvars;
20063 typval_T *rettv;
20064{
20065 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020066 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020067 char_u *fname;
20068 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020069 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020070
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020071 if (check_restricted() || check_secure())
20072 return;
20073
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020074 if (argvars[0].v_type != VAR_LIST)
20075 {
20076 EMSG2(_(e_listarg), "writefile()");
20077 return;
20078 }
20079 if (argvars[0].vval.v_list == NULL)
20080 return;
20081
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020082 if (argvars[2].v_type != VAR_UNKNOWN)
20083 {
20084 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20085 binary = TRUE;
20086 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20087 append = TRUE;
20088 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020089
20090 /* Always open the file in binary mode, library functions have a mind of
20091 * their own about CR-LF conversion. */
20092 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020093 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20094 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020095 {
20096 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20097 ret = -1;
20098 }
20099 else
20100 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020101 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20102 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020103 fclose(fd);
20104 }
20105
20106 rettv->vval.v_number = ret;
20107}
20108
20109/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020110 * "xor(expr, expr)" function
20111 */
20112 static void
20113f_xor(argvars, rettv)
20114 typval_T *argvars;
20115 typval_T *rettv;
20116{
20117 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20118 ^ get_tv_number_chk(&argvars[1], NULL);
20119}
20120
20121
20122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020124 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 */
20126 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020127var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020129 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020130 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020132 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020134 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135
Bram Moolenaara5525202006-03-02 22:52:09 +000020136 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020137 if (varp->v_type == VAR_LIST)
20138 {
20139 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020140 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020141 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020142 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020143
20144 l = varp->vval.v_list;
20145 if (l == NULL)
20146 return NULL;
20147
20148 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020149 pos.lnum = list_find_nr(l, 0L, &error);
20150 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020151 return NULL; /* invalid line number */
20152
20153 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020154 pos.col = list_find_nr(l, 1L, &error);
20155 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020156 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020157 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020158
20159 /* We accept "$" for the column number: last column. */
20160 li = list_find(l, 1L);
20161 if (li != NULL && li->li_tv.v_type == VAR_STRING
20162 && li->li_tv.vval.v_string != NULL
20163 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20164 pos.col = len + 1;
20165
Bram Moolenaara5525202006-03-02 22:52:09 +000020166 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020167 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020168 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020169 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020170
Bram Moolenaara5525202006-03-02 22:52:09 +000020171#ifdef FEAT_VIRTUALEDIT
20172 /* Get the virtual offset. Defaults to zero. */
20173 pos.coladd = list_find_nr(l, 2L, &error);
20174 if (error)
20175 pos.coladd = 0;
20176#endif
20177
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020178 return &pos;
20179 }
20180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020181 name = get_tv_string_chk(varp);
20182 if (name == NULL)
20183 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020184 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020186 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20187 {
20188 if (VIsual_active)
20189 return &VIsual;
20190 return &curwin->w_cursor;
20191 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020192 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020194 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20196 return NULL;
20197 return pp;
20198 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020199
20200#ifdef FEAT_VIRTUALEDIT
20201 pos.coladd = 0;
20202#endif
20203
Bram Moolenaar477933c2007-07-17 14:32:23 +000020204 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020205 {
20206 pos.col = 0;
20207 if (name[1] == '0') /* "w0": first visible line */
20208 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020209 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020210 pos.lnum = curwin->w_topline;
20211 return &pos;
20212 }
20213 else if (name[1] == '$') /* "w$": last visible line */
20214 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020215 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020216 pos.lnum = curwin->w_botline - 1;
20217 return &pos;
20218 }
20219 }
20220 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020222 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 {
20224 pos.lnum = curbuf->b_ml.ml_line_count;
20225 pos.col = 0;
20226 }
20227 else
20228 {
20229 pos.lnum = curwin->w_cursor.lnum;
20230 pos.col = (colnr_T)STRLEN(ml_get_curline());
20231 }
20232 return &pos;
20233 }
20234 return NULL;
20235}
20236
20237/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020238 * Convert list in "arg" into a position and optional file number.
20239 * When "fnump" is NULL there is no file number, only 3 items.
20240 * Note that the column is passed on as-is, the caller may want to decrement
20241 * it to use 1 for the first column.
20242 * Return FAIL when conversion is not possible, doesn't check the position for
20243 * validity.
20244 */
20245 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020246list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020247 typval_T *arg;
20248 pos_T *posp;
20249 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020250 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020251{
20252 list_T *l = arg->vval.v_list;
20253 long i = 0;
20254 long n;
20255
Bram Moolenaar493c1782014-05-28 14:34:46 +020020256 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20257 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020258 if (arg->v_type != VAR_LIST
20259 || l == NULL
20260 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020261 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020262 return FAIL;
20263
20264 if (fnump != NULL)
20265 {
20266 n = list_find_nr(l, i++, NULL); /* fnum */
20267 if (n < 0)
20268 return FAIL;
20269 if (n == 0)
20270 n = curbuf->b_fnum; /* current buffer */
20271 *fnump = n;
20272 }
20273
20274 n = list_find_nr(l, i++, NULL); /* lnum */
20275 if (n < 0)
20276 return FAIL;
20277 posp->lnum = n;
20278
20279 n = list_find_nr(l, i++, NULL); /* col */
20280 if (n < 0)
20281 return FAIL;
20282 posp->col = n;
20283
20284#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020285 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020286 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020287 posp->coladd = 0;
20288 else
20289 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020290#endif
20291
Bram Moolenaar493c1782014-05-28 14:34:46 +020020292 if (curswantp != NULL)
20293 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20294
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020295 return OK;
20296}
20297
20298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 * Get the length of an environment variable name.
20300 * Advance "arg" to the first character after the name.
20301 * Return 0 for error.
20302 */
20303 static int
20304get_env_len(arg)
20305 char_u **arg;
20306{
20307 char_u *p;
20308 int len;
20309
20310 for (p = *arg; vim_isIDc(*p); ++p)
20311 ;
20312 if (p == *arg) /* no name found */
20313 return 0;
20314
20315 len = (int)(p - *arg);
20316 *arg = p;
20317 return len;
20318}
20319
20320/*
20321 * Get the length of the name of a function or internal variable.
20322 * "arg" is advanced to the first non-white character after the name.
20323 * Return 0 if something is wrong.
20324 */
20325 static int
20326get_id_len(arg)
20327 char_u **arg;
20328{
20329 char_u *p;
20330 int len;
20331
20332 /* Find the end of the name. */
20333 for (p = *arg; eval_isnamec(*p); ++p)
20334 ;
20335 if (p == *arg) /* no name found */
20336 return 0;
20337
20338 len = (int)(p - *arg);
20339 *arg = skipwhite(p);
20340
20341 return len;
20342}
20343
20344/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020345 * Get the length of the name of a variable or function.
20346 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020348 * Return -1 if curly braces expansion failed.
20349 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 * If the name contains 'magic' {}'s, expand them and return the
20351 * expanded name in an allocated string via 'alias' - caller must free.
20352 */
20353 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020354get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 char_u **arg;
20356 char_u **alias;
20357 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020358 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359{
20360 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 char_u *p;
20362 char_u *expr_start;
20363 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364
20365 *alias = NULL; /* default to no alias */
20366
20367 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20368 && (*arg)[2] == (int)KE_SNR)
20369 {
20370 /* hard coded <SNR>, already translated */
20371 *arg += 3;
20372 return get_id_len(arg) + 3;
20373 }
20374 len = eval_fname_script(*arg);
20375 if (len > 0)
20376 {
20377 /* literal "<SID>", "s:" or "<SNR>" */
20378 *arg += len;
20379 }
20380
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020384 p = find_name_end(*arg, &expr_start, &expr_end,
20385 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 if (expr_start != NULL)
20387 {
20388 char_u *temp_string;
20389
20390 if (!evaluate)
20391 {
20392 len += (int)(p - *arg);
20393 *arg = skipwhite(p);
20394 return len;
20395 }
20396
20397 /*
20398 * Include any <SID> etc in the expanded string:
20399 * Thus the -len here.
20400 */
20401 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20402 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020403 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 *alias = temp_string;
20405 *arg = skipwhite(p);
20406 return (int)STRLEN(temp_string);
20407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408
20409 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020410 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 EMSG2(_(e_invexpr2), *arg);
20412
20413 return len;
20414}
20415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020416/*
20417 * Find the end of a variable or function name, taking care of magic braces.
20418 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20419 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020420 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020421 * Return a pointer to just after the name. Equal to "arg" if there is no
20422 * valid name.
20423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020425find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 char_u *arg;
20427 char_u **expr_start;
20428 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020429 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 int mb_nest = 0;
20432 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 char_u *p;
20434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020435 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020437 *expr_start = NULL;
20438 *expr_end = NULL;
20439 }
20440
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020441 /* Quick check for valid starting character. */
20442 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20443 return arg;
20444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020445 for (p = arg; *p != NUL
20446 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020447 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020448 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020449 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020450 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020451 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020452 if (*p == '\'')
20453 {
20454 /* skip over 'string' to avoid counting [ and ] inside it. */
20455 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20456 ;
20457 if (*p == NUL)
20458 break;
20459 }
20460 else if (*p == '"')
20461 {
20462 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20463 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20464 if (*p == '\\' && p[1] != NUL)
20465 ++p;
20466 if (*p == NUL)
20467 break;
20468 }
20469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020470 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020472 if (*p == '[')
20473 ++br_nest;
20474 else if (*p == ']')
20475 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020478 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020480 if (*p == '{')
20481 {
20482 mb_nest++;
20483 if (expr_start != NULL && *expr_start == NULL)
20484 *expr_start = p;
20485 }
20486 else if (*p == '}')
20487 {
20488 mb_nest--;
20489 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20490 *expr_end = p;
20491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 }
20494
20495 return p;
20496}
20497
20498/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020499 * Expands out the 'magic' {}'s in a variable/function name.
20500 * Note that this can call itself recursively, to deal with
20501 * constructs like foo{bar}{baz}{bam}
20502 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20503 * "in_start" ^
20504 * "expr_start" ^
20505 * "expr_end" ^
20506 * "in_end" ^
20507 *
20508 * Returns a new allocated string, which the caller must free.
20509 * Returns NULL for failure.
20510 */
20511 static char_u *
20512make_expanded_name(in_start, expr_start, expr_end, in_end)
20513 char_u *in_start;
20514 char_u *expr_start;
20515 char_u *expr_end;
20516 char_u *in_end;
20517{
20518 char_u c1;
20519 char_u *retval = NULL;
20520 char_u *temp_result;
20521 char_u *nextcmd = NULL;
20522
20523 if (expr_end == NULL || in_end == NULL)
20524 return NULL;
20525 *expr_start = NUL;
20526 *expr_end = NUL;
20527 c1 = *in_end;
20528 *in_end = NUL;
20529
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020530 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020531 if (temp_result != NULL && nextcmd == NULL)
20532 {
20533 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20534 + (in_end - expr_end) + 1));
20535 if (retval != NULL)
20536 {
20537 STRCPY(retval, in_start);
20538 STRCAT(retval, temp_result);
20539 STRCAT(retval, expr_end + 1);
20540 }
20541 }
20542 vim_free(temp_result);
20543
20544 *in_end = c1; /* put char back for error messages */
20545 *expr_start = '{';
20546 *expr_end = '}';
20547
20548 if (retval != NULL)
20549 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020550 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020551 if (expr_start != NULL)
20552 {
20553 /* Further expansion! */
20554 temp_result = make_expanded_name(retval, expr_start,
20555 expr_end, temp_result);
20556 vim_free(retval);
20557 retval = temp_result;
20558 }
20559 }
20560
20561 return retval;
20562}
20563
20564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020566 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 */
20568 static int
20569eval_isnamec(c)
20570 int c;
20571{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020572 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20573}
20574
20575/*
20576 * Return TRUE if character "c" can be used as the first character in a
20577 * variable or function name (excluding '{' and '}').
20578 */
20579 static int
20580eval_isnamec1(c)
20581 int c;
20582{
20583 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584}
20585
20586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 * Set number v: variable to "val".
20588 */
20589 void
20590set_vim_var_nr(idx, val)
20591 int idx;
20592 long val;
20593{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020594 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595}
20596
20597/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020598 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 */
20600 long
20601get_vim_var_nr(idx)
20602 int idx;
20603{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020604 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605}
20606
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020607/*
20608 * Get string v: variable value. Uses a static buffer, can only be used once.
20609 */
20610 char_u *
20611get_vim_var_str(idx)
20612 int idx;
20613{
20614 return get_tv_string(&vimvars[idx].vv_tv);
20615}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020616
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020618 * Get List v: variable value. Caller must take care of reference count when
20619 * needed.
20620 */
20621 list_T *
20622get_vim_var_list(idx)
20623 int idx;
20624{
20625 return vimvars[idx].vv_list;
20626}
20627
20628/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020629 * Set v:char to character "c".
20630 */
20631 void
20632set_vim_var_char(c)
20633 int c;
20634{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020635 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020636
20637#ifdef FEAT_MBYTE
20638 if (has_mbyte)
20639 buf[(*mb_char2bytes)(c, buf)] = NUL;
20640 else
20641#endif
20642 {
20643 buf[0] = c;
20644 buf[1] = NUL;
20645 }
20646 set_vim_var_string(VV_CHAR, buf, -1);
20647}
20648
20649/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020650 * Set v:count to "count" and v:count1 to "count1".
20651 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 */
20653 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020654set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 long count;
20656 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020657 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020659 if (set_prevcount)
20660 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020661 vimvars[VV_COUNT].vv_nr = count;
20662 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663}
20664
20665/*
20666 * Set string v: variable to a copy of "val".
20667 */
20668 void
20669set_vim_var_string(idx, val, len)
20670 int idx;
20671 char_u *val;
20672 int len; /* length of "val" to use or -1 (whole string) */
20673{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020674 /* Need to do this (at least) once, since we can't initialize a union.
20675 * Will always be invoked when "v:progname" is set. */
20676 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20677
Bram Moolenaare9a41262005-01-15 22:18:47 +000020678 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020680 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020682 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685}
20686
20687/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020688 * Set List v: variable to "val".
20689 */
20690 void
20691set_vim_var_list(idx, val)
20692 int idx;
20693 list_T *val;
20694{
20695 list_unref(vimvars[idx].vv_list);
20696 vimvars[idx].vv_list = val;
20697 if (val != NULL)
20698 ++val->lv_refcount;
20699}
20700
20701/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020702 * Set Dictionary v: variable to "val".
20703 */
20704 void
20705set_vim_var_dict(idx, val)
20706 int idx;
20707 dict_T *val;
20708{
20709 int todo;
20710 hashitem_T *hi;
20711
20712 dict_unref(vimvars[idx].vv_dict);
20713 vimvars[idx].vv_dict = val;
20714 if (val != NULL)
20715 {
20716 ++val->dv_refcount;
20717
20718 /* Set readonly */
20719 todo = (int)val->dv_hashtab.ht_used;
20720 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20721 {
20722 if (HASHITEM_EMPTY(hi))
20723 continue;
20724 --todo;
20725 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20726 }
20727 }
20728}
20729
20730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 * Set v:register if needed.
20732 */
20733 void
20734set_reg_var(c)
20735 int c;
20736{
20737 char_u regname;
20738
20739 if (c == 0 || c == ' ')
20740 regname = '"';
20741 else
20742 regname = c;
20743 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020744 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 set_vim_var_string(VV_REG, &regname, 1);
20746}
20747
20748/*
20749 * Get or set v:exception. If "oldval" == NULL, return the current value.
20750 * Otherwise, restore the value to "oldval" and return NULL.
20751 * Must always be called in pairs to save and restore v:exception! Does not
20752 * take care of memory allocations.
20753 */
20754 char_u *
20755v_exception(oldval)
20756 char_u *oldval;
20757{
20758 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020759 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760
Bram Moolenaare9a41262005-01-15 22:18:47 +000020761 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 return NULL;
20763}
20764
20765/*
20766 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20767 * Otherwise, restore the value to "oldval" and return NULL.
20768 * Must always be called in pairs to save and restore v:throwpoint! Does not
20769 * take care of memory allocations.
20770 */
20771 char_u *
20772v_throwpoint(oldval)
20773 char_u *oldval;
20774{
20775 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020776 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777
Bram Moolenaare9a41262005-01-15 22:18:47 +000020778 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 return NULL;
20780}
20781
20782#if defined(FEAT_AUTOCMD) || defined(PROTO)
20783/*
20784 * Set v:cmdarg.
20785 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20786 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20787 * Must always be called in pairs!
20788 */
20789 char_u *
20790set_cmdarg(eap, oldarg)
20791 exarg_T *eap;
20792 char_u *oldarg;
20793{
20794 char_u *oldval;
20795 char_u *newval;
20796 unsigned len;
20797
Bram Moolenaare9a41262005-01-15 22:18:47 +000020798 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020799 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020801 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020802 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020803 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 }
20805
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020806 if (eap->force_bin == FORCE_BIN)
20807 len = 6;
20808 else if (eap->force_bin == FORCE_NOBIN)
20809 len = 8;
20810 else
20811 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020812
20813 if (eap->read_edit)
20814 len += 7;
20815
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020816 if (eap->force_ff != 0)
20817 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20818# ifdef FEAT_MBYTE
20819 if (eap->force_enc != 0)
20820 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020821 if (eap->bad_char != 0)
20822 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020823# endif
20824
20825 newval = alloc(len + 1);
20826 if (newval == NULL)
20827 return NULL;
20828
20829 if (eap->force_bin == FORCE_BIN)
20830 sprintf((char *)newval, " ++bin");
20831 else if (eap->force_bin == FORCE_NOBIN)
20832 sprintf((char *)newval, " ++nobin");
20833 else
20834 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020835
20836 if (eap->read_edit)
20837 STRCAT(newval, " ++edit");
20838
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020839 if (eap->force_ff != 0)
20840 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20841 eap->cmd + eap->force_ff);
20842# ifdef FEAT_MBYTE
20843 if (eap->force_enc != 0)
20844 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20845 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020846 if (eap->bad_char == BAD_KEEP)
20847 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20848 else if (eap->bad_char == BAD_DROP)
20849 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20850 else if (eap->bad_char != 0)
20851 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020852# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020853 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020854 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855}
20856#endif
20857
20858/*
20859 * Get the value of internal variable "name".
20860 * Return OK or FAIL.
20861 */
20862 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020863get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 char_u *name;
20865 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020866 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020867 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020868 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020869 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870{
20871 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020872 typval_T *tv = NULL;
20873 typval_T atv;
20874 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876
20877 /* truncate the name, so that we can use strcmp() */
20878 cc = name[len];
20879 name[len] = NUL;
20880
20881 /*
20882 * Check for "b:changedtick".
20883 */
20884 if (STRCMP(name, "b:changedtick") == 0)
20885 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020886 atv.v_type = VAR_NUMBER;
20887 atv.vval.v_number = curbuf->b_changedtick;
20888 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 }
20890
20891 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 * Check for user-defined variables.
20893 */
20894 else
20895 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020896 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020898 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020900 if (dip != NULL)
20901 *dip = v;
20902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 }
20904
Bram Moolenaare9a41262005-01-15 22:18:47 +000020905 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020907 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 ret = FAIL;
20910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020911 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020912 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913
20914 name[len] = cc;
20915
20916 return ret;
20917}
20918
20919/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020920 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20921 * Also handle function call with Funcref variable: func(expr)
20922 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20923 */
20924 static int
20925handle_subscript(arg, rettv, evaluate, verbose)
20926 char_u **arg;
20927 typval_T *rettv;
20928 int evaluate; /* do more than finding the end */
20929 int verbose; /* give error messages */
20930{
20931 int ret = OK;
20932 dict_T *selfdict = NULL;
20933 char_u *s;
20934 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020935 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020936
20937 while (ret == OK
20938 && (**arg == '['
20939 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020940 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020941 && !vim_iswhite(*(*arg - 1)))
20942 {
20943 if (**arg == '(')
20944 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020945 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020946 if (evaluate)
20947 {
20948 functv = *rettv;
20949 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020950
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020951 /* Invoke the function. Recursive! */
20952 s = functv.vval.v_string;
20953 }
20954 else
20955 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020956 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020957 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20958 &len, evaluate, selfdict);
20959
20960 /* Clear the funcref afterwards, so that deleting it while
20961 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020962 if (evaluate)
20963 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020964
20965 /* Stop the expression evaluation when immediately aborting on
20966 * error, or when an interrupt occurred or an exception was thrown
20967 * but not caught. */
20968 if (aborting())
20969 {
20970 if (ret == OK)
20971 clear_tv(rettv);
20972 ret = FAIL;
20973 }
20974 dict_unref(selfdict);
20975 selfdict = NULL;
20976 }
20977 else /* **arg == '[' || **arg == '.' */
20978 {
20979 dict_unref(selfdict);
20980 if (rettv->v_type == VAR_DICT)
20981 {
20982 selfdict = rettv->vval.v_dict;
20983 if (selfdict != NULL)
20984 ++selfdict->dv_refcount;
20985 }
20986 else
20987 selfdict = NULL;
20988 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20989 {
20990 clear_tv(rettv);
20991 ret = FAIL;
20992 }
20993 }
20994 }
20995 dict_unref(selfdict);
20996 return ret;
20997}
20998
20999/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021000 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021001 * value).
21002 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021003 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021005{
Bram Moolenaar33570922005-01-25 22:26:29 +000021006 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021007}
21008
21009/*
21010 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 * The string "s" must have been allocated, it is consumed.
21012 * Return NULL for out of memory, the variable otherwise.
21013 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021015alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 char_u *s;
21017{
Bram Moolenaar33570922005-01-25 22:26:29 +000021018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021020 rettv = alloc_tv();
21021 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023 rettv->v_type = VAR_STRING;
21024 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 }
21026 else
21027 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029}
21030
21031/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021032 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021034 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021036 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037{
21038 if (varp != NULL)
21039 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021040 switch (varp->v_type)
21041 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021042 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 func_unref(varp->vval.v_string);
21044 /*FALLTHROUGH*/
21045 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021046 vim_free(varp->vval.v_string);
21047 break;
21048 case VAR_LIST:
21049 list_unref(varp->vval.v_list);
21050 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021051 case VAR_DICT:
21052 dict_unref(varp->vval.v_dict);
21053 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021054 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021055#ifdef FEAT_FLOAT
21056 case VAR_FLOAT:
21057#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021058 case VAR_UNKNOWN:
21059 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021060 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021061 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021062 break;
21063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 vim_free(varp);
21065 }
21066}
21067
21068/*
21069 * Free the memory for a variable value and set the value to NULL or 0.
21070 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021071 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021072clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074{
21075 if (varp != NULL)
21076 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021077 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021079 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021080 func_unref(varp->vval.v_string);
21081 /*FALLTHROUGH*/
21082 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021083 vim_free(varp->vval.v_string);
21084 varp->vval.v_string = NULL;
21085 break;
21086 case VAR_LIST:
21087 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021088 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021089 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021090 case VAR_DICT:
21091 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021092 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021093 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021094 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021095 varp->vval.v_number = 0;
21096 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021097#ifdef FEAT_FLOAT
21098 case VAR_FLOAT:
21099 varp->vval.v_float = 0.0;
21100 break;
21101#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021102 case VAR_UNKNOWN:
21103 break;
21104 default:
21105 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021107 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 }
21109}
21110
21111/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021112 * Set the value of a variable to NULL without freeing items.
21113 */
21114 static void
21115init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021117{
21118 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021120}
21121
21122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 * Get the number value of a variable.
21124 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021125 * For incompatible types, return 0.
21126 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21127 * caller of incompatible types: it sets *denote to TRUE if "denote"
21128 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 */
21130 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021131get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021132 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021134 int error = FALSE;
21135
21136 return get_tv_number_chk(varp, &error); /* return 0L on error */
21137}
21138
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021139 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021140get_tv_number_chk(varp, denote)
21141 typval_T *varp;
21142 int *denote;
21143{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021144 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021146 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021148 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021149 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021150#ifdef FEAT_FLOAT
21151 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021152 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021153 break;
21154#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021155 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021156 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021157 break;
21158 case VAR_STRING:
21159 if (varp->vval.v_string != NULL)
21160 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021161 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021162 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021163 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021164 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021165 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021166 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021167 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021168 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021169 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021170 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021173 if (denote == NULL) /* useful for values that must be unsigned */
21174 n = -1;
21175 else
21176 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021177 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178}
21179
21180/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021181 * Get the lnum from the first argument.
21182 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021183 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 */
21185 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021186get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021187 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188{
Bram Moolenaar33570922005-01-25 22:26:29 +000021189 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 linenr_T lnum;
21191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021192 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 if (lnum == 0) /* no valid number, try using line() */
21194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 rettv.v_type = VAR_NUMBER;
21196 f_line(argvars, &rettv);
21197 lnum = rettv.vval.v_number;
21198 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 }
21200 return lnum;
21201}
21202
21203/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021204 * Get the lnum from the first argument.
21205 * Also accepts "$", then "buf" is used.
21206 * Returns 0 on error.
21207 */
21208 static linenr_T
21209get_tv_lnum_buf(argvars, buf)
21210 typval_T *argvars;
21211 buf_T *buf;
21212{
21213 if (argvars[0].v_type == VAR_STRING
21214 && argvars[0].vval.v_string != NULL
21215 && argvars[0].vval.v_string[0] == '$'
21216 && buf != NULL)
21217 return buf->b_ml.ml_line_count;
21218 return get_tv_number_chk(&argvars[0], NULL);
21219}
21220
21221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 * Get the string value of a variable.
21223 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021224 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21225 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 * If the String variable has never been set, return an empty string.
21227 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021228 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21229 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 */
21231 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021232get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021233 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234{
21235 static char_u mybuf[NUMBUFLEN];
21236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021237 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238}
21239
21240 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021241get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021242 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021243 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021245 char_u *res = get_tv_string_buf_chk(varp, buf);
21246
21247 return res != NULL ? res : (char_u *)"";
21248}
21249
Bram Moolenaar7d647822014-04-05 21:28:56 +020021250/*
21251 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21252 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021253 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021254get_tv_string_chk(varp)
21255 typval_T *varp;
21256{
21257 static char_u mybuf[NUMBUFLEN];
21258
21259 return get_tv_string_buf_chk(varp, mybuf);
21260}
21261
21262 static char_u *
21263get_tv_string_buf_chk(varp, buf)
21264 typval_T *varp;
21265 char_u *buf;
21266{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021267 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021269 case VAR_NUMBER:
21270 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21271 return buf;
21272 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021273 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021274 break;
21275 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021276 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021277 break;
21278 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021279 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021280 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021281#ifdef FEAT_FLOAT
21282 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021283 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284 break;
21285#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286 case VAR_STRING:
21287 if (varp->vval.v_string != NULL)
21288 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021289 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021290 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021291 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021294 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295}
21296
21297/*
21298 * Find variable "name" in the list of variables.
21299 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021300 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021301 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021304 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021305find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021308 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021311 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312
Bram Moolenaara7043832005-01-21 11:56:39 +000021313 ht = find_var_ht(name, &varname);
21314 if (htp != NULL)
21315 *htp = ht;
21316 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021318 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319}
21320
21321/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021322 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021323 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021325 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021326find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021327 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021328 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021329 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021330 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021331{
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 hashitem_T *hi;
21333
21334 if (*varname == NUL)
21335 {
21336 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021337 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021339 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 case 'g': return &globvars_var;
21341 case 'v': return &vimvars_var;
21342 case 'b': return &curbuf->b_bufvar;
21343 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021344#ifdef FEAT_WINDOWS
21345 case 't': return &curtab->tp_winvar;
21346#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021347 case 'l': return current_funccal == NULL
21348 ? NULL : &current_funccal->l_vars_var;
21349 case 'a': return current_funccal == NULL
21350 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021351 }
21352 return NULL;
21353 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021354
21355 hi = hash_find(ht, varname);
21356 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021357 {
21358 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021359 * worked find the variable again. Don't auto-load a script if it was
21360 * loaded already, otherwise it would be loaded every time when
21361 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021362 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021363 {
21364 /* Note: script_autoload() may make "hi" invalid. It must either
21365 * be obtained again or not used. */
21366 if (!script_autoload(varname, FALSE) || aborting())
21367 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021368 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021369 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021370 if (HASHITEM_EMPTY(hi))
21371 return NULL;
21372 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021373 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021374}
21375
21376/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021378 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021379 * Set "varname" to the start of name without ':'.
21380 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021381 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021382find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 char_u *name;
21384 char_u **varname;
21385{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021386 hashitem_T *hi;
21387
Bram Moolenaar73627d02015-08-11 15:46:09 +020021388 if (name[0] == NUL)
21389 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 if (name[1] != ':')
21391 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021392 /* The name must not start with a colon or #. */
21393 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 return NULL;
21395 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021396
21397 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021398 hi = hash_find(&compat_hashtab, name);
21399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021400 return &compat_hashtab;
21401
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021403 return &globvarht; /* global variable */
21404 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 }
21406 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021407 if (*name == 'g') /* global variable */
21408 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021409 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21410 */
21411 if (vim_strchr(name + 2, ':') != NULL
21412 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021413 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021415 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021417 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021418#ifdef FEAT_WINDOWS
21419 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021420 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021421#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021422 if (*name == 'v') /* v: variable */
21423 return &vimvarht;
21424 if (*name == 'a' && current_funccal != NULL) /* function argument */
21425 return &current_funccal->l_avars.dv_hashtab;
21426 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21427 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 if (*name == 's' /* script variable */
21429 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21430 return &SCRIPT_VARS(current_SID);
21431 return NULL;
21432}
21433
21434/*
21435 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021436 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 * Returns NULL when it doesn't exist.
21438 */
21439 char_u *
21440get_var_value(name)
21441 char_u *name;
21442{
Bram Moolenaar33570922005-01-25 22:26:29 +000021443 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021445 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 if (v == NULL)
21447 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449}
21450
21451/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021452 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 * sourcing this script and when executing functions defined in the script.
21454 */
21455 void
21456new_script_vars(id)
21457 scid_T id;
21458{
Bram Moolenaara7043832005-01-21 11:56:39 +000021459 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021460 hashtab_T *ht;
21461 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021462
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21464 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021465 /* Re-allocating ga_data means that an ht_array pointing to
21466 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021468 for (i = 1; i <= ga_scripts.ga_len; ++i)
21469 {
21470 ht = &SCRIPT_VARS(i);
21471 if (ht->ht_mask == HT_INIT_SIZE - 1)
21472 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021473 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021474 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021475 }
21476
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 while (ga_scripts.ga_len < id)
21478 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021479 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021480 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021481 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 }
21484 }
21485}
21486
21487/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21489 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 */
21491 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021492init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 dict_T *dict;
21494 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021495 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496{
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021498 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021499 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021500 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021501 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 dict_var->di_tv.vval.v_dict = dict;
21503 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021504 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21506 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507}
21508
21509/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021510 * Unreference a dictionary initialized by init_var_dict().
21511 */
21512 void
21513unref_var_dict(dict)
21514 dict_T *dict;
21515{
21516 /* Now the dict needs to be freed if no one else is using it, go back to
21517 * normal reference counting. */
21518 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21519 dict_unref(dict);
21520}
21521
21522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021524 * Frees all allocated variables and the value they contain.
21525 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 */
21527 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021528vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021529 hashtab_T *ht;
21530{
21531 vars_clear_ext(ht, TRUE);
21532}
21533
21534/*
21535 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21536 */
21537 static void
21538vars_clear_ext(ht, free_val)
21539 hashtab_T *ht;
21540 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541{
Bram Moolenaara7043832005-01-21 11:56:39 +000021542 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 hashitem_T *hi;
21544 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021547 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021548 for (hi = ht->ht_array; todo > 0; ++hi)
21549 {
21550 if (!HASHITEM_EMPTY(hi))
21551 {
21552 --todo;
21553
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021555 * ht_array might change then. hash_clear() takes care of it
21556 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 v = HI2DI(hi);
21558 if (free_val)
21559 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021560 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021561 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021562 }
21563 }
21564 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021565 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566}
21567
Bram Moolenaara7043832005-01-21 11:56:39 +000021568/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021569 * Delete a variable from hashtab "ht" at item "hi".
21570 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021573delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021574 hashtab_T *ht;
21575 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576{
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021578
21579 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021580 clear_tv(&di->di_tv);
21581 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582}
21583
21584/*
21585 * List the value of one internal variable.
21586 */
21587 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021588list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021591 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021593 char_u *tofree;
21594 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021595 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021596
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021597 current_copyID += COPYID_INC;
21598 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021600 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602}
21603
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021605list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 char_u *prefix;
21607 char_u *name;
21608 int type;
21609 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021610 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611{
Bram Moolenaar31859182007-08-14 20:41:13 +000021612 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21613 msg_start();
21614 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 if (name != NULL) /* "a:" vars don't have a name stored */
21616 msg_puts(name);
21617 msg_putchar(' ');
21618 msg_advance(22);
21619 if (type == VAR_NUMBER)
21620 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021621 else if (type == VAR_FUNC)
21622 msg_putchar('*');
21623 else if (type == VAR_LIST)
21624 {
21625 msg_putchar('[');
21626 if (*string == '[')
21627 ++string;
21628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021629 else if (type == VAR_DICT)
21630 {
21631 msg_putchar('{');
21632 if (*string == '{')
21633 ++string;
21634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 else
21636 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021637
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021639
21640 if (type == VAR_FUNC)
21641 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021642 if (*first)
21643 {
21644 msg_clr_eos();
21645 *first = FALSE;
21646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647}
21648
21649/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021650 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 * If the variable already exists, the value is updated.
21652 * Otherwise the variable is created.
21653 */
21654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021655set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021658 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659{
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021664 ht = find_var_ht(name, &varname);
21665 if (ht == NULL || *varname == NUL)
21666 {
21667 EMSG2(_(e_illvar), name);
21668 return;
21669 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021670 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021671
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021672 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21673 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021678 if (var_check_ro(v->di_flags, name, FALSE)
21679 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 return;
21681 if (v->di_tv.v_type != tv->v_type
21682 && !((v->di_tv.v_type == VAR_STRING
21683 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021684 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021685 || tv->v_type == VAR_NUMBER))
21686#ifdef FEAT_FLOAT
21687 && !((v->di_tv.v_type == VAR_NUMBER
21688 || v->di_tv.v_type == VAR_FLOAT)
21689 && (tv->v_type == VAR_NUMBER
21690 || tv->v_type == VAR_FLOAT))
21691#endif
21692 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021693 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021694 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021695 return;
21696 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021697
21698 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021699 * Handle setting internal v: variables separately where needed to
21700 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021701 */
21702 if (ht == &vimvarht)
21703 {
21704 if (v->di_tv.v_type == VAR_STRING)
21705 {
21706 vim_free(v->di_tv.vval.v_string);
21707 if (copy || tv->v_type != VAR_STRING)
21708 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21709 else
21710 {
21711 /* Take over the string to avoid an extra alloc/free. */
21712 v->di_tv.vval.v_string = tv->vval.v_string;
21713 tv->vval.v_string = NULL;
21714 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021715 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021717 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021718 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021720 if (STRCMP(varname, "searchforward") == 0)
21721 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021722#ifdef FEAT_SEARCH_EXTRA
21723 else if (STRCMP(varname, "hlsearch") == 0)
21724 {
21725 no_hlsearch = !v->di_tv.vval.v_number;
21726 redraw_all_later(SOME_VALID);
21727 }
21728#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021729 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021730 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021731 else if (v->di_tv.v_type != tv->v_type)
21732 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 }
21734
21735 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 }
21737 else /* add a new variable */
21738 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021739 /* Can't add "v:" variable. */
21740 if (ht == &vimvarht)
21741 {
21742 EMSG2(_(e_illvar), name);
21743 return;
21744 }
21745
Bram Moolenaar92124a32005-06-17 22:03:40 +000021746 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021747 if (!valid_varname(varname))
21748 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021750 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21751 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021752 if (v == NULL)
21753 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021757 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 return;
21759 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021760 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021763 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021765 else
21766 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021768 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021769 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771}
21772
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021773/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021774 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 * Also give an error message.
21776 */
21777 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021778var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 int flags;
21780 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021781 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021782{
21783 if (flags & DI_FLAGS_RO)
21784 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021785 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 return TRUE;
21787 }
21788 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21789 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021790 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 return TRUE;
21792 }
21793 return FALSE;
21794}
21795
21796/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021797 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21798 * Also give an error message.
21799 */
21800 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021801var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021802 int flags;
21803 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021804 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021805{
21806 if (flags & DI_FLAGS_FIX)
21807 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021808 EMSG2(_("E795: Cannot delete variable %s"),
21809 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021810 return TRUE;
21811 }
21812 return FALSE;
21813}
21814
21815/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021816 * Check if a funcref is assigned to a valid variable name.
21817 * Return TRUE and give an error if not.
21818 */
21819 static int
21820var_check_func_name(name, new_var)
21821 char_u *name; /* points to start of variable name */
21822 int new_var; /* TRUE when creating the variable */
21823{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021824 /* Allow for w: b: s: and t:. */
21825 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021826 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21827 ? name[2] : name[0]))
21828 {
21829 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21830 name);
21831 return TRUE;
21832 }
21833 /* Don't allow hiding a function. When "v" is not NULL we might be
21834 * assigning another function to the same var, the type is checked
21835 * below. */
21836 if (new_var && function_exists(name))
21837 {
21838 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21839 name);
21840 return TRUE;
21841 }
21842 return FALSE;
21843}
21844
21845/*
21846 * Check if a variable name is valid.
21847 * Return FALSE and give an error if not.
21848 */
21849 static int
21850valid_varname(varname)
21851 char_u *varname;
21852{
21853 char_u *p;
21854
21855 for (p = varname; *p != NUL; ++p)
21856 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21857 && *p != AUTOLOAD_CHAR)
21858 {
21859 EMSG2(_(e_illvar), varname);
21860 return FALSE;
21861 }
21862 return TRUE;
21863}
21864
21865/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021866 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021867 * Also give an error message, using "name" or _("name") when use_gettext is
21868 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021869 */
21870 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021871tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021872 int lock;
21873 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021874 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021875{
21876 if (lock & VAR_LOCKED)
21877 {
21878 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021879 name == NULL ? (char_u *)_("Unknown")
21880 : use_gettext ? (char_u *)_(name)
21881 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021882 return TRUE;
21883 }
21884 if (lock & VAR_FIXED)
21885 {
21886 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021887 name == NULL ? (char_u *)_("Unknown")
21888 : use_gettext ? (char_u *)_(name)
21889 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021890 return TRUE;
21891 }
21892 return FALSE;
21893}
21894
21895/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021898 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021899 * It is OK for "from" and "to" to point to the same item. This is used to
21900 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021901 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021902 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021903copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021904 typval_T *from;
21905 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021907 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021908 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021909 switch (from->v_type)
21910 {
21911 case VAR_NUMBER:
21912 to->vval.v_number = from->vval.v_number;
21913 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021914#ifdef FEAT_FLOAT
21915 case VAR_FLOAT:
21916 to->vval.v_float = from->vval.v_float;
21917 break;
21918#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 case VAR_STRING:
21920 case VAR_FUNC:
21921 if (from->vval.v_string == NULL)
21922 to->vval.v_string = NULL;
21923 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021924 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021926 if (from->v_type == VAR_FUNC)
21927 func_ref(to->vval.v_string);
21928 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021929 break;
21930 case VAR_LIST:
21931 if (from->vval.v_list == NULL)
21932 to->vval.v_list = NULL;
21933 else
21934 {
21935 to->vval.v_list = from->vval.v_list;
21936 ++to->vval.v_list->lv_refcount;
21937 }
21938 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021939 case VAR_DICT:
21940 if (from->vval.v_dict == NULL)
21941 to->vval.v_dict = NULL;
21942 else
21943 {
21944 to->vval.v_dict = from->vval.v_dict;
21945 ++to->vval.v_dict->dv_refcount;
21946 }
21947 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021949 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 break;
21951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952}
21953
21954/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021955 * Make a copy of an item.
21956 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021957 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21958 * reference to an already copied list/dict can be used.
21959 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021960 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021961 static int
21962item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 typval_T *from;
21964 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021965 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021966 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021967{
21968 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021969 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021970
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021972 {
21973 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021974 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021975 }
21976 ++recurse;
21977
21978 switch (from->v_type)
21979 {
21980 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021981#ifdef FEAT_FLOAT
21982 case VAR_FLOAT:
21983#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021984 case VAR_STRING:
21985 case VAR_FUNC:
21986 copy_tv(from, to);
21987 break;
21988 case VAR_LIST:
21989 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021990 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021991 if (from->vval.v_list == NULL)
21992 to->vval.v_list = NULL;
21993 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21994 {
21995 /* use the copy made earlier */
21996 to->vval.v_list = from->vval.v_list->lv_copylist;
21997 ++to->vval.v_list->lv_refcount;
21998 }
21999 else
22000 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22001 if (to->vval.v_list == NULL)
22002 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022003 break;
22004 case VAR_DICT:
22005 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022006 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022007 if (from->vval.v_dict == NULL)
22008 to->vval.v_dict = NULL;
22009 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22010 {
22011 /* use the copy made earlier */
22012 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22013 ++to->vval.v_dict->dv_refcount;
22014 }
22015 else
22016 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22017 if (to->vval.v_dict == NULL)
22018 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022019 break;
22020 default:
22021 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022022 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022023 }
22024 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022025 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026}
22027
22028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 * ":echo expr1 ..." print each argument separated with a space, add a
22030 * newline at the end.
22031 * ":echon expr1 ..." print each argument plain.
22032 */
22033 void
22034ex_echo(eap)
22035 exarg_T *eap;
22036{
22037 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022038 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022039 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 char_u *p;
22041 int needclr = TRUE;
22042 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022043 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044
22045 if (eap->skip)
22046 ++emsg_skip;
22047 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22048 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022049 /* If eval1() causes an error message the text from the command may
22050 * still need to be cleared. E.g., "echo 22,44". */
22051 need_clr_eos = needclr;
22052
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022054 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 {
22056 /*
22057 * Report the invalid expression unless the expression evaluation
22058 * has been cancelled due to an aborting error, an interrupt, or an
22059 * exception.
22060 */
22061 if (!aborting())
22062 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022063 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 break;
22065 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022066 need_clr_eos = FALSE;
22067
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 if (!eap->skip)
22069 {
22070 if (atstart)
22071 {
22072 atstart = FALSE;
22073 /* Call msg_start() after eval1(), evaluating the expression
22074 * may cause a message to appear. */
22075 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022076 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022077 /* Mark the saved text as finishing the line, so that what
22078 * follows is displayed on a new line when scrolling back
22079 * at the more prompt. */
22080 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
22084 else if (eap->cmdidx == CMD_echo)
22085 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022086 current_copyID += COPYID_INC;
22087 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022088 if (p != NULL)
22089 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022091 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022093 if (*p != TAB && needclr)
22094 {
22095 /* remove any text still there from the command */
22096 msg_clr_eos();
22097 needclr = FALSE;
22098 }
22099 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 }
22101 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022102 {
22103#ifdef FEAT_MBYTE
22104 if (has_mbyte)
22105 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022106 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022107
22108 (void)msg_outtrans_len_attr(p, i, echo_attr);
22109 p += i - 1;
22110 }
22111 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022113 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022116 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022118 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 arg = skipwhite(arg);
22120 }
22121 eap->nextcmd = check_nextcmd(arg);
22122
22123 if (eap->skip)
22124 --emsg_skip;
22125 else
22126 {
22127 /* remove text that may still be there from the command */
22128 if (needclr)
22129 msg_clr_eos();
22130 if (eap->cmdidx == CMD_echo)
22131 msg_end();
22132 }
22133}
22134
22135/*
22136 * ":echohl {name}".
22137 */
22138 void
22139ex_echohl(eap)
22140 exarg_T *eap;
22141{
22142 int id;
22143
22144 id = syn_name2id(eap->arg);
22145 if (id == 0)
22146 echo_attr = 0;
22147 else
22148 echo_attr = syn_id2attr(id);
22149}
22150
22151/*
22152 * ":execute expr1 ..." execute the result of an expression.
22153 * ":echomsg expr1 ..." Print a message
22154 * ":echoerr expr1 ..." Print an error
22155 * Each gets spaces around each argument and a newline at the end for
22156 * echo commands
22157 */
22158 void
22159ex_execute(eap)
22160 exarg_T *eap;
22161{
22162 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 int ret = OK;
22165 char_u *p;
22166 garray_T ga;
22167 int len;
22168 int save_did_emsg;
22169
22170 ga_init2(&ga, 1, 80);
22171
22172 if (eap->skip)
22173 ++emsg_skip;
22174 while (*arg != NUL && *arg != '|' && *arg != '\n')
22175 {
22176 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022177 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 {
22179 /*
22180 * Report the invalid expression unless the expression evaluation
22181 * has been cancelled due to an aborting error, an interrupt, or an
22182 * exception.
22183 */
22184 if (!aborting())
22185 EMSG2(_(e_invexpr2), p);
22186 ret = FAIL;
22187 break;
22188 }
22189
22190 if (!eap->skip)
22191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022192 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 len = (int)STRLEN(p);
22194 if (ga_grow(&ga, len + 2) == FAIL)
22195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022196 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 ret = FAIL;
22198 break;
22199 }
22200 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 ga.ga_len += len;
22204 }
22205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022206 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 arg = skipwhite(arg);
22208 }
22209
22210 if (ret != FAIL && ga.ga_data != NULL)
22211 {
22212 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022215 out_flush();
22216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 else if (eap->cmdidx == CMD_echoerr)
22218 {
22219 /* We don't want to abort following commands, restore did_emsg. */
22220 save_did_emsg = did_emsg;
22221 EMSG((char_u *)ga.ga_data);
22222 if (!force_abort)
22223 did_emsg = save_did_emsg;
22224 }
22225 else if (eap->cmdidx == CMD_execute)
22226 do_cmdline((char_u *)ga.ga_data,
22227 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22228 }
22229
22230 ga_clear(&ga);
22231
22232 if (eap->skip)
22233 --emsg_skip;
22234
22235 eap->nextcmd = check_nextcmd(arg);
22236}
22237
22238/*
22239 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22240 * "arg" points to the "&" or '+' when called, to "option" when returning.
22241 * Returns NULL when no option name found. Otherwise pointer to the char
22242 * after the option name.
22243 */
22244 static char_u *
22245find_option_end(arg, opt_flags)
22246 char_u **arg;
22247 int *opt_flags;
22248{
22249 char_u *p = *arg;
22250
22251 ++p;
22252 if (*p == 'g' && p[1] == ':')
22253 {
22254 *opt_flags = OPT_GLOBAL;
22255 p += 2;
22256 }
22257 else if (*p == 'l' && p[1] == ':')
22258 {
22259 *opt_flags = OPT_LOCAL;
22260 p += 2;
22261 }
22262 else
22263 *opt_flags = 0;
22264
22265 if (!ASCII_ISALPHA(*p))
22266 return NULL;
22267 *arg = p;
22268
22269 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22270 p += 4; /* termcap option */
22271 else
22272 while (ASCII_ISALPHA(*p))
22273 ++p;
22274 return p;
22275}
22276
22277/*
22278 * ":function"
22279 */
22280 void
22281ex_function(eap)
22282 exarg_T *eap;
22283{
22284 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022285 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 int j;
22287 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022289 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 char_u *name = NULL;
22291 char_u *p;
22292 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022293 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 garray_T newargs;
22295 garray_T newlines;
22296 int varargs = FALSE;
22297 int mustend = FALSE;
22298 int flags = 0;
22299 ufunc_T *fp;
22300 int indent;
22301 int nesting;
22302 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022303 dictitem_T *v;
22304 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022305 static int func_nr = 0; /* number for nameless function */
22306 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 hashtab_T *ht;
22308 int todo;
22309 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022310 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311
22312 /*
22313 * ":function" without argument: list functions.
22314 */
22315 if (ends_excmd(*eap->arg))
22316 {
22317 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022318 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022319 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022320 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022321 {
22322 if (!HASHITEM_EMPTY(hi))
22323 {
22324 --todo;
22325 fp = HI2UF(hi);
22326 if (!isdigit(*fp->uf_name))
22327 list_func_head(fp, FALSE);
22328 }
22329 }
22330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 eap->nextcmd = check_nextcmd(eap->arg);
22332 return;
22333 }
22334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022335 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022336 * ":function /pat": list functions matching pattern.
22337 */
22338 if (*eap->arg == '/')
22339 {
22340 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22341 if (!eap->skip)
22342 {
22343 regmatch_T regmatch;
22344
22345 c = *p;
22346 *p = NUL;
22347 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22348 *p = c;
22349 if (regmatch.regprog != NULL)
22350 {
22351 regmatch.rm_ic = p_ic;
22352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022353 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022354 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22355 {
22356 if (!HASHITEM_EMPTY(hi))
22357 {
22358 --todo;
22359 fp = HI2UF(hi);
22360 if (!isdigit(*fp->uf_name)
22361 && vim_regexec(&regmatch, fp->uf_name, 0))
22362 list_func_head(fp, FALSE);
22363 }
22364 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022365 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022366 }
22367 }
22368 if (*p == '/')
22369 ++p;
22370 eap->nextcmd = check_nextcmd(p);
22371 return;
22372 }
22373
22374 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022375 * Get the function name. There are these situations:
22376 * func normal function name
22377 * "name" == func, "fudi.fd_dict" == NULL
22378 * dict.func new dictionary entry
22379 * "name" == NULL, "fudi.fd_dict" set,
22380 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22381 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022382 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22384 * dict.func existing dict entry that's not a Funcref
22385 * "name" == NULL, "fudi.fd_dict" set,
22386 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022387 * s:func script-local function name
22388 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022391 name = trans_function_name(&p, eap->skip, 0, &fudi);
22392 paren = (vim_strchr(p, '(') != NULL);
22393 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 {
22395 /*
22396 * Return on an invalid expression in braces, unless the expression
22397 * evaluation has been cancelled due to an aborting error, an
22398 * interrupt, or an exception.
22399 */
22400 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022402 if (!eap->skip && fudi.fd_newkey != NULL)
22403 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022404 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 else
22408 eap->skip = TRUE;
22409 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022410
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 /* An error in a function call during evaluation of an expression in magic
22412 * braces should not cause the function not to be defined. */
22413 saved_did_emsg = did_emsg;
22414 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415
22416 /*
22417 * ":function func" with only function name: list function.
22418 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 {
22421 if (!ends_excmd(*skipwhite(p)))
22422 {
22423 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022424 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 }
22426 eap->nextcmd = check_nextcmd(p);
22427 if (eap->nextcmd != NULL)
22428 *p = NUL;
22429 if (!eap->skip && !got_int)
22430 {
22431 fp = find_func(name);
22432 if (fp != NULL)
22433 {
22434 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022435 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022437 if (FUNCLINE(fp, j) == NULL)
22438 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 msg_putchar('\n');
22440 msg_outnum((long)(j + 1));
22441 if (j < 9)
22442 msg_putchar(' ');
22443 if (j < 99)
22444 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022445 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 out_flush(); /* show a line at a time */
22447 ui_breakcheck();
22448 }
22449 if (!got_int)
22450 {
22451 msg_putchar('\n');
22452 msg_puts((char_u *)" endfunction");
22453 }
22454 }
22455 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022456 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022458 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 }
22460
22461 /*
22462 * ":function name(arg1, arg2)" Define function.
22463 */
22464 p = skipwhite(p);
22465 if (*p != '(')
22466 {
22467 if (!eap->skip)
22468 {
22469 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022470 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 }
22472 /* attempt to continue by skipping some text */
22473 if (vim_strchr(p, '(') != NULL)
22474 p = vim_strchr(p, '(');
22475 }
22476 p = skipwhite(p + 1);
22477
22478 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22479 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22480
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022481 if (!eap->skip)
22482 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022483 /* Check the name of the function. Unless it's a dictionary function
22484 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022485 if (name != NULL)
22486 arg = name;
22487 else
22488 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022489 if (arg != NULL && (fudi.fd_di == NULL
22490 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022491 {
22492 if (*arg == K_SPECIAL)
22493 j = 3;
22494 else
22495 j = 0;
22496 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22497 : eval_isnamec(arg[j])))
22498 ++j;
22499 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022500 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022501 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022502 /* Disallow using the g: dict. */
22503 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22504 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022505 }
22506
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 /*
22508 * Isolate the arguments: "arg1, arg2, ...)"
22509 */
22510 while (*p != ')')
22511 {
22512 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22513 {
22514 varargs = TRUE;
22515 p += 3;
22516 mustend = TRUE;
22517 }
22518 else
22519 {
22520 arg = p;
22521 while (ASCII_ISALNUM(*p) || *p == '_')
22522 ++p;
22523 if (arg == p || isdigit(*arg)
22524 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22525 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22526 {
22527 if (!eap->skip)
22528 EMSG2(_("E125: Illegal argument: %s"), arg);
22529 break;
22530 }
22531 if (ga_grow(&newargs, 1) == FAIL)
22532 goto erret;
22533 c = *p;
22534 *p = NUL;
22535 arg = vim_strsave(arg);
22536 if (arg == NULL)
22537 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022538
22539 /* Check for duplicate argument name. */
22540 for (i = 0; i < newargs.ga_len; ++i)
22541 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22542 {
22543 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022544 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022545 goto erret;
22546 }
22547
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22549 *p = c;
22550 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 if (*p == ',')
22552 ++p;
22553 else
22554 mustend = TRUE;
22555 }
22556 p = skipwhite(p);
22557 if (mustend && *p != ')')
22558 {
22559 if (!eap->skip)
22560 EMSG2(_(e_invarg2), eap->arg);
22561 break;
22562 }
22563 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022564 if (*p != ')')
22565 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 ++p; /* skip the ')' */
22567
Bram Moolenaare9a41262005-01-15 22:18:47 +000022568 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 for (;;)
22570 {
22571 p = skipwhite(p);
22572 if (STRNCMP(p, "range", 5) == 0)
22573 {
22574 flags |= FC_RANGE;
22575 p += 5;
22576 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022577 else if (STRNCMP(p, "dict", 4) == 0)
22578 {
22579 flags |= FC_DICT;
22580 p += 4;
22581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 else if (STRNCMP(p, "abort", 5) == 0)
22583 {
22584 flags |= FC_ABORT;
22585 p += 5;
22586 }
22587 else
22588 break;
22589 }
22590
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022591 /* When there is a line break use what follows for the function body.
22592 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22593 if (*p == '\n')
22594 line_arg = p + 1;
22595 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 EMSG(_(e_trailing));
22597
22598 /*
22599 * Read the body of the function, until ":endfunction" is found.
22600 */
22601 if (KeyTyped)
22602 {
22603 /* Check if the function already exists, don't let the user type the
22604 * whole function before telling him it doesn't work! For a script we
22605 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022606 if (!eap->skip && !eap->forceit)
22607 {
22608 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22609 EMSG(_(e_funcdict));
22610 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022611 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022614 if (!eap->skip && did_emsg)
22615 goto erret;
22616
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 msg_putchar('\n'); /* don't overwrite the function name */
22618 cmdline_row = msg_row;
22619 }
22620
22621 indent = 2;
22622 nesting = 0;
22623 for (;;)
22624 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022625 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022626 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022627 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022628 saved_wait_return = FALSE;
22629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022631 sourcing_lnum_off = sourcing_lnum;
22632
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022633 if (line_arg != NULL)
22634 {
22635 /* Use eap->arg, split up in parts by line breaks. */
22636 theline = line_arg;
22637 p = vim_strchr(theline, '\n');
22638 if (p == NULL)
22639 line_arg += STRLEN(line_arg);
22640 else
22641 {
22642 *p = NUL;
22643 line_arg = p + 1;
22644 }
22645 }
22646 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 theline = getcmdline(':', 0L, indent);
22648 else
22649 theline = eap->getline(':', eap->cookie, indent);
22650 if (KeyTyped)
22651 lines_left = Rows - 1;
22652 if (theline == NULL)
22653 {
22654 EMSG(_("E126: Missing :endfunction"));
22655 goto erret;
22656 }
22657
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022658 /* Detect line continuation: sourcing_lnum increased more than one. */
22659 if (sourcing_lnum > sourcing_lnum_off + 1)
22660 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22661 else
22662 sourcing_lnum_off = 0;
22663
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 if (skip_until != NULL)
22665 {
22666 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22667 * don't check for ":endfunc". */
22668 if (STRCMP(theline, skip_until) == 0)
22669 {
22670 vim_free(skip_until);
22671 skip_until = NULL;
22672 }
22673 }
22674 else
22675 {
22676 /* skip ':' and blanks*/
22677 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22678 ;
22679
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022680 /* Check for "endfunction". */
22681 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022683 if (line_arg == NULL)
22684 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 break;
22686 }
22687
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022688 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 * at "end". */
22690 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22691 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022692 else if (STRNCMP(p, "if", 2) == 0
22693 || STRNCMP(p, "wh", 2) == 0
22694 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 || STRNCMP(p, "try", 3) == 0)
22696 indent += 2;
22697
22698 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022699 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022701 if (*p == '!')
22702 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022704 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22705 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022707 ++nesting;
22708 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 }
22710 }
22711
22712 /* Check for ":append" or ":insert". */
22713 p = skip_range(p, NULL);
22714 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22715 || (p[0] == 'i'
22716 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22717 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22718 skip_until = vim_strsave((char_u *)".");
22719
22720 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22721 arg = skipwhite(skiptowhite(p));
22722 if (arg[0] == '<' && arg[1] =='<'
22723 && ((p[0] == 'p' && p[1] == 'y'
22724 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22725 || (p[0] == 'p' && p[1] == 'e'
22726 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22727 || (p[0] == 't' && p[1] == 'c'
22728 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022729 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22730 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22732 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022733 || (p[0] == 'm' && p[1] == 'z'
22734 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 ))
22736 {
22737 /* ":python <<" continues until a dot, like ":append" */
22738 p = skipwhite(arg + 2);
22739 if (*p == NUL)
22740 skip_until = vim_strsave((char_u *)".");
22741 else
22742 skip_until = vim_strsave(p);
22743 }
22744 }
22745
22746 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022747 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022748 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022749 if (line_arg == NULL)
22750 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022752 }
22753
22754 /* Copy the line to newly allocated memory. get_one_sourceline()
22755 * allocates 250 bytes per line, this saves 80% on average. The cost
22756 * is an extra alloc/free. */
22757 p = vim_strsave(theline);
22758 if (p != NULL)
22759 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022760 if (line_arg == NULL)
22761 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022762 theline = p;
22763 }
22764
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022765 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22766
22767 /* Add NULL lines for continuation lines, so that the line count is
22768 * equal to the index in the growarray. */
22769 while (sourcing_lnum_off-- > 0)
22770 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022771
22772 /* Check for end of eap->arg. */
22773 if (line_arg != NULL && *line_arg == NUL)
22774 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 }
22776
22777 /* Don't define the function when skipping commands or when an error was
22778 * detected. */
22779 if (eap->skip || did_emsg)
22780 goto erret;
22781
22782 /*
22783 * If there are no errors, add the function
22784 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022785 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022786 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022787 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022789 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022790 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022791 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022792 goto erret;
22793 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022794
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022795 fp = find_func(name);
22796 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022798 if (!eap->forceit)
22799 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022800 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022801 goto erret;
22802 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022803 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022804 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022805 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022806 name);
22807 goto erret;
22808 }
22809 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022810 ga_clear_strings(&(fp->uf_args));
22811 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022812 vim_free(name);
22813 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 }
22816 else
22817 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022818 char numbuf[20];
22819
22820 fp = NULL;
22821 if (fudi.fd_newkey == NULL && !eap->forceit)
22822 {
22823 EMSG(_(e_funcdict));
22824 goto erret;
22825 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022826 if (fudi.fd_di == NULL)
22827 {
22828 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022829 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022830 goto erret;
22831 }
22832 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022833 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022834 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022835
22836 /* Give the function a sequential number. Can only be used with a
22837 * Funcref! */
22838 vim_free(name);
22839 sprintf(numbuf, "%d", ++func_nr);
22840 name = vim_strsave((char_u *)numbuf);
22841 if (name == NULL)
22842 goto erret;
22843 }
22844
22845 if (fp == NULL)
22846 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022847 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 {
22849 int slen, plen;
22850 char_u *scriptname;
22851
22852 /* Check that the autoload name matches the script name. */
22853 j = FAIL;
22854 if (sourcing_name != NULL)
22855 {
22856 scriptname = autoload_name(name);
22857 if (scriptname != NULL)
22858 {
22859 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022860 plen = (int)STRLEN(p);
22861 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022862 if (slen > plen && fnamecmp(p,
22863 sourcing_name + slen - plen) == 0)
22864 j = OK;
22865 vim_free(scriptname);
22866 }
22867 }
22868 if (j == FAIL)
22869 {
22870 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22871 goto erret;
22872 }
22873 }
22874
22875 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 if (fp == NULL)
22877 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022878
22879 if (fudi.fd_dict != NULL)
22880 {
22881 if (fudi.fd_di == NULL)
22882 {
22883 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022884 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022885 if (fudi.fd_di == NULL)
22886 {
22887 vim_free(fp);
22888 goto erret;
22889 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022890 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22891 {
22892 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022893 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022894 goto erret;
22895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 }
22897 else
22898 /* overwrite existing dict entry */
22899 clear_tv(&fudi.fd_di->di_tv);
22900 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022901 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022902 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022904
22905 /* behave like "dict" was used */
22906 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022907 }
22908
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022910 STRCPY(fp->uf_name, name);
22911 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022913 fp->uf_args = newargs;
22914 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022915#ifdef FEAT_PROFILE
22916 fp->uf_tml_count = NULL;
22917 fp->uf_tml_total = NULL;
22918 fp->uf_tml_self = NULL;
22919 fp->uf_profiling = FALSE;
22920 if (prof_def_func())
22921 func_do_profile(fp);
22922#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022923 fp->uf_varargs = varargs;
22924 fp->uf_flags = flags;
22925 fp->uf_calls = 0;
22926 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022927 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928
22929erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 ga_clear_strings(&newargs);
22931 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022932ret_free:
22933 vim_free(skip_until);
22934 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022937 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938}
22939
22940/*
22941 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022942 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022944 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022945 * TFN_INT: internal function name OK
22946 * TFN_QUIET: be quiet
22947 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 * Advances "pp" to just after the function name (if no error).
22949 */
22950 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022951trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 char_u **pp;
22953 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022955 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022957 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 char_u *start;
22959 char_u *end;
22960 int lead;
22961 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022963 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022964
22965 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022968
22969 /* Check for hard coded <SNR>: already translated function ID (from a user
22970 * command). */
22971 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22972 && (*pp)[2] == (int)KE_SNR)
22973 {
22974 *pp += 3;
22975 len = get_id_len(pp) + 3;
22976 return vim_strnsave(start, len);
22977 }
22978
22979 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22980 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022982 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022984
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022985 /* Note that TFN_ flags use the same values as GLV_ flags. */
22986 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022987 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022988 if (end == start)
22989 {
22990 if (!skip)
22991 EMSG(_("E129: Function name required"));
22992 goto theend;
22993 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022994 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022995 {
22996 /*
22997 * Report an invalid expression in braces, unless the expression
22998 * evaluation has been cancelled due to an aborting error, an
22999 * interrupt, or an exception.
23000 */
23001 if (!aborting())
23002 {
23003 if (end != NULL)
23004 EMSG2(_(e_invarg2), start);
23005 }
23006 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023007 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023008 goto theend;
23009 }
23010
23011 if (lv.ll_tv != NULL)
23012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023013 if (fdp != NULL)
23014 {
23015 fdp->fd_dict = lv.ll_dict;
23016 fdp->fd_newkey = lv.ll_newkey;
23017 lv.ll_newkey = NULL;
23018 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023020 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23021 {
23022 name = vim_strsave(lv.ll_tv->vval.v_string);
23023 *pp = end;
23024 }
23025 else
23026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023027 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23028 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023029 EMSG(_(e_funcref));
23030 else
23031 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023032 name = NULL;
23033 }
23034 goto theend;
23035 }
23036
23037 if (lv.ll_name == NULL)
23038 {
23039 /* Error found, but continue after the function name. */
23040 *pp = end;
23041 goto theend;
23042 }
23043
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023044 /* Check if the name is a Funcref. If so, use the value. */
23045 if (lv.ll_exp_name != NULL)
23046 {
23047 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023048 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023049 if (name == lv.ll_exp_name)
23050 name = NULL;
23051 }
23052 else
23053 {
23054 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023055 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023056 if (name == *pp)
23057 name = NULL;
23058 }
23059 if (name != NULL)
23060 {
23061 name = vim_strsave(name);
23062 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023063 if (STRNCMP(name, "<SNR>", 5) == 0)
23064 {
23065 /* Change "<SNR>" to the byte sequence. */
23066 name[0] = K_SPECIAL;
23067 name[1] = KS_EXTRA;
23068 name[2] = (int)KE_SNR;
23069 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23070 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023071 goto theend;
23072 }
23073
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023074 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023075 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023076 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023077 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23078 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23079 {
23080 /* When there was "s:" already or the name expanded to get a
23081 * leading "s:" then remove it. */
23082 lv.ll_name += 2;
23083 len -= 2;
23084 lead = 2;
23085 }
23086 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023087 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023088 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023089 /* skip over "s:" and "g:" */
23090 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023091 lv.ll_name += 2;
23092 len = (int)(end - lv.ll_name);
23093 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023094
23095 /*
23096 * Copy the function name to allocated memory.
23097 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23098 * Accept <SNR>123_name() outside a script.
23099 */
23100 if (skip)
23101 lead = 0; /* do nothing */
23102 else if (lead > 0)
23103 {
23104 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023105 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23106 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023107 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023108 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023109 if (current_SID <= 0)
23110 {
23111 EMSG(_(e_usingsid));
23112 goto theend;
23113 }
23114 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23115 lead += (int)STRLEN(sid_buf);
23116 }
23117 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023118 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023119 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023120 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023121 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023122 goto theend;
23123 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023124 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023125 {
23126 char_u *cp = vim_strchr(lv.ll_name, ':');
23127
23128 if (cp != NULL && cp < end)
23129 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023130 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023131 goto theend;
23132 }
23133 }
23134
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023135 name = alloc((unsigned)(len + lead + 1));
23136 if (name != NULL)
23137 {
23138 if (lead > 0)
23139 {
23140 name[0] = K_SPECIAL;
23141 name[1] = KS_EXTRA;
23142 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023143 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023144 STRCPY(name + 3, sid_buf);
23145 }
23146 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023147 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023148 }
23149 *pp = end;
23150
23151theend:
23152 clear_lval(&lv);
23153 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154}
23155
23156/*
23157 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23158 * Return 2 if "p" starts with "s:".
23159 * Return 0 otherwise.
23160 */
23161 static int
23162eval_fname_script(p)
23163 char_u *p;
23164{
23165 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23166 || STRNICMP(p + 1, "SNR>", 4) == 0))
23167 return 5;
23168 if (p[0] == 's' && p[1] == ':')
23169 return 2;
23170 return 0;
23171}
23172
23173/*
23174 * Return TRUE if "p" starts with "<SID>" or "s:".
23175 * Only works if eval_fname_script() returned non-zero for "p"!
23176 */
23177 static int
23178eval_fname_sid(p)
23179 char_u *p;
23180{
23181 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23182}
23183
23184/*
23185 * List the head of the function: "name(arg1, arg2)".
23186 */
23187 static void
23188list_func_head(fp, indent)
23189 ufunc_T *fp;
23190 int indent;
23191{
23192 int j;
23193
23194 msg_start();
23195 if (indent)
23196 MSG_PUTS(" ");
23197 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023198 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 {
23200 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023201 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 }
23203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023204 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023206 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 {
23208 if (j)
23209 MSG_PUTS(", ");
23210 msg_puts(FUNCARG(fp, j));
23211 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023212 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 {
23214 if (j)
23215 MSG_PUTS(", ");
23216 MSG_PUTS("...");
23217 }
23218 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023219 if (fp->uf_flags & FC_ABORT)
23220 MSG_PUTS(" abort");
23221 if (fp->uf_flags & FC_RANGE)
23222 MSG_PUTS(" range");
23223 if (fp->uf_flags & FC_DICT)
23224 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023225 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023226 if (p_verbose > 0)
23227 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228}
23229
23230/*
23231 * Find a function by name, return pointer to it in ufuncs.
23232 * Return NULL for unknown function.
23233 */
23234 static ufunc_T *
23235find_func(name)
23236 char_u *name;
23237{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023238 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023240 hi = hash_find(&func_hashtab, name);
23241 if (!HASHITEM_EMPTY(hi))
23242 return HI2UF(hi);
23243 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244}
23245
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023246#if defined(EXITFREE) || defined(PROTO)
23247 void
23248free_all_functions()
23249{
23250 hashitem_T *hi;
23251
23252 /* Need to start all over every time, because func_free() may change the
23253 * hash table. */
23254 while (func_hashtab.ht_used > 0)
23255 for (hi = func_hashtab.ht_array; ; ++hi)
23256 if (!HASHITEM_EMPTY(hi))
23257 {
23258 func_free(HI2UF(hi));
23259 break;
23260 }
23261}
23262#endif
23263
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023264 int
23265translated_function_exists(name)
23266 char_u *name;
23267{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023268 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023269 return find_internal_func(name) >= 0;
23270 return find_func(name) != NULL;
23271}
23272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023273/*
23274 * Return TRUE if a function "name" exists.
23275 */
23276 static int
23277function_exists(name)
23278 char_u *name;
23279{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023280 char_u *nm = name;
23281 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023282 int n = FALSE;
23283
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023284 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23285 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023286 nm = skipwhite(nm);
23287
23288 /* Only accept "funcname", "funcname ", "funcname (..." and
23289 * "funcname(...", not "funcname!...". */
23290 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023291 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023292 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023293 return n;
23294}
23295
Bram Moolenaara1544c02013-05-30 12:35:52 +020023296 char_u *
23297get_expanded_name(name, check)
23298 char_u *name;
23299 int check;
23300{
23301 char_u *nm = name;
23302 char_u *p;
23303
23304 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23305
23306 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023307 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023308 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023309
Bram Moolenaara1544c02013-05-30 12:35:52 +020023310 vim_free(p);
23311 return NULL;
23312}
23313
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023314/*
23315 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023316 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23317 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023318 */
23319 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023320builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023321 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023322 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023323{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023324 char_u *p;
23325
23326 if (!ASCII_ISLOWER(name[0]))
23327 return FALSE;
23328 p = vim_strchr(name, AUTOLOAD_CHAR);
23329 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023330}
23331
Bram Moolenaar05159a02005-02-26 23:04:13 +000023332#if defined(FEAT_PROFILE) || defined(PROTO)
23333/*
23334 * Start profiling function "fp".
23335 */
23336 static void
23337func_do_profile(fp)
23338 ufunc_T *fp;
23339{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023340 int len = fp->uf_lines.ga_len;
23341
23342 if (len == 0)
23343 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023344 fp->uf_tm_count = 0;
23345 profile_zero(&fp->uf_tm_self);
23346 profile_zero(&fp->uf_tm_total);
23347 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023348 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023349 if (fp->uf_tml_total == NULL)
23350 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023351 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023352 if (fp->uf_tml_self == NULL)
23353 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023354 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023355 fp->uf_tml_idx = -1;
23356 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23357 || fp->uf_tml_self == NULL)
23358 return; /* out of memory */
23359
23360 fp->uf_profiling = TRUE;
23361}
23362
23363/*
23364 * Dump the profiling results for all functions in file "fd".
23365 */
23366 void
23367func_dump_profile(fd)
23368 FILE *fd;
23369{
23370 hashitem_T *hi;
23371 int todo;
23372 ufunc_T *fp;
23373 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023374 ufunc_T **sorttab;
23375 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023376
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023377 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023378 if (todo == 0)
23379 return; /* nothing to dump */
23380
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023381 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023382
Bram Moolenaar05159a02005-02-26 23:04:13 +000023383 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23384 {
23385 if (!HASHITEM_EMPTY(hi))
23386 {
23387 --todo;
23388 fp = HI2UF(hi);
23389 if (fp->uf_profiling)
23390 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023391 if (sorttab != NULL)
23392 sorttab[st_len++] = fp;
23393
Bram Moolenaar05159a02005-02-26 23:04:13 +000023394 if (fp->uf_name[0] == K_SPECIAL)
23395 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23396 else
23397 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23398 if (fp->uf_tm_count == 1)
23399 fprintf(fd, "Called 1 time\n");
23400 else
23401 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23402 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23403 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23404 fprintf(fd, "\n");
23405 fprintf(fd, "count total (s) self (s)\n");
23406
23407 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23408 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023409 if (FUNCLINE(fp, i) == NULL)
23410 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023411 prof_func_line(fd, fp->uf_tml_count[i],
23412 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023413 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23414 }
23415 fprintf(fd, "\n");
23416 }
23417 }
23418 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023419
23420 if (sorttab != NULL && st_len > 0)
23421 {
23422 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23423 prof_total_cmp);
23424 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23425 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23426 prof_self_cmp);
23427 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23428 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023429
23430 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023431}
Bram Moolenaar73830342005-02-28 22:48:19 +000023432
23433 static void
23434prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23435 FILE *fd;
23436 ufunc_T **sorttab;
23437 int st_len;
23438 char *title;
23439 int prefer_self; /* when equal print only self time */
23440{
23441 int i;
23442 ufunc_T *fp;
23443
23444 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23445 fprintf(fd, "count total (s) self (s) function\n");
23446 for (i = 0; i < 20 && i < st_len; ++i)
23447 {
23448 fp = sorttab[i];
23449 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23450 prefer_self);
23451 if (fp->uf_name[0] == K_SPECIAL)
23452 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23453 else
23454 fprintf(fd, " %s()\n", fp->uf_name);
23455 }
23456 fprintf(fd, "\n");
23457}
23458
23459/*
23460 * Print the count and times for one function or function line.
23461 */
23462 static void
23463prof_func_line(fd, count, total, self, prefer_self)
23464 FILE *fd;
23465 int count;
23466 proftime_T *total;
23467 proftime_T *self;
23468 int prefer_self; /* when equal print only self time */
23469{
23470 if (count > 0)
23471 {
23472 fprintf(fd, "%5d ", count);
23473 if (prefer_self && profile_equal(total, self))
23474 fprintf(fd, " ");
23475 else
23476 fprintf(fd, "%s ", profile_msg(total));
23477 if (!prefer_self && profile_equal(total, self))
23478 fprintf(fd, " ");
23479 else
23480 fprintf(fd, "%s ", profile_msg(self));
23481 }
23482 else
23483 fprintf(fd, " ");
23484}
23485
23486/*
23487 * Compare function for total time sorting.
23488 */
23489 static int
23490#ifdef __BORLANDC__
23491_RTLENTRYF
23492#endif
23493prof_total_cmp(s1, s2)
23494 const void *s1;
23495 const void *s2;
23496{
23497 ufunc_T *p1, *p2;
23498
23499 p1 = *(ufunc_T **)s1;
23500 p2 = *(ufunc_T **)s2;
23501 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23502}
23503
23504/*
23505 * Compare function for self time sorting.
23506 */
23507 static int
23508#ifdef __BORLANDC__
23509_RTLENTRYF
23510#endif
23511prof_self_cmp(s1, s2)
23512 const void *s1;
23513 const void *s2;
23514{
23515 ufunc_T *p1, *p2;
23516
23517 p1 = *(ufunc_T **)s1;
23518 p2 = *(ufunc_T **)s2;
23519 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23520}
23521
Bram Moolenaar05159a02005-02-26 23:04:13 +000023522#endif
23523
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023524/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023525 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023526 * Return TRUE if a package was loaded.
23527 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023528 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023529script_autoload(name, reload)
23530 char_u *name;
23531 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023532{
23533 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023534 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023535 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023536 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023538 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023539 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023540 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023541 return FALSE;
23542
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023543 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023544
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023545 /* Find the name in the list of previously loaded package names. Skip
23546 * "autoload/", it's always the same. */
23547 for (i = 0; i < ga_loaded.ga_len; ++i)
23548 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23549 break;
23550 if (!reload && i < ga_loaded.ga_len)
23551 ret = FALSE; /* was loaded already */
23552 else
23553 {
23554 /* Remember the name if it wasn't loaded already. */
23555 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23556 {
23557 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23558 tofree = NULL;
23559 }
23560
23561 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023562 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023563 ret = TRUE;
23564 }
23565
23566 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023567 return ret;
23568}
23569
23570/*
23571 * Return the autoload script name for a function or variable name.
23572 * Returns NULL when out of memory.
23573 */
23574 static char_u *
23575autoload_name(name)
23576 char_u *name;
23577{
23578 char_u *p;
23579 char_u *scriptname;
23580
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023581 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023582 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23583 if (scriptname == NULL)
23584 return FALSE;
23585 STRCPY(scriptname, "autoload/");
23586 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023587 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023588 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023589 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023590 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023591 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023592}
23593
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23595
23596/*
23597 * Function given to ExpandGeneric() to obtain the list of user defined
23598 * function names.
23599 */
23600 char_u *
23601get_user_func_name(xp, idx)
23602 expand_T *xp;
23603 int idx;
23604{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023605 static long_u done;
23606 static hashitem_T *hi;
23607 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608
23609 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023611 done = 0;
23612 hi = func_hashtab.ht_array;
23613 }
23614 if (done < func_hashtab.ht_used)
23615 {
23616 if (done++ > 0)
23617 ++hi;
23618 while (HASHITEM_EMPTY(hi))
23619 ++hi;
23620 fp = HI2UF(hi);
23621
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023622 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023623 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023624
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023625 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23626 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627
23628 cat_func_name(IObuff, fp);
23629 if (xp->xp_context != EXPAND_USER_FUNC)
23630 {
23631 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023632 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 STRCAT(IObuff, ")");
23634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 return IObuff;
23636 }
23637 return NULL;
23638}
23639
23640#endif /* FEAT_CMDL_COMPL */
23641
23642/*
23643 * Copy the function name of "fp" to buffer "buf".
23644 * "buf" must be able to hold the function name plus three bytes.
23645 * Takes care of script-local function names.
23646 */
23647 static void
23648cat_func_name(buf, fp)
23649 char_u *buf;
23650 ufunc_T *fp;
23651{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023652 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 {
23654 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023655 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 }
23657 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023658 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659}
23660
23661/*
23662 * ":delfunction {name}"
23663 */
23664 void
23665ex_delfunction(eap)
23666 exarg_T *eap;
23667{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023668 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 char_u *p;
23670 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023671 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672
23673 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023674 name = trans_function_name(&p, eap->skip, 0, &fudi);
23675 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 {
23678 if (fudi.fd_dict != NULL && !eap->skip)
23679 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 if (!ends_excmd(*skipwhite(p)))
23683 {
23684 vim_free(name);
23685 EMSG(_(e_trailing));
23686 return;
23687 }
23688 eap->nextcmd = check_nextcmd(p);
23689 if (eap->nextcmd != NULL)
23690 *p = NUL;
23691
23692 if (!eap->skip)
23693 fp = find_func(name);
23694 vim_free(name);
23695
23696 if (!eap->skip)
23697 {
23698 if (fp == NULL)
23699 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023700 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 return;
23702 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023703 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 {
23705 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23706 return;
23707 }
23708
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023709 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023711 /* Delete the dict item that refers to the function, it will
23712 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023713 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023715 else
23716 func_free(fp);
23717 }
23718}
23719
23720/*
23721 * Free a function and remove it from the list of functions.
23722 */
23723 static void
23724func_free(fp)
23725 ufunc_T *fp;
23726{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023727 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728
23729 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 ga_clear_strings(&(fp->uf_args));
23731 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023732#ifdef FEAT_PROFILE
23733 vim_free(fp->uf_tml_count);
23734 vim_free(fp->uf_tml_total);
23735 vim_free(fp->uf_tml_self);
23736#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023737
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023738 /* remove the function from the function hashtable */
23739 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23740 if (HASHITEM_EMPTY(hi))
23741 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023742 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023743 hash_remove(&func_hashtab, hi);
23744
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023745 vim_free(fp);
23746}
23747
23748/*
23749 * Unreference a Function: decrement the reference count and free it when it
23750 * becomes zero. Only for numbered functions.
23751 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023752 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753func_unref(name)
23754 char_u *name;
23755{
23756 ufunc_T *fp;
23757
23758 if (name != NULL && isdigit(*name))
23759 {
23760 fp = find_func(name);
23761 if (fp == NULL)
23762 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023763 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023764 {
23765 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023766 * when "uf_calls" becomes zero. */
23767 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768 func_free(fp);
23769 }
23770 }
23771}
23772
23773/*
23774 * Count a reference to a Function.
23775 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023776 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023777func_ref(name)
23778 char_u *name;
23779{
23780 ufunc_T *fp;
23781
23782 if (name != NULL && isdigit(*name))
23783 {
23784 fp = find_func(name);
23785 if (fp == NULL)
23786 EMSG2(_(e_intern2), "func_ref()");
23787 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023788 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789 }
23790}
23791
23792/*
23793 * Call a user function.
23794 */
23795 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023796call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 ufunc_T *fp; /* pointer to function */
23798 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023799 typval_T *argvars; /* arguments */
23800 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 linenr_T firstline; /* first line of range */
23802 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804{
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 char_u *save_sourcing_name;
23806 linenr_T save_sourcing_lnum;
23807 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023808 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023809 int save_did_emsg;
23810 static int depth = 0;
23811 dictitem_T *v;
23812 int fixvar_idx = 0; /* index in fixvar[] */
23813 int i;
23814 int ai;
23815 char_u numbuf[NUMBUFLEN];
23816 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023817#ifdef FEAT_PROFILE
23818 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023819 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821
23822 /* If depth of calling is getting too high, don't execute the function */
23823 if (depth >= p_mfd)
23824 {
23825 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023826 rettv->v_type = VAR_NUMBER;
23827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 return;
23829 }
23830 ++depth;
23831
23832 line_breakcheck(); /* check for CTRL-C hit */
23833
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023834 fc = (funccall_T *)alloc(sizeof(funccall_T));
23835 fc->caller = current_funccal;
23836 current_funccal = fc;
23837 fc->func = fp;
23838 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023839 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023840 fc->linenr = 0;
23841 fc->returned = FALSE;
23842 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023844 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23845 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846
Bram Moolenaar33570922005-01-25 22:26:29 +000023847 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023848 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023849 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23850 * each argument variable and saves a lot of time.
23851 */
23852 /*
23853 * Init l: variables.
23854 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023855 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023856 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023857 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023858 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23859 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023860 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023861 name = v->di_key;
23862 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023864 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023865 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023866 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023867 v->di_tv.vval.v_dict = selfdict;
23868 ++selfdict->dv_refcount;
23869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023870
Bram Moolenaar33570922005-01-25 22:26:29 +000023871 /*
23872 * Init a: variables.
23873 * Set a:0 to "argcount".
23874 * Set a:000 to a list with room for the "..." arguments.
23875 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023876 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023877 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023878 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023879 /* Use "name" to avoid a warning from some compiler that checks the
23880 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023881 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023882 name = v->di_key;
23883 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023884 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023885 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023886 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023887 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023888 v->di_tv.vval.v_list = &fc->l_varlist;
23889 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23890 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23891 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023892
23893 /*
23894 * Set a:firstline to "firstline" and a:lastline to "lastline".
23895 * Set a:name to named arguments.
23896 * Set a:N to the "..." arguments.
23897 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023898 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023899 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023900 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023901 (varnumber_T)lastline);
23902 for (i = 0; i < argcount; ++i)
23903 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023904 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023905 if (ai < 0)
23906 /* named argument a:name */
23907 name = FUNCARG(fp, i);
23908 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023910 /* "..." argument a:1, a:2, etc. */
23911 sprintf((char *)numbuf, "%d", ai + 1);
23912 name = numbuf;
23913 }
23914 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23915 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023916 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023917 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23918 }
23919 else
23920 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023921 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23922 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023923 if (v == NULL)
23924 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023925 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023926 }
23927 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023928 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023930 /* Note: the values are copied directly to avoid alloc/free.
23931 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023932 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023933 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023934
23935 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23936 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023937 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23938 fc->l_listitems[ai].li_tv = argvars[i];
23939 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023940 }
23941 }
23942
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 /* Don't redraw while executing the function. */
23944 ++RedrawingDisabled;
23945 save_sourcing_name = sourcing_name;
23946 save_sourcing_lnum = sourcing_lnum;
23947 sourcing_lnum = 1;
23948 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 if (sourcing_name != NULL)
23951 {
23952 if (save_sourcing_name != NULL
23953 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23954 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23955 else
23956 STRCPY(sourcing_name, "function ");
23957 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23958
23959 if (p_verbose >= 12)
23960 {
23961 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023962 verbose_enter_scroll();
23963
Bram Moolenaar555b2802005-05-19 21:08:39 +000023964 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 if (p_verbose >= 14)
23966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023968 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023969 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023970 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971
23972 msg_puts((char_u *)"(");
23973 for (i = 0; i < argcount; ++i)
23974 {
23975 if (i > 0)
23976 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023977 if (argvars[i].v_type == VAR_NUMBER)
23978 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 else
23980 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023981 /* Do not want errors such as E724 here. */
23982 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023983 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023984 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023985 if (s != NULL)
23986 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023987 if (vim_strsize(s) > MSG_BUF_CLEN)
23988 {
23989 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23990 s = buf;
23991 }
23992 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023993 vim_free(tofree);
23994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 }
23996 }
23997 msg_puts((char_u *)")");
23998 }
23999 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024000
24001 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 --no_wait_return;
24003 }
24004 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024005#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024006 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024007 {
24008 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24009 func_do_profile(fp);
24010 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024011 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024012 {
24013 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024014 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024015 profile_zero(&fp->uf_tm_children);
24016 }
24017 script_prof_save(&wait_start);
24018 }
24019#endif
24020
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024022 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 save_did_emsg = did_emsg;
24024 did_emsg = FALSE;
24025
24026 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024027 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24029
24030 --RedrawingDisabled;
24031
24032 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024033 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024035 clear_tv(rettv);
24036 rettv->v_type = VAR_NUMBER;
24037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038 }
24039
Bram Moolenaar05159a02005-02-26 23:04:13 +000024040#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024041 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024042 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024043 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024044 profile_end(&call_start);
24045 profile_sub_wait(&wait_start, &call_start);
24046 profile_add(&fp->uf_tm_total, &call_start);
24047 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024048 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024049 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024050 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24051 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024052 }
24053 }
24054#endif
24055
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 /* when being verbose, mention the return value */
24057 if (p_verbose >= 12)
24058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024060 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024063 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024064 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024065 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024066 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024069 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024070 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024071 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024072 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024073
Bram Moolenaar555b2802005-05-19 21:08:39 +000024074 /* The value may be very long. Skip the middle part, so that we
24075 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024076 * truncate it at the end. Don't want errors such as E724 here. */
24077 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024078 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024079 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024080 if (s != NULL)
24081 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024082 if (vim_strsize(s) > MSG_BUF_CLEN)
24083 {
24084 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24085 s = buf;
24086 }
24087 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024088 vim_free(tofree);
24089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 }
24091 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024092
24093 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 --no_wait_return;
24095 }
24096
24097 vim_free(sourcing_name);
24098 sourcing_name = save_sourcing_name;
24099 sourcing_lnum = save_sourcing_lnum;
24100 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024101#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024102 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024103 script_prof_restore(&wait_start);
24104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105
24106 if (p_verbose >= 12 && sourcing_name != NULL)
24107 {
24108 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024109 verbose_enter_scroll();
24110
Bram Moolenaar555b2802005-05-19 21:08:39 +000024111 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024113
24114 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 --no_wait_return;
24116 }
24117
24118 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024119 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024121
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024122 /* If the a:000 list and the l: and a: dicts are not referenced we can
24123 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024124 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24125 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24126 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24127 {
24128 free_funccal(fc, FALSE);
24129 }
24130 else
24131 {
24132 hashitem_T *hi;
24133 listitem_T *li;
24134 int todo;
24135
24136 /* "fc" is still in use. This can happen when returning "a:000" or
24137 * assigning "l:" to a global variable.
24138 * Link "fc" in the list for garbage collection later. */
24139 fc->caller = previous_funccal;
24140 previous_funccal = fc;
24141
24142 /* Make a copy of the a: variables, since we didn't do that above. */
24143 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24144 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24145 {
24146 if (!HASHITEM_EMPTY(hi))
24147 {
24148 --todo;
24149 v = HI2DI(hi);
24150 copy_tv(&v->di_tv, &v->di_tv);
24151 }
24152 }
24153
24154 /* Make a copy of the a:000 items, since we didn't do that above. */
24155 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24156 copy_tv(&li->li_tv, &li->li_tv);
24157 }
24158}
24159
24160/*
24161 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024162 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024163 */
24164 static int
24165can_free_funccal(fc, copyID)
24166 funccall_T *fc;
24167 int copyID;
24168{
24169 return (fc->l_varlist.lv_copyID != copyID
24170 && fc->l_vars.dv_copyID != copyID
24171 && fc->l_avars.dv_copyID != copyID);
24172}
24173
24174/*
24175 * Free "fc" and what it contains.
24176 */
24177 static void
24178free_funccal(fc, free_val)
24179 funccall_T *fc;
24180 int free_val; /* a: vars were allocated */
24181{
24182 listitem_T *li;
24183
24184 /* The a: variables typevals may not have been allocated, only free the
24185 * allocated variables. */
24186 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24187
24188 /* free all l: variables */
24189 vars_clear(&fc->l_vars.dv_hashtab);
24190
24191 /* Free the a:000 variables if they were allocated. */
24192 if (free_val)
24193 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24194 clear_tv(&li->li_tv);
24195
24196 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197}
24198
24199/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024200 * Add a number variable "name" to dict "dp" with value "nr".
24201 */
24202 static void
24203add_nr_var(dp, v, name, nr)
24204 dict_T *dp;
24205 dictitem_T *v;
24206 char *name;
24207 varnumber_T nr;
24208{
24209 STRCPY(v->di_key, name);
24210 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24211 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24212 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024213 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024214 v->di_tv.vval.v_number = nr;
24215}
24216
24217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 * ":return [expr]"
24219 */
24220 void
24221ex_return(eap)
24222 exarg_T *eap;
24223{
24224 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024225 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 int returning = FALSE;
24227
24228 if (current_funccal == NULL)
24229 {
24230 EMSG(_("E133: :return not inside a function"));
24231 return;
24232 }
24233
24234 if (eap->skip)
24235 ++emsg_skip;
24236
24237 eap->nextcmd = NULL;
24238 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024239 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024240 {
24241 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024242 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024244 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 }
24246 /* It's safer to return also on error. */
24247 else if (!eap->skip)
24248 {
24249 /*
24250 * Return unless the expression evaluation has been cancelled due to an
24251 * aborting error, an interrupt, or an exception.
24252 */
24253 if (!aborting())
24254 returning = do_return(eap, FALSE, TRUE, NULL);
24255 }
24256
24257 /* When skipping or the return gets pending, advance to the next command
24258 * in this line (!returning). Otherwise, ignore the rest of the line.
24259 * Following lines will be ignored by get_func_line(). */
24260 if (returning)
24261 eap->nextcmd = NULL;
24262 else if (eap->nextcmd == NULL) /* no argument */
24263 eap->nextcmd = check_nextcmd(arg);
24264
24265 if (eap->skip)
24266 --emsg_skip;
24267}
24268
24269/*
24270 * Return from a function. Possibly makes the return pending. Also called
24271 * for a pending return at the ":endtry" or after returning from an extra
24272 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024273 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024274 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 * FALSE when the return gets pending.
24276 */
24277 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024278do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 exarg_T *eap;
24280 int reanimate;
24281 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024282 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283{
24284 int idx;
24285 struct condstack *cstack = eap->cstack;
24286
24287 if (reanimate)
24288 /* Undo the return. */
24289 current_funccal->returned = FALSE;
24290
24291 /*
24292 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24293 * not in its finally clause (which then is to be executed next) is found.
24294 * In this case, make the ":return" pending for execution at the ":endtry".
24295 * Otherwise, return normally.
24296 */
24297 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24298 if (idx >= 0)
24299 {
24300 cstack->cs_pending[idx] = CSTP_RETURN;
24301
24302 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024303 /* A pending return again gets pending. "rettv" points to an
24304 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024306 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 else
24308 {
24309 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024310 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024312 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024314 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 {
24316 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024317 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024318 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 else
24320 EMSG(_(e_outofmem));
24321 }
24322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024323 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324
24325 if (reanimate)
24326 {
24327 /* The pending return value could be overwritten by a ":return"
24328 * without argument in a finally clause; reset the default
24329 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024330 current_funccal->rettv->v_type = VAR_NUMBER;
24331 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332 }
24333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024334 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 }
24336 else
24337 {
24338 current_funccal->returned = TRUE;
24339
24340 /* If the return is carried out now, store the return value. For
24341 * a return immediately after reanimation, the value is already
24342 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024343 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024345 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024346 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024348 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 }
24350 }
24351
24352 return idx < 0;
24353}
24354
24355/*
24356 * Free the variable with a pending return value.
24357 */
24358 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024359discard_pending_return(rettv)
24360 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361{
Bram Moolenaar33570922005-01-25 22:26:29 +000024362 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363}
24364
24365/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024366 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 * is an allocated string. Used by report_pending() for verbose messages.
24368 */
24369 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024370get_return_cmd(rettv)
24371 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024373 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024374 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024375 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024377 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024378 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024379 if (s == NULL)
24380 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024381
24382 STRCPY(IObuff, ":return ");
24383 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24384 if (STRLEN(s) + 8 >= IOSIZE)
24385 STRCPY(IObuff + IOSIZE - 4, "...");
24386 vim_free(tofree);
24387 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388}
24389
24390/*
24391 * Get next function line.
24392 * Called by do_cmdline() to get the next line.
24393 * Returns allocated string, or NULL for end of function.
24394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 char_u *
24396get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024397 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024399 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400{
Bram Moolenaar33570922005-01-25 22:26:29 +000024401 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402 ufunc_T *fp = fcp->func;
24403 char_u *retval;
24404 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405
24406 /* If breakpoints have been added/deleted need to check for it. */
24407 if (fcp->dbg_tick != debug_tick)
24408 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024409 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 sourcing_lnum);
24411 fcp->dbg_tick = debug_tick;
24412 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024413#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024414 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024415 func_line_end(cookie);
24416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417
Bram Moolenaar05159a02005-02-26 23:04:13 +000024418 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024419 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24420 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421 retval = NULL;
24422 else
24423 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024424 /* Skip NULL lines (continuation lines). */
24425 while (fcp->linenr < gap->ga_len
24426 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24427 ++fcp->linenr;
24428 if (fcp->linenr >= gap->ga_len)
24429 retval = NULL;
24430 else
24431 {
24432 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24433 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024434#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024435 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024436 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024437#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 }
24440
24441 /* Did we encounter a breakpoint? */
24442 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24443 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024444 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024446 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447 sourcing_lnum);
24448 fcp->dbg_tick = debug_tick;
24449 }
24450
24451 return retval;
24452}
24453
Bram Moolenaar05159a02005-02-26 23:04:13 +000024454#if defined(FEAT_PROFILE) || defined(PROTO)
24455/*
24456 * Called when starting to read a function line.
24457 * "sourcing_lnum" must be correct!
24458 * When skipping lines it may not actually be executed, but we won't find out
24459 * until later and we need to store the time now.
24460 */
24461 void
24462func_line_start(cookie)
24463 void *cookie;
24464{
24465 funccall_T *fcp = (funccall_T *)cookie;
24466 ufunc_T *fp = fcp->func;
24467
24468 if (fp->uf_profiling && sourcing_lnum >= 1
24469 && sourcing_lnum <= fp->uf_lines.ga_len)
24470 {
24471 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024472 /* Skip continuation lines. */
24473 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24474 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024475 fp->uf_tml_execed = FALSE;
24476 profile_start(&fp->uf_tml_start);
24477 profile_zero(&fp->uf_tml_children);
24478 profile_get_wait(&fp->uf_tml_wait);
24479 }
24480}
24481
24482/*
24483 * Called when actually executing a function line.
24484 */
24485 void
24486func_line_exec(cookie)
24487 void *cookie;
24488{
24489 funccall_T *fcp = (funccall_T *)cookie;
24490 ufunc_T *fp = fcp->func;
24491
24492 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24493 fp->uf_tml_execed = TRUE;
24494}
24495
24496/*
24497 * Called when done with a function line.
24498 */
24499 void
24500func_line_end(cookie)
24501 void *cookie;
24502{
24503 funccall_T *fcp = (funccall_T *)cookie;
24504 ufunc_T *fp = fcp->func;
24505
24506 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24507 {
24508 if (fp->uf_tml_execed)
24509 {
24510 ++fp->uf_tml_count[fp->uf_tml_idx];
24511 profile_end(&fp->uf_tml_start);
24512 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024513 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024514 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24515 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024516 }
24517 fp->uf_tml_idx = -1;
24518 }
24519}
24520#endif
24521
Bram Moolenaar071d4272004-06-13 20:20:40 +000024522/*
24523 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024524 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 */
24526 int
24527func_has_ended(cookie)
24528 void *cookie;
24529{
Bram Moolenaar33570922005-01-25 22:26:29 +000024530 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531
24532 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24533 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024534 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 || fcp->returned);
24536}
24537
24538/*
24539 * return TRUE if cookie indicates a function which "abort"s on errors.
24540 */
24541 int
24542func_has_abort(cookie)
24543 void *cookie;
24544{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024545 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546}
24547
24548#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24549typedef enum
24550{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024551 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24552 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24553 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554} var_flavour_T;
24555
24556static var_flavour_T var_flavour __ARGS((char_u *varname));
24557
24558 static var_flavour_T
24559var_flavour(varname)
24560 char_u *varname;
24561{
24562 char_u *p = varname;
24563
24564 if (ASCII_ISUPPER(*p))
24565 {
24566 while (*(++p))
24567 if (ASCII_ISLOWER(*p))
24568 return VAR_FLAVOUR_SESSION;
24569 return VAR_FLAVOUR_VIMINFO;
24570 }
24571 else
24572 return VAR_FLAVOUR_DEFAULT;
24573}
24574#endif
24575
24576#if defined(FEAT_VIMINFO) || defined(PROTO)
24577/*
24578 * Restore global vars that start with a capital from the viminfo file
24579 */
24580 int
24581read_viminfo_varlist(virp, writing)
24582 vir_T *virp;
24583 int writing;
24584{
24585 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024586 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024587 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588
24589 if (!writing && (find_viminfo_parameter('!') != NULL))
24590 {
24591 tab = vim_strchr(virp->vir_line + 1, '\t');
24592 if (tab != NULL)
24593 {
24594 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024595 switch (*tab)
24596 {
24597 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024598#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024599 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024600#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024601 case 'D': type = VAR_DICT; break;
24602 case 'L': type = VAR_LIST; break;
24603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604
24605 tab = vim_strchr(tab, '\t');
24606 if (tab != NULL)
24607 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024608 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024609 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024610 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024612#ifdef FEAT_FLOAT
24613 else if (type == VAR_FLOAT)
24614 (void)string2float(tab + 1, &tv.vval.v_float);
24615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024617 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024618 if (type == VAR_DICT || type == VAR_LIST)
24619 {
24620 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24621
24622 if (etv == NULL)
24623 /* Failed to parse back the dict or list, use it as a
24624 * string. */
24625 tv.v_type = VAR_STRING;
24626 else
24627 {
24628 vim_free(tv.vval.v_string);
24629 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024630 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024631 }
24632 }
24633
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024634 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024635
24636 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024637 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024638 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24639 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640 }
24641 }
24642 }
24643
24644 return viminfo_readline(virp);
24645}
24646
24647/*
24648 * Write global vars that start with a capital to the viminfo file
24649 */
24650 void
24651write_viminfo_varlist(fp)
24652 FILE *fp;
24653{
Bram Moolenaar33570922005-01-25 22:26:29 +000024654 hashitem_T *hi;
24655 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024656 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024657 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024658 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024659 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024660 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661
24662 if (find_viminfo_parameter('!') == NULL)
24663 return;
24664
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024665 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024666
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024667 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024668 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024670 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024672 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024673 this_var = HI2DI(hi);
24674 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024675 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024676 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024677 {
24678 case VAR_STRING: s = "STR"; break;
24679 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024680#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024681 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024682#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024683 case VAR_DICT: s = "DIC"; break;
24684 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024685 default: continue;
24686 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024687 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024688 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024689 if (p != NULL)
24690 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024691 vim_free(tofree);
24692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 }
24694 }
24695}
24696#endif
24697
24698#if defined(FEAT_SESSION) || defined(PROTO)
24699 int
24700store_session_globals(fd)
24701 FILE *fd;
24702{
Bram Moolenaar33570922005-01-25 22:26:29 +000024703 hashitem_T *hi;
24704 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024705 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 char_u *p, *t;
24707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024708 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024709 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024711 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024713 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024714 this_var = HI2DI(hi);
24715 if ((this_var->di_tv.v_type == VAR_NUMBER
24716 || this_var->di_tv.v_type == VAR_STRING)
24717 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024718 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024719 /* Escape special characters with a backslash. Turn a LF and
24720 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024721 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024722 (char_u *)"\\\"\n\r");
24723 if (p == NULL) /* out of memory */
24724 break;
24725 for (t = p; *t != NUL; ++t)
24726 if (*t == '\n')
24727 *t = 'n';
24728 else if (*t == '\r')
24729 *t = 'r';
24730 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024731 this_var->di_key,
24732 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24733 : ' ',
24734 p,
24735 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24736 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024737 || put_eol(fd) == FAIL)
24738 {
24739 vim_free(p);
24740 return FAIL;
24741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 vim_free(p);
24743 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024744#ifdef FEAT_FLOAT
24745 else if (this_var->di_tv.v_type == VAR_FLOAT
24746 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24747 {
24748 float_T f = this_var->di_tv.vval.v_float;
24749 int sign = ' ';
24750
24751 if (f < 0)
24752 {
24753 f = -f;
24754 sign = '-';
24755 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024756 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024757 this_var->di_key, sign, f) < 0)
24758 || put_eol(fd) == FAIL)
24759 return FAIL;
24760 }
24761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 }
24763 }
24764 return OK;
24765}
24766#endif
24767
Bram Moolenaar661b1822005-07-28 22:36:45 +000024768/*
24769 * Display script name where an item was last set.
24770 * Should only be invoked when 'verbose' is non-zero.
24771 */
24772 void
24773last_set_msg(scriptID)
24774 scid_T scriptID;
24775{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024776 char_u *p;
24777
Bram Moolenaar661b1822005-07-28 22:36:45 +000024778 if (scriptID != 0)
24779 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024780 p = home_replace_save(NULL, get_scriptname(scriptID));
24781 if (p != NULL)
24782 {
24783 verbose_enter();
24784 MSG_PUTS(_("\n\tLast set from "));
24785 MSG_PUTS(p);
24786 vim_free(p);
24787 verbose_leave();
24788 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024789 }
24790}
24791
Bram Moolenaard812df62008-11-09 12:46:09 +000024792/*
24793 * List v:oldfiles in a nice way.
24794 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024795 void
24796ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024797 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024798{
24799 list_T *l = vimvars[VV_OLDFILES].vv_list;
24800 listitem_T *li;
24801 int nr = 0;
24802
24803 if (l == NULL)
24804 msg((char_u *)_("No old files"));
24805 else
24806 {
24807 msg_start();
24808 msg_scroll = TRUE;
24809 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24810 {
24811 msg_outnum((long)++nr);
24812 MSG_PUTS(": ");
24813 msg_outtrans(get_tv_string(&li->li_tv));
24814 msg_putchar('\n');
24815 out_flush(); /* output one line at a time */
24816 ui_breakcheck();
24817 }
24818 /* Assume "got_int" was set to truncate the listing. */
24819 got_int = FALSE;
24820
24821#ifdef FEAT_BROWSE_CMD
24822 if (cmdmod.browse)
24823 {
24824 quit_more = FALSE;
24825 nr = prompt_for_number(FALSE);
24826 msg_starthere();
24827 if (nr > 0)
24828 {
24829 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24830 (long)nr);
24831
24832 if (p != NULL)
24833 {
24834 p = expand_env_save(p);
24835 eap->arg = p;
24836 eap->cmdidx = CMD_edit;
24837 cmdmod.browse = FALSE;
24838 do_exedit(eap, NULL);
24839 vim_free(p);
24840 }
24841 }
24842 }
24843#endif
24844 }
24845}
24846
Bram Moolenaar53744302015-07-17 17:38:22 +020024847/* reset v:option_new, v:option_old and v:option_type */
24848 void
24849reset_v_option_vars()
24850{
24851 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24852 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24853 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24854}
24855
24856
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857#endif /* FEAT_EVAL */
24858
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024860#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861
24862#ifdef WIN3264
24863/*
24864 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24865 */
24866static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24867static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24868static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24869
24870/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024871 * Get the short path (8.3) for the filename in "fnamep".
24872 * Only works for a valid file name.
24873 * When the path gets longer "fnamep" is changed and the allocated buffer
24874 * is put in "bufp".
24875 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24876 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 */
24878 static int
24879get_short_pathname(fnamep, bufp, fnamelen)
24880 char_u **fnamep;
24881 char_u **bufp;
24882 int *fnamelen;
24883{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024884 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 char_u *newbuf;
24886
24887 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 l = GetShortPathName(*fnamep, *fnamep, len);
24889 if (l > len - 1)
24890 {
24891 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024892 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 newbuf = vim_strnsave(*fnamep, l);
24894 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896
24897 vim_free(*bufp);
24898 *fnamep = *bufp = newbuf;
24899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024900 /* Really should always succeed, as the buffer is big enough. */
24901 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 }
24903
24904 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024905 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906}
24907
24908/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024909 * Get the short path (8.3) for the filename in "fname". The converted
24910 * path is returned in "bufp".
24911 *
24912 * Some of the directories specified in "fname" may not exist. This function
24913 * will shorten the existing directories at the beginning of the path and then
24914 * append the remaining non-existing path.
24915 *
24916 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024917 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024918 * bufp - Pointer to an allocated buffer for the filename.
24919 * fnamelen - Length of the filename pointed to by fname
24920 *
24921 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 */
24923 static int
24924shortpath_for_invalid_fname(fname, bufp, fnamelen)
24925 char_u **fname;
24926 char_u **bufp;
24927 int *fnamelen;
24928{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024929 char_u *short_fname, *save_fname, *pbuf_unused;
24930 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024932 int old_len, len;
24933 int new_len, sfx_len;
24934 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935
24936 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024937 old_len = *fnamelen;
24938 save_fname = vim_strnsave(*fname, old_len);
24939 pbuf_unused = NULL;
24940 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024942 endp = save_fname + old_len - 1; /* Find the end of the copy */
24943 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024945 /*
24946 * Try shortening the supplied path till it succeeds by removing one
24947 * directory at a time from the tail of the path.
24948 */
24949 len = 0;
24950 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024952 /* go back one path-separator */
24953 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24954 --endp;
24955 if (endp <= save_fname)
24956 break; /* processed the complete path */
24957
24958 /*
24959 * Replace the path separator with a NUL and try to shorten the
24960 * resulting path.
24961 */
24962 ch = *endp;
24963 *endp = 0;
24964 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024965 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024966 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24967 {
24968 retval = FAIL;
24969 goto theend;
24970 }
24971 *endp = ch; /* preserve the string */
24972
24973 if (len > 0)
24974 break; /* successfully shortened the path */
24975
24976 /* failed to shorten the path. Skip the path separator */
24977 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 }
24979
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024980 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024982 /*
24983 * Succeeded in shortening the path. Now concatenate the shortened
24984 * path with the remaining path at the tail.
24985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024987 /* Compute the length of the new path. */
24988 sfx_len = (int)(save_endp - endp) + 1;
24989 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024991 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024993 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024995 /* There is not enough space in the currently allocated string,
24996 * copy it to a buffer big enough. */
24997 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024998 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024999 {
25000 retval = FAIL;
25001 goto theend;
25002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003 }
25004 else
25005 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025006 /* Transfer short_fname to the main buffer (it's big enough),
25007 * unless get_short_pathname() did its work in-place. */
25008 *fname = *bufp = save_fname;
25009 if (short_fname != save_fname)
25010 vim_strncpy(save_fname, short_fname, len);
25011 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025013
25014 /* concat the not-shortened part of the path */
25015 vim_strncpy(*fname + len, endp, sfx_len);
25016 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025018
25019theend:
25020 vim_free(pbuf_unused);
25021 vim_free(save_fname);
25022
25023 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024}
25025
25026/*
25027 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025028 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029 */
25030 static int
25031shortpath_for_partial(fnamep, bufp, fnamelen)
25032 char_u **fnamep;
25033 char_u **bufp;
25034 int *fnamelen;
25035{
25036 int sepcount, len, tflen;
25037 char_u *p;
25038 char_u *pbuf, *tfname;
25039 int hasTilde;
25040
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025041 /* Count up the path separators from the RHS.. so we know which part
25042 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025044 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025045 if (vim_ispathsep(*p))
25046 ++sepcount;
25047
25048 /* Need full path first (use expand_env() to remove a "~/") */
25049 hasTilde = (**fnamep == '~');
25050 if (hasTilde)
25051 pbuf = tfname = expand_env_save(*fnamep);
25052 else
25053 pbuf = tfname = FullName_save(*fnamep, FALSE);
25054
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025055 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025057 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059
25060 if (len == 0)
25061 {
25062 /* Don't have a valid filename, so shorten the rest of the
25063 * path if we can. This CAN give us invalid 8.3 filenames, but
25064 * there's not a lot of point in guessing what it might be.
25065 */
25066 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025067 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069 }
25070
25071 /* Count the paths backward to find the beginning of the desired string. */
25072 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025073 {
25074#ifdef FEAT_MBYTE
25075 if (has_mbyte)
25076 p -= mb_head_off(tfname, p);
25077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078 if (vim_ispathsep(*p))
25079 {
25080 if (sepcount == 0 || (hasTilde && sepcount == 1))
25081 break;
25082 else
25083 sepcount --;
25084 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 if (hasTilde)
25087 {
25088 --p;
25089 if (p >= tfname)
25090 *p = '~';
25091 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 }
25094 else
25095 ++p;
25096
25097 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25098 vim_free(*bufp);
25099 *fnamelen = (int)STRLEN(p);
25100 *bufp = pbuf;
25101 *fnamep = p;
25102
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025103 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104}
25105#endif /* WIN3264 */
25106
25107/*
25108 * Adjust a filename, according to a string of modifiers.
25109 * *fnamep must be NUL terminated when called. When returning, the length is
25110 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025111 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 * When there is an error, *fnamep is set to NULL.
25113 */
25114 int
25115modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25116 char_u *src; /* string with modifiers */
25117 int *usedlen; /* characters after src that are used */
25118 char_u **fnamep; /* file name so far */
25119 char_u **bufp; /* buffer for allocated file name or NULL */
25120 int *fnamelen; /* length of fnamep */
25121{
25122 int valid = 0;
25123 char_u *tail;
25124 char_u *s, *p, *pbuf;
25125 char_u dirname[MAXPATHL];
25126 int c;
25127 int has_fullname = 0;
25128#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025129 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 int has_shortname = 0;
25131#endif
25132
25133repeat:
25134 /* ":p" - full path/file_name */
25135 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25136 {
25137 has_fullname = 1;
25138
25139 valid |= VALID_PATH;
25140 *usedlen += 2;
25141
25142 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25143 if ((*fnamep)[0] == '~'
25144#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25145 && ((*fnamep)[1] == '/'
25146# ifdef BACKSLASH_IN_FILENAME
25147 || (*fnamep)[1] == '\\'
25148# endif
25149 || (*fnamep)[1] == NUL)
25150
25151#endif
25152 )
25153 {
25154 *fnamep = expand_env_save(*fnamep);
25155 vim_free(*bufp); /* free any allocated file name */
25156 *bufp = *fnamep;
25157 if (*fnamep == NULL)
25158 return -1;
25159 }
25160
25161 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025162 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163 {
25164 if (vim_ispathsep(*p)
25165 && p[1] == '.'
25166 && (p[2] == NUL
25167 || vim_ispathsep(p[2])
25168 || (p[2] == '.'
25169 && (p[3] == NUL || vim_ispathsep(p[3])))))
25170 break;
25171 }
25172
25173 /* FullName_save() is slow, don't use it when not needed. */
25174 if (*p != NUL || !vim_isAbsName(*fnamep))
25175 {
25176 *fnamep = FullName_save(*fnamep, *p != NUL);
25177 vim_free(*bufp); /* free any allocated file name */
25178 *bufp = *fnamep;
25179 if (*fnamep == NULL)
25180 return -1;
25181 }
25182
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025183#ifdef WIN3264
25184# if _WIN32_WINNT >= 0x0500
25185 if (vim_strchr(*fnamep, '~') != NULL)
25186 {
25187 /* Expand 8.3 filename to full path. Needed to make sure the same
25188 * file does not have two different names.
25189 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25190 p = alloc(_MAX_PATH + 1);
25191 if (p != NULL)
25192 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025193 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025194 {
25195 vim_free(*bufp);
25196 *bufp = *fnamep = p;
25197 }
25198 else
25199 vim_free(p);
25200 }
25201 }
25202# endif
25203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 /* Append a path separator to a directory. */
25205 if (mch_isdir(*fnamep))
25206 {
25207 /* Make room for one or two extra characters. */
25208 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25209 vim_free(*bufp); /* free any allocated file name */
25210 *bufp = *fnamep;
25211 if (*fnamep == NULL)
25212 return -1;
25213 add_pathsep(*fnamep);
25214 }
25215 }
25216
25217 /* ":." - path relative to the current directory */
25218 /* ":~" - path relative to the home directory */
25219 /* ":8" - shortname path - postponed till after */
25220 while (src[*usedlen] == ':'
25221 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25222 {
25223 *usedlen += 2;
25224 if (c == '8')
25225 {
25226#ifdef WIN3264
25227 has_shortname = 1; /* Postpone this. */
25228#endif
25229 continue;
25230 }
25231 pbuf = NULL;
25232 /* Need full path first (use expand_env() to remove a "~/") */
25233 if (!has_fullname)
25234 {
25235 if (c == '.' && **fnamep == '~')
25236 p = pbuf = expand_env_save(*fnamep);
25237 else
25238 p = pbuf = FullName_save(*fnamep, FALSE);
25239 }
25240 else
25241 p = *fnamep;
25242
25243 has_fullname = 0;
25244
25245 if (p != NULL)
25246 {
25247 if (c == '.')
25248 {
25249 mch_dirname(dirname, MAXPATHL);
25250 s = shorten_fname(p, dirname);
25251 if (s != NULL)
25252 {
25253 *fnamep = s;
25254 if (pbuf != NULL)
25255 {
25256 vim_free(*bufp); /* free any allocated file name */
25257 *bufp = pbuf;
25258 pbuf = NULL;
25259 }
25260 }
25261 }
25262 else
25263 {
25264 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25265 /* Only replace it when it starts with '~' */
25266 if (*dirname == '~')
25267 {
25268 s = vim_strsave(dirname);
25269 if (s != NULL)
25270 {
25271 *fnamep = s;
25272 vim_free(*bufp);
25273 *bufp = s;
25274 }
25275 }
25276 }
25277 vim_free(pbuf);
25278 }
25279 }
25280
25281 tail = gettail(*fnamep);
25282 *fnamelen = (int)STRLEN(*fnamep);
25283
25284 /* ":h" - head, remove "/file_name", can be repeated */
25285 /* Don't remove the first "/" or "c:\" */
25286 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25287 {
25288 valid |= VALID_HEAD;
25289 *usedlen += 2;
25290 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025291 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025292 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 *fnamelen = (int)(tail - *fnamep);
25294#ifdef VMS
25295 if (*fnamelen > 0)
25296 *fnamelen += 1; /* the path separator is part of the path */
25297#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025298 if (*fnamelen == 0)
25299 {
25300 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25301 p = vim_strsave((char_u *)".");
25302 if (p == NULL)
25303 return -1;
25304 vim_free(*bufp);
25305 *bufp = *fnamep = tail = p;
25306 *fnamelen = 1;
25307 }
25308 else
25309 {
25310 while (tail > s && !after_pathsep(s, tail))
25311 mb_ptr_back(*fnamep, tail);
25312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025313 }
25314
25315 /* ":8" - shortname */
25316 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25317 {
25318 *usedlen += 2;
25319#ifdef WIN3264
25320 has_shortname = 1;
25321#endif
25322 }
25323
25324#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025325 /*
25326 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 */
25328 if (has_shortname)
25329 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025330 /* Copy the string if it is shortened by :h and when it wasn't copied
25331 * yet, because we are going to change it in place. Avoids changing
25332 * the buffer name for "%:8". */
25333 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 {
25335 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025336 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 return -1;
25338 vim_free(*bufp);
25339 *bufp = *fnamep = p;
25340 }
25341
25342 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025343 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 if (!has_fullname && !vim_isAbsName(*fnamep))
25345 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025346 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025347 return -1;
25348 }
25349 else
25350 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025351 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352
Bram Moolenaardc935552011-08-17 15:23:23 +020025353 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025355 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356 return -1;
25357
25358 if (l == 0)
25359 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025360 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025362 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 return -1;
25364 }
25365 *fnamelen = l;
25366 }
25367 }
25368#endif /* WIN3264 */
25369
25370 /* ":t" - tail, just the basename */
25371 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25372 {
25373 *usedlen += 2;
25374 *fnamelen -= (int)(tail - *fnamep);
25375 *fnamep = tail;
25376 }
25377
25378 /* ":e" - extension, can be repeated */
25379 /* ":r" - root, without extension, can be repeated */
25380 while (src[*usedlen] == ':'
25381 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25382 {
25383 /* find a '.' in the tail:
25384 * - for second :e: before the current fname
25385 * - otherwise: The last '.'
25386 */
25387 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25388 s = *fnamep - 2;
25389 else
25390 s = *fnamep + *fnamelen - 1;
25391 for ( ; s > tail; --s)
25392 if (s[0] == '.')
25393 break;
25394 if (src[*usedlen + 1] == 'e') /* :e */
25395 {
25396 if (s > tail)
25397 {
25398 *fnamelen += (int)(*fnamep - (s + 1));
25399 *fnamep = s + 1;
25400#ifdef VMS
25401 /* cut version from the extension */
25402 s = *fnamep + *fnamelen - 1;
25403 for ( ; s > *fnamep; --s)
25404 if (s[0] == ';')
25405 break;
25406 if (s > *fnamep)
25407 *fnamelen = s - *fnamep;
25408#endif
25409 }
25410 else if (*fnamep <= tail)
25411 *fnamelen = 0;
25412 }
25413 else /* :r */
25414 {
25415 if (s > tail) /* remove one extension */
25416 *fnamelen = (int)(s - *fnamep);
25417 }
25418 *usedlen += 2;
25419 }
25420
25421 /* ":s?pat?foo?" - substitute */
25422 /* ":gs?pat?foo?" - global substitute */
25423 if (src[*usedlen] == ':'
25424 && (src[*usedlen + 1] == 's'
25425 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25426 {
25427 char_u *str;
25428 char_u *pat;
25429 char_u *sub;
25430 int sep;
25431 char_u *flags;
25432 int didit = FALSE;
25433
25434 flags = (char_u *)"";
25435 s = src + *usedlen + 2;
25436 if (src[*usedlen + 1] == 'g')
25437 {
25438 flags = (char_u *)"g";
25439 ++s;
25440 }
25441
25442 sep = *s++;
25443 if (sep)
25444 {
25445 /* find end of pattern */
25446 p = vim_strchr(s, sep);
25447 if (p != NULL)
25448 {
25449 pat = vim_strnsave(s, (int)(p - s));
25450 if (pat != NULL)
25451 {
25452 s = p + 1;
25453 /* find end of substitution */
25454 p = vim_strchr(s, sep);
25455 if (p != NULL)
25456 {
25457 sub = vim_strnsave(s, (int)(p - s));
25458 str = vim_strnsave(*fnamep, *fnamelen);
25459 if (sub != NULL && str != NULL)
25460 {
25461 *usedlen = (int)(p + 1 - src);
25462 s = do_string_sub(str, pat, sub, flags);
25463 if (s != NULL)
25464 {
25465 *fnamep = s;
25466 *fnamelen = (int)STRLEN(s);
25467 vim_free(*bufp);
25468 *bufp = s;
25469 didit = TRUE;
25470 }
25471 }
25472 vim_free(sub);
25473 vim_free(str);
25474 }
25475 vim_free(pat);
25476 }
25477 }
25478 /* after using ":s", repeat all the modifiers */
25479 if (didit)
25480 goto repeat;
25481 }
25482 }
25483
Bram Moolenaar26df0922014-02-23 23:39:13 +010025484 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25485 {
25486 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25487 if (p == NULL)
25488 return -1;
25489 vim_free(*bufp);
25490 *bufp = *fnamep = p;
25491 *fnamelen = (int)STRLEN(p);
25492 *usedlen += 2;
25493 }
25494
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 return valid;
25496}
25497
25498/*
25499 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25500 * "flags" can be "g" to do a global substitute.
25501 * Returns an allocated string, NULL for error.
25502 */
25503 char_u *
25504do_string_sub(str, pat, sub, flags)
25505 char_u *str;
25506 char_u *pat;
25507 char_u *sub;
25508 char_u *flags;
25509{
25510 int sublen;
25511 regmatch_T regmatch;
25512 int i;
25513 int do_all;
25514 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025515 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 garray_T ga;
25517 char_u *ret;
25518 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025519 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520
25521 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25522 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025523 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524
25525 ga_init2(&ga, 1, 200);
25526
25527 do_all = (flags[0] == 'g');
25528
25529 regmatch.rm_ic = p_ic;
25530 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25531 if (regmatch.regprog != NULL)
25532 {
25533 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025534 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25536 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025537 /* Skip empty match except for first match. */
25538 if (regmatch.startp[0] == regmatch.endp[0])
25539 {
25540 if (zero_width == regmatch.startp[0])
25541 {
25542 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025543 i = MB_PTR2LEN(tail);
25544 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25545 (size_t)i);
25546 ga.ga_len += i;
25547 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025548 continue;
25549 }
25550 zero_width = regmatch.startp[0];
25551 }
25552
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 /*
25554 * Get some space for a temporary buffer to do the substitution
25555 * into. It will contain:
25556 * - The text up to where the match is.
25557 * - The substituted text.
25558 * - The text after the match.
25559 */
25560 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025561 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25563 {
25564 ga_clear(&ga);
25565 break;
25566 }
25567
25568 /* copy the text up to where the match is */
25569 i = (int)(regmatch.startp[0] - tail);
25570 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25571 /* add the substituted text */
25572 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25573 + ga.ga_len + i, TRUE, TRUE, FALSE);
25574 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025575 tail = regmatch.endp[0];
25576 if (*tail == NUL)
25577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025578 if (!do_all)
25579 break;
25580 }
25581
25582 if (ga.ga_data != NULL)
25583 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25584
Bram Moolenaar473de612013-06-08 18:19:48 +020025585 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 }
25587
25588 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25589 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025590 if (p_cpo == empty_option)
25591 p_cpo = save_cpo;
25592 else
25593 /* Darn, evaluating {sub} expression changed the value. */
25594 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595
25596 return ret;
25597}
25598
25599#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */