blob: e12813c2354f8b2500640b3fc552e213b694a238 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000371};
372
373/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_type vv_di.di_tv.v_type
375#define vv_nr vv_di.di_tv.vval.v_number
376#define vv_float vv_di.di_tv.vval.v_float
377#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000378#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200379#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000381
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200382static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000383#define vimvarht vimvardict.dv_hashtab
384
Bram Moolenaara40058a2005-07-11 22:42:07 +0000385static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
386static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
388static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
389static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
391static void list_glob_vars __ARGS((int *first));
392static void list_buf_vars __ARGS((int *first));
393static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000395static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_vim_vars __ARGS((int *first));
398static void list_script_vars __ARGS((int *first));
399static void list_func_vars __ARGS((int *first));
400static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000401static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
402static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100403static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404static void clear_lval __ARGS((lval_T *lp));
405static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
406static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
408static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
409static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
410static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
411static void item_lock __ARGS((typval_T *tv, int deep, int lock));
412static int tv_islocked __ARGS((typval_T *tv));
413
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
415static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000420static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
421static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000423static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000428static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100430static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
431static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
432static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000435static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100440static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000442static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200443static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100458static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200460static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200473static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100489static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100491static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200510static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
515static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200525static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200527#ifdef FEAT_FLOAT
528static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
529#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000532static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
539static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000543static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000552static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000554static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200558static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000561static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200562static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200572static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000573static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000574static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200577static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000578static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100584static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000587static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000601static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100606static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200624#ifdef FEAT_LUA
625static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000631static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200632static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100649static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200657#ifdef FEAT_PYTHON3
658static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
660#ifdef FEAT_PYTHON
661static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000665static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#ifdef FEAT_FLOAT
678static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
679#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200680static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100682static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000685static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000687static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200692static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000695static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000696static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000697static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000698static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200700static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000701static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100703#ifdef FEAT_CRYPT
704static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
705#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000706static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200707static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
710static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200711static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000712#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000714static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000715static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
719static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
721#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000722static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200723static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724#ifdef HAVE_STRFTIME
725static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
727static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200733static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200734static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000740static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200741static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200743static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000744static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000745static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000746static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000747static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000748static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000750static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200751#ifdef FEAT_FLOAT
752static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
754#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#ifdef FEAT_FLOAT
759static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200762static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200763static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100764static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100768static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000775static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100779static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780
Bram Moolenaar493c1782014-05-28 14:34:46 +0200781static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000782static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static int get_env_len __ARGS((char_u **arg));
784static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000786static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
787#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
788#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
789 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000790static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200793static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000794static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static typval_T *alloc_tv __ARGS((void));
796static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void init_tv __ARGS((typval_T *varp));
798static long get_tv_number __ARGS((typval_T *varp));
799static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000800static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static char_u *get_tv_string __ARGS((typval_T *varp));
802static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000803static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100804static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
805static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000806static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
807static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
808static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000809static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
810static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200812static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
813static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200814static int var_check_func_name __ARGS((char_u *name, int new_var));
815static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000817static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
819static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
820static int eval_fname_script __ARGS((char_u *p));
821static int eval_fname_sid __ARGS((char_u *p));
822static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static ufunc_T *find_func __ARGS((char_u *name));
824static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200825static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
827static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000828static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
829static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
830static int
831# ifdef __BORLANDC__
832 _RTLENTRYF
833# endif
834 prof_total_cmp __ARGS((const void *s1, const void *s2));
835static int
836# ifdef __BORLANDC__
837 _RTLENTRYF
838# endif
839 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200841static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000842static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000843static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000844static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000845static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000846static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
847static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000849static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
850static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000851static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000852static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200854static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200855static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200857
858#ifdef EBCDIC
859static int compare_func_name __ARGS((const void *s1, const void *s2));
860static void sortFunctions __ARGS(());
861#endif
862
Bram Moolenaar33570922005-01-25 22:26:29 +0000863/*
864 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000865 */
866 void
867eval_init()
868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 int i;
870 struct vimvar *p;
871
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200872 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
873 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200874 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
878 for (i = 0; i < VV_LEN; ++i)
879 {
880 p = &vimvars[i];
881 STRCPY(p->vv_di.di_key, p->vv_name);
882 if (p->vv_flags & VV_RO)
883 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
884 else if (p->vv_flags & VV_RO_SBX)
885 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
886 else
887 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000888
889 /* add to v: scope dict, unless the value is not always available */
890 if (p->vv_type != VAR_UNKNOWN)
891 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000893 /* add to compat scope dict */
894 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000896 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100897 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200898 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200899 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
902 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100903 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904 */
905 sortFunctions();
906#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000907}
908
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909#if defined(EXITFREE) || defined(PROTO)
910 void
911eval_clear()
912{
913 int i;
914 struct vimvar *p;
915
916 for (i = 0; i < VV_LEN; ++i)
917 {
918 p = &vimvars[i];
919 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000921 vim_free(p->vv_str);
922 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000923 }
924 else if (p->vv_di.di_tv.v_type == VAR_LIST)
925 {
926 list_unref(p->vv_list);
927 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000928 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929 }
930 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000931 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932 hash_clear(&compat_hashtab);
933
Bram Moolenaard9fba312005-06-26 22:34:35 +0000934 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200936 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100937# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938
939 /* global variables */
940 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000941
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000942 /* autoloaded script names */
943 ga_clear_strings(&ga_loaded);
944
Bram Moolenaarcca74132013-09-25 21:00:28 +0200945 /* Script-local variables. First clear all the variables and in a second
946 * loop free the scriptvar_T, because a variable in one script might hold
947 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200948 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200949 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200950 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200951 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200952 ga_clear(&ga_scripts);
953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000954 /* unreferenced lists and dicts */
955 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000956
957 /* functions */
958 free_all_functions();
959 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000960}
961#endif
962
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 * Return the name of the executed function.
965 */
966 char_u *
967func_name(cookie)
968 void *cookie;
969{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000970 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Return the address holding the next breakpoint line for a funccall cookie.
975 */
976 linenr_T *
977func_breakpoint(cookie)
978 void *cookie;
979{
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983/*
984 * Return the address holding the debug tick for a funccall cookie.
985 */
986 int *
987func_dbg_tick(cookie)
988 void *cookie;
989{
Bram Moolenaar33570922005-01-25 22:26:29 +0000990 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
993/*
994 * Return the nesting level for a funccall cookie.
995 */
996 int
997func_level(cookie)
998 void *cookie;
999{
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001004funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001006/* pointer to list of previously used funccal, still around because some
1007 * item in it is still being used. */
1008funccall_T *previous_funccal = NULL;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/*
1011 * Return TRUE when a function was ended by a ":return" command.
1012 */
1013 int
1014current_func_returned()
1015{
1016 return current_funccal->returned;
1017}
1018
1019
1020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Set an internal variable to a string value. Creates the variable if it does
1022 * not already exist.
1023 */
1024 void
1025set_internal_string_var(name, value)
1026 char_u *name;
1027 char_u *value;
1028{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001029 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 val = vim_strsave(value);
1033 if (val != NULL)
1034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001038 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 }
1042}
1043
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001045static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046static char_u *redir_endp = NULL;
1047static char_u *redir_varname = NULL;
1048
1049/*
1050 * Start recording command output to a variable
1051 * Returns OK if successfully completed the setup. FAIL otherwise.
1052 */
1053 int
1054var_redir_start(name, append)
1055 char_u *name;
1056 int append; /* append to an existing variable */
1057{
1058 int save_emsg;
1059 int err;
1060 typval_T tv;
1061
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001063 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 {
1065 EMSG(_(e_invarg));
1066 return FAIL;
1067 }
1068
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 redir_varname = vim_strsave(name);
1071 if (redir_varname == NULL)
1072 return FAIL;
1073
1074 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1075 if (redir_lval == NULL)
1076 {
1077 var_redir_stop();
1078 return FAIL;
1079 }
1080
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001081 /* The output is stored in growarray "redir_ga" until redirection ends. */
1082 ga_init2(&redir_ga, (int)sizeof(char), 500);
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001085 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1088 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_endp != NULL && *redir_endp != NUL)
1091 /* Trailing characters are present after the variable name */
1092 EMSG(_(e_trailing));
1093 else
1094 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 var_redir_stop();
1097 return FAIL;
1098 }
1099
1100 /* check if we can write to the variable: set it to or append an empty
1101 * string */
1102 save_emsg = did_emsg;
1103 did_emsg = FALSE;
1104 tv.v_type = VAR_STRING;
1105 tv.vval.v_string = (char_u *)"";
1106 if (append)
1107 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1108 else
1109 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001110 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001112 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (err)
1114 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 var_redir_stop();
1117 return FAIL;
1118 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
1120 return OK;
1121}
1122
1123/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 * Append "value[value_len]" to the variable set by var_redir_start().
1125 * The actual appending is postponed until redirection ends, because the value
1126 * appended may in fact be the string we write to, changing it may cause freed
1127 * memory to be used:
1128 * :redir => foo
1129 * :let foo
1130 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
1139 if (redir_lval == NULL)
1140 return;
1141
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001147 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 {
1149 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 }
1152 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154}
1155
1156/*
1157 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 */
1160 void
1161var_redir_stop()
1162{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 typval_T tv;
1164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 if (redir_lval != NULL)
1166 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* If there was no error: assign the text to the variable. */
1168 if (redir_endp != NULL)
1169 {
1170 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1171 tv.v_type = VAR_STRING;
1172 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001173 /* Call get_lval() again, if it's inside a Dict or List it may
1174 * have changed. */
1175 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1178 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1179 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001181
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 /* free the collected output */
1183 vim_free(redir_ga.ga_data);
1184 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 vim_free(redir_lval);
1187 redir_lval = NULL;
1188 }
1189 vim_free(redir_varname);
1190 redir_varname = NULL;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193# if defined(FEAT_MBYTE) || defined(PROTO)
1194 int
1195eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1196 char_u *enc_from;
1197 char_u *enc_to;
1198 char_u *fname_from;
1199 char_u *fname_to;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1204 set_vim_var_string(VV_CC_TO, enc_to, -1);
1205 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1206 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1207 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_CC_FROM, NULL, -1);
1210 set_vim_var_string(VV_CC_TO, NULL, -1);
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1213
1214 if (err)
1215 return FAIL;
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1221 int
1222eval_printexpr(fname, args)
1223 char_u *fname;
1224 char_u *args;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, fname, -1);
1229 set_vim_var_string(VV_CMDARG, args, -1);
1230 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1231 err = TRUE;
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_CMDARG, NULL, -1);
1234
1235 if (err)
1236 {
1237 mch_remove(fname);
1238 return FAIL;
1239 }
1240 return OK;
1241}
1242# endif
1243
1244# if defined(FEAT_DIFF) || defined(PROTO)
1245 void
1246eval_diff(origfile, newfile, outfile)
1247 char_u *origfile;
1248 char_u *newfile;
1249 char_u *outfile;
1250{
1251 int err = FALSE;
1252
1253 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1254 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1255 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1256 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260}
1261
1262 void
1263eval_patch(origfile, difffile, outfile)
1264 char_u *origfile;
1265 char_u *difffile;
1266 char_u *outfile;
1267{
1268 int err;
1269
1270 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1271 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1272 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1273 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1276 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1277}
1278# endif
1279
1280/*
1281 * Top level evaluation function, returning a boolean.
1282 * Sets "error" to TRUE if there was an error.
1283 * Return TRUE or FALSE.
1284 */
1285 int
1286eval_to_bool(arg, error, nextcmd, skip)
1287 char_u *arg;
1288 int *error;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int retval = FALSE;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 else
1300 {
1301 *error = FALSE;
1302 if (!skip)
1303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001304 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 }
1308 if (skip)
1309 --emsg_skip;
1310
1311 return retval;
1312}
1313
1314/*
1315 * Top level evaluation function, returning a string. If "skip" is TRUE,
1316 * only parsing to "nextcmd" is done, without reporting errors. Return
1317 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1318 */
1319 char_u *
1320eval_to_string_skip(arg, nextcmd, skip)
1321 char_u *arg;
1322 char_u **nextcmd;
1323 int skip; /* only parse, don't execute */
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *retval;
1327
1328 if (skip)
1329 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 retval = NULL;
1332 else
1333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 retval = vim_strsave(get_tv_string(&tv));
1335 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 if (skip)
1338 --emsg_skip;
1339
1340 return retval;
1341}
1342
1343/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344 * Skip over an expression at "*pp".
1345 * Return FAIL for an error, OK otherwise.
1346 */
1347 int
1348skip_expr(pp)
1349 char_u **pp;
1350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352
1353 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001355}
1356
1357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 * When "convert" is TRUE convert a List into a sequence of lines and convert
1360 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 * Return pointer to allocated memory, or NULL for failure.
1362 */
1363 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *arg;
1366 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001372#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 {
1382 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001383 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001384 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001385 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001386 if (tv.vval.v_list->lv_len > 0)
1387 ga_append(&ga, NL);
1388 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 ga_append(&ga, NUL);
1390 retval = (char_u *)ga.ga_data;
1391 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001392#ifdef FEAT_FLOAT
1393 else if (convert && tv.v_type == VAR_FLOAT)
1394 {
1395 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1396 retval = vim_strsave(numbuf);
1397 }
1398#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 else
1400 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403
1404 return retval;
1405}
1406
1407/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001408 * Call eval_to_string() without using current local variables and using
1409 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 */
1411 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *arg;
1414 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 char_u *retval;
1418 void *save_funccalp;
1419
1420 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 if (use_sandbox)
1422 ++sandbox;
1423 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001425 if (use_sandbox)
1426 --sandbox;
1427 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 restore_funccal(save_funccalp);
1429 return retval;
1430}
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432/*
1433 * Top level evaluation function, returning a number.
1434 * Evaluates "expr" silently.
1435 * Returns -1 for an error.
1436 */
1437 int
1438eval_to_number(expr)
1439 char_u *expr;
1440{
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001443 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 ++emsg_off;
1446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = -1;
1449 else
1450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454 --emsg_off;
1455
1456 return retval;
1457}
1458
Bram Moolenaara40058a2005-07-11 22:42:07 +00001459/*
1460 * Prepare v: variable "idx" to be used.
1461 * Save the current typeval in "save_tv".
1462 * When not used yet add the variable to the v: hashtable.
1463 */
1464 static void
1465prepare_vimvar(idx, save_tv)
1466 int idx;
1467 typval_T *save_tv;
1468{
1469 *save_tv = vimvars[idx].vv_tv;
1470 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1471 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1472}
1473
1474/*
1475 * Restore v: variable "idx" to typeval "save_tv".
1476 * When no longer defined, remove the variable from the v: hashtable.
1477 */
1478 static void
1479restore_vimvar(idx, save_tv)
1480 int idx;
1481 typval_T *save_tv;
1482{
1483 hashitem_T *hi;
1484
Bram Moolenaara40058a2005-07-11 22:42:07 +00001485 vimvars[idx].vv_tv = *save_tv;
1486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1487 {
1488 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1489 if (HASHITEM_EMPTY(hi))
1490 EMSG2(_(e_intern2), "restore_vimvar()");
1491 else
1492 hash_remove(&vimvarht, hi);
1493 }
1494}
1495
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001496#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497/*
1498 * Evaluate an expression to a list with suggestions.
1499 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001500 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 */
1502 list_T *
1503eval_spell_expr(badword, expr)
1504 char_u *badword;
1505 char_u *expr;
1506{
1507 typval_T save_val;
1508 typval_T rettv;
1509 list_T *list = NULL;
1510 char_u *p = skipwhite(expr);
1511
1512 /* Set "v:val" to the bad word. */
1513 prepare_vimvar(VV_VAL, &save_val);
1514 vimvars[VV_VAL].vv_type = VAR_STRING;
1515 vimvars[VV_VAL].vv_str = badword;
1516 if (p_verbose == 0)
1517 ++emsg_off;
1518
1519 if (eval1(&p, &rettv, TRUE) == OK)
1520 {
1521 if (rettv.v_type != VAR_LIST)
1522 clear_tv(&rettv);
1523 else
1524 list = rettv.vval.v_list;
1525 }
1526
1527 if (p_verbose == 0)
1528 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001529 restore_vimvar(VV_VAL, &save_val);
1530
1531 return list;
1532}
1533
1534/*
1535 * "list" is supposed to contain two items: a word and a number. Return the
1536 * word in "pp" and the number as the return value.
1537 * Return -1 if anything isn't right.
1538 * Used to get the good word and score from the eval_spell_expr() result.
1539 */
1540 int
1541get_spellword(list, pp)
1542 list_T *list;
1543 char_u **pp;
1544{
1545 listitem_T *li;
1546
1547 li = list->lv_first;
1548 if (li == NULL)
1549 return -1;
1550 *pp = get_tv_string(&li->li_tv);
1551
1552 li = li->li_next;
1553 if (li == NULL)
1554 return -1;
1555 return get_tv_number(&li->li_tv);
1556}
1557#endif
1558
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001559/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 * Top level evaluation function.
1561 * Returns an allocated typval_T with the result.
1562 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563 */
1564 typval_T *
1565eval_expr(arg, nextcmd)
1566 char_u *arg;
1567 char_u **nextcmd;
1568{
1569 typval_T *tv;
1570
1571 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001572 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001573 {
1574 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001575 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001576 }
1577
1578 return tv;
1579}
1580
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 * Uses argv[argc] for the function arguments. Only Number and String
1585 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001588 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 char_u *func;
1591 int argc;
1592 char_u **argv;
1593 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar33570922005-01-25 22:26:29 +00001597 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 long n;
1599 int len;
1600 int i;
1601 int doesrange;
1602 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001605 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 for (i = 0; i < argc; i++)
1610 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001611 /* Pass a NULL or empty argument as an empty string */
1612 if (argv[i] == NULL || *argv[i] == NUL)
1613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001614 argvars[i].v_type = VAR_STRING;
1615 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001616 continue;
1617 }
1618
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001619 if (str_arg_only)
1620 len = 0;
1621 else
1622 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001623 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (len != 0 && len == (int)STRLEN(argv[i]))
1625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001626 argvars[i].v_type = VAR_NUMBER;
1627 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 else
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_STRING;
1632 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
1635
1636 if (safe)
1637 {
1638 save_funccalp = save_funccal();
1639 ++sandbox;
1640 }
1641
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1643 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (safe)
1647 {
1648 --sandbox;
1649 restore_funccal(save_funccalp);
1650 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 vim_free(argvars);
1652
1653 if (ret == FAIL)
1654 clear_tv(rettv);
1655
1656 return ret;
1657}
1658
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001659/*
1660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1676 return -1;
1677
1678 retval = get_tv_number_chk(&rettv, NULL);
1679 clear_tv(&rettv);
1680 return retval;
1681}
1682
1683#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1684 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1685
Bram Moolenaar4f688582007-07-24 12:34:30 +00001686# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001688 * Call vimL function "func" and return the result as a string.
1689 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 * Uses argv[argc] for the function arguments.
1691 */
1692 void *
1693call_func_retstr(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001700 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001702 /* All arguments are passed as strings, no conversion to number. */
1703 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001704 return NULL;
1705
1706 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 return retval;
1709}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001712/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001713 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001715 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 */
1717 void *
1718call_func_retlist(func, argc, argv, safe)
1719 char_u *func;
1720 int argc;
1721 char_u **argv;
1722 int safe; /* use the sandbox */
1723{
1724 typval_T rettv;
1725
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001726 /* All arguments are passed as strings, no conversion to number. */
1727 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 return NULL;
1729
1730 if (rettv.v_type != VAR_LIST)
1731 {
1732 clear_tv(&rettv);
1733 return NULL;
1734 }
1735
1736 return rettv.vval.v_list;
1737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
1739
1740/*
1741 * Save the current function call pointer, and set it to NULL.
1742 * Used when executing autocommands and for ":source".
1743 */
1744 void *
1745save_funccal()
1746{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 current_funccal = NULL;
1750 return (void *)fc;
1751}
1752
1753 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754restore_funccal(vfc)
1755 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757 funccall_T *fc = (funccall_T *)vfc;
1758
1759 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762#if defined(FEAT_PROFILE) || defined(PROTO)
1763/*
1764 * Prepare profiling for entering a child or something else that is not
1765 * counted for the script/function itself.
1766 * Should always be called in pair with prof_child_exit().
1767 */
1768 void
1769prof_child_enter(tm)
1770 proftime_T *tm; /* place to store waittime */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 profile_start(&fc->prof_child);
1776 script_prof_save(tm);
1777}
1778
1779/*
1780 * Take care of time spent in a child.
1781 * Should always be called after prof_child_enter().
1782 */
1783 void
1784prof_child_exit(tm)
1785 proftime_T *tm; /* where waittime was stored */
1786{
1787 funccall_T *fc = current_funccal;
1788
1789 if (fc != NULL && fc->func->uf_profiling)
1790 {
1791 profile_end(&fc->prof_child);
1792 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1793 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1794 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1795 }
1796 script_prof_restore(tm);
1797}
1798#endif
1799
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#ifdef FEAT_FOLDING
1802/*
1803 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1804 * it in "*cp". Doesn't give error messages.
1805 */
1806 int
1807eval_foldexpr(arg, cp)
1808 char_u *arg;
1809 int *cp;
1810{
Bram Moolenaar33570922005-01-25 22:26:29 +00001811 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 int retval;
1813 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001814 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1815 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 ++sandbox;
1820 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 retval = 0;
1824 else
1825 {
1826 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 if (tv.v_type == VAR_NUMBER)
1828 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001829 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a string, check if there is a non-digit before
1834 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (!VIM_ISDIGIT(*s) && *s != '-')
1837 *cp = *s++;
1838 retval = atol((char *)s);
1839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001843 if (use_sandbox)
1844 --sandbox;
1845 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 return retval;
1848}
1849#endif
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 * ":let" list all variable values
1853 * ":let var1 var2" list variable values
1854 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 * ":let var += expr" assignment command.
1856 * ":let var -= expr" assignment command.
1857 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
1860 void
1861ex_let(eap)
1862 exarg_T *eap;
1863{
1864 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 int var_count = 0;
1869 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001871 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 argend = skip_var_list(arg, &var_count, &semicolon);
1875 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001877 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1878 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001879 expr = skipwhite(argend);
1880 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1881 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001883 /*
1884 * ":let" without "=": list variables
1885 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 if (*arg == '[')
1887 EMSG(_(e_invarg));
1888 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001894 list_glob_vars(&first);
1895 list_buf_vars(&first);
1896 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001897#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001899#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_script_vars(&first);
1901 list_func_vars(&first);
1902 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 eap->nextcmd = check_nextcmd(arg);
1905 }
1906 else
1907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 op[0] = '=';
1909 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1913 op[0] = *expr; /* +=, -= or .= */
1914 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 else
1917 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (eap->skip)
1920 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 {
1924 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 --emsg_skip;
1927 }
1928 else if (i != FAIL)
1929 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 }
1935}
1936
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937/*
1938 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1939 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 * When "nextchars" is not NULL it points to a string with characters that
1941 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1942 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 * Returns OK or FAIL;
1944 */
1945 static int
1946ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1947 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 int copy; /* copy values from "tv", don't move */
1950 int semicolon; /* from skip_var_list() */
1951 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953{
1954 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 listitem_T *item;
1958 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959
1960 if (*arg != '[')
1961 {
1962 /*
1963 * ":let var = expr" or ":for var in list"
1964 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 return OK;
1968 }
1969
1970 /*
1971 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1972 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001973 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 {
1975 EMSG(_(e_listreq));
1976 return FAIL;
1977 }
1978
1979 i = list_len(l);
1980 if (semicolon == 0 && var_count < i)
1981 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001982 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return FAIL;
1984 }
1985 if (var_count - semicolon > i)
1986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001987 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 return FAIL;
1989 }
1990
1991 item = l->lv_first;
1992 while (*arg != ']')
1993 {
1994 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 item = item->li_next;
1997 if (arg == NULL)
1998 return FAIL;
1999
2000 arg = skipwhite(arg);
2001 if (*arg == ';')
2002 {
2003 /* Put the rest of the list (may be empty) in the var after ';'.
2004 * Create a new list for this. */
2005 l = list_alloc();
2006 if (l == NULL)
2007 return FAIL;
2008 while (item != NULL)
2009 {
2010 list_append_tv(l, &item->li_tv);
2011 item = item->li_next;
2012 }
2013
2014 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002015 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 ltv.vval.v_list = l;
2017 l->lv_refcount = 1;
2018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2020 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 clear_tv(&ltv);
2022 if (arg == NULL)
2023 return FAIL;
2024 break;
2025 }
2026 else if (*arg != ',' && *arg != ']')
2027 {
2028 EMSG2(_(e_intern2), "ex_let_vars()");
2029 return FAIL;
2030 }
2031 }
2032
2033 return OK;
2034}
2035
2036/*
2037 * Skip over assignable variable "var" or list of variables "[var, var]".
2038 * Used for ":let varvar = expr" and ":for varvar in expr".
2039 * For "[var, var]" increment "*var_count" for each variable.
2040 * for "[var, var; var]" set "semicolon".
2041 * Return NULL for an error.
2042 */
2043 static char_u *
2044skip_var_list(arg, var_count, semicolon)
2045 char_u *arg;
2046 int *var_count;
2047 int *semicolon;
2048{
2049 char_u *p, *s;
2050
2051 if (*arg == '[')
2052 {
2053 /* "[var, var]": find the matching ']'. */
2054 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002055 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2058 s = skip_var_one(p);
2059 if (s == p)
2060 {
2061 EMSG2(_(e_invarg2), p);
2062 return NULL;
2063 }
2064 ++*var_count;
2065
2066 p = skipwhite(s);
2067 if (*p == ']')
2068 break;
2069 else if (*p == ';')
2070 {
2071 if (*semicolon == 1)
2072 {
2073 EMSG(_("Double ; in list of variables"));
2074 return NULL;
2075 }
2076 *semicolon = 1;
2077 }
2078 else if (*p != ',')
2079 {
2080 EMSG2(_(e_invarg2), p);
2081 return NULL;
2082 }
2083 }
2084 return p + 1;
2085 }
2086 else
2087 return skip_var_one(arg);
2088}
2089
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002091 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002092 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 static char_u *
2095skip_var_one(arg)
2096 char_u *arg;
2097{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 if (*arg == '@' && arg[1] != NUL)
2099 return arg + 2;
2100 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2101 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102}
2103
Bram Moolenaara7043832005-01-21 11:56:39 +00002104/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 * List variables for hashtab "ht" with prefix "prefix".
2106 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002114{
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 hashitem_T *hi;
2116 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 int todo;
2118
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2121 {
2122 if (!HASHITEM_EMPTY(hi))
2123 {
2124 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 di = HI2DI(hi);
2126 if (empty || di->di_tv.v_type != VAR_STRING
2127 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 }
2130 }
2131}
2132
2133/*
2134 * List global variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_glob_vars(first)
2138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
2143/*
2144 * List buffer variables.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_buf_vars(first)
2148 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_win_vars(first)
2165 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002167 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171#ifdef FEAT_WINDOWS
2172/*
2173 * List tab page variables.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_tab_vars(first)
2177 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181}
2182#endif
2183
Bram Moolenaara7043832005-01-21 11:56:39 +00002184/*
2185 * List Vim variables.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_vim_vars(first)
2189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192}
2193
2194/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195 * List script-local variables, if there is a script.
2196 */
2197 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_script_vars(first)
2199 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200{
2201 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2203 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204}
2205
2206/*
2207 * List function variables, if there is a function.
2208 */
2209 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210list_func_vars(first)
2211 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212{
2213 if (current_funccal != NULL)
2214 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216}
2217
2218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 * List variables in "arg".
2220 */
2221 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 exarg_T *eap;
2224 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226{
2227 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 char_u *name_start;
2231 char_u *arg_subsc;
2232 char_u *tofree;
2233 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234
2235 while (!ends_excmd(*arg) && !got_int)
2236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2241 {
2242 emsg_severe = TRUE;
2243 EMSG(_(e_trailing));
2244 break;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* get_name_len() takes care of expanding curly braces */
2250 name_start = name = arg;
2251 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2252 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* This is mainly to keep test 49 working: when expanding
2255 * curly braces fails overrule the exception error message. */
2256 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 emsg_severe = TRUE;
2259 EMSG2(_(e_invarg2), arg);
2260 break;
2261 }
2262 error = TRUE;
2263 }
2264 else
2265 {
2266 if (tofree != NULL)
2267 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002268 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 else
2271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* handle d.key, l[idx], f(expr) */
2273 arg_subsc = arg;
2274 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 case 'g': list_glob_vars(first); break;
2283 case 'b': list_buf_vars(first); break;
2284 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'v': list_vim_vars(first); break;
2289 case 's': list_script_vars(first); break;
2290 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 default:
2292 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 }
2295 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 {
2297 char_u numbuf[NUMBUFLEN];
2298 char_u *tf;
2299 int c;
2300 char_u *s;
2301
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002302 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 c = *arg;
2304 *arg = NUL;
2305 list_one_var_a((char_u *)"",
2306 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 tv.v_type,
2308 s == NULL ? (char_u *)"" : s,
2309 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 *arg = c;
2311 vim_free(tf);
2312 }
2313 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317
2318 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320
2321 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
2323
2324 return arg;
2325}
2326
2327/*
2328 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2329 * Returns a pointer to the char just after the var name.
2330 * Returns NULL if there is an error.
2331 */
2332 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002335 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002337 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339{
2340 int c1;
2341 char_u *name;
2342 char_u *p;
2343 char_u *arg_end = NULL;
2344 int len;
2345 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347
2348 /*
2349 * ":let $VAR = expr": Set environment variable.
2350 */
2351 if (*arg == '$')
2352 {
2353 /* Find the end of the name. */
2354 ++arg;
2355 name = arg;
2356 len = get_env_len(&arg);
2357 if (len == 0)
2358 EMSG2(_(e_invarg2), name - 1);
2359 else
2360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 if (op != NULL && (*op == '+' || *op == '-'))
2362 EMSG2(_(e_letwrong), op);
2363 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002366 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 {
2368 c1 = name[len];
2369 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 p = get_tv_string_chk(tv);
2371 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 {
2373 int mustfree = FALSE;
2374 char_u *s = vim_getenv(name, &mustfree);
2375
2376 if (s != NULL)
2377 {
2378 p = tofree = concat_str(s, p);
2379 if (mustfree)
2380 vim_free(s);
2381 }
2382 }
2383 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 if (STRICMP(name, "HOME") == 0)
2387 init_homedir();
2388 else if (didset_vim && STRICMP(name, "VIM") == 0)
2389 didset_vim = FALSE;
2390 else if (didset_vimruntime
2391 && STRICMP(name, "VIMRUNTIME") == 0)
2392 didset_vimruntime = FALSE;
2393 arg_end = arg;
2394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
2398 }
2399 }
2400
2401 /*
2402 * ":let &option = expr": Set option value.
2403 * ":let &l:option = expr": Set local option value.
2404 * ":let &g:option = expr": Set global option value.
2405 */
2406 else if (*arg == '&')
2407 {
2408 /* Find the end of the name. */
2409 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002410 if (p == NULL || (endchars != NULL
2411 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 EMSG(_(e_letunexp));
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 long n;
2416 int opt_type;
2417 long numval;
2418 char_u *stringval = NULL;
2419 char_u *s;
2420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 c1 = *p;
2422 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423
2424 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 s = get_tv_string_chk(tv); /* != NULL if number or string */
2426 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 opt_type = get_option_value(arg, &numval,
2429 &stringval, opt_flags);
2430 if ((opt_type == 1 && *op == '.')
2431 || (opt_type == 0 && *op != '.'))
2432 EMSG2(_(e_letwrong), op);
2433 else
2434 {
2435 if (opt_type == 1) /* number */
2436 {
2437 if (*op == '+')
2438 n = numval + n;
2439 else
2440 n = numval - n;
2441 }
2442 else if (opt_type == 0 && stringval != NULL) /* string */
2443 {
2444 s = concat_str(stringval, s);
2445 vim_free(stringval);
2446 stringval = s;
2447 }
2448 }
2449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 if (s != NULL)
2451 {
2452 set_option_value(arg, n, s, opt_flags);
2453 arg_end = p;
2454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 }
2458 }
2459
2460 /*
2461 * ":let @r = expr": Set register contents.
2462 */
2463 else if (*arg == '@')
2464 {
2465 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 if (op != NULL && (*op == '+' || *op == '-'))
2467 EMSG2(_(e_letwrong), op);
2468 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 char_u *s;
2475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 p = get_tv_string_chk(tv);
2477 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002479 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (s != NULL)
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 vim_free(s);
2484 }
2485 }
2486 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 arg_end = arg + 1;
2490 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 }
2493 }
2494
2495 /*
2496 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002499 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002503 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2507 EMSG(_(e_letunexp));
2508 else
2509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 arg_end = p;
2512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516
2517 else
2518 EMSG2(_(e_invarg2), arg);
2519
2520 return arg_end;
2521}
2522
2523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2525 */
2526 static int
2527check_changedtick(arg)
2528 char_u *arg;
2529{
2530 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2531 {
2532 EMSG2(_(e_readonlyvar), arg);
2533 return TRUE;
2534 }
2535 return FALSE;
2536}
2537
2538/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Get an lval: variable, Dict item or List item that can be assigned a value
2540 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2541 * "name.key", "name.key[expr]" etc.
2542 * Indexing only works if "name" is an existing List or Dictionary.
2543 * "name" points to the start of the name.
2544 * If "rettv" is not NULL it points to the value to be assigned.
2545 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2546 * wrong; must end in space or cmd separator.
2547 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 * flags:
2549 * GLV_QUIET: do not give error messages
2550 * GLV_NO_AUTOLOAD: do not use script autoloading
2551 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002553 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 */
2556 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 typval_T *rettv;
2560 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 int unlet;
2562 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002564 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 char_u *expr_start, *expr_end;
2568 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 dictitem_T *v;
2570 typval_T var1;
2571 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002577 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581
2582 if (skip)
2583 {
2584 /* When skipping just find the end of the name. */
2585 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002586 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 }
2588
2589 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (expr_start != NULL)
2592 {
2593 /* Don't expand the name when we already know there is an error. */
2594 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2595 && *p != '[' && *p != '.')
2596 {
2597 EMSG(_(e_trailing));
2598 return NULL;
2599 }
2600
2601 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2602 if (lp->ll_exp_name == NULL)
2603 {
2604 /* Report an invalid expression in braces, unless the
2605 * expression evaluation has been cancelled due to an
2606 * aborting error, an interrupt, or an exception. */
2607 if (!aborting() && !quiet)
2608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002609 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 EMSG2(_(e_invarg2), name);
2611 return NULL;
2612 }
2613 }
2614 lp->ll_name = lp->ll_exp_name;
2615 }
2616 else
2617 lp->ll_name = name;
2618
2619 /* Without [idx] or .key we are done. */
2620 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2621 return p;
2622
2623 cc = *p;
2624 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002625 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (v == NULL && !quiet)
2627 EMSG2(_(e_undefvar), lp->ll_name);
2628 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 if (v == NULL)
2630 return NULL;
2631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /*
2633 * Loop until no more [idx] or .key is following.
2634 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2639 && !(lp->ll_tv->v_type == VAR_DICT
2640 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_("E689: Can only index a List or Dictionary"));
2644 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E708: [:] must come last"));
2650 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 len = -1;
2654 if (*p == '.')
2655 {
2656 key = p + 1;
2657 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2658 ;
2659 if (len == 0)
2660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_(e_emptykey));
2663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = key + len;
2666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 else
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (*p == ':')
2672 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 else
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 empty1 = FALSE;
2676 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 if (get_tv_string_chk(&var1) == NULL)
2679 {
2680 /* not a number or string */
2681 clear_tv(&var1);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /* Optionally get the second index [ :expr]. */
2687 if (*p == ':')
2688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (rettv != NULL && (rettv->v_type != VAR_LIST
2698 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (!empty1)
2703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706 p = skipwhite(p + 1);
2707 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 else
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002718 if (get_tv_string_chk(&var2) == NULL)
2719 {
2720 /* not a number or string */
2721 if (!empty1)
2722 clear_tv(&var1);
2723 clear_tv(&var2);
2724 return NULL;
2725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (!quiet)
2735 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742
2743 /* Skip to past ']'. */
2744 ++p;
2745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
2749 if (len == -1)
2750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*key == NUL)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 lp->ll_list = NULL;
2762 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 /* When assigning to a scope dictionary check that a function and
2766 * variable name is valid (only variable name unless it is l: or
2767 * g: dictionary). Disallow overwriting a builtin function. */
2768 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 int prevval;
2771 int wrong;
2772
2773 if (len != -1)
2774 {
2775 prevval = key[len];
2776 key[len] = NUL;
2777 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002778 else
2779 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2781 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 || !valid_varname(key);
2784 if (len != -1)
2785 key[len] = prevval;
2786 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 return NULL;
2788 }
2789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 /* Can't add "v:" variable. */
2793 if (lp->ll_dict == &vimvardict)
2794 {
2795 EMSG2(_(e_illvar), name);
2796 return NULL;
2797 }
2798
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 p = NULL;
2816 break;
2817 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002819 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 return NULL;
2821
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 else
2827 {
2828 /*
2829 * Get the number and item for the only or first index of the List.
2830 */
2831 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 else
2834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002835 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 clear_tv(&var1);
2837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_list = lp->ll_tv->vval.v_list;
2840 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2841 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002843 if (lp->ll_n1 < 0)
2844 {
2845 lp->ll_n1 = 0;
2846 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2847 }
2848 }
2849 if (lp->ll_li == NULL)
2850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 if (!quiet)
2854 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857
2858 /*
2859 * May need to find the item or absolute index for the second
2860 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 * When no index given: "lp->ll_empty2" is TRUE.
2862 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 {
2873 if (!quiet)
2874 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2881 if (lp->ll_n1 < 0)
2882 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2883 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 {
2885 if (!quiet)
2886 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return p;
2896}
2897
2898/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 */
2901 static void
2902clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904{
2905 vim_free(lp->ll_exp_name);
2906 vim_free(lp->ll_newkey);
2907}
2908
2909/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 listitem_T *ri;
2924 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925
2926 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 cc = *endp;
2931 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002938 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 if ((di == NULL
2942 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2943 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2944 FALSE)))
2945 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 set_var(lp->ll_name, &tv, FALSE);
2947 clear_tv(&tv);
2948 }
2949 }
2950 else
2951 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002955 else if (tv_check_lock(lp->ll_newkey == NULL
2956 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002957 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 else if (lp->ll_range)
2960 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 listitem_T *ll_li = lp->ll_li;
2962 int ll_n1 = lp->ll_n1;
2963
2964 /*
2965 * Check whether any of the list items is locked
2966 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002967 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002970 return;
2971 ri = ri->li_next;
2972 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2973 break;
2974 ll_li = ll_li->li_next;
2975 ++ll_n1;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /*
2979 * Assign the List values to the list items.
2980 */
2981 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2985 else
2986 {
2987 clear_tv(&lp->ll_li->li_tv);
2988 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = ri->li_next;
2991 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2992 break;
2993 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002996 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = NULL;
2999 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 lp->ll_li = lp->ll_li->li_next;
3003 ++lp->ll_n1;
3004 }
3005 if (ri != NULL)
3006 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 else if (lp->ll_empty2
3008 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 : lp->ll_n1 != lp->ll_n2)
3010 EMSG(_("E711: List value has not enough items"));
3011 }
3012 else
3013 {
3014 /*
3015 * Assign to a List or Dictionary item.
3016 */
3017 if (lp->ll_newkey != NULL)
3018 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 if (op != NULL && *op != '=')
3020 {
3021 EMSG2(_(e_letwrong), op);
3022 return;
3023 }
3024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 if (di == NULL)
3028 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3030 {
3031 vim_free(di);
3032 return;
3033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 else if (op != NULL && *op != '=')
3037 {
3038 tv_op(lp->ll_tv, rettv, op);
3039 return;
3040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 /*
3045 * Assign the value to the variable or list item.
3046 */
3047 if (copy)
3048 copy_tv(rettv, lp->ll_tv);
3049 else
3050 {
3051 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003052 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 }
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056}
3057
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3060 * Returns OK or FAIL.
3061 */
3062 static int
3063tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 typval_T *tv1;
3065 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 char_u *op;
3067{
3068 long n;
3069 char_u numbuf[NUMBUFLEN];
3070 char_u *s;
3071
3072 /* Can't do anything with a Funcref or a Dict on the right. */
3073 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3074 {
3075 switch (tv1->v_type)
3076 {
3077 case VAR_DICT:
3078 case VAR_FUNC:
3079 break;
3080
3081 case VAR_LIST:
3082 if (*op != '+' || tv2->v_type != VAR_LIST)
3083 break;
3084 /* List += List */
3085 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3086 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3087 return OK;
3088
3089 case VAR_NUMBER:
3090 case VAR_STRING:
3091 if (tv2->v_type == VAR_LIST)
3092 break;
3093 if (*op == '+' || *op == '-')
3094 {
3095 /* nr += nr or nr -= nr*/
3096 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097#ifdef FEAT_FLOAT
3098 if (tv2->v_type == VAR_FLOAT)
3099 {
3100 float_T f = n;
3101
3102 if (*op == '+')
3103 f += tv2->vval.v_float;
3104 else
3105 f -= tv2->vval.v_float;
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_FLOAT;
3108 tv1->vval.v_float = f;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#endif
3112 {
3113 if (*op == '+')
3114 n += get_tv_number(tv2);
3115 else
3116 n -= get_tv_number(tv2);
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_NUMBER;
3119 tv1->vval.v_number = n;
3120 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 }
3122 else
3123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124 if (tv2->v_type == VAR_FLOAT)
3125 break;
3126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 /* str .= str */
3128 s = get_tv_string(tv1);
3129 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_STRING;
3132 tv1->vval.v_string = s;
3133 }
3134 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135
3136#ifdef FEAT_FLOAT
3137 case VAR_FLOAT:
3138 {
3139 float_T f;
3140
3141 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3142 && tv2->v_type != VAR_NUMBER
3143 && tv2->v_type != VAR_STRING))
3144 break;
3145 if (tv2->v_type == VAR_FLOAT)
3146 f = tv2->vval.v_float;
3147 else
3148 f = get_tv_number(tv2);
3149 if (*op == '+')
3150 tv1->vval.v_float += f;
3151 else
3152 tv1->vval.v_float -= f;
3153 }
3154 return OK;
3155#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 }
3157 }
3158
3159 EMSG2(_(e_letwrong), op);
3160 return FAIL;
3161}
3162
3163/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * Add a watcher to a list.
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
3171 lw->lw_next = l->lv_watch;
3172 l->lv_watch = lw;
3173}
3174
3175/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003176 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 * No warning when it isn't found...
3178 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003179 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 list_T *l;
3182 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 lwp = &l->lv_watch;
3187 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3188 {
3189 if (lw == lwrem)
3190 {
3191 *lwp = lw->lw_next;
3192 break;
3193 }
3194 lwp = &lw->lw_next;
3195 }
3196}
3197
3198/*
3199 * Just before removing an item from a list: advance watchers to the next
3200 * item.
3201 */
3202 static void
3203list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 list_T *l;
3205 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208
3209 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3210 if (lw->lw_item == item)
3211 lw->lw_item = item->li_next;
3212}
3213
3214/*
3215 * Evaluate the expression used in a ":for var in expr" command.
3216 * "arg" points to "var".
3217 * Set "*errp" to TRUE for an error, FALSE otherwise;
3218 * Return a pointer that holds the info. Null when there is an error.
3219 */
3220 void *
3221eval_for_line(arg, errp, nextcmdp, skip)
3222 char_u *arg;
3223 int *errp;
3224 char_u **nextcmdp;
3225 int skip;
3226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 typval_T tv;
3230 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 *errp = TRUE; /* default: there is an error */
3233
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 if (fi == NULL)
3236 return NULL;
3237
3238 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3239 if (expr == NULL)
3240 return fi;
3241
3242 expr = skipwhite(expr);
3243 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3244 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003245 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 return fi;
3247 }
3248
3249 if (skip)
3250 ++emsg_skip;
3251 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3252 {
3253 *errp = FALSE;
3254 if (!skip)
3255 {
3256 l = tv.vval.v_list;
3257 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 clear_tv(&tv);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 else
3263 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003264 /* No need to increment the refcount, it's already set for the
3265 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 fi->fi_list = l;
3267 list_add_watch(l, &fi->fi_lw);
3268 fi->fi_lw.lw_item = l->lv_first;
3269 }
3270 }
3271 }
3272 if (skip)
3273 --emsg_skip;
3274
3275 return fi;
3276}
3277
3278/*
3279 * Use the first item in a ":for" list. Advance to the next.
3280 * Assign the values to the variable (list). "arg" points to the first one.
3281 * Return TRUE when a valid item was found, FALSE when at end of list or
3282 * something wrong.
3283 */
3284 int
3285next_for_item(fi_void, arg)
3286 void *fi_void;
3287 char_u *arg;
3288{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292
3293 item = fi->fi_lw.lw_item;
3294 if (item == NULL)
3295 result = FALSE;
3296 else
3297 {
3298 fi->fi_lw.lw_item = item->li_next;
3299 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3300 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3301 }
3302 return result;
3303}
3304
3305/*
3306 * Free the structure used to store info used by ":for".
3307 */
3308 void
3309free_for_info(fi_void)
3310 void *fi_void;
3311{
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003314 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317 list_unref(fi->fi_list);
3318 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 vim_free(fi);
3320}
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3323
3324 void
3325set_context_for_expression(xp, arg, cmdidx)
3326 expand_T *xp;
3327 char_u *arg;
3328 cmdidx_T cmdidx;
3329{
3330 int got_eq = FALSE;
3331 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 if (cmdidx == CMD_let)
3335 {
3336 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003337 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 {
3339 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (vim_iswhite(*p))
3345 break;
3346 }
3347 return;
3348 }
3349 }
3350 else
3351 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3352 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 while ((xp->xp_pattern = vim_strpbrk(arg,
3354 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3355 {
3356 c = *xp->xp_pattern;
3357 if (c == '&')
3358 {
3359 c = xp->xp_pattern[1];
3360 if (c == '&')
3361 {
3362 ++xp->xp_pattern;
3363 xp->xp_context = cmdidx != CMD_let || got_eq
3364 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3365 }
3366 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003369 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3370 xp->xp_pattern += 2;
3371
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else if (c == '$')
3375 {
3376 /* environment variable */
3377 xp->xp_context = EXPAND_ENV_VARS;
3378 }
3379 else if (c == '=')
3380 {
3381 got_eq = TRUE;
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003384 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 && xp->xp_context == EXPAND_FUNCTIONS
3386 && vim_strchr(xp->xp_pattern, '(') == NULL)
3387 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 break;
3390 }
3391 else if (cmdidx != CMD_let || got_eq)
3392 {
3393 if (c == '"') /* string */
3394 {
3395 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3396 if (c == '\\' && xp->xp_pattern[1] != NUL)
3397 ++xp->xp_pattern;
3398 xp->xp_context = EXPAND_NOTHING;
3399 }
3400 else if (c == '\'') /* literal string */
3401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003402 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3404 /* skip */ ;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '|')
3408 {
3409 if (xp->xp_pattern[1] == '|')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_EXPRESSION;
3413 }
3414 else
3415 xp->xp_context = EXPAND_COMMANDS;
3416 }
3417 else
3418 xp->xp_context = EXPAND_EXPRESSION;
3419 }
3420 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003421 /* Doesn't look like something valid, expand as an expression
3422 * anyway. */
3423 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 arg = xp->xp_pattern;
3425 if (*arg != NUL)
3426 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3427 /* skip */ ;
3428 }
3429 xp->xp_pattern = arg;
3430}
3431
3432#endif /* FEAT_CMDL_COMPL */
3433
3434/*
3435 * ":1,25call func(arg1, arg2)" function call.
3436 */
3437 void
3438ex_call(eap)
3439 exarg_T *eap;
3440{
3441 char_u *arg = eap->arg;
3442 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003444 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 linenr_T lnum;
3448 int doesrange;
3449 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003452 if (eap->skip)
3453 {
3454 /* trans_function_name() doesn't work well when skipping, use eval0()
3455 * instead to skip to any following command, e.g. for:
3456 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003457 ++emsg_skip;
3458 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3459 clear_tv(&rettv);
3460 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 return;
3462 }
3463
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003464 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003465 if (fudi.fd_newkey != NULL)
3466 {
3467 /* Still need to give an error message for missing key. */
3468 EMSG2(_(e_dictkey), fudi.fd_newkey);
3469 vim_free(fudi.fd_newkey);
3470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 if (tofree == NULL)
3472 return;
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 /* Increase refcount on dictionary, it could get deleted when evaluating
3475 * the arguments. */
3476 if (fudi.fd_dict != NULL)
3477 ++fudi.fd_dict->dv_refcount;
3478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003480 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003481 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar532c7802005-01-27 14:44:31 +00003483 /* Skip white space to allow ":call func ()". Not good, but required for
3484 * backward compatibility. */
3485 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (*startarg != '(')
3489 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003490 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 goto end;
3492 }
3493
3494 /*
3495 * When skipping, evaluate the function once, to find the end of the
3496 * arguments.
3497 * When the function takes a range, this is discovered after the first
3498 * call, and the loop is broken.
3499 */
3500 if (eap->skip)
3501 {
3502 ++emsg_skip;
3503 lnum = eap->line2; /* do it once, also with an invalid range */
3504 }
3505 else
3506 lnum = eap->line1;
3507 for ( ; lnum <= eap->line2; ++lnum)
3508 {
3509 if (!eap->skip && eap->addr_count > 0)
3510 {
3511 curwin->w_cursor.lnum = lnum;
3512 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003513#ifdef FEAT_VIRTUALEDIT
3514 curwin->w_cursor.coladd = 0;
3515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003519 eap->line1, eap->line2, &doesrange,
3520 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 failed = TRUE;
3523 break;
3524 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003525
3526 /* Handle a function returning a Funcref, Dictionary or List. */
3527 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3528 {
3529 failed = TRUE;
3530 break;
3531 }
3532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (doesrange || eap->skip)
3535 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (aborting())
3542 break;
3543 }
3544 if (eap->skip)
3545 --emsg_skip;
3546
3547 if (!failed)
3548 {
3549 /* Check for trailing illegal characters and a following command. */
3550 if (!ends_excmd(*arg))
3551 {
3552 emsg_severe = TRUE;
3553 EMSG(_(e_trailing));
3554 }
3555 else
3556 eap->nextcmd = check_nextcmd(arg);
3557 }
3558
3559end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
3564/*
3565 * ":unlet[!] var1 ... " command.
3566 */
3567 void
3568ex_unlet(eap)
3569 exarg_T *eap;
3570{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 ex_unletlock(eap, eap->arg, 0);
3572}
3573
3574/*
3575 * ":lockvar" and ":unlockvar" commands
3576 */
3577 void
3578ex_lockvar(eap)
3579 exarg_T *eap;
3580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
3599ex_unletlock(eap, argstart, deep)
3600 exarg_T *eap;
3601 char_u *argstart;
3602 int deep;
3603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 int forceit;
3658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
3683 int ll_n1 = lp->ll_n1;
3684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003721do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di->di_tv.vval.v_dict;
3743 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hi = hash_find(ht, varname);
3745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003747 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 if (var_check_fixed(di->di_flags, name, FALSE)
3749 || var_check_ro(di->di_flags, name, FALSE)
3750 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 return FAIL;
3752 delete_var(ht, hi);
3753 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 if (forceit)
3757 return OK;
3758 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return FAIL;
3760}
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762/*
3763 * Lock or unlock variable indicated by "lp".
3764 * "deep" is the levels to go (-1 for unlimited);
3765 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3766 */
3767 static int
3768do_lock_var(lp, name_end, deep, lock)
3769 lval_T *lp;
3770 char_u *name_end;
3771 int deep;
3772 int lock;
3773{
3774 int ret = OK;
3775 int cc;
3776 dictitem_T *di;
3777
3778 if (deep == 0) /* nothing to do */
3779 return OK;
3780
3781 if (lp->ll_tv == NULL)
3782 {
3783 cc = *name_end;
3784 *name_end = NUL;
3785
3786 /* Normal name or expanded name. */
3787 if (check_changedtick(lp->ll_name))
3788 ret = FAIL;
3789 else
3790 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003791 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792 if (di == NULL)
3793 ret = FAIL;
3794 else
3795 {
3796 if (lock)
3797 di->di_flags |= DI_FLAGS_LOCK;
3798 else
3799 di->di_flags &= ~DI_FLAGS_LOCK;
3800 item_lock(&di->di_tv, deep, lock);
3801 }
3802 }
3803 *name_end = cc;
3804 }
3805 else if (lp->ll_range)
3806 {
3807 listitem_T *li = lp->ll_li;
3808
3809 /* (un)lock a range of List items. */
3810 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3811 {
3812 item_lock(&li->li_tv, deep, lock);
3813 li = li->li_next;
3814 ++lp->ll_n1;
3815 }
3816 }
3817 else if (lp->ll_list != NULL)
3818 /* (un)lock a List item. */
3819 item_lock(&lp->ll_li->li_tv, deep, lock);
3820 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003821 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 item_lock(&lp->ll_di->di_tv, deep, lock);
3823
3824 return ret;
3825}
3826
3827/*
3828 * Lock or unlock an item. "deep" is nr of levels to go.
3829 */
3830 static void
3831item_lock(tv, deep, lock)
3832 typval_T *tv;
3833 int deep;
3834 int lock;
3835{
3836 static int recurse = 0;
3837 list_T *l;
3838 listitem_T *li;
3839 dict_T *d;
3840 hashitem_T *hi;
3841 int todo;
3842
3843 if (recurse >= DICT_MAXNEST)
3844 {
3845 EMSG(_("E743: variable nested too deep for (un)lock"));
3846 return;
3847 }
3848 if (deep == 0)
3849 return;
3850 ++recurse;
3851
3852 /* lock/unlock the item itself */
3853 if (lock)
3854 tv->v_lock |= VAR_LOCKED;
3855 else
3856 tv->v_lock &= ~VAR_LOCKED;
3857
3858 switch (tv->v_type)
3859 {
3860 case VAR_LIST:
3861 if ((l = tv->vval.v_list) != NULL)
3862 {
3863 if (lock)
3864 l->lv_lock |= VAR_LOCKED;
3865 else
3866 l->lv_lock &= ~VAR_LOCKED;
3867 if (deep < 0 || deep > 1)
3868 /* recursive: lock/unlock the items the List contains */
3869 for (li = l->lv_first; li != NULL; li = li->li_next)
3870 item_lock(&li->li_tv, deep - 1, lock);
3871 }
3872 break;
3873 case VAR_DICT:
3874 if ((d = tv->vval.v_dict) != NULL)
3875 {
3876 if (lock)
3877 d->dv_lock |= VAR_LOCKED;
3878 else
3879 d->dv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 {
3882 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003883 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3885 {
3886 if (!HASHITEM_EMPTY(hi))
3887 {
3888 --todo;
3889 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3890 }
3891 }
3892 }
3893 }
3894 }
3895 --recurse;
3896}
3897
Bram Moolenaara40058a2005-07-11 22:42:07 +00003898/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003899 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3900 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003901 */
3902 static int
3903tv_islocked(tv)
3904 typval_T *tv;
3905{
3906 return (tv->v_lock & VAR_LOCKED)
3907 || (tv->v_type == VAR_LIST
3908 && tv->vval.v_list != NULL
3909 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3910 || (tv->v_type == VAR_DICT
3911 && tv->vval.v_dict != NULL
3912 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3913}
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3916/*
3917 * Delete all "menutrans_" variables.
3918 */
3919 void
3920del_menutrans_vars()
3921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003926 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 {
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3933 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 }
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938#endif
3939
3940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3941
3942/*
3943 * Local string buffer for the next two functions to store a variable name
3944 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3945 * get_user_var_name().
3946 */
3947
3948static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3949
3950static char_u *varnamebuf = NULL;
3951static int varnamebuflen = 0;
3952
3953/*
3954 * Function to concatenate a prefix and a variable name.
3955 */
3956 static char_u *
3957cat_prefix_varname(prefix, name)
3958 int prefix;
3959 char_u *name;
3960{
3961 int len;
3962
3963 len = (int)STRLEN(name) + 3;
3964 if (len > varnamebuflen)
3965 {
3966 vim_free(varnamebuf);
3967 len += 10; /* some additional space */
3968 varnamebuf = alloc(len);
3969 if (varnamebuf == NULL)
3970 {
3971 varnamebuflen = 0;
3972 return NULL;
3973 }
3974 varnamebuflen = len;
3975 }
3976 *varnamebuf = prefix;
3977 varnamebuf[1] = ':';
3978 STRCPY(varnamebuf + 2, name);
3979 return varnamebuf;
3980}
3981
3982/*
3983 * Function given to ExpandGeneric() to obtain the list of user defined
3984 * (global/buffer/window/built-in) variable names.
3985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u *
3987get_user_var_name(xp, idx)
3988 expand_T *xp;
3989 int idx;
3990{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 static long_u gdone;
3992 static long_u bdone;
3993 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994#ifdef FEAT_WINDOWS
3995 static long_u tdone;
3996#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static int vidx;
3998 static hashitem_T *hi;
3999 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 tdone = 0;
4006#endif
4007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004008
4009 /* Global variables */
4010 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 else
4015 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 while (HASHITEM_EMPTY(hi))
4017 ++hi;
4018 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4019 return cat_prefix_varname('g', hi->hi_key);
4020 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022
4023 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004024 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return (char_u *)"b:changedtick";
4039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
4041 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004042 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004045 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 else
4048 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 while (HASHITEM_EMPTY(hi))
4050 ++hi;
4051 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054#ifdef FEAT_WINDOWS
4055 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004056 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 if (tdone < ht->ht_used)
4058 {
4059 if (tdone++ == 0)
4060 hi = ht->ht_array;
4061 else
4062 ++hi;
4063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 return cat_prefix_varname('t', hi->hi_key);
4066 }
4067#endif
4068
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 /* v: variables */
4070 if (vidx < VV_LEN)
4071 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 vim_free(varnamebuf);
4074 varnamebuf = NULL;
4075 varnamebuflen = 0;
4076 return NULL;
4077}
4078
4079#endif /* FEAT_CMDL_COMPL */
4080
4081/*
4082 * types for expressions.
4083 */
4084typedef enum
4085{
4086 TYPE_UNKNOWN = 0
4087 , TYPE_EQUAL /* == */
4088 , TYPE_NEQUAL /* != */
4089 , TYPE_GREATER /* > */
4090 , TYPE_GEQUAL /* >= */
4091 , TYPE_SMALLER /* < */
4092 , TYPE_SEQUAL /* <= */
4093 , TYPE_MATCH /* =~ */
4094 , TYPE_NOMATCH /* !~ */
4095} exptype_T;
4096
4097/*
4098 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4101 */
4102
4103/*
4104 * Handle zero level expression.
4105 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004107 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * Return OK or FAIL.
4109 */
4110 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 char_u **nextcmd;
4115 int evaluate;
4116{
4117 int ret;
4118 char_u *p;
4119
4120 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (ret == FAIL || !ends_excmd(*p))
4123 {
4124 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /*
4127 * Report the invalid expression unless the expression evaluation has
4128 * been cancelled due to an aborting error, an interrupt, or an
4129 * exception.
4130 */
4131 if (!aborting())
4132 EMSG2(_(e_invexpr2), arg);
4133 ret = FAIL;
4134 }
4135 if (nextcmd != NULL)
4136 *nextcmd = check_nextcmd(p);
4137
4138 return ret;
4139}
4140
4141/*
4142 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004143 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004148 * Note: "rettv.v_lock" is not set.
4149 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
4158 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 if ((*arg)[0] == '?')
4168 {
4169 result = FALSE;
4170 if (evaluate)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
4173
4174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 /*
4182 * Get the second variable.
4183 */
4184 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 /*
4189 * Check for the ":".
4190 */
4191 if ((*arg)[0] != ':')
4192 {
4193 EMSG(_("E109: Missing ':' after '?'"));
4194 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197 }
4198
4199 /*
4200 * Get the third variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
4203 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4204 {
4205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208 }
4209 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212
4213 return OK;
4214}
4215
4216/*
4217 * Handle first level expression:
4218 * expr2 || expr2 || expr2 logical OR
4219 *
4220 * "arg" must point to the first non-white of the expression.
4221 * "arg" is advanced to the next non-white after the recognized expression.
4222 *
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int evaluate;
4230{
Bram Moolenaar33570922005-01-25 22:26:29 +00004231 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 long result;
4233 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 /*
4237 * Get the first variable.
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
4241
4242 /*
4243 * Repeat until there is no following "||".
4244 */
4245 first = TRUE;
4246 result = FALSE;
4247 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4248 {
4249 if (evaluate && first)
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (error)
4255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 first = FALSE;
4257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 2);
4263 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4264 return FAIL;
4265
4266 /*
4267 * Compute the result.
4268 */
4269 if (evaluate && !result)
4270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (error)
4275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 if (evaluate)
4278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 rettv->v_type = VAR_NUMBER;
4280 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283
4284 return OK;
4285}
4286
4287/*
4288 * Handle second level expression:
4289 * expr3 && expr3 && expr3 logical AND
4290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int evaluate;
4301{
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 long result;
4304 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /*
4308 * Get the first variable.
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return FAIL;
4312
4313 /*
4314 * Repeat until there is no following "&&".
4315 */
4316 first = TRUE;
4317 result = TRUE;
4318 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4319 {
4320 if (evaluate && first)
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 first = FALSE;
4328 }
4329
4330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 2);
4334 if (eval4(arg, &var2, evaluate && result) == FAIL)
4335 return FAIL;
4336
4337 /*
4338 * Compute the result.
4339 */
4340 if (evaluate && result)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (error)
4346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 if (evaluate)
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 rettv->v_type = VAR_NUMBER;
4351 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355 return OK;
4356}
4357
4358/*
4359 * Handle third level expression:
4360 * var1 == var2
4361 * var1 =~ var2
4362 * var1 != var2
4363 * var1 !~ var2
4364 * var1 > var2
4365 * var1 >= var2
4366 * var1 < var2
4367 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 * var1 is var2
4369 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004434 i = p[len];
4435 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 {
4437 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4438 type_is = TRUE;
4439 }
4440 }
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 if (type != TYPE_UNKNOWN)
4448 {
4449 /* extra question mark appended: ignore case */
4450 if (p[len] == '?')
4451 {
4452 ic = TRUE;
4453 ++len;
4454 }
4455 /* extra '#' appended: match case */
4456 else if (p[len] == '#')
4457 {
4458 ic = FALSE;
4459 ++len;
4460 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
4463 ic = p_ic;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(p + len);
4469 if (eval5(arg, &var2, evaluate) == FAIL)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FAIL;
4473 }
4474
4475 if (evaluate)
4476 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 if (type_is && rettv->v_type != var2.v_type)
4478 {
4479 /* For "is" a different type always means FALSE, for "notis"
4480 * it means TRUE. */
4481 n1 = (type == TYPE_NEQUAL);
4482 }
4483 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4484 {
4485 if (type_is)
4486 {
4487 n1 = (rettv->v_type == var2.v_type
4488 && rettv->vval.v_list == var2.vval.v_list);
4489 if (type == TYPE_NEQUAL)
4490 n1 = !n1;
4491 }
4492 else if (rettv->v_type != var2.v_type
4493 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4494 {
4495 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004496 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004498 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 clear_tv(rettv);
4500 clear_tv(&var2);
4501 return FAIL;
4502 }
4503 else
4504 {
4505 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004506 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4507 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004513 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4514 {
4515 if (type_is)
4516 {
4517 n1 = (rettv->v_type == var2.v_type
4518 && rettv->vval.v_dict == var2.vval.v_dict);
4519 if (type == TYPE_NEQUAL)
4520 n1 = !n1;
4521 }
4522 else if (rettv->v_type != var2.v_type
4523 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4524 {
4525 if (rettv->v_type != var2.v_type)
4526 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4527 else
4528 EMSG(_("E736: Invalid operation for Dictionary"));
4529 clear_tv(rettv);
4530 clear_tv(&var2);
4531 return FAIL;
4532 }
4533 else
4534 {
4535 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004536 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4537 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 }
4542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4544 {
4545 if (rettv->v_type != var2.v_type
4546 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4547 {
4548 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004551 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 clear_tv(rettv);
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 else
4557 {
4558 /* Compare two Funcrefs for being equal or unequal. */
4559 if (rettv->vval.v_string == NULL
4560 || var2.vval.v_string == NULL)
4561 n1 = FALSE;
4562 else
4563 n1 = STRCMP(rettv->vval.v_string,
4564 var2.vval.v_string) == 0;
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 }
4569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570#ifdef FEAT_FLOAT
4571 /*
4572 * If one of the two variables is a float, compare as a float.
4573 * When using "=~" or "!~", always compare as string.
4574 */
4575 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
4578 float_T f1, f2;
4579
4580 if (rettv->v_type == VAR_FLOAT)
4581 f1 = rettv->vval.v_float;
4582 else
4583 f1 = get_tv_number(rettv);
4584 if (var2.v_type == VAR_FLOAT)
4585 f2 = var2.vval.v_float;
4586 else
4587 f2 = get_tv_number(&var2);
4588 n1 = FALSE;
4589 switch (type)
4590 {
4591 case TYPE_EQUAL: n1 = (f1 == f2); break;
4592 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4593 case TYPE_GREATER: n1 = (f1 > f2); break;
4594 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4595 case TYPE_SMALLER: n1 = (f1 < f2); break;
4596 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4597 case TYPE_UNKNOWN:
4598 case TYPE_MATCH:
4599 case TYPE_NOMATCH: break; /* avoid gcc warning */
4600 }
4601 }
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If one of the two variables is a number, compare as a number.
4606 * When using "=~" or "!~", always compare as string.
4607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 n1 = get_tv_number(rettv);
4612 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 switch (type)
4614 {
4615 case TYPE_EQUAL: n1 = (n1 == n2); break;
4616 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4617 case TYPE_GREATER: n1 = (n1 > n2); break;
4618 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4619 case TYPE_SMALLER: n1 = (n1 < n2); break;
4620 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4621 case TYPE_UNKNOWN:
4622 case TYPE_MATCH:
4623 case TYPE_NOMATCH: break; /* avoid gcc warning */
4624 }
4625 }
4626 else
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 s1 = get_tv_string_buf(rettv, buf1);
4629 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4631 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4632 else
4633 i = 0;
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (i == 0); break;
4638 case TYPE_NEQUAL: n1 = (i != 0); break;
4639 case TYPE_GREATER: n1 = (i > 0); break;
4640 case TYPE_GEQUAL: n1 = (i >= 0); break;
4641 case TYPE_SMALLER: n1 = (i < 0); break;
4642 case TYPE_SEQUAL: n1 = (i <= 0); break;
4643
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH:
4646 /* avoid 'l' flag in 'cpoptions' */
4647 save_cpo = p_cpo;
4648 p_cpo = (char_u *)"";
4649 regmatch.regprog = vim_regcomp(s2,
4650 RE_MAGIC + RE_STRING);
4651 regmatch.rm_ic = ic;
4652 if (regmatch.regprog != NULL)
4653 {
4654 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004655 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type == TYPE_NOMATCH)
4657 n1 = !n1;
4658 }
4659 p_cpo = save_cpo;
4660 break;
4661
4662 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4663 }
4664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * Handle fourth level expression:
4677 * + number addition
4678 * - number subtraction
4679 * . string concatenation
4680 *
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4683 *
4684 * Return OK or FAIL.
4685 */
4686 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 int evaluate;
4691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004872eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004876 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005054eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005058 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005137 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
5234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005236 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005350 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005360 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362 if (verbose)
5363 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 return FAIL;
5365 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366#ifdef FEAT_FLOAT
5367 else if (rettv->v_type == VAR_FLOAT)
5368 {
5369 if (verbose)
5370 EMSG(_(e_float_as_string));
5371 return FAIL;
5372 }
5373#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005375 init_tv(&var1);
5376 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 /*
5380 * dict.name
5381 */
5382 key = *arg + 1;
5383 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5384 ;
5385 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 }
5389 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 /*
5392 * something[idx]
5393 *
5394 * Get the (first) variable from inside the [].
5395 */
5396 *arg = skipwhite(*arg + 1);
5397 if (**arg == ':')
5398 empty1 = TRUE;
5399 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5400 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005401 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5402 {
5403 /* not a number or string */
5404 clear_tv(&var1);
5405 return FAIL;
5406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407
5408 /*
5409 * Get the second variable from inside the [:].
5410 */
5411 if (**arg == ':')
5412 {
5413 range = TRUE;
5414 *arg = skipwhite(*arg + 1);
5415 if (**arg == ']')
5416 empty2 = TRUE;
5417 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 if (!empty1)
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
5423 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5424 {
5425 /* not a number or string */
5426 if (!empty1)
5427 clear_tv(&var1);
5428 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 return FAIL;
5430 }
5431 }
5432
5433 /* Check for the ']'. */
5434 if (**arg != ']')
5435 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005436 if (verbose)
5437 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 clear_tv(&var1);
5439 if (range)
5440 clear_tv(&var2);
5441 return FAIL;
5442 }
5443 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445
5446 if (evaluate)
5447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 n1 = 0;
5449 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 n1 = get_tv_number(&var1);
5452 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 }
5454 if (range)
5455 {
5456 if (empty2)
5457 n2 = -1;
5458 else
5459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 n2 = get_tv_number(&var2);
5461 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 }
5464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
5467 case VAR_NUMBER:
5468 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 len = (long)STRLEN(s);
5471 if (range)
5472 {
5473 /* The resulting variable is a substring. If the indexes
5474 * are out of range the result is empty. */
5475 if (n1 < 0)
5476 {
5477 n1 = len + n1;
5478 if (n1 < 0)
5479 n1 = 0;
5480 }
5481 if (n2 < 0)
5482 n2 = len + n2;
5483 else if (n2 >= len)
5484 n2 = len;
5485 if (n1 >= len || n2 < 0 || n1 > n2)
5486 s = NULL;
5487 else
5488 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5489 }
5490 else
5491 {
5492 /* The resulting variable is a string of a single
5493 * character. If the index is too big or negative the
5494 * result is empty. */
5495 if (n1 >= len || n1 < 0)
5496 s = NULL;
5497 else
5498 s = vim_strnsave(s + n1, 1);
5499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 clear_tv(rettv);
5501 rettv->v_type = VAR_STRING;
5502 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 break;
5504
5505 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 if (n1 < 0)
5508 n1 = len + n1;
5509 if (!empty1 && (n1 < 0 || n1 >= len))
5510 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005511 /* For a range we allow invalid values and return an empty
5512 * list. A list index out of range is an error. */
5513 if (!range)
5514 {
5515 if (verbose)
5516 EMSGN(_(e_listidx), n1);
5517 return FAIL;
5518 }
5519 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 if (range)
5522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 list_T *l;
5524 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525
5526 if (n2 < 0)
5527 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005528 else if (n2 >= len)
5529 n2 = len - 1;
5530 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005531 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 l = list_alloc();
5533 if (l == NULL)
5534 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 n1 <= n2; ++n1)
5537 {
5538 if (list_append_tv(l, &item->li_tv) == FAIL)
5539 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005540 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 return FAIL;
5542 }
5543 item = item->li_next;
5544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 clear_tv(rettv);
5546 rettv->v_type = VAR_LIST;
5547 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005548 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 }
5550 else
5551 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005552 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 clear_tv(rettv);
5554 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557
5558 case VAR_DICT:
5559 if (range)
5560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005561 if (verbose)
5562 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 if (len == -1)
5564 clear_tv(&var1);
5565 return FAIL;
5566 }
5567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569
5570 if (len == -1)
5571 {
5572 key = get_tv_string(&var1);
5573 if (*key == NUL)
5574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005575 if (verbose)
5576 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 clear_tv(&var1);
5578 return FAIL;
5579 }
5580 }
5581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005582 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005585 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 if (item == NULL)
5589 return FAIL;
5590
5591 copy_tv(&item->di_tv, &var1);
5592 clear_tv(rettv);
5593 *rettv = var1;
5594 }
5595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 }
5597 }
5598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 return OK;
5600}
5601
5602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 * Get an option value.
5604 * "arg" points to the '&' or '+' before the option name.
5605 * "arg" is advanced to character after the option name.
5606 * Return OK or FAIL.
5607 */
5608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005611 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 int evaluate;
5613{
5614 char_u *option_end;
5615 long numval;
5616 char_u *stringval;
5617 int opt_type;
5618 int c;
5619 int working = (**arg == '+'); /* has("+option") */
5620 int ret = OK;
5621 int opt_flags;
5622
5623 /*
5624 * Isolate the option name and find its value.
5625 */
5626 option_end = find_option_end(arg, &opt_flags);
5627 if (option_end == NULL)
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 EMSG2(_("E112: Option name missing: %s"), *arg);
5631 return FAIL;
5632 }
5633
5634 if (!evaluate)
5635 {
5636 *arg = option_end;
5637 return OK;
5638 }
5639
5640 c = *option_end;
5641 *option_end = NUL;
5642 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645 if (opt_type == -3) /* invalid name */
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 EMSG2(_("E113: Unknown option: %s"), *arg);
5649 ret = FAIL;
5650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 if (opt_type == -2) /* hidden string option */
5654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 rettv->v_type = VAR_STRING;
5656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else if (opt_type == -1) /* hidden number option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_NUMBER;
5661 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == 1) /* number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else /* string option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_STRING;
5671 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 }
5674 else if (working && (opt_type == -2 || opt_type == -1))
5675 ret = FAIL;
5676
5677 *option_end = c; /* put back for error messages */
5678 *arg = option_end;
5679
5680 return ret;
5681}
5682
5683/*
5684 * Allocate a variable for a string constant.
5685 * Return OK or FAIL.
5686 */
5687 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 int evaluate;
5692{
5693 char_u *p;
5694 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 int extra = 0;
5696
5697 /*
5698 * Find the end of the string, skipping backslashed characters.
5699 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 if (*p == '\\' && p[1] != NUL)
5703 {
5704 ++p;
5705 /* A "\<x>" form occupies at least 4 characters, and produces up
5706 * to 6 characters: reserve space for 2 extra */
5707 if (*p == '<')
5708 extra += 2;
5709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711
5712 if (*p != '"')
5713 {
5714 EMSG2(_("E114: Missing quote: %s"), *arg);
5715 return FAIL;
5716 }
5717
5718 /* If only parsing, set *arg and return here */
5719 if (!evaluate)
5720 {
5721 *arg = p + 1;
5722 return OK;
5723 }
5724
5725 /*
5726 * Copy the string into allocated memory, handling backslashed
5727 * characters.
5728 */
5729 name = alloc((unsigned)(p - *arg + extra));
5730 if (name == NULL)
5731 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 rettv->v_type = VAR_STRING;
5733 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
5737 if (*p == '\\')
5738 {
5739 switch (*++p)
5740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 case 'b': *name++ = BS; ++p; break;
5742 case 'e': *name++ = ESC; ++p; break;
5743 case 'f': *name++ = FF; ++p; break;
5744 case 'n': *name++ = NL; ++p; break;
5745 case 'r': *name++ = CAR; ++p; break;
5746 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 case 'X': /* hex: "\x1", "\x12" */
5749 case 'x':
5750 case 'u': /* Unicode: "\u0023" */
5751 case 'U':
5752 if (vim_isxdigit(p[1]))
5753 {
5754 int n, nr;
5755 int c = toupper(*p);
5756
5757 if (c == 'X')
5758 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005759 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005761 else
5762 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 nr = 0;
5764 while (--n >= 0 && vim_isxdigit(p[1]))
5765 {
5766 ++p;
5767 nr = (nr << 4) + hex2nr(*p);
5768 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770#ifdef FEAT_MBYTE
5771 /* For "\u" store the number according to
5772 * 'encoding'. */
5773 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
5776#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 break;
5780
5781 /* octal: "\1", "\12", "\123" */
5782 case '0':
5783 case '1':
5784 case '2':
5785 case '3':
5786 case '4':
5787 case '5':
5788 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 case '7': *name = *p++ - '0';
5790 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *name = (*name << 3) + *p++ - '0';
5793 if (*p >= '0' && *p <= '7')
5794 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798
5799 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 if (extra != 0)
5802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 break;
5805 }
5806 /* FALLTHROUGH */
5807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810 }
5811 }
5812 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 *arg = p + 1;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return OK;
5820}
5821
5822/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * Return OK or FAIL.
5825 */
5826 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005827get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 int evaluate;
5831{
5832 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 int reduce = 0;
5835
5836 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 */
5839 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 break;
5845 ++reduce;
5846 ++p;
5847 }
5848 }
5849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 return FAIL;
5854 }
5855
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 if (!evaluate)
5858 {
5859 *arg = p + 1;
5860 return OK;
5861 }
5862
5863 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 */
5866 str = alloc((unsigned)((p - *arg) - reduce));
5867 if (str == NULL)
5868 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 rettv->v_type = VAR_STRING;
5870 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 break;
5878 ++p;
5879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 *arg = p + 1;
5884
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 return OK;
5886}
5887
5888/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 * Allocate a variable for a List and fill it from "*arg".
5890 * Return OK or FAIL.
5891 */
5892 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005893get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 int evaluate;
5897{
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *l = NULL;
5899 typval_T tv;
5900 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901
5902 if (evaluate)
5903 {
5904 l = list_alloc();
5905 if (l == NULL)
5906 return FAIL;
5907 }
5908
5909 *arg = skipwhite(*arg + 1);
5910 while (**arg != ']' && **arg != NUL)
5911 {
5912 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5913 goto failret;
5914 if (evaluate)
5915 {
5916 item = listitem_alloc();
5917 if (item != NULL)
5918 {
5919 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005920 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 list_append(l, item);
5922 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005923 else
5924 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 }
5926
5927 if (**arg == ']')
5928 break;
5929 if (**arg != ',')
5930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 goto failret;
5933 }
5934 *arg = skipwhite(*arg + 1);
5935 }
5936
5937 if (**arg != ']')
5938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940failret:
5941 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005942 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 return FAIL;
5944 }
5945
5946 *arg = skipwhite(*arg + 1);
5947 if (evaluate)
5948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005949 rettv->v_type = VAR_LIST;
5950 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 ++l->lv_refcount;
5952 }
5953
5954 return OK;
5955}
5956
5957/*
5958 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005959 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005961 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962list_alloc()
5963{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 list_T *l;
5965
5966 l = (list_T *)alloc_clear(sizeof(list_T));
5967 if (l != NULL)
5968 {
5969 /* Prepend the list to the list of lists for garbage collection. */
5970 if (first_list != NULL)
5971 first_list->lv_used_prev = l;
5972 l->lv_used_prev = NULL;
5973 l->lv_used_next = first_list;
5974 first_list = l;
5975 }
5976 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977}
5978
5979/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005980 * Allocate an empty list for a return value.
5981 * Returns OK or FAIL.
5982 */
5983 static int
5984rettv_list_alloc(rettv)
5985 typval_T *rettv;
5986{
5987 list_T *l = list_alloc();
5988
5989 if (l == NULL)
5990 return FAIL;
5991
5992 rettv->vval.v_list = l;
5993 rettv->v_type = VAR_LIST;
5994 ++l->lv_refcount;
5995 return OK;
5996}
5997
5998/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 * Unreference a list: decrement the reference count and free it when it
6000 * becomes zero.
6001 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006002 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006006 if (l != NULL && --l->lv_refcount <= 0)
6007 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008}
6009
6010/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006011 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Ignores the reference count.
6013 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006014 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015list_free(l, recurse)
6016 list_T *l;
6017 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006021 /* Remove the list from the list of lists for garbage collection. */
6022 if (l->lv_used_prev == NULL)
6023 first_list = l->lv_used_next;
6024 else
6025 l->lv_used_prev->lv_used_next = l->lv_used_next;
6026 if (l->lv_used_next != NULL)
6027 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6028
Bram Moolenaard9fba312005-06-26 22:34:35 +00006029 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006031 /* Remove the item before deleting it. */
6032 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006033 if (recurse || (item->li_tv.v_type != VAR_LIST
6034 && item->li_tv.v_type != VAR_DICT))
6035 clear_tv(&item->li_tv);
6036 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 }
6038 vim_free(l);
6039}
6040
6041/*
6042 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006043 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006045 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046listitem_alloc()
6047{
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049}
6050
6051/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006052 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006054 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006058 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 vim_free(item);
6060}
6061
6062/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006063 * Remove a list item from a List and free it. Also clears the value.
6064 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006065 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 list_T *l;
6068 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006070 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071 listitem_free(item);
6072}
6073
6074/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 * Get the number of items in a list.
6076 */
6077 static long
6078list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 if (l == NULL)
6082 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006083 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084}
6085
6086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE when two lists have exactly the same values.
6088 */
6089 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 list_T *l1;
6092 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095{
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006098 if (l1 == NULL || l2 == NULL)
6099 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006100 if (l1 == l2)
6101 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006102 if (list_len(l1) != list_len(l2))
6103 return FALSE;
6104
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105 for (item1 = l1->lv_first, item2 = l2->lv_first;
6106 item1 != NULL && item2 != NULL;
6107 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 return FALSE;
6110 return item1 == NULL && item2 == NULL;
6111}
6112
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006113#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6114 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006115/*
6116 * Return the dictitem that an entry in a hashtable points to.
6117 */
6118 dictitem_T *
6119dict_lookup(hi)
6120 hashitem_T *hi;
6121{
6122 return HI2DI(hi);
6123}
6124#endif
6125
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006127 * Return TRUE when two dictionaries have exactly the same key/values.
6128 */
6129 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006130dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 dict_T *d1;
6132 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 hashitem_T *hi;
6137 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006140 if (d1 == NULL || d2 == NULL)
6141 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006142 if (d1 == d2)
6143 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 if (dict_len(d1) != dict_len(d2))
6145 return FALSE;
6146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006147 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 if (!HASHITEM_EMPTY(hi))
6151 {
6152 item2 = dict_find(d2, hi->hi_key, -1);
6153 if (item2 == NULL)
6154 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006155 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006156 return FALSE;
6157 --todo;
6158 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 }
6160 return TRUE;
6161}
6162
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163static int tv_equal_recurse_limit;
6164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006167 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006168 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006169 */
6170 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 typval_T *tv1;
6173 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 int ic; /* ignore case */
6175 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176{
6177 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006178 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006180 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006185 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186 * recursiveness to a limit. We guess they are equal then.
6187 * A fixed limit has the problem of still taking an awful long time.
6188 * Reduce the limit every time running into it. That should work fine for
6189 * deeply linked structures that are not recursively linked and catch
6190 * recursiveness quickly. */
6191 if (!recursive)
6192 tv_equal_recurse_limit = 1000;
6193 if (recursive_cnt >= tv_equal_recurse_limit)
6194 {
6195 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006196 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 ++recursive_cnt;
6203 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6204 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006205 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206
6207 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006208 ++recursive_cnt;
6209 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6210 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006211 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006212
6213 case VAR_FUNC:
6214 return (tv1->vval.v_string != NULL
6215 && tv2->vval.v_string != NULL
6216 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6217
6218 case VAR_NUMBER:
6219 return tv1->vval.v_number == tv2->vval.v_number;
6220
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006221#ifdef FEAT_FLOAT
6222 case VAR_FLOAT:
6223 return tv1->vval.v_float == tv2->vval.v_float;
6224#endif
6225
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226 case VAR_STRING:
6227 s1 = get_tv_string_buf(tv1, buf1);
6228 s2 = get_tv_string_buf(tv2, buf2);
6229 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231
6232 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006233 return TRUE;
6234}
6235
6236/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 * Locate item with index "n" in list "l" and return it.
6238 * A negative index is counted from the end; -1 is the last item.
6239 * Returns NULL when "n" is out of range.
6240 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 long n;
6245{
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 long idx;
6248
6249 if (l == NULL)
6250 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006251
6252 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006254 n = l->lv_len + n;
6255
6256 /* Check for index out of range. */
6257 if (n < 0 || n >= l->lv_len)
6258 return NULL;
6259
6260 /* When there is a cached index may start search from there. */
6261 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 if (n < l->lv_idx / 2)
6264 {
6265 /* closest to the start of the list */
6266 item = l->lv_first;
6267 idx = 0;
6268 }
6269 else if (n > (l->lv_idx + l->lv_len) / 2)
6270 {
6271 /* closest to the end of the list */
6272 item = l->lv_last;
6273 idx = l->lv_len - 1;
6274 }
6275 else
6276 {
6277 /* closest to the cached index */
6278 item = l->lv_idx_item;
6279 idx = l->lv_idx;
6280 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 }
6282 else
6283 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 if (n < l->lv_len / 2)
6285 {
6286 /* closest to the start of the list */
6287 item = l->lv_first;
6288 idx = 0;
6289 }
6290 else
6291 {
6292 /* closest to the end of the list */
6293 item = l->lv_last;
6294 idx = l->lv_len - 1;
6295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297
6298 while (n > idx)
6299 {
6300 /* search forward */
6301 item = item->li_next;
6302 ++idx;
6303 }
6304 while (n < idx)
6305 {
6306 /* search backward */
6307 item = item->li_prev;
6308 --idx;
6309 }
6310
6311 /* cache the used index */
6312 l->lv_idx = idx;
6313 l->lv_idx_item = item;
6314
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 return item;
6316}
6317
6318/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006319 * Get list item "l[idx]" as a number.
6320 */
6321 static long
6322list_find_nr(l, idx, errorp)
6323 list_T *l;
6324 long idx;
6325 int *errorp; /* set to TRUE when something wrong */
6326{
6327 listitem_T *li;
6328
6329 li = list_find(l, idx);
6330 if (li == NULL)
6331 {
6332 if (errorp != NULL)
6333 *errorp = TRUE;
6334 return -1L;
6335 }
6336 return get_tv_number_chk(&li->li_tv, errorp);
6337}
6338
6339/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006340 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6341 */
6342 char_u *
6343list_find_str(l, idx)
6344 list_T *l;
6345 long idx;
6346{
6347 listitem_T *li;
6348
6349 li = list_find(l, idx - 1);
6350 if (li == NULL)
6351 {
6352 EMSGN(_(e_listidx), idx);
6353 return NULL;
6354 }
6355 return get_tv_string(&li->li_tv);
6356}
6357
6358/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006359 * Locate "item" list "l" and return its index.
6360 * Returns -1 when "item" is not in the list.
6361 */
6362 static long
6363list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 list_T *l;
6365 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366{
6367 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006369
6370 if (l == NULL)
6371 return -1;
6372 idx = 0;
6373 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6374 ++idx;
6375 if (li == NULL)
6376 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006377 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378}
6379
6380/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 * Append item "item" to the end of list "l".
6382 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006383 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 list_T *l;
6386 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387{
6388 if (l->lv_last == NULL)
6389 {
6390 /* empty list */
6391 l->lv_first = item;
6392 l->lv_last = item;
6393 item->li_prev = NULL;
6394 }
6395 else
6396 {
6397 l->lv_last->li_next = item;
6398 item->li_prev = l->lv_last;
6399 l->lv_last = item;
6400 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 item->li_next = NULL;
6403}
6404
6405/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006409 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 list_T *l;
6412 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006414 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415
Bram Moolenaar05159a02005-02-26 23:04:13 +00006416 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006418 copy_tv(tv, &li->li_tv);
6419 list_append(l, li);
6420 return OK;
6421}
6422
6423/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006424 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 * Return FAIL when out of memory.
6426 */
6427 int
6428list_append_dict(list, dict)
6429 list_T *list;
6430 dict_T *dict;
6431{
6432 listitem_T *li = listitem_alloc();
6433
6434 if (li == NULL)
6435 return FAIL;
6436 li->li_tv.v_type = VAR_DICT;
6437 li->li_tv.v_lock = 0;
6438 li->li_tv.vval.v_dict = dict;
6439 list_append(list, li);
6440 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 return OK;
6442}
6443
6444/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006445 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006446 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006447 * Returns FAIL when out of memory.
6448 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006449 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 list_T *l;
6452 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454{
6455 listitem_T *li = listitem_alloc();
6456
6457 if (li == NULL)
6458 return FAIL;
6459 list_append(l, li);
6460 li->li_tv.v_type = VAR_STRING;
6461 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462 if (str == NULL)
6463 li->li_tv.vval.v_string = NULL;
6464 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006465 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 return FAIL;
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006471 * Append "n" to list "l".
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
6475list_append_number(l, n)
6476 list_T *l;
6477 varnumber_T n;
6478{
6479 listitem_T *li;
6480
6481 li = listitem_alloc();
6482 if (li == NULL)
6483 return FAIL;
6484 li->li_tv.v_type = VAR_NUMBER;
6485 li->li_tv.v_lock = 0;
6486 li->li_tv.vval.v_number = n;
6487 list_append(l, li);
6488 return OK;
6489}
6490
6491/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 * If "item" is NULL append at the end.
6494 * Return FAIL when out of memory.
6495 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006496 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 list_T *l;
6499 typval_T *tv;
6500 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501{
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503
6504 if (ni == NULL)
6505 return FAIL;
6506 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006507 list_insert(l, ni, item);
6508 return OK;
6509}
6510
6511 void
6512list_insert(l, ni, item)
6513 list_T *l;
6514 listitem_T *ni;
6515 listitem_T *item;
6516{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 if (item == NULL)
6518 /* Append new item at end of list. */
6519 list_append(l, ni);
6520 else
6521 {
6522 /* Insert new item before existing item. */
6523 ni->li_prev = item->li_prev;
6524 ni->li_next = item;
6525 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 ++l->lv_idx;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 l->lv_idx_item = NULL;
6534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538}
6539
6540/*
6541 * Extend "l1" with "l2".
6542 * If "bef" is NULL append at the end, otherwise insert before this item.
6543 * Returns FAIL when out of memory.
6544 */
6545 static int
6546list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 list_T *l1;
6548 list_T *l2;
6549 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550{
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006552 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006554 /* We also quit the loop when we have inserted the original item count of
6555 * the list, avoid a hang when we extend a list with itself. */
6556 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6558 return FAIL;
6559 return OK;
6560}
6561
6562/*
6563 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6564 * Return FAIL when out of memory.
6565 */
6566 static int
6567list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 list_T *l1;
6569 list_T *l2;
6570 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571{
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006574 if (l1 == NULL || l2 == NULL)
6575 return FAIL;
6576
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 if (l == NULL)
6580 return FAIL;
6581 tv->v_type = VAR_LIST;
6582 tv->vval.v_list = l;
6583
6584 /* append all items from the second list */
6585 return list_extend(l, l2, NULL);
6586}
6587
6588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 * Returns NULL when out of memory.
6593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 list_T *copy;
6601 listitem_T *item;
6602 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 if (orig == NULL)
6605 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606
6607 copy = list_alloc();
6608 if (copy != NULL)
6609 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 if (copyID != 0)
6611 {
6612 /* Do this before adding the items, because one of the items may
6613 * refer back to this list. */
6614 orig->lv_copyID = copyID;
6615 orig->lv_copylist = copy;
6616 }
6617 for (item = orig->lv_first; item != NULL && !got_int;
6618 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 {
6620 ni = listitem_alloc();
6621 if (ni == NULL)
6622 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 {
6625 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6626 {
6627 vim_free(ni);
6628 break;
6629 }
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 list_append(copy, ni);
6634 }
6635 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006636 if (item != NULL)
6637 {
6638 list_unref(copy);
6639 copy = NULL;
6640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 }
6642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return copy;
6644}
6645
6646/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006648 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006649 * This used to be called list_remove, but that conflicts with a Sun header
6650 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006652 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006653vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006654 list_T *l;
6655 listitem_T *item;
6656 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657{
Bram Moolenaar33570922005-01-25 22:26:29 +00006658 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006660 /* notify watchers */
6661 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006663 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 list_fix_watch(l, ip);
6665 if (ip == item2)
6666 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668
6669 if (item2->li_next == NULL)
6670 l->lv_last = item->li_prev;
6671 else
6672 item2->li_next->li_prev = item->li_prev;
6673 if (item->li_prev == NULL)
6674 l->lv_first = item2->li_next;
6675 else
6676 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006677 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678}
6679
6680/*
6681 * Return an allocated string with the string representation of a list.
6682 * May return NULL.
6683 */
6684 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006685list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006686 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688{
6689 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690
6691 if (tv->vval.v_list == NULL)
6692 return NULL;
6693 ga_init2(&ga, (int)sizeof(char), 80);
6694 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006696 {
6697 vim_free(ga.ga_data);
6698 return NULL;
6699 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 ga_append(&ga, ']');
6701 ga_append(&ga, NUL);
6702 return (char_u *)ga.ga_data;
6703}
6704
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006705typedef struct join_S {
6706 char_u *s;
6707 char_u *tofree;
6708} join_T;
6709
6710 static int
6711list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6712 garray_T *gap; /* to store the result in */
6713 list_T *l;
6714 char_u *sep;
6715 int echo_style;
6716 int copyID;
6717 garray_T *join_gap; /* to keep each list item string */
6718{
6719 int i;
6720 join_T *p;
6721 int len;
6722 int sumlen = 0;
6723 int first = TRUE;
6724 char_u *tofree;
6725 char_u numbuf[NUMBUFLEN];
6726 listitem_T *item;
6727 char_u *s;
6728
6729 /* Stringify each item in the list. */
6730 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6731 {
6732 if (echo_style)
6733 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6734 else
6735 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6736 if (s == NULL)
6737 return FAIL;
6738
6739 len = (int)STRLEN(s);
6740 sumlen += len;
6741
Bram Moolenaarcde88542015-08-11 19:14:00 +02006742 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006743 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6744 if (tofree != NULL || s != numbuf)
6745 {
6746 p->s = s;
6747 p->tofree = tofree;
6748 }
6749 else
6750 {
6751 p->s = vim_strnsave(s, len);
6752 p->tofree = p->s;
6753 }
6754
6755 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006756 if (did_echo_string_emsg) /* recursion error, bail out */
6757 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006758 }
6759
6760 /* Allocate result buffer with its total size, avoid re-allocation and
6761 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6762 if (join_gap->ga_len >= 2)
6763 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6764 if (ga_grow(gap, sumlen + 2) == FAIL)
6765 return FAIL;
6766
6767 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6768 {
6769 if (first)
6770 first = FALSE;
6771 else
6772 ga_concat(gap, sep);
6773 p = ((join_T *)join_gap->ga_data) + i;
6774
6775 if (p->s != NULL)
6776 ga_concat(gap, p->s);
6777 line_breakcheck();
6778 }
6779
6780 return OK;
6781}
6782
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006783/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006785 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006786 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006788 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006789list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006793 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006794 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796 garray_T join_ga;
6797 int retval;
6798 join_T *p;
6799 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800
Bram Moolenaard39a7512015-04-16 22:51:22 +02006801 if (l->lv_len < 1)
6802 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006803 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6804 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6805
6806 /* Dispose each item in join_ga. */
6807 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809 p = (join_T *)join_ga.ga_data;
6810 for (i = 0; i < join_ga.ga_len; ++i)
6811 {
6812 vim_free(p->tofree);
6813 ++p;
6814 }
6815 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006817
6818 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819}
6820
6821/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 * Garbage collection for lists and dictionaries.
6823 *
6824 * We use reference counts to be able to free most items right away when they
6825 * are no longer used. But for composite items it's possible that it becomes
6826 * unused while the reference count is > 0: When there is a recursive
6827 * reference. Example:
6828 * :let l = [1, 2, 3]
6829 * :let d = {9: l}
6830 * :let l[1] = d
6831 *
6832 * Since this is quite unusual we handle this with garbage collection: every
6833 * once in a while find out which lists and dicts are not referenced from any
6834 * variable.
6835 *
6836 * Here is a good reference text about garbage collection (refers to Python
6837 * but it applies to all reference-counting mechanisms):
6838 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840
6841/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 * Do garbage collection for lists and dicts.
6843 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 int
6846garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006847{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 buf_T *buf;
6851 win_T *wp;
6852 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006853 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006854 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006856#ifdef FEAT_WINDOWS
6857 tabpage_T *tp;
6858#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006860 /* Only do this once. */
6861 want_garbage_collect = FALSE;
6862 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006863 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006864
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 /* We advance by two because we add one for items referenced through
6866 * previous_funccal. */
6867 current_copyID += COPYID_INC;
6868 copyID = current_copyID;
6869
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 /*
6871 * 1. Go through all accessible variables and mark all lists and dicts
6872 * with copyID.
6873 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874
6875 /* Don't free variables in the previous_funccal list unless they are only
6876 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006877 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6879 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6881 NULL);
6882 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6883 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 }
6885
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 /* script-local variables */
6887 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889
6890 /* buffer-local variables */
6891 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006899#ifdef FEAT_AUTOCMD
6900 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006903#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905#ifdef FEAT_WINDOWS
6906 /* tabpage-local variables */
6907 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6909 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910#endif
6911
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914
6915 /* function-local variables */
6916 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6917 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6919 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 }
6921
Bram Moolenaard812df62008-11-09 12:46:09 +00006922 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006924
Bram Moolenaar1dced572012-04-05 16:54:08 +02006925#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006927#endif
6928
Bram Moolenaardb913952012-06-29 12:54:53 +02006929#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006931#endif
6932
6933#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006935#endif
6936
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 /*
6940 * 2. Free lists and dictionaries that are not referenced.
6941 */
6942 did_free = free_unref_items(copyID);
6943
6944 /*
6945 * 3. Check if any funccal can be freed now.
6946 */
6947 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 if (can_free_funccal(*pfc, copyID))
6950 {
6951 fc = *pfc;
6952 *pfc = fc->caller;
6953 free_funccal(fc, TRUE);
6954 did_free = TRUE;
6955 did_free_funccal = TRUE;
6956 }
6957 else
6958 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 if (did_free_funccal)
6961 /* When a funccal was freed some more items might be garbage
6962 * collected, so run again. */
6963 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 else if (p_verbose > 0)
6966 {
6967 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6968 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969
6970 return did_free;
6971}
6972
6973/*
6974 * Free lists and dictionaries that are no longer referenced.
6975 */
6976 static int
6977free_unref_items(copyID)
6978 int copyID;
6979{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 dict_T *dd, *dd_next;
6981 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 int did_free = FALSE;
6983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 */
6987 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006988 {
6989 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 /* Free the Dictionary and ordinary items it contains, but don't
6993 * recurse into Lists and Dictionaries, they will be in the list
6994 * of dicts or list of lists. */
6995 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 dd = dd_next;
6999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000
7001 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 * Go through the list of lists and free items without the copyID.
7003 * But don't free a list that has a watcher (used in a for loop), these
7004 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 */
7006 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 {
7008 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7010 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007012 /* Free the List and ordinary items it contains, but don't recurse
7013 * into Lists and Dictionaries, they will be in the list of dicts
7014 * or list of lists. */
7015 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007018 ll = ll_next;
7019 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 return did_free;
7021}
7022
7023/*
7024 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 * "list_stack" is used to add lists to be marked. Can be NULL.
7026 *
7027 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 int
7030set_ref_in_ht(ht, copyID, list_stack)
7031 hashtab_T *ht;
7032 int copyID;
7033 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034{
7035 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 hashtab_T *cur_ht;
7039 ht_stack_T *ht_stack = NULL;
7040 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 cur_ht = ht;
7043 for (;;)
7044 {
7045 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 /* Mark each item in the hashtab. If the item contains a hashtab
7048 * it is added to ht_stack, if it contains a list it is added to
7049 * list_stack. */
7050 todo = (int)cur_ht->ht_used;
7051 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7052 if (!HASHITEM_EMPTY(hi))
7053 {
7054 --todo;
7055 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7056 &ht_stack, list_stack);
7057 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059
7060 if (ht_stack == NULL)
7061 break;
7062
7063 /* take an item from the stack */
7064 cur_ht = ht_stack->ht;
7065 tempitem = ht_stack;
7066 ht_stack = ht_stack->prev;
7067 free(tempitem);
7068 }
7069
7070 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071}
7072
7073/*
7074 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7076 *
7077 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 int
7080set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 list_T *l;
7082 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 listitem_T *li;
7086 int abort = FALSE;
7087 list_T *cur_l;
7088 list_stack_T *list_stack = NULL;
7089 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 cur_l = l;
7092 for (;;)
7093 {
7094 if (!abort)
7095 /* Mark each item in the list. If the item contains a hashtab
7096 * it is added to ht_stack, if it contains a list it is added to
7097 * list_stack. */
7098 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7099 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7100 ht_stack, &list_stack);
7101 if (list_stack == NULL)
7102 break;
7103
7104 /* take an item from the stack */
7105 cur_l = list_stack->list;
7106 tempitem = list_stack;
7107 list_stack = list_stack->prev;
7108 free(tempitem);
7109 }
7110
7111 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112}
7113
7114/*
7115 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 * "list_stack" is used to add lists to be marked. Can be NULL.
7117 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7118 *
7119 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007121 int
7122set_ref_in_item(tv, copyID, ht_stack, list_stack)
7123 typval_T *tv;
7124 int copyID;
7125 ht_stack_T **ht_stack;
7126 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127{
7128 dict_T *dd;
7129 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007131
7132 switch (tv->v_type)
7133 {
7134 case VAR_DICT:
7135 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007136 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007138 /* Didn't see this dict yet. */
7139 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 if (ht_stack == NULL)
7141 {
7142 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7143 }
7144 else
7145 {
7146 ht_stack_T *newitem = (ht_stack_T*)malloc(
7147 sizeof(ht_stack_T));
7148 if (newitem == NULL)
7149 abort = TRUE;
7150 else
7151 {
7152 newitem->ht = &dd->dv_hashtab;
7153 newitem->prev = *ht_stack;
7154 *ht_stack = newitem;
7155 }
7156 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007157 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007158 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007159
7160 case VAR_LIST:
7161 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007162 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007163 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 /* Didn't see this list yet. */
7165 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 if (list_stack == NULL)
7167 {
7168 abort = set_ref_in_list(ll, copyID, ht_stack);
7169 }
7170 else
7171 {
7172 list_stack_T *newitem = (list_stack_T*)malloc(
7173 sizeof(list_stack_T));
7174 if (newitem == NULL)
7175 abort = TRUE;
7176 else
7177 {
7178 newitem->list = ll;
7179 newitem->prev = *list_stack;
7180 *list_stack = newitem;
7181 }
7182 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007183 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007185 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007187}
7188
7189/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 * Allocate an empty header for a dictionary.
7191 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007192 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193dict_alloc()
7194{
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007200 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201 if (first_dict != NULL)
7202 first_dict->dv_used_prev = d;
7203 d->dv_used_next = first_dict;
7204 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007205 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007208 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007209 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007210 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007211 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007212 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214}
7215
7216/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007217 * Allocate an empty dict for a return value.
7218 * Returns OK or FAIL.
7219 */
7220 static int
7221rettv_dict_alloc(rettv)
7222 typval_T *rettv;
7223{
7224 dict_T *d = dict_alloc();
7225
7226 if (d == NULL)
7227 return FAIL;
7228
7229 rettv->vval.v_dict = d;
7230 rettv->v_type = VAR_DICT;
7231 ++d->dv_refcount;
7232 return OK;
7233}
7234
7235
7236/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 * Unreference a Dictionary: decrement the reference count and free it when it
7238 * becomes zero.
7239 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007240 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007244 if (d != NULL && --d->dv_refcount <= 0)
7245 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246}
7247
7248/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007249 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 * Ignores the reference count.
7251 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007252 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007253dict_free(d, recurse)
7254 dict_T *d;
7255 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007257 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007259 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007261 /* Remove the dict from the list of dicts for garbage collection. */
7262 if (d->dv_used_prev == NULL)
7263 first_dict = d->dv_used_next;
7264 else
7265 d->dv_used_prev->dv_used_next = d->dv_used_next;
7266 if (d->dv_used_next != NULL)
7267 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7268
7269 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007270 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007271 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 if (!HASHITEM_EMPTY(hi))
7275 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007276 /* Remove the item before deleting it, just in case there is
7277 * something recursive causing trouble. */
7278 di = HI2DI(hi);
7279 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007280 if (recurse || (di->di_tv.v_type != VAR_LIST
7281 && di->di_tv.v_type != VAR_DICT))
7282 clear_tv(&di->di_tv);
7283 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 --todo;
7285 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 vim_free(d);
7289}
7290
7291/*
7292 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 * The "key" is copied to the new item.
7294 * Note that the value of the item "di_tv" still needs to be initialized!
7295 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007297 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298dictitem_alloc(key)
7299 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007307 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007308 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Make a copy of a Dictionary item.
7314 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7322 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 if (di != NULL)
7324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007326 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 copy_tv(&org->di_tv, &di->di_tv);
7328 }
7329 return di;
7330}
7331
7332/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 * Remove item "item" from Dictionary "dict" and free it.
7334 */
7335 static void
7336dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dict_T *dict;
7338 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339{
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 if (HASHITEM_EMPTY(hi))
7344 EMSG2(_(e_intern2), "dictitem_remove()");
7345 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 dictitem_free(item);
7348}
7349
7350/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 * Free a dict item. Also clears the value.
7352 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007353 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007358 if (item->di_flags & DI_FLAGS_ALLOC)
7359 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360}
7361
7362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7364 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007365 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 * Returns NULL when out of memory.
7367 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373{
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dict_T *copy;
7375 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378
7379 if (orig == NULL)
7380 return NULL;
7381
7382 copy = dict_alloc();
7383 if (copy != NULL)
7384 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 if (copyID != 0)
7386 {
7387 orig->dv_copyID = copyID;
7388 orig->dv_copydict = copy;
7389 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007390 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 --todo;
7396
7397 di = dictitem_alloc(hi->hi_key);
7398 if (di == NULL)
7399 break;
7400 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 {
7402 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7403 copyID) == FAIL)
7404 {
7405 vim_free(di);
7406 break;
7407 }
7408 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 else
7410 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7411 if (dict_add(copy, di) == FAIL)
7412 {
7413 dictitem_free(di);
7414 break;
7415 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 if (todo > 0)
7421 {
7422 dict_unref(copy);
7423 copy = NULL;
7424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 }
7426
7427 return copy;
7428}
7429
7430/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007432 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007434 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 dict_T *d;
7437 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438{
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440}
7441
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007443 * Add a number or string entry to dictionary "d".
7444 * When "str" is NULL use number "nr", otherwise use "str".
7445 * Returns FAIL when out of memory and when key already exists.
7446 */
7447 int
7448dict_add_nr_str(d, key, nr, str)
7449 dict_T *d;
7450 char *key;
7451 long nr;
7452 char_u *str;
7453{
7454 dictitem_T *item;
7455
7456 item = dictitem_alloc((char_u *)key);
7457 if (item == NULL)
7458 return FAIL;
7459 item->di_tv.v_lock = 0;
7460 if (str == NULL)
7461 {
7462 item->di_tv.v_type = VAR_NUMBER;
7463 item->di_tv.vval.v_number = nr;
7464 }
7465 else
7466 {
7467 item->di_tv.v_type = VAR_STRING;
7468 item->di_tv.vval.v_string = vim_strsave(str);
7469 }
7470 if (dict_add(d, item) == FAIL)
7471 {
7472 dictitem_free(item);
7473 return FAIL;
7474 }
7475 return OK;
7476}
7477
7478/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007479 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007480 * Returns FAIL when out of memory and when key already exists.
7481 */
7482 int
7483dict_add_list(d, key, list)
7484 dict_T *d;
7485 char *key;
7486 list_T *list;
7487{
7488 dictitem_T *item;
7489
7490 item = dictitem_alloc((char_u *)key);
7491 if (item == NULL)
7492 return FAIL;
7493 item->di_tv.v_lock = 0;
7494 item->di_tv.v_type = VAR_LIST;
7495 item->di_tv.vval.v_list = list;
7496 if (dict_add(d, item) == FAIL)
7497 {
7498 dictitem_free(item);
7499 return FAIL;
7500 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007501 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007502 return OK;
7503}
7504
7505/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 * Get the number of items in a Dictionary.
7507 */
7508 static long
7509dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (d == NULL)
7513 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007514 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515}
7516
7517/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 * Find item "key[len]" in Dictionary "d".
7519 * If "len" is negative use strlen(key).
7520 * Returns NULL when not found.
7521 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007522 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 char_u *key;
7526 int len;
7527{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528#define AKEYLEN 200
7529 char_u buf[AKEYLEN];
7530 char_u *akey;
7531 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 if (len < 0)
7535 akey = key;
7536 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538 tofree = akey = vim_strnsave(key, len);
7539 if (akey == NULL)
7540 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007541 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 else
7543 {
7544 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007545 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007546 akey = buf;
7547 }
7548
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 vim_free(tofree);
7551 if (HASHITEM_EMPTY(hi))
7552 return NULL;
7553 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554}
7555
7556/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007557 * Get a string item from a dictionary.
7558 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007559 * Returns NULL if the entry doesn't exist or out of memory.
7560 */
7561 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563 dict_T *d;
7564 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007565 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007566{
7567 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007568 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007569
7570 di = dict_find(d, key, -1);
7571 if (di == NULL)
7572 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 s = get_tv_string(&di->di_tv);
7574 if (save && s != NULL)
7575 s = vim_strsave(s);
7576 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007577}
7578
7579/*
7580 * Get a number item from a dictionary.
7581 * Returns 0 if the entry doesn't exist or out of memory.
7582 */
7583 long
7584get_dict_number(d, key)
7585 dict_T *d;
7586 char_u *key;
7587{
7588 dictitem_T *di;
7589
7590 di = dict_find(d, key, -1);
7591 if (di == NULL)
7592 return 0;
7593 return get_tv_number(&di->di_tv);
7594}
7595
7596/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 * Return an allocated string with the string representation of a Dictionary.
7598 * May return NULL.
7599 */
7600 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604{
7605 garray_T ga;
7606 int first = TRUE;
7607 char_u *tofree;
7608 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 return NULL;
7616 ga_init2(&ga, (int)sizeof(char), 80);
7617 ga_append(&ga, '{');
7618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007619 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007620 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 --todo;
7625
7626 if (first)
7627 first = FALSE;
7628 else
7629 ga_concat(&ga, (char_u *)", ");
7630
7631 tofree = string_quote(hi->hi_key, FALSE);
7632 if (tofree != NULL)
7633 {
7634 ga_concat(&ga, tofree);
7635 vim_free(tofree);
7636 }
7637 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007638 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 if (s != NULL)
7640 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007642 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007643 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007644 line_breakcheck();
7645
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007648 if (todo > 0)
7649 {
7650 vim_free(ga.ga_data);
7651 return NULL;
7652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653
7654 ga_append(&ga, '}');
7655 ga_append(&ga, NUL);
7656 return (char_u *)ga.ga_data;
7657}
7658
7659/*
7660 * Allocate a variable for a Dictionary and fill it from "*arg".
7661 * Return OK or FAIL. Returns NOTDONE for {expr}.
7662 */
7663 static int
7664get_dict_tv(arg, rettv, evaluate)
7665 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 int evaluate;
7668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 dict_T *d = NULL;
7670 typval_T tvkey;
7671 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007672 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676
7677 /*
7678 * First check if it's not a curly-braces thing: {expr}.
7679 * Must do this without evaluating, otherwise a function may be called
7680 * twice. Unfortunately this means we need to call eval1() twice for the
7681 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 if (*start != '}')
7685 {
7686 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7687 return FAIL;
7688 if (*start == '}')
7689 return NOTDONE;
7690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691
7692 if (evaluate)
7693 {
7694 d = dict_alloc();
7695 if (d == NULL)
7696 return FAIL;
7697 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 tvkey.v_type = VAR_UNKNOWN;
7699 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700
7701 *arg = skipwhite(*arg + 1);
7702 while (**arg != '}' && **arg != NUL)
7703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 goto failret;
7706 if (**arg != ':')
7707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007708 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007709 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007712 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007714 key = get_tv_string_buf_chk(&tvkey, buf);
7715 if (key == NULL || *key == NUL)
7716 {
7717 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7718 if (key != NULL)
7719 EMSG(_(e_emptykey));
7720 clear_tv(&tvkey);
7721 goto failret;
7722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724
7725 *arg = skipwhite(*arg + 1);
7726 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7727 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007728 if (evaluate)
7729 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 goto failret;
7731 }
7732 if (evaluate)
7733 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007734 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 if (item != NULL)
7736 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007737 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 clear_tv(&tv);
7740 goto failret;
7741 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742 item = dictitem_alloc(key);
7743 clear_tv(&tvkey);
7744 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007747 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 if (dict_add(d, item) == FAIL)
7749 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 }
7751 }
7752
7753 if (**arg == '}')
7754 break;
7755 if (**arg != ',')
7756 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007757 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 goto failret;
7759 }
7760 *arg = skipwhite(*arg + 1);
7761 }
7762
7763 if (**arg != '}')
7764 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007765 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766failret:
7767 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007768 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 return FAIL;
7770 }
7771
7772 *arg = skipwhite(*arg + 1);
7773 if (evaluate)
7774 {
7775 rettv->v_type = VAR_DICT;
7776 rettv->vval.v_dict = d;
7777 ++d->dv_refcount;
7778 }
7779
7780 return OK;
7781}
7782
Bram Moolenaar8c711452005-01-14 21:53:12 +00007783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007784 * Return a string with the string representation of a variable.
7785 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007786 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007789 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007790 */
7791 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007792echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007796 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 static int recurse = 0;
7799 char_u *r = NULL;
7800
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007803 if (!did_echo_string_emsg)
7804 {
7805 /* Only give this message once for a recursive call to avoid
7806 * flooding the user with errors. And stop iterating over lists
7807 * and dicts. */
7808 did_echo_string_emsg = TRUE;
7809 EMSG(_("E724: variable nested too deep for displaying"));
7810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007812 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 }
7814 ++recurse;
7815
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816 switch (tv->v_type)
7817 {
7818 case VAR_FUNC:
7819 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 r = tv->vval.v_string;
7821 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007824 if (tv->vval.v_list == NULL)
7825 {
7826 *tofree = NULL;
7827 r = NULL;
7828 }
7829 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7830 {
7831 *tofree = NULL;
7832 r = (char_u *)"[...]";
7833 }
7834 else
7835 {
7836 tv->vval.v_list->lv_copyID = copyID;
7837 *tofree = list2string(tv, copyID);
7838 r = *tofree;
7839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007840 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841
Bram Moolenaar8c711452005-01-14 21:53:12 +00007842 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843 if (tv->vval.v_dict == NULL)
7844 {
7845 *tofree = NULL;
7846 r = NULL;
7847 }
7848 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7849 {
7850 *tofree = NULL;
7851 r = (char_u *)"{...}";
7852 }
7853 else
7854 {
7855 tv->vval.v_dict->dv_copyID = copyID;
7856 *tofree = dict2string(tv, copyID);
7857 r = *tofree;
7858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007861 case VAR_STRING:
7862 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 *tofree = NULL;
7864 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#ifdef FEAT_FLOAT
7868 case VAR_FLOAT:
7869 *tofree = NULL;
7870 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7871 r = numbuf;
7872 break;
7873#endif
7874
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879
Bram Moolenaar8502c702014-06-17 12:51:16 +02007880 if (--recurse == 0)
7881 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883}
7884
7885/*
7886 * Return a string with the string representation of a variable.
7887 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7888 * "numbuf" is used for a number.
7889 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007890 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 */
7892 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007893tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 char_u **tofree;
7896 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007897 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898{
7899 switch (tv->v_type)
7900 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 case VAR_FUNC:
7902 *tofree = string_quote(tv->vval.v_string, TRUE);
7903 return *tofree;
7904 case VAR_STRING:
7905 *tofree = string_quote(tv->vval.v_string, FALSE);
7906 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907#ifdef FEAT_FLOAT
7908 case VAR_FLOAT:
7909 *tofree = NULL;
7910 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7911 return numbuf;
7912#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007915 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007920 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921}
7922
7923/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 * Return string "str" in ' quotes, doubling ' characters.
7925 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007927 */
7928 static char_u *
7929string_quote(str, function)
7930 char_u *str;
7931 int function;
7932{
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007934 char_u *p, *r, *s;
7935
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 len = (function ? 13 : 3);
7937 if (str != NULL)
7938 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007939 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 for (p = str; *p != NUL; mb_ptr_adv(p))
7941 if (*p == '\'')
7942 ++len;
7943 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 s = r = alloc(len);
7945 if (r != NULL)
7946 {
7947 if (function)
7948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 r += 10;
7951 }
7952 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 if (str != NULL)
7955 for (p = str; *p != NUL; )
7956 {
7957 if (*p == '\'')
7958 *r++ = '\'';
7959 MB_COPY_CHAR(p, r);
7960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 if (function)
7963 *r++ = ')';
7964 *r++ = NUL;
7965 }
7966 return s;
7967}
7968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969#ifdef FEAT_FLOAT
7970/*
7971 * Convert the string "text" to a floating point number.
7972 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7973 * this always uses a decimal point.
7974 * Returns the length of the text that was consumed.
7975 */
7976 static int
7977string2float(text, value)
7978 char_u *text;
7979 float_T *value; /* result stored here */
7980{
7981 char *s = (char *)text;
7982 float_T f;
7983
7984 f = strtod(s, &s);
7985 *value = f;
7986 return (int)((char_u *)s - text);
7987}
7988#endif
7989
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 * Get the value of an environment variable.
7992 * "arg" is pointing to the '$'. It is advanced to after the name.
7993 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007994 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */
7996 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 int evaluate;
8001{
8002 char_u *string = NULL;
8003 int len;
8004 int cc;
8005 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008006 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
8008 ++*arg;
8009 name = *arg;
8010 len = get_env_len(arg);
8011 if (evaluate)
8012 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008013 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008014 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008015
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008016 cc = name[len];
8017 name[len] = NUL;
8018 /* first try vim_getenv(), fast for normal environment vars */
8019 string = vim_getenv(name, &mustfree);
8020 if (string != NULL && *string != NUL)
8021 {
8022 if (!mustfree)
8023 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008025 else
8026 {
8027 if (mustfree)
8028 vim_free(string);
8029
8030 /* next try expanding things like $VIM and ${HOME} */
8031 string = expand_env_save(name - 1);
8032 if (string != NULL && *string == '$')
8033 {
8034 vim_free(string);
8035 string = NULL;
8036 }
8037 }
8038 name[len] = cc;
8039
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040 rettv->v_type = VAR_STRING;
8041 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 }
8043
8044 return OK;
8045}
8046
8047/*
8048 * Array with names and number of arguments of all internal functions
8049 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8050 */
8051static struct fst
8052{
8053 char *f_name; /* function name */
8054 char f_min_argc; /* minimal number of arguments */
8055 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008057 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058} functions[] =
8059{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#ifdef FEAT_FLOAT
8061 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008062 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008064 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008065 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"append", 2, 2, f_append},
8067 {"argc", 0, 0, f_argc},
8068 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008069 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008070 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008074 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008077 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"bufexists", 1, 1, f_bufexists},
8079 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8080 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8081 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8082 {"buflisted", 1, 1, f_buflisted},
8083 {"bufloaded", 1, 1, f_bufloaded},
8084 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008085 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"bufwinnr", 1, 1, f_bufwinnr},
8087 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008088 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008089 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008090 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008091#ifdef FEAT_FLOAT
8092 {"ceil", 1, 1, f_ceil},
8093#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008094 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008095 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008097 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008099#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008100 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008101 {"complete_add", 1, 1, f_complete_add},
8102 {"complete_check", 0, 0, f_complete_check},
8103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008105 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#ifdef FEAT_FLOAT
8107 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008108 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008110 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008112 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008113 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"delete", 1, 1, f_delete},
8115 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008116 {"diff_filler", 1, 1, f_diff_filler},
8117 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008118 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008120 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"eventhandler", 0, 0, f_eventhandler},
8122 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008123 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008125#ifdef FEAT_FLOAT
8126 {"exp", 1, 1, f_exp},
8127#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008128 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008130 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8132 {"filereadable", 1, 1, f_filereadable},
8133 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008135 {"finddir", 1, 3, f_finddir},
8136 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#ifdef FEAT_FLOAT
8138 {"float2nr", 1, 1, f_float2nr},
8139 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008140 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008142 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"fnamemodify", 2, 2, f_fnamemodify},
8144 {"foldclosed", 1, 1, f_foldclosed},
8145 {"foldclosedend", 1, 1, f_foldclosedend},
8146 {"foldlevel", 1, 1, f_foldlevel},
8147 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008148 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008150 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008151 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008152 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008153 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008154 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"getchar", 0, 1, f_getchar},
8156 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008157 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"getcmdline", 0, 0, f_getcmdline},
8159 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008160 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008161 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008162 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008164 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008165 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"getfsize", 1, 1, f_getfsize},
8167 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008168 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008170 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008171 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008172 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008173 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008174 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008175 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008177 {"gettabvar", 2, 3, f_gettabvar},
8178 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"getwinposx", 0, 0, f_getwinposx},
8180 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008181 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008182 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008183 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008184 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008187 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008188 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8190 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8191 {"histadd", 2, 2, f_histadd},
8192 {"histdel", 1, 2, f_histdel},
8193 {"histget", 1, 2, f_histget},
8194 {"histnr", 1, 1, f_histnr},
8195 {"hlID", 1, 1, f_hlID},
8196 {"hlexists", 1, 1, f_hlexists},
8197 {"hostname", 0, 0, f_hostname},
8198 {"iconv", 3, 3, f_iconv},
8199 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008200 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008201 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008203 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"inputrestore", 0, 0, f_inputrestore},
8205 {"inputsave", 0, 0, f_inputsave},
8206 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008208 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008210 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008211 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008212 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008213 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"libcall", 3, 3, f_libcall},
8217 {"libcallnr", 3, 3, f_libcallnr},
8218 {"line", 1, 1, f_line},
8219 {"line2byte", 1, 1, f_line2byte},
8220 {"lispindent", 1, 1, f_lispindent},
8221 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008222#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008223 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008224 {"log10", 1, 1, f_log10},
8225#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008226#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008227 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008228#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008230 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008231 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008232 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008233 {"matchadd", 2, 5, f_matchadd},
8234 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008235 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008236 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008237 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008238 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008239 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008240 {"max", 1, 1, f_max},
8241 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008242#ifdef vim_mkdir
8243 {"mkdir", 1, 3, f_mkdir},
8244#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008246#ifdef FEAT_MZSCHEME
8247 {"mzeval", 1, 1, f_mzeval},
8248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008250 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008251 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008252 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008253#ifdef FEAT_FLOAT
8254 {"pow", 2, 2, f_pow},
8255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008257 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008258 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008259#ifdef FEAT_PYTHON3
8260 {"py3eval", 1, 1, f_py3eval},
8261#endif
8262#ifdef FEAT_PYTHON
8263 {"pyeval", 1, 1, f_pyeval},
8264#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008266 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008267 {"reltime", 0, 2, f_reltime},
8268 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"remote_expr", 2, 3, f_remote_expr},
8270 {"remote_foreground", 1, 1, f_remote_foreground},
8271 {"remote_peek", 1, 2, f_remote_peek},
8272 {"remote_read", 1, 1, f_remote_read},
8273 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008274 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008276 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008278 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279#ifdef FEAT_FLOAT
8280 {"round", 1, 1, f_round},
8281#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008282 {"screenattr", 2, 2, f_screenattr},
8283 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008284 {"screencol", 0, 0, f_screencol},
8285 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008286 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008287 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008288 {"searchpair", 3, 7, f_searchpair},
8289 {"searchpairpos", 3, 7, f_searchpairpos},
8290 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"server2client", 2, 2, f_server2client},
8292 {"serverlist", 0, 0, f_serverlist},
8293 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008294 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"setcmdpos", 1, 1, f_setcmdpos},
8296 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008297 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008298 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008299 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008300 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008302 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008303 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008305#ifdef FEAT_CRYPT
8306 {"sha256", 1, 1, f_sha256},
8307#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008308 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008309 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#ifdef FEAT_FLOAT
8312 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008313 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008314#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008315 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008316 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008317 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008318 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008319 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008320#ifdef FEAT_FLOAT
8321 {"sqrt", 1, 1, f_sqrt},
8322 {"str2float", 1, 1, f_str2float},
8323#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008324 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008325 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008326 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327#ifdef HAVE_STRFTIME
8328 {"strftime", 1, 2, f_strftime},
8329#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"strlen", 1, 1, f_strlen},
8333 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008334 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008336 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008337 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"substitute", 4, 4, f_substitute},
8339 {"synID", 3, 3, f_synID},
8340 {"synIDattr", 2, 3, f_synIDattr},
8341 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008342 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008343 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008344 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008345 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008346 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008347 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008348 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008349 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008350 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008351#ifdef FEAT_FLOAT
8352 {"tan", 1, 1, f_tan},
8353 {"tanh", 1, 1, f_tanh},
8354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008356 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"tolower", 1, 1, f_tolower},
8358 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008359 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008360#ifdef FEAT_FLOAT
8361 {"trunc", 1, 1, f_trunc},
8362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008364 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008365 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008366 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008367 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"virtcol", 1, 1, f_virtcol},
8369 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008370 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"winbufnr", 1, 1, f_winbufnr},
8372 {"wincol", 0, 0, f_wincol},
8373 {"winheight", 1, 1, f_winheight},
8374 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008375 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008377 {"winrestview", 1, 1, f_winrestview},
8378 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008380 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008381 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382};
8383
8384#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8385
8386/*
8387 * Function given to ExpandGeneric() to obtain the list of internal
8388 * or user defined function names.
8389 */
8390 char_u *
8391get_function_name(xp, idx)
8392 expand_T *xp;
8393 int idx;
8394{
8395 static int intidx = -1;
8396 char_u *name;
8397
8398 if (idx == 0)
8399 intidx = -1;
8400 if (intidx < 0)
8401 {
8402 name = get_user_func_name(xp, idx);
8403 if (name != NULL)
8404 return name;
8405 }
8406 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8407 {
8408 STRCPY(IObuff, functions[intidx].f_name);
8409 STRCAT(IObuff, "(");
8410 if (functions[intidx].f_max_argc == 0)
8411 STRCAT(IObuff, ")");
8412 return IObuff;
8413 }
8414
8415 return NULL;
8416}
8417
8418/*
8419 * Function given to ExpandGeneric() to obtain the list of internal or
8420 * user defined variable or function names.
8421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 char_u *
8423get_expr_name(xp, idx)
8424 expand_T *xp;
8425 int idx;
8426{
8427 static int intidx = -1;
8428 char_u *name;
8429
8430 if (idx == 0)
8431 intidx = -1;
8432 if (intidx < 0)
8433 {
8434 name = get_function_name(xp, idx);
8435 if (name != NULL)
8436 return name;
8437 }
8438 return get_user_var_name(xp, ++intidx);
8439}
8440
8441#endif /* FEAT_CMDL_COMPL */
8442
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008443#if defined(EBCDIC) || defined(PROTO)
8444/*
8445 * Compare struct fst by function name.
8446 */
8447 static int
8448compare_func_name(s1, s2)
8449 const void *s1;
8450 const void *s2;
8451{
8452 struct fst *p1 = (struct fst *)s1;
8453 struct fst *p2 = (struct fst *)s2;
8454
8455 return STRCMP(p1->f_name, p2->f_name);
8456}
8457
8458/*
8459 * Sort the function table by function name.
8460 * The sorting of the table above is ASCII dependant.
8461 * On machines using EBCDIC we have to sort it.
8462 */
8463 static void
8464sortFunctions()
8465{
8466 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8467
8468 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8469}
8470#endif
8471
8472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473/*
8474 * Find internal function in table above.
8475 * Return index, or -1 if not found
8476 */
8477 static int
8478find_internal_func(name)
8479 char_u *name; /* name of the function */
8480{
8481 int first = 0;
8482 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8483 int cmp;
8484 int x;
8485
8486 /*
8487 * Find the function name in the table. Binary search.
8488 */
8489 while (first <= last)
8490 {
8491 x = first + ((unsigned)(last - first) >> 1);
8492 cmp = STRCMP(name, functions[x].f_name);
8493 if (cmp < 0)
8494 last = x - 1;
8495 else if (cmp > 0)
8496 first = x + 1;
8497 else
8498 return x;
8499 }
8500 return -1;
8501}
8502
8503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8505 * name it contains, otherwise return "name".
8506 */
8507 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008508deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 char_u *name;
8510 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008511 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512{
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 int cc;
8515
8516 cc = name[*lenp];
8517 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008518 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008522 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008523 {
8524 *lenp = 0;
8525 return (char_u *)""; /* just in case */
8526 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008527 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 }
8530
8531 return name;
8532}
8533
8534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 * Allocate a variable for the result of a function.
8536 * Return OK or FAIL.
8537 */
8538 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8540 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 char_u *name; /* name of the function */
8542 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 char_u **arg; /* argument, pointing to the '(' */
8545 linenr_T firstline; /* first line of range */
8546 linenr_T lastline; /* last line of range */
8547 int *doesrange; /* return: function handled range */
8548 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 char_u *argp;
8552 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008553 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 int argcount = 0; /* number of arguments found */
8555
8556 /*
8557 * Get the arguments.
8558 */
8559 argp = *arg;
8560 while (argcount < MAX_FUNC_ARGS)
8561 {
8562 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8563 if (*argp == ')' || *argp == ',' || *argp == NUL)
8564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8566 {
8567 ret = FAIL;
8568 break;
8569 }
8570 ++argcount;
8571 if (*argp != ',')
8572 break;
8573 }
8574 if (*argp == ')')
8575 ++argp;
8576 else
8577 ret = FAIL;
8578
8579 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 {
8584 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008585 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008587 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 *arg = skipwhite(argp);
8594 return ret;
8595}
8596
8597
8598/*
8599 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008600 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008601 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 */
8603 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008604call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008606 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008610 typval_T *argvars; /* vars for arguments, must have "argcount"
8611 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 linenr_T firstline; /* first line of range */
8613 linenr_T lastline; /* last line of range */
8614 int *doesrange; /* return: function handled range */
8615 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617{
8618 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619#define ERROR_UNKNOWN 0
8620#define ERROR_TOOMANY 1
8621#define ERROR_TOOFEW 2
8622#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623#define ERROR_DICT 4
8624#define ERROR_NONE 5
8625#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 int error = ERROR_NONE;
8627 int i;
8628 int llen;
8629 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630#define FLEN_FIXED 40
8631 char_u fname_buf[FLEN_FIXED + 1];
8632 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008633 char_u *name;
8634
8635 /* Make a copy of the name, if it comes from a funcref variable it could
8636 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008637 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008638 if (name == NULL)
8639 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 /*
8642 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8643 * Change <SNR>123_name() to K_SNR 123_name().
8644 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 llen = eval_fname_script(name);
8647 if (llen > 0)
8648 {
8649 fname_buf[0] = K_SPECIAL;
8650 fname_buf[1] = KS_EXTRA;
8651 fname_buf[2] = (int)KE_SNR;
8652 i = 3;
8653 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8654 {
8655 if (current_SID <= 0)
8656 error = ERROR_SCRIPT;
8657 else
8658 {
8659 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8660 i = (int)STRLEN(fname_buf);
8661 }
8662 }
8663 if (i + STRLEN(name + llen) < FLEN_FIXED)
8664 {
8665 STRCPY(fname_buf + i, name + llen);
8666 fname = fname_buf;
8667 }
8668 else
8669 {
8670 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8671 if (fname == NULL)
8672 error = ERROR_OTHER;
8673 else
8674 {
8675 mch_memmove(fname, fname_buf, (size_t)i);
8676 STRCPY(fname + i, name + llen);
8677 }
8678 }
8679 }
8680 else
8681 fname = name;
8682
8683 *doesrange = FALSE;
8684
8685
8686 /* execute the function if no errors detected and executing */
8687 if (evaluate && error == ERROR_NONE)
8688 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008689 char_u *rfname = fname;
8690
8691 /* Ignore "g:" before a function name. */
8692 if (fname[0] == 'g' && fname[1] == ':')
8693 rfname = fname + 2;
8694
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008695 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8696 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 error = ERROR_UNKNOWN;
8698
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {
8701 /*
8702 * User defined function.
8703 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008704 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008707 /* Trigger FuncUndefined event, may load the function. */
8708 if (fp == NULL
8709 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008710 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008711 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008713 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008717 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 {
8720 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008721 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008722 }
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (fp != NULL)
8725 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008726 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008728 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008730 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008733 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 else
8735 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008736 int did_save_redo = FALSE;
8737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 /*
8739 * Call the user function.
8740 * Save and restore search patterns, script variables and
8741 * redo buffer.
8742 */
8743 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008744 if (!ins_compl_active())
8745 {
8746 saveRedobuff();
8747 did_save_redo = TRUE;
8748 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8753 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8754 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008755 /* Function was unreferenced while being used, free it
8756 * now. */
8757 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008758 if (did_save_redo)
8759 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 restore_search_patterns();
8761 error = ERROR_NONE;
8762 }
8763 }
8764 }
8765 else
8766 {
8767 /*
8768 * Find the function name in the table, call its implementation.
8769 */
8770 i = find_internal_func(fname);
8771 if (i >= 0)
8772 {
8773 if (argcount < functions[i].f_min_argc)
8774 error = ERROR_TOOFEW;
8775 else if (argcount > functions[i].f_max_argc)
8776 error = ERROR_TOOMANY;
8777 else
8778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008779 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 error = ERROR_NONE;
8782 }
8783 }
8784 }
8785 /*
8786 * The function call (or "FuncUndefined" autocommand sequence) might
8787 * have been aborted by an error, an interrupt, or an explicitly thrown
8788 * exception that has not been caught so far. This situation can be
8789 * tested for by calling aborting(). For an error in an internal
8790 * function or for the "E132" error in call_user_func(), however, the
8791 * throw point at which the "force_abort" flag (temporarily reset by
8792 * emsg()) is normally updated has not been reached yet. We need to
8793 * update that flag first to make aborting() reliable.
8794 */
8795 update_force_abort();
8796 }
8797 if (error == ERROR_NONE)
8798 ret = OK;
8799
8800 /*
8801 * Report an error unless the argument evaluation or function call has been
8802 * cancelled due to an aborting error, an interrupt, or an exception.
8803 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008804 if (!aborting())
8805 {
8806 switch (error)
8807 {
8808 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008809 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008810 break;
8811 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008812 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 break;
8814 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008815 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008816 name);
8817 break;
8818 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008819 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008820 name);
8821 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008822 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008823 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008824 name);
8825 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008826 }
8827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (fname != name && fname != fname_buf)
8830 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008831 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 return ret;
8834}
8835
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836/*
8837 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008838 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008839 */
8840 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008841emsg_funcname(ermsg, name)
8842 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843 char_u *name;
8844{
8845 char_u *p;
8846
8847 if (*name == K_SPECIAL)
8848 p = concat_str((char_u *)"<SNR>", name + 3);
8849 else
8850 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008851 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008852 if (p != name)
8853 vim_free(p);
8854}
8855
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008856/*
8857 * Return TRUE for a non-zero Number and a non-empty String.
8858 */
8859 static int
8860non_zero_arg(argvars)
8861 typval_T *argvars;
8862{
8863 return ((argvars[0].v_type == VAR_NUMBER
8864 && argvars[0].vval.v_number != 0)
8865 || (argvars[0].v_type == VAR_STRING
8866 && argvars[0].vval.v_string != NULL
8867 && *argvars[0].vval.v_string != NUL));
8868}
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870/*********************************************
8871 * Implementation of the built-in functions
8872 */
8873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008875static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8876
8877/*
8878 * Get the float value of "argvars[0]" into "f".
8879 * Returns FAIL when the argument is not a Number or Float.
8880 */
8881 static int
8882get_float_arg(argvars, f)
8883 typval_T *argvars;
8884 float_T *f;
8885{
8886 if (argvars[0].v_type == VAR_FLOAT)
8887 {
8888 *f = argvars[0].vval.v_float;
8889 return OK;
8890 }
8891 if (argvars[0].v_type == VAR_NUMBER)
8892 {
8893 *f = (float_T)argvars[0].vval.v_number;
8894 return OK;
8895 }
8896 EMSG(_("E808: Number or Float required"));
8897 return FAIL;
8898}
8899
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008900/*
8901 * "abs(expr)" function
8902 */
8903 static void
8904f_abs(argvars, rettv)
8905 typval_T *argvars;
8906 typval_T *rettv;
8907{
8908 if (argvars[0].v_type == VAR_FLOAT)
8909 {
8910 rettv->v_type = VAR_FLOAT;
8911 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8912 }
8913 else
8914 {
8915 varnumber_T n;
8916 int error = FALSE;
8917
8918 n = get_tv_number_chk(&argvars[0], &error);
8919 if (error)
8920 rettv->vval.v_number = -1;
8921 else if (n > 0)
8922 rettv->vval.v_number = n;
8923 else
8924 rettv->vval.v_number = -n;
8925 }
8926}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008927
8928/*
8929 * "acos()" function
8930 */
8931 static void
8932f_acos(argvars, rettv)
8933 typval_T *argvars;
8934 typval_T *rettv;
8935{
8936 float_T f;
8937
8938 rettv->v_type = VAR_FLOAT;
8939 if (get_float_arg(argvars, &f) == OK)
8940 rettv->vval.v_float = acos(f);
8941 else
8942 rettv->vval.v_float = 0.0;
8943}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#endif
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008947 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 */
8949 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008950f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 typval_T *argvars;
8952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
Bram Moolenaar33570922005-01-25 22:26:29 +00008954 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008959 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008960 && !tv_check_lock(l->lv_lock,
8961 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008962 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 }
8965 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008966 EMSG(_(e_listreq));
8967}
8968
8969/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008970 * "and(expr, expr)" function
8971 */
8972 static void
8973f_and(argvars, rettv)
8974 typval_T *argvars;
8975 typval_T *rettv;
8976{
8977 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8978 & get_tv_number_chk(&argvars[1], NULL);
8979}
8980
8981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008982 * "append(lnum, string/list)" function
8983 */
8984 static void
8985f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *argvars;
8987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008988{
8989 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 list_T *l = NULL;
8992 listitem_T *li = NULL;
8993 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 long added = 0;
8995
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008996 /* When coming here from Insert mode, sync undo, so that this can be
8997 * undone separately from what was previously inserted. */
8998 if (u_sync_once == 2)
8999 {
9000 u_sync_once = 1; /* notify that u_sync() was called */
9001 u_sync(TRUE);
9002 }
9003
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 lnum = get_tv_lnum(argvars);
9005 if (lnum >= 0
9006 && lnum <= curbuf->b_ml.ml_line_count
9007 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009009 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011 l = argvars[1].vval.v_list;
9012 if (l == NULL)
9013 return;
9014 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 for (;;)
9017 {
9018 if (l == NULL)
9019 tv = &argvars[1]; /* append a string */
9020 else if (li == NULL)
9021 break; /* end of list */
9022 else
9023 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 line = get_tv_string_chk(tv);
9025 if (line == NULL) /* type error */
9026 {
9027 rettv->vval.v_number = 1; /* Failed */
9028 break;
9029 }
9030 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031 ++added;
9032 if (l == NULL)
9033 break;
9034 li = li->li_next;
9035 }
9036
9037 appended_lines_mark(lnum, added);
9038 if (curwin->w_cursor.lnum > lnum)
9039 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 else
9042 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043}
9044
9045/*
9046 * "argc()" function
9047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009050 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054}
9055
9056/*
9057 * "argidx()" function
9058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065}
9066
9067/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009068 * "arglistid()" function
9069 */
9070 static void
9071f_arglistid(argvars, rettv)
9072 typval_T *argvars UNUSED;
9073 typval_T *rettv;
9074{
9075 win_T *wp;
9076 tabpage_T *tp = NULL;
9077 long n;
9078
9079 rettv->vval.v_number = -1;
9080 if (argvars[0].v_type != VAR_UNKNOWN)
9081 {
9082 if (argvars[1].v_type != VAR_UNKNOWN)
9083 {
9084 n = get_tv_number(&argvars[1]);
9085 if (n >= 0)
9086 tp = find_tabpage(n);
9087 }
9088 else
9089 tp = curtab;
9090
9091 if (tp != NULL)
9092 {
9093 wp = find_win_by_nr(&argvars[0], tp);
9094 if (wp != NULL)
9095 rettv->vval.v_number = wp->w_alist->id;
9096 }
9097 }
9098 else
9099 rettv->vval.v_number = curwin->w_alist->id;
9100}
9101
9102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * "argv(nr)" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110 int idx;
9111
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009112 if (argvars[0].v_type != VAR_UNKNOWN)
9113 {
9114 idx = get_tv_number_chk(&argvars[0], NULL);
9115 if (idx >= 0 && idx < ARGCOUNT)
9116 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9117 else
9118 rettv->vval.v_string = NULL;
9119 rettv->v_type = VAR_STRING;
9120 }
9121 else if (rettv_list_alloc(rettv) == OK)
9122 for (idx = 0; idx < ARGCOUNT; ++idx)
9123 list_append_string(rettv->vval.v_list,
9124 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009127#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009128/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009129 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009130 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009131 static void
9132f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009133 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009134 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009135{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009136 float_T f;
9137
9138 rettv->v_type = VAR_FLOAT;
9139 if (get_float_arg(argvars, &f) == OK)
9140 rettv->vval.v_float = asin(f);
9141 else
9142 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009143}
9144
9145/*
9146 * "atan()" function
9147 */
9148 static void
9149f_atan(argvars, rettv)
9150 typval_T *argvars;
9151 typval_T *rettv;
9152{
9153 float_T f;
9154
9155 rettv->v_type = VAR_FLOAT;
9156 if (get_float_arg(argvars, &f) == OK)
9157 rettv->vval.v_float = atan(f);
9158 else
9159 rettv->vval.v_float = 0.0;
9160}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009161
9162/*
9163 * "atan2()" function
9164 */
9165 static void
9166f_atan2(argvars, rettv)
9167 typval_T *argvars;
9168 typval_T *rettv;
9169{
9170 float_T fx, fy;
9171
9172 rettv->v_type = VAR_FLOAT;
9173 if (get_float_arg(argvars, &fx) == OK
9174 && get_float_arg(&argvars[1], &fy) == OK)
9175 rettv->vval.v_float = atan2(fx, fy);
9176 else
9177 rettv->vval.v_float = 0.0;
9178}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009179#endif
9180
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181/*
9182 * "browse(save, title, initdir, default)" function
9183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189#ifdef FEAT_BROWSE
9190 int save;
9191 char_u *title;
9192 char_u *initdir;
9193 char_u *defname;
9194 char_u buf[NUMBUFLEN];
9195 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 save = get_tv_number_chk(&argvars[0], &error);
9199 title = get_tv_string_chk(&argvars[1]);
9200 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9201 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 if (error || title == NULL || initdir == NULL || defname == NULL)
9204 rettv->vval.v_string = NULL;
9205 else
9206 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009207 do_browse(save ? BROWSE_SAVE : 0,
9208 title, defname, NULL, initdir, NULL, curbuf);
9209#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009213}
9214
9215/*
9216 * "browsedir(title, initdir)" function
9217 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009220 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009221 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009222{
9223#ifdef FEAT_BROWSE
9224 char_u *title;
9225 char_u *initdir;
9226 char_u buf[NUMBUFLEN];
9227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 title = get_tv_string_chk(&argvars[0]);
9229 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 if (title == NULL || initdir == NULL)
9232 rettv->vval.v_string = NULL;
9233 else
9234 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009235 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241
Bram Moolenaar33570922005-01-25 22:26:29 +00009242static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009243
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244/*
9245 * Find a buffer by number or exact name.
9246 */
9247 static buf_T *
9248find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009249 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009253 if (avar->v_type == VAR_NUMBER)
9254 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009255 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009257 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009258 if (buf == NULL)
9259 {
9260 /* No full path name match, try a match with a URL or a "nofile"
9261 * buffer, these don't use the full path. */
9262 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9263 if (buf->b_fname != NULL
9264 && (path_with_url(buf->b_fname)
9265#ifdef FEAT_QUICKFIX
9266 || bt_nofile(buf)
9267#endif
9268 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009269 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009270 break;
9271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 }
9273 return buf;
9274}
9275
9276/*
9277 * "bufexists(expr)" function
9278 */
9279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 typval_T *argvars;
9282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285}
9286
9287/*
9288 * "buflisted(expr)" function
9289 */
9290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009292 typval_T *argvars;
9293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
9295 buf_T *buf;
9296
9297 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299}
9300
9301/*
9302 * "bufloaded(expr)" function
9303 */
9304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 typval_T *argvars;
9307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 buf_T *buf;
9310
9311 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
9314
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009315static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317/*
9318 * Get buffer by number or pattern.
9319 */
9320 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009321get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009322 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009323 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 int save_magic;
9327 char_u *save_cpo;
9328 buf_T *buf;
9329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 if (tv->v_type == VAR_NUMBER)
9331 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009332 if (tv->v_type != VAR_STRING)
9333 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 if (name == NULL || *name == NUL)
9335 return curbuf;
9336 if (name[0] == '$' && name[1] == NUL)
9337 return lastbuf;
9338
9339 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9340 save_magic = p_magic;
9341 p_magic = TRUE;
9342 save_cpo = p_cpo;
9343 p_cpo = (char_u *)"";
9344
9345 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009346 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 p_magic = save_magic;
9349 p_cpo = save_cpo;
9350
9351 /* If not found, try expanding the name, like done for bufexists(). */
9352 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 return buf;
9356}
9357
9358/*
9359 * "bufname(expr)" function
9360 */
9361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 buf_T *buf;
9367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009370 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 --emsg_off;
9377}
9378
9379/*
9380 * "bufnr(expr)" function
9381 */
9382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *argvars;
9385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009388 int error = FALSE;
9389 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009391 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009393 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009394 --emsg_off;
9395
9396 /* If the buffer isn't found and the second argument is not zero create a
9397 * new buffer. */
9398 if (buf == NULL
9399 && argvars[1].v_type != VAR_UNKNOWN
9400 && get_tv_number_chk(&argvars[1], &error) != 0
9401 && !error
9402 && (name = get_tv_string_chk(&argvars[0])) != NULL
9403 && !error)
9404 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410}
9411
9412/*
9413 * "bufwinnr(nr)" function
9414 */
9415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 typval_T *argvars;
9418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_WINDOWS
9421 win_T *wp;
9422 int winnr = 0;
9423#endif
9424 buf_T *buf;
9425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009426 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009428 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#ifdef FEAT_WINDOWS
9430 for (wp = firstwin; wp; wp = wp->w_next)
9431 {
9432 ++winnr;
9433 if (wp->w_buffer == buf)
9434 break;
9435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#endif
9440 --emsg_off;
9441}
9442
9443/*
9444 * "byte2line(byte)" function
9445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#else
9454 long boff = 0;
9455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 (linenr_T)0, &boff);
9462#endif
9463}
9464
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009465 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009466byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009469 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009470{
9471#ifdef FEAT_MBYTE
9472 char_u *t;
9473#endif
9474 char_u *str;
9475 long idx;
9476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 str = get_tv_string_chk(&argvars[0]);
9478 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009481 return;
9482
9483#ifdef FEAT_MBYTE
9484 t = str;
9485 for ( ; idx > 0; idx--)
9486 {
9487 if (*t == NUL) /* EOL reached */
9488 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009489 if (enc_utf8 && comp)
9490 t += utf_ptr2len(t);
9491 else
9492 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009493 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009494 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009495#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009498#endif
9499}
9500
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009501/*
9502 * "byteidx()" function
9503 */
9504 static void
9505f_byteidx(argvars, rettv)
9506 typval_T *argvars;
9507 typval_T *rettv;
9508{
9509 byteidx(argvars, rettv, FALSE);
9510}
9511
9512/*
9513 * "byteidxcomp()" function
9514 */
9515 static void
9516f_byteidxcomp(argvars, rettv)
9517 typval_T *argvars;
9518 typval_T *rettv;
9519{
9520 byteidx(argvars, rettv, TRUE);
9521}
9522
Bram Moolenaardb913952012-06-29 12:54:53 +02009523 int
9524func_call(name, args, selfdict, rettv)
9525 char_u *name;
9526 typval_T *args;
9527 dict_T *selfdict;
9528 typval_T *rettv;
9529{
9530 listitem_T *item;
9531 typval_T argv[MAX_FUNC_ARGS + 1];
9532 int argc = 0;
9533 int dummy;
9534 int r = 0;
9535
9536 for (item = args->vval.v_list->lv_first; item != NULL;
9537 item = item->li_next)
9538 {
9539 if (argc == MAX_FUNC_ARGS)
9540 {
9541 EMSG(_("E699: Too many arguments"));
9542 break;
9543 }
9544 /* Make a copy of each argument. This is needed to be able to set
9545 * v_lock to VAR_FIXED in the copy without changing the original list.
9546 */
9547 copy_tv(&item->li_tv, &argv[argc++]);
9548 }
9549
9550 if (item == NULL)
9551 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9552 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9553 &dummy, TRUE, selfdict);
9554
9555 /* Free the arguments. */
9556 while (argc > 0)
9557 clear_tv(&argv[--argc]);
9558
9559 return r;
9560}
9561
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009563 * "call(func, arglist)" function
9564 */
9565 static void
9566f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009569{
9570 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009571 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009572
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009573 if (argvars[1].v_type != VAR_LIST)
9574 {
9575 EMSG(_(e_listreq));
9576 return;
9577 }
9578 if (argvars[1].vval.v_list == NULL)
9579 return;
9580
9581 if (argvars[0].v_type == VAR_FUNC)
9582 func = argvars[0].vval.v_string;
9583 else
9584 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 if (*func == NUL)
9586 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009587
Bram Moolenaare9a41262005-01-15 22:18:47 +00009588 if (argvars[2].v_type != VAR_UNKNOWN)
9589 {
9590 if (argvars[2].v_type != VAR_DICT)
9591 {
9592 EMSG(_(e_dictreq));
9593 return;
9594 }
9595 selfdict = argvars[2].vval.v_dict;
9596 }
9597
Bram Moolenaardb913952012-06-29 12:54:53 +02009598 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599}
9600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009601#ifdef FEAT_FLOAT
9602/*
9603 * "ceil({float})" function
9604 */
9605 static void
9606f_ceil(argvars, rettv)
9607 typval_T *argvars;
9608 typval_T *rettv;
9609{
9610 float_T f;
9611
9612 rettv->v_type = VAR_FLOAT;
9613 if (get_float_arg(argvars, &f) == OK)
9614 rettv->vval.v_float = ceil(f);
9615 else
9616 rettv->vval.v_float = 0.0;
9617}
9618#endif
9619
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009620/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009621 * "changenr()" function
9622 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009623 static void
9624f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009625 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009626 typval_T *rettv;
9627{
9628 rettv->vval.v_number = curbuf->b_u_seq_cur;
9629}
9630
9631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 * "char2nr(string)" function
9633 */
9634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639#ifdef FEAT_MBYTE
9640 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009641 {
9642 int utf8 = 0;
9643
9644 if (argvars[1].v_type != VAR_UNKNOWN)
9645 utf8 = get_tv_number_chk(&argvars[1], NULL);
9646
9647 if (utf8)
9648 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9649 else
9650 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 else
9653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655}
9656
9657/*
9658 * "cindent(lnum)" function
9659 */
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665#ifdef FEAT_CINDENT
9666 pos_T pos;
9667 linenr_T lnum;
9668
9669 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9672 {
9673 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 curwin->w_cursor = pos;
9676 }
9677 else
9678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681
9682/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009683 * "clearmatches()" function
9684 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009685 static void
9686f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009687 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009688 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009689{
9690#ifdef FEAT_SEARCH_EXTRA
9691 clear_matches(curwin);
9692#endif
9693}
9694
9695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 * "col(string)" function
9697 */
9698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 typval_T *argvars;
9701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 colnr_T col = 0;
9704 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009705 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009707 fp = var2fpos(&argvars[0], FALSE, &fnum);
9708 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 {
9710 if (fp->col == MAXCOL)
9711 {
9712 /* '> can be MAXCOL, get the length of the line then */
9713 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009714 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
9716 col = MAXCOL;
9717 }
9718 else
9719 {
9720 col = fp->col + 1;
9721#ifdef FEAT_VIRTUALEDIT
9722 /* col(".") when the cursor is on the NUL at the end of the line
9723 * because of "coladd" can be seen as an extra column. */
9724 if (virtual_active() && fp == &curwin->w_cursor)
9725 {
9726 char_u *p = ml_get_cursor();
9727
9728 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9729 curwin->w_virtcol - curwin->w_cursor.coladd))
9730 {
9731# ifdef FEAT_MBYTE
9732 int l;
9733
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009734 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 col += l;
9736# else
9737 if (*p != NUL && p[1] == NUL)
9738 ++col;
9739# endif
9740 }
9741 }
9742#endif
9743 }
9744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747
Bram Moolenaar572cb562005-08-05 21:35:02 +00009748#if defined(FEAT_INS_EXPAND)
9749/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009750 * "complete()" function
9751 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009752 static void
9753f_complete(argvars, rettv)
9754 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009755 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009756{
9757 int startcol;
9758
9759 if ((State & INSERT) == 0)
9760 {
9761 EMSG(_("E785: complete() can only be used in Insert mode"));
9762 return;
9763 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009764
9765 /* Check for undo allowed here, because if something was already inserted
9766 * the line was already saved for undo and this check isn't done. */
9767 if (!undo_allowed())
9768 return;
9769
Bram Moolenaarade00832006-03-10 21:46:58 +00009770 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9771 {
9772 EMSG(_(e_invarg));
9773 return;
9774 }
9775
9776 startcol = get_tv_number_chk(&argvars[0], NULL);
9777 if (startcol <= 0)
9778 return;
9779
9780 set_completion(startcol - 1, argvars[1].vval.v_list);
9781}
9782
9783/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009784 * "complete_add()" function
9785 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009786 static void
9787f_complete_add(argvars, rettv)
9788 typval_T *argvars;
9789 typval_T *rettv;
9790{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009791 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009792}
9793
9794/*
9795 * "complete_check()" function
9796 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009797 static void
9798f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009799 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009800 typval_T *rettv;
9801{
9802 int saved = RedrawingDisabled;
9803
9804 RedrawingDisabled = 0;
9805 ins_compl_check_keys(0);
9806 rettv->vval.v_number = compl_interrupted;
9807 RedrawingDisabled = saved;
9808}
9809#endif
9810
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811/*
9812 * "confirm(message, buttons[, default [, type]])" function
9813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009816 typval_T *argvars UNUSED;
9817 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9820 char_u *message;
9821 char_u *buttons = NULL;
9822 char_u buf[NUMBUFLEN];
9823 char_u buf2[NUMBUFLEN];
9824 int def = 1;
9825 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 char_u *typestr;
9827 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 message = get_tv_string_chk(&argvars[0]);
9830 if (message == NULL)
9831 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9835 if (buttons == NULL)
9836 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009840 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9843 if (typestr == NULL)
9844 error = TRUE;
9845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 switch (TOUPPER_ASC(*typestr))
9848 {
9849 case 'E': type = VIM_ERROR; break;
9850 case 'Q': type = VIM_QUESTION; break;
9851 case 'I': type = VIM_INFO; break;
9852 case 'W': type = VIM_WARNING; break;
9853 case 'G': type = VIM_GENERIC; break;
9854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 }
9856 }
9857 }
9858 }
9859
9860 if (buttons == NULL || *buttons == NUL)
9861 buttons = (char_u *)_("&Ok");
9862
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009863 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009865 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#endif
9867}
9868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869/*
9870 * "copy()" function
9871 */
9872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009874 typval_T *argvars;
9875 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009876{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009877 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009880#ifdef FEAT_FLOAT
9881/*
9882 * "cos()" function
9883 */
9884 static void
9885f_cos(argvars, rettv)
9886 typval_T *argvars;
9887 typval_T *rettv;
9888{
9889 float_T f;
9890
9891 rettv->v_type = VAR_FLOAT;
9892 if (get_float_arg(argvars, &f) == OK)
9893 rettv->vval.v_float = cos(f);
9894 else
9895 rettv->vval.v_float = 0.0;
9896}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009897
9898/*
9899 * "cosh()" function
9900 */
9901 static void
9902f_cosh(argvars, rettv)
9903 typval_T *argvars;
9904 typval_T *rettv;
9905{
9906 float_T f;
9907
9908 rettv->v_type = VAR_FLOAT;
9909 if (get_float_arg(argvars, &f) == OK)
9910 rettv->vval.v_float = cosh(f);
9911 else
9912 rettv->vval.v_float = 0.0;
9913}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009914#endif
9915
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917 * "count()" function
9918 */
9919 static void
9920f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009921 typval_T *argvars;
9922 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009923{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009924 long n = 0;
9925 int ic = FALSE;
9926
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009928 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009929 listitem_T *li;
9930 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 if ((l = argvars[0].vval.v_list) != NULL)
9934 {
9935 li = l->lv_first;
9936 if (argvars[2].v_type != VAR_UNKNOWN)
9937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 int error = FALSE;
9939
9940 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009941 if (argvars[3].v_type != VAR_UNKNOWN)
9942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 idx = get_tv_number_chk(&argvars[3], &error);
9944 if (!error)
9945 {
9946 li = list_find(l, idx);
9947 if (li == NULL)
9948 EMSGN(_(e_listidx), idx);
9949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 if (error)
9952 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 }
9954
9955 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009956 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 ++n;
9958 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 else if (argvars[0].v_type == VAR_DICT)
9961 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 int todo;
9963 dict_T *d;
9964 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009965
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009966 if ((d = argvars[0].vval.v_dict) != NULL)
9967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968 int error = FALSE;
9969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 if (argvars[2].v_type != VAR_UNKNOWN)
9971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 if (argvars[3].v_type != VAR_UNKNOWN)
9974 EMSG(_(e_invarg));
9975 }
9976
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009977 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009978 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009979 {
9980 if (!HASHITEM_EMPTY(hi))
9981 {
9982 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009983 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009984 ++n;
9985 }
9986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 }
9988 }
9989 else
9990 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009991 rettv->vval.v_number = n;
9992}
9993
9994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9996 *
9997 * Checks the existence of a cscope connection.
9998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010001 typval_T *argvars UNUSED;
10002 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004#ifdef FEAT_CSCOPE
10005 int num = 0;
10006 char_u *dbpath = NULL;
10007 char_u *prepend = NULL;
10008 char_u buf[NUMBUFLEN];
10009
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010010 if (argvars[0].v_type != VAR_UNKNOWN
10011 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 num = (int)get_tv_number(&argvars[0]);
10014 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 }
10018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020#endif
10021}
10022
10023/*
10024 * "cursor(lnum, col)" function
10025 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010026 * Moves the cursor to the specified line and column.
10027 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010031 typval_T *argvars;
10032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033{
10034 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010035#ifdef FEAT_VIRTUALEDIT
10036 long coladd = 0;
10037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010039 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010040 if (argvars[1].v_type == VAR_UNKNOWN)
10041 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010042 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010043 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010044
Bram Moolenaar493c1782014-05-28 14:34:46 +020010045 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010046 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010047 line = pos.lnum;
10048 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010049#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010050 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010051#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010052 if (curswant >= 0)
10053 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010054 }
10055 else
10056 {
10057 line = get_tv_lnum(argvars);
10058 col = get_tv_number_chk(&argvars[1], NULL);
10059#ifdef FEAT_VIRTUALEDIT
10060 if (argvars[2].v_type != VAR_UNKNOWN)
10061 coladd = get_tv_number_chk(&argvars[2], NULL);
10062#endif
10063 }
10064 if (line < 0 || col < 0
10065#ifdef FEAT_VIRTUALEDIT
10066 || coladd < 0
10067#endif
10068 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (line > 0)
10071 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (col > 0)
10073 curwin->w_cursor.col = col - 1;
10074#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010075 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
10077
10078 /* Make sure the cursor is in a valid position. */
10079 check_cursor();
10080#ifdef FEAT_MBYTE
10081 /* Correct cursor for multi-byte character. */
10082 if (has_mbyte)
10083 mb_adjust_cursor();
10084#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010085
10086 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010087 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088}
10089
10090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 */
10093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010095 typval_T *argvars;
10096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010098 int noref = 0;
10099
10100 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010102 if (noref < 0 || noref > 1)
10103 EMSG(_(e_invarg));
10104 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010105 {
10106 current_copyID += COPYID_INC;
10107 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
10112 * "delete()" function
10113 */
10114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010116 typval_T *argvars;
10117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123}
10124
10125/*
10126 * "did_filetype()" function
10127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010130 typval_T *argvars UNUSED;
10131 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132{
10133#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135#endif
10136}
10137
10138/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010139 * "diff_filler()" function
10140 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010143 typval_T *argvars UNUSED;
10144 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010145{
10146#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010148#endif
10149}
10150
10151/*
10152 * "diff_hlID()" function
10153 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010156 typval_T *argvars UNUSED;
10157 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010158{
10159#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010161 static linenr_T prev_lnum = 0;
10162 static int changedtick = 0;
10163 static int fnum = 0;
10164 static int change_start = 0;
10165 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010166 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010167 int filler_lines;
10168 int col;
10169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 if (lnum < 0) /* ignore type error in {lnum} arg */
10171 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010172 if (lnum != prev_lnum
10173 || changedtick != curbuf->b_changedtick
10174 || fnum != curbuf->b_fnum)
10175 {
10176 /* New line, buffer, change: need to get the values. */
10177 filler_lines = diff_check(curwin, lnum);
10178 if (filler_lines < 0)
10179 {
10180 if (filler_lines == -1)
10181 {
10182 change_start = MAXCOL;
10183 change_end = -1;
10184 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10185 hlID = HLF_ADD; /* added line */
10186 else
10187 hlID = HLF_CHD; /* changed line */
10188 }
10189 else
10190 hlID = HLF_ADD; /* added line */
10191 }
10192 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010193 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010194 prev_lnum = lnum;
10195 changedtick = curbuf->b_changedtick;
10196 fnum = curbuf->b_fnum;
10197 }
10198
10199 if (hlID == HLF_CHD || hlID == HLF_TXD)
10200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010201 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010202 if (col >= change_start && col <= change_end)
10203 hlID = HLF_TXD; /* changed text */
10204 else
10205 hlID = HLF_CHD; /* changed line */
10206 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010207 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010208#endif
10209}
10210
10211/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010212 * "empty({expr})" function
10213 */
10214 static void
10215f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010216 typval_T *argvars;
10217 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010218{
10219 int n;
10220
10221 switch (argvars[0].v_type)
10222 {
10223 case VAR_STRING:
10224 case VAR_FUNC:
10225 n = argvars[0].vval.v_string == NULL
10226 || *argvars[0].vval.v_string == NUL;
10227 break;
10228 case VAR_NUMBER:
10229 n = argvars[0].vval.v_number == 0;
10230 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010231#ifdef FEAT_FLOAT
10232 case VAR_FLOAT:
10233 n = argvars[0].vval.v_float == 0.0;
10234 break;
10235#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010236 case VAR_LIST:
10237 n = argvars[0].vval.v_list == NULL
10238 || argvars[0].vval.v_list->lv_first == NULL;
10239 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 case VAR_DICT:
10241 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010244 default:
10245 EMSG2(_(e_intern2), "f_empty()");
10246 n = 0;
10247 }
10248
10249 rettv->vval.v_number = n;
10250}
10251
10252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 * "escape({string}, {chars})" function
10254 */
10255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010257 typval_T *argvars;
10258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 char_u buf[NUMBUFLEN];
10261
Bram Moolenaar758711c2005-02-02 23:11:38 +000010262 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10263 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265}
10266
10267/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010268 * "eval()" function
10269 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270 static void
10271f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010272 typval_T *argvars;
10273 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010274{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010275 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 s = get_tv_string_chk(&argvars[0]);
10278 if (s != NULL)
10279 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010280
Bram Moolenaar615b9972015-01-14 17:15:05 +010010281 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10283 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010284 if (p != NULL && !aborting())
10285 EMSG2(_(e_invexpr2), p);
10286 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010290 else if (*s != NUL)
10291 EMSG(_(e_trailing));
10292}
10293
10294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 * "eventhandler()" function
10296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303}
10304
10305/*
10306 * "executable()" function
10307 */
10308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 typval_T *argvars;
10311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010313 char_u *name = get_tv_string(&argvars[0]);
10314
10315 /* Check in $PATH and also check directly if there is a directory name. */
10316 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10317 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010318}
10319
10320/*
10321 * "exepath()" function
10322 */
10323 static void
10324f_exepath(argvars, rettv)
10325 typval_T *argvars;
10326 typval_T *rettv;
10327{
10328 char_u *p = NULL;
10329
Bram Moolenaarb5971142015-03-21 17:32:19 +010010330 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010331 rettv->v_type = VAR_STRING;
10332 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
10334
10335/*
10336 * "exists()" function
10337 */
10338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010340 typval_T *argvars;
10341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
10343 char_u *p;
10344 char_u *name;
10345 int n = FALSE;
10346 int len = 0;
10347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (*p == '$') /* environment variable */
10350 {
10351 /* first try "normal" environment variables (fast) */
10352 if (mch_getenv(p + 1) != NULL)
10353 n = TRUE;
10354 else
10355 {
10356 /* try expanding things like $VIM and ${HOME} */
10357 p = expand_env_save(p);
10358 if (p != NULL && *p != '$')
10359 n = TRUE;
10360 vim_free(p);
10361 }
10362 }
10363 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010366 if (*skipwhite(p) != NUL)
10367 n = FALSE; /* trailing garbage */
10368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 else if (*p == '*') /* internal or user defined function */
10370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373 else if (*p == ':')
10374 {
10375 n = cmd_exists(p + 1);
10376 }
10377 else if (*p == '#')
10378 {
10379#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010380 if (p[1] == '#')
10381 n = autocmd_supported(p + 2);
10382 else
10383 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384#endif
10385 }
10386 else /* internal variable */
10387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010388 char_u *tofree;
10389 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010391 /* get_name_len() takes care of expanding curly braces */
10392 name = p;
10393 len = get_name_len(&p, &tofree, TRUE, FALSE);
10394 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010396 if (tofree != NULL)
10397 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010398 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010399 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010401 /* handle d.key, l[idx], f(expr) */
10402 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10403 if (n)
10404 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 }
10406 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010407 if (*p != NUL)
10408 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010410 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 }
10412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414}
10415
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010416#ifdef FEAT_FLOAT
10417/*
10418 * "exp()" function
10419 */
10420 static void
10421f_exp(argvars, rettv)
10422 typval_T *argvars;
10423 typval_T *rettv;
10424{
10425 float_T f;
10426
10427 rettv->v_type = VAR_FLOAT;
10428 if (get_float_arg(argvars, &f) == OK)
10429 rettv->vval.v_float = exp(f);
10430 else
10431 rettv->vval.v_float = 0.0;
10432}
10433#endif
10434
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435/*
10436 * "expand()" function
10437 */
10438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010440 typval_T *argvars;
10441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 char_u *s;
10444 int len;
10445 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010446 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010449 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010452 if (argvars[1].v_type != VAR_UNKNOWN
10453 && argvars[2].v_type != VAR_UNKNOWN
10454 && get_tv_number_chk(&argvars[2], &error)
10455 && !error)
10456 {
10457 rettv->v_type = VAR_LIST;
10458 rettv->vval.v_list = NULL;
10459 }
10460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 if (*s == '%' || *s == '#' || *s == '<')
10463 {
10464 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010465 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010467 if (rettv->v_type == VAR_LIST)
10468 {
10469 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10470 list_append_string(rettv->vval.v_list, result, -1);
10471 else
10472 vim_free(result);
10473 }
10474 else
10475 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 }
10477 else
10478 {
10479 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010480 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 if (argvars[1].v_type != VAR_UNKNOWN
10482 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010483 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 if (!error)
10485 {
10486 ExpandInit(&xpc);
10487 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010488 if (p_wic)
10489 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010490 if (rettv->v_type == VAR_STRING)
10491 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10492 options, WILD_ALL);
10493 else if (rettv_list_alloc(rettv) != FAIL)
10494 {
10495 int i;
10496
10497 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10498 for (i = 0; i < xpc.xp_numfiles; i++)
10499 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10500 ExpandCleanup(&xpc);
10501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 }
10503 else
10504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 }
10506}
10507
10508/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010509 * Go over all entries in "d2" and add them to "d1".
10510 * When "action" is "error" then a duplicate key is an error.
10511 * When "action" is "force" then a duplicate key is overwritten.
10512 * Otherwise duplicate keys are ignored ("action" is "keep").
10513 */
10514 void
10515dict_extend(d1, d2, action)
10516 dict_T *d1;
10517 dict_T *d2;
10518 char_u *action;
10519{
10520 dictitem_T *di1;
10521 hashitem_T *hi2;
10522 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010523 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010524
10525 todo = (int)d2->dv_hashtab.ht_used;
10526 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10527 {
10528 if (!HASHITEM_EMPTY(hi2))
10529 {
10530 --todo;
10531 di1 = dict_find(d1, hi2->hi_key, -1);
10532 if (d1->dv_scope != 0)
10533 {
10534 /* Disallow replacing a builtin function in l: and g:.
10535 * Check the key to be valid when adding to any
10536 * scope. */
10537 if (d1->dv_scope == VAR_DEF_SCOPE
10538 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10539 && var_check_func_name(hi2->hi_key,
10540 di1 == NULL))
10541 break;
10542 if (!valid_varname(hi2->hi_key))
10543 break;
10544 }
10545 if (di1 == NULL)
10546 {
10547 di1 = dictitem_copy(HI2DI(hi2));
10548 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10549 dictitem_free(di1);
10550 }
10551 else if (*action == 'e')
10552 {
10553 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10554 break;
10555 }
10556 else if (*action == 'f' && HI2DI(hi2) != di1)
10557 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010558 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10559 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010560 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010561 clear_tv(&di1->di_tv);
10562 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10563 }
10564 }
10565 }
10566}
10567
10568/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010569 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010571 */
10572 static void
10573f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T *argvars;
10575 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010576{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010577 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010578
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010580 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010581 list_T *l1, *l2;
10582 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010584 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010585
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 l1 = argvars[0].vval.v_list;
10587 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010588 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010589 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 {
10591 if (argvars[2].v_type != VAR_UNKNOWN)
10592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 before = get_tv_number_chk(&argvars[2], &error);
10594 if (error)
10595 return; /* type error; errmsg already given */
10596
Bram Moolenaar758711c2005-02-02 23:11:38 +000010597 if (before == l1->lv_len)
10598 item = NULL;
10599 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010601 item = list_find(l1, before);
10602 if (item == NULL)
10603 {
10604 EMSGN(_(e_listidx), before);
10605 return;
10606 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 }
10608 }
10609 else
10610 item = NULL;
10611 list_extend(l1, l2, item);
10612
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 copy_tv(&argvars[0], rettv);
10614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10617 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010618 dict_T *d1, *d2;
10619 char_u *action;
10620 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010621
10622 d1 = argvars[0].vval.v_dict;
10623 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010624 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010625 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010626 {
10627 /* Check the third argument. */
10628 if (argvars[2].v_type != VAR_UNKNOWN)
10629 {
10630 static char *(av[]) = {"keep", "force", "error"};
10631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 action = get_tv_string_chk(&argvars[2]);
10633 if (action == NULL)
10634 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010635 for (i = 0; i < 3; ++i)
10636 if (STRCMP(action, av[i]) == 0)
10637 break;
10638 if (i == 3)
10639 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010640 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641 return;
10642 }
10643 }
10644 else
10645 action = (char_u *)"force";
10646
Bram Moolenaara9922d62013-05-30 13:01:18 +020010647 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010648
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 copy_tv(&argvars[0], rettv);
10650 }
10651 }
10652 else
10653 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010654}
10655
10656/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010657 * "feedkeys()" function
10658 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010659 static void
10660f_feedkeys(argvars, rettv)
10661 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010662 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663{
10664 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010665 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010666 char_u *keys, *flags;
10667 char_u nbuf[NUMBUFLEN];
10668 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010669 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010670
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010671 /* This is not allowed in the sandbox. If the commands would still be
10672 * executed in the sandbox it would be OK, but it probably happens later,
10673 * when "sandbox" is no longer set. */
10674 if (check_secure())
10675 return;
10676
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010677 keys = get_tv_string(&argvars[0]);
10678 if (*keys != NUL)
10679 {
10680 if (argvars[1].v_type != VAR_UNKNOWN)
10681 {
10682 flags = get_tv_string_buf(&argvars[1], nbuf);
10683 for ( ; *flags != NUL; ++flags)
10684 {
10685 switch (*flags)
10686 {
10687 case 'n': remap = FALSE; break;
10688 case 'm': remap = TRUE; break;
10689 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010690 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010691 }
10692 }
10693 }
10694
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010695 /* Need to escape K_SPECIAL and CSI before putting the string in the
10696 * typeahead buffer. */
10697 keys_esc = vim_strsave_escape_csi(keys);
10698 if (keys_esc != NULL)
10699 {
10700 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010701 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010702 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010703 if (vgetc_busy)
10704 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010705 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010706 }
10707}
10708
10709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 * "filereadable()" function
10711 */
10712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010717 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 char_u *p;
10719 int n;
10720
Bram Moolenaarc236c162008-07-13 17:41:49 +000010721#ifndef O_NONBLOCK
10722# define O_NONBLOCK 0
10723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010725 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10726 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 {
10728 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010729 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 }
10731 else
10732 n = FALSE;
10733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735}
10736
10737/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010738 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 * rights to write into.
10740 */
10741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010743 typval_T *argvars;
10744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010746 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010749static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010750
10751 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010752findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010754 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010755 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010756{
10757#ifdef FEAT_SEARCHPATH
10758 char_u *fname;
10759 char_u *fresult = NULL;
10760 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10761 char_u *p;
10762 char_u pathbuf[NUMBUFLEN];
10763 int count = 1;
10764 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010765 int error = FALSE;
10766#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010767
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010768 rettv->vval.v_string = NULL;
10769 rettv->v_type = VAR_STRING;
10770
10771#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10777 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010778 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 else
10780 {
10781 if (*p != NUL)
10782 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010785 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010786 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010787 }
10788
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010789 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10790 error = TRUE;
10791
10792 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794 do
10795 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010796 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010797 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 fresult = find_file_in_path_option(first ? fname : NULL,
10799 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010800 0, first, path,
10801 find_what,
10802 curbuf->b_ffname,
10803 find_what == FINDFILE_DIR
10804 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010806
10807 if (fresult != NULL && rettv->v_type == VAR_LIST)
10808 list_append_string(rettv->vval.v_list, fresult, -1);
10809
10810 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010812
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010813 if (rettv->v_type == VAR_STRING)
10814 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010815#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010816}
10817
Bram Moolenaar33570922005-01-25 22:26:29 +000010818static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10819static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010820
10821/*
10822 * Implementation of map() and filter().
10823 */
10824 static void
10825filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *argvars;
10827 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010828 int map;
10829{
10830 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 listitem_T *li, *nli;
10833 list_T *l = NULL;
10834 dictitem_T *di;
10835 hashtab_T *ht;
10836 hashitem_T *hi;
10837 dict_T *d = NULL;
10838 typval_T save_val;
10839 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010841 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010842 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010843 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010844 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010845 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010846 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010847
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010849 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010850 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010851 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 return;
10853 }
10854 else if (argvars[0].v_type == VAR_DICT)
10855 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010856 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010857 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 return;
10859 }
10860 else
10861 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010862 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010863 return;
10864 }
10865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 expr = get_tv_string_buf_chk(&argvars[1], buf);
10867 /* On type errors, the preceding call has already displayed an error
10868 * message. Avoid a misleading error message for an empty string that
10869 * was not passed as argument. */
10870 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 prepare_vimvar(VV_VAL, &save_val);
10873 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010874
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010875 /* We reset "did_emsg" to be able to detect whether an error
10876 * occurred during evaluation of the expression. */
10877 save_did_emsg = did_emsg;
10878 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010879
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010880 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 vimvars[VV_KEY].vv_type = VAR_STRING;
10884
10885 ht = &d->dv_hashtab;
10886 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010887 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010888 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 if (!HASHITEM_EMPTY(hi))
10891 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010892 int r;
10893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 --todo;
10895 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010896 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010897 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10898 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 break;
10900 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010901 r = filter_map_one(&di->di_tv, expr, map, &rem);
10902 clear_tv(&vimvars[VV_KEY].vv_tv);
10903 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 break;
10905 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010906 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010907 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10908 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010909 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 }
10913 }
10914 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 }
10916 else
10917 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010918 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 for (li = l->lv_first; li != NULL; li = nli)
10921 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010922 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010923 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010925 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010926 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010927 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010928 break;
10929 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010931 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010932 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010934
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010935 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010937
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010938 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940
10941 copy_tv(&argvars[0], rettv);
10942}
10943
10944 static int
10945filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 char_u *expr;
10948 int map;
10949 int *remp;
10950{
Bram Moolenaar33570922005-01-25 22:26:29 +000010951 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010953 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 s = expr;
10957 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010958 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959 if (*s != NUL) /* check for trailing chars after expr */
10960 {
10961 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010962 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010963 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010964 }
10965 if (map)
10966 {
10967 /* map(): replace the list item value */
10968 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010969 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 *tv = rettv;
10971 }
10972 else
10973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 int error = FALSE;
10975
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 /* On type error, nothing has been removed; return FAIL to stop the
10980 * loop. The error message was given by get_tv_number_chk(). */
10981 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010982 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010983 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010984 retval = OK;
10985theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010987 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010988}
10989
10990/*
10991 * "filter()" function
10992 */
10993 static void
10994f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010995 typval_T *argvars;
10996 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010997{
10998 filter_map(argvars, rettv, FALSE);
10999}
11000
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011002 * "finddir({fname}[, {path}[, {count}]])" function
11003 */
11004 static void
11005f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 typval_T *argvars;
11007 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011009 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011010}
11011
11012/*
11013 * "findfile({fname}[, {path}[, {count}]])" function
11014 */
11015 static void
11016f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011017 typval_T *argvars;
11018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011020 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011021}
11022
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011023#ifdef FEAT_FLOAT
11024/*
11025 * "float2nr({float})" function
11026 */
11027 static void
11028f_float2nr(argvars, rettv)
11029 typval_T *argvars;
11030 typval_T *rettv;
11031{
11032 float_T f;
11033
11034 if (get_float_arg(argvars, &f) == OK)
11035 {
11036 if (f < -0x7fffffff)
11037 rettv->vval.v_number = -0x7fffffff;
11038 else if (f > 0x7fffffff)
11039 rettv->vval.v_number = 0x7fffffff;
11040 else
11041 rettv->vval.v_number = (varnumber_T)f;
11042 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011043}
11044
11045/*
11046 * "floor({float})" function
11047 */
11048 static void
11049f_floor(argvars, rettv)
11050 typval_T *argvars;
11051 typval_T *rettv;
11052{
11053 float_T f;
11054
11055 rettv->v_type = VAR_FLOAT;
11056 if (get_float_arg(argvars, &f) == OK)
11057 rettv->vval.v_float = floor(f);
11058 else
11059 rettv->vval.v_float = 0.0;
11060}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011061
11062/*
11063 * "fmod()" function
11064 */
11065 static void
11066f_fmod(argvars, rettv)
11067 typval_T *argvars;
11068 typval_T *rettv;
11069{
11070 float_T fx, fy;
11071
11072 rettv->v_type = VAR_FLOAT;
11073 if (get_float_arg(argvars, &fx) == OK
11074 && get_float_arg(&argvars[1], &fy) == OK)
11075 rettv->vval.v_float = fmod(fx, fy);
11076 else
11077 rettv->vval.v_float = 0.0;
11078}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011079#endif
11080
Bram Moolenaar0d660222005-01-07 21:51:51 +000011081/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011082 * "fnameescape({string})" function
11083 */
11084 static void
11085f_fnameescape(argvars, rettv)
11086 typval_T *argvars;
11087 typval_T *rettv;
11088{
11089 rettv->vval.v_string = vim_strsave_fnameescape(
11090 get_tv_string(&argvars[0]), FALSE);
11091 rettv->v_type = VAR_STRING;
11092}
11093
11094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 * "fnamemodify({fname}, {mods})" function
11096 */
11097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101{
11102 char_u *fname;
11103 char_u *mods;
11104 int usedlen = 0;
11105 int len;
11106 char_u *fbuf = NULL;
11107 char_u buf[NUMBUFLEN];
11108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 fname = get_tv_string_chk(&argvars[0]);
11110 mods = get_tv_string_buf_chk(&argvars[1], buf);
11111 if (fname == NULL || mods == NULL)
11112 fname = NULL;
11113 else
11114 {
11115 len = (int)STRLEN(fname);
11116 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 vim_free(fbuf);
11125}
11126
Bram Moolenaar33570922005-01-25 22:26:29 +000011127static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
11129/*
11130 * "foldclosed()" function
11131 */
11132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011136 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138#ifdef FEAT_FOLDING
11139 linenr_T lnum;
11140 linenr_T first, last;
11141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11144 {
11145 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11146 {
11147 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 return;
11152 }
11153 }
11154#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156}
11157
11158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 * "foldclosed()" function
11160 */
11161 static void
11162f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165{
11166 foldclosed_both(argvars, rettv, FALSE);
11167}
11168
11169/*
11170 * "foldclosedend()" function
11171 */
11172 static void
11173f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011174 typval_T *argvars;
11175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176{
11177 foldclosed_both(argvars, rettv, TRUE);
11178}
11179
11180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 * "foldlevel()" function
11182 */
11183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011185 typval_T *argvars UNUSED;
11186 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187{
11188#ifdef FEAT_FOLDING
11189 linenr_T lnum;
11190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195}
11196
11197/*
11198 * "foldtext()" function
11199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011202 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
11205#ifdef FEAT_FOLDING
11206 linenr_T lnum;
11207 char_u *s;
11208 char_u *r;
11209 int len;
11210 char *txt;
11211#endif
11212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011216 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11217 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11218 <= curbuf->b_ml.ml_line_count
11219 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 {
11221 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11223 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 {
11225 if (!linewhite(lnum))
11226 break;
11227 ++lnum;
11228 }
11229
11230 /* Find interesting text in this line. */
11231 s = skipwhite(ml_get(lnum));
11232 /* skip C comment-start */
11233 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011236 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011238 {
11239 s = skipwhite(ml_get(lnum + 1));
11240 if (*s == '*')
11241 s = skipwhite(s + 1);
11242 }
11243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 txt = _("+-%s%3ld lines: ");
11245 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 + 20 /* for %3ld */
11248 + STRLEN(s))); /* concatenated */
11249 if (r != NULL)
11250 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011251 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11252 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11253 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 len = (int)STRLEN(r);
11255 STRCAT(r, s);
11256 /* remove 'foldmarker' and 'commentstring' */
11257 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 }
11260 }
11261#endif
11262}
11263
11264/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011265 * "foldtextresult(lnum)" function
11266 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011271{
11272#ifdef FEAT_FOLDING
11273 linenr_T lnum;
11274 char_u *text;
11275 char_u buf[51];
11276 foldinfo_T foldinfo;
11277 int fold_count;
11278#endif
11279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->v_type = VAR_STRING;
11281 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011282#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 /* treat illegal types and illegal string values for {lnum} the same */
11285 if (lnum < 0)
11286 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011287 fold_count = foldedCount(curwin, lnum, &foldinfo);
11288 if (fold_count > 0)
11289 {
11290 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11291 &foldinfo, buf);
11292 if (text == buf)
11293 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011295 }
11296#endif
11297}
11298
11299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 * "foreground()" function
11301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011304 typval_T *argvars UNUSED;
11305 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307#ifdef FEAT_GUI
11308 if (gui.in_use)
11309 gui_mch_set_foreground();
11310#else
11311# ifdef WIN32
11312 win32_set_foreground();
11313# endif
11314#endif
11315}
11316
11317/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011318 * "function()" function
11319 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011324{
11325 char_u *s;
11326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011329 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011330 /* Don't check an autoload name for existence here. */
11331 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011332 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011333 else
11334 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011335 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011336 {
11337 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011338 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011339
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011340 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11341 * also be called from another script. Using trans_function_name()
11342 * would also work, but some plugins depend on the name being
11343 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011345 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011346 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011347 if (rettv->vval.v_string != NULL)
11348 {
11349 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011350 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011351 }
11352 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011353 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011354 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011356 }
11357}
11358
11359/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011360 * "garbagecollect()" function
11361 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011362 static void
11363f_garbagecollect(argvars, rettv)
11364 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011365 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011366{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011367 /* This is postponed until we are back at the toplevel, because we may be
11368 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11369 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011370
11371 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11372 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011373}
11374
11375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376 * "get()" function
11377 */
11378 static void
11379f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382{
Bram Moolenaar33570922005-01-25 22:26:29 +000011383 listitem_T *li;
11384 list_T *l;
11385 dictitem_T *di;
11386 dict_T *d;
11387 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011390 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011391 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 int error = FALSE;
11394
11395 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11396 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011397 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011398 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011400 else if (argvars[0].v_type == VAR_DICT)
11401 {
11402 if ((d = argvars[0].vval.v_dict) != NULL)
11403 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011404 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011405 if (di != NULL)
11406 tv = &di->di_tv;
11407 }
11408 }
11409 else
11410 EMSG2(_(e_listdictarg), "get()");
11411
11412 if (tv == NULL)
11413 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011414 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011415 copy_tv(&argvars[2], rettv);
11416 }
11417 else
11418 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419}
11420
Bram Moolenaar342337a2005-07-21 21:11:17 +000011421static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011422
11423/*
11424 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011425 * Return a range (from start to end) of lines in rettv from the specified
11426 * buffer.
11427 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428 */
11429 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011430get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011431 buf_T *buf;
11432 linenr_T start;
11433 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011434 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011435 typval_T *rettv;
11436{
11437 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011438
Bram Moolenaar959a1432013-12-14 12:17:38 +010011439 rettv->v_type = VAR_STRING;
11440 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011441 if (retlist && rettv_list_alloc(rettv) == FAIL)
11442 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011443
11444 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11445 return;
11446
11447 if (!retlist)
11448 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011449 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11450 p = ml_get_buf(buf, start, FALSE);
11451 else
11452 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011453 rettv->vval.v_string = vim_strsave(p);
11454 }
11455 else
11456 {
11457 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011458 return;
11459
11460 if (start < 1)
11461 start = 1;
11462 if (end > buf->b_ml.ml_line_count)
11463 end = buf->b_ml.ml_line_count;
11464 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011465 if (list_append_string(rettv->vval.v_list,
11466 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011467 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011468 }
11469}
11470
11471/*
11472 * "getbufline()" function
11473 */
11474 static void
11475f_getbufline(argvars, rettv)
11476 typval_T *argvars;
11477 typval_T *rettv;
11478{
11479 linenr_T lnum;
11480 linenr_T end;
11481 buf_T *buf;
11482
11483 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11484 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011485 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011486 --emsg_off;
11487
Bram Moolenaar661b1822005-07-28 22:36:45 +000011488 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011489 if (argvars[2].v_type == VAR_UNKNOWN)
11490 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011491 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011492 end = get_tv_lnum_buf(&argvars[2], buf);
11493
Bram Moolenaar342337a2005-07-21 21:11:17 +000011494 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011495}
11496
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497/*
11498 * "getbufvar()" function
11499 */
11500 static void
11501f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011504{
11505 buf_T *buf;
11506 buf_T *save_curbuf;
11507 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011509 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11512 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011514 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011515
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011516 rettv->v_type = VAR_STRING;
11517 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011518
11519 if (buf != NULL && varname != NULL)
11520 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011521 /* set curbuf to be our buf, temporarily */
11522 save_curbuf = curbuf;
11523 curbuf = buf;
11524
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011526 {
11527 if (get_option_tv(&varname, rettv, TRUE) == OK)
11528 done = TRUE;
11529 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011530 else if (STRCMP(varname, "changedtick") == 0)
11531 {
11532 rettv->v_type = VAR_NUMBER;
11533 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011534 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011536 else
11537 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011538 /* Look up the variable. */
11539 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11540 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11541 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011545 done = TRUE;
11546 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011548
11549 /* restore previous notion of curbuf */
11550 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 }
11552
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011553 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11554 /* use the default value */
11555 copy_tv(&argvars[2], rettv);
11556
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557 --emsg_off;
11558}
11559
11560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 * "getchar()" function
11562 */
11563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 typval_T *argvars;
11566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567{
11568 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011571 /* Position the cursor. Needed after a message that ends in a space. */
11572 windgoto(msg_row, msg_col);
11573
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 ++no_mapping;
11575 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011576 for (;;)
11577 {
11578 if (argvars[0].v_type == VAR_UNKNOWN)
11579 /* getchar(): blocking wait. */
11580 n = safe_vgetc();
11581 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11582 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011583 n = vpeekc_any();
11584 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011585 /* illegal argument or getchar(0) and no char avail: return zero */
11586 n = 0;
11587 else
11588 /* getchar(0) and char avail: return char */
11589 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011590
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011591 if (n == K_IGNORE)
11592 continue;
11593 break;
11594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 --no_mapping;
11596 --allow_keys;
11597
Bram Moolenaar219b8702006-11-01 14:32:36 +000011598 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11599 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11600 vimvars[VV_MOUSE_COL].vv_nr = 0;
11601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 if (IS_SPECIAL(n) || mod_mask != 0)
11604 {
11605 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11606 int i = 0;
11607
11608 /* Turn a special key into three bytes, plus modifier. */
11609 if (mod_mask != 0)
11610 {
11611 temp[i++] = K_SPECIAL;
11612 temp[i++] = KS_MODIFIER;
11613 temp[i++] = mod_mask;
11614 }
11615 if (IS_SPECIAL(n))
11616 {
11617 temp[i++] = K_SPECIAL;
11618 temp[i++] = K_SECOND(n);
11619 temp[i++] = K_THIRD(n);
11620 }
11621#ifdef FEAT_MBYTE
11622 else if (has_mbyte)
11623 i += (*mb_char2bytes)(n, temp + i);
11624#endif
11625 else
11626 temp[i++] = n;
11627 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->v_type = VAR_STRING;
11629 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011630
11631#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011632 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011633 {
11634 int row = mouse_row;
11635 int col = mouse_col;
11636 win_T *win;
11637 linenr_T lnum;
11638# ifdef FEAT_WINDOWS
11639 win_T *wp;
11640# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011641 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011642
11643 if (row >= 0 && col >= 0)
11644 {
11645 /* Find the window at the mouse coordinates and compute the
11646 * text position. */
11647 win = mouse_find_win(&row, &col);
11648 (void)mouse_comp_pos(win, &row, &col, &lnum);
11649# ifdef FEAT_WINDOWS
11650 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011651 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011652# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011653 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011654 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11655 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11656 }
11657 }
11658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 }
11660}
11661
11662/*
11663 * "getcharmod()" function
11664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671}
11672
11673/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011674 * "getcharsearch()" function
11675 */
11676 static void
11677f_getcharsearch(argvars, rettv)
11678 typval_T *argvars UNUSED;
11679 typval_T *rettv;
11680{
11681 if (rettv_dict_alloc(rettv) != FAIL)
11682 {
11683 dict_T *dict = rettv->vval.v_dict;
11684
11685 dict_add_nr_str(dict, "char", 0L, last_csearch());
11686 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11687 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11688 }
11689}
11690
11691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 * "getcmdline()" function
11693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011696 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->v_type = VAR_STRING;
11700 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701}
11702
11703/*
11704 * "getcmdpos()" function
11705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011708 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712}
11713
11714/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011715 * "getcmdtype()" function
11716 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011717 static void
11718f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011719 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011720 typval_T *rettv;
11721{
11722 rettv->v_type = VAR_STRING;
11723 rettv->vval.v_string = alloc(2);
11724 if (rettv->vval.v_string != NULL)
11725 {
11726 rettv->vval.v_string[0] = get_cmdline_type();
11727 rettv->vval.v_string[1] = NUL;
11728 }
11729}
11730
11731/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011732 * "getcmdwintype()" function
11733 */
11734 static void
11735f_getcmdwintype(argvars, rettv)
11736 typval_T *argvars UNUSED;
11737 typval_T *rettv;
11738{
11739 rettv->v_type = VAR_STRING;
11740 rettv->vval.v_string = NULL;
11741#ifdef FEAT_CMDWIN
11742 rettv->vval.v_string = alloc(2);
11743 if (rettv->vval.v_string != NULL)
11744 {
11745 rettv->vval.v_string[0] = cmdwin_type;
11746 rettv->vval.v_string[1] = NUL;
11747 }
11748#endif
11749}
11750
11751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 * "getcwd()" function
11753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011759 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011762 rettv->vval.v_string = NULL;
11763 cwd = alloc(MAXPATHL);
11764 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011766 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11767 {
11768 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011770 if (rettv->vval.v_string != NULL)
11771 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011773 }
11774 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 }
11776}
11777
11778/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011779 * "getfontname()" function
11780 */
11781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->v_type = VAR_STRING;
11787 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011788#ifdef FEAT_GUI
11789 if (gui.in_use)
11790 {
11791 GuiFont font;
11792 char_u *name = NULL;
11793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011794 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011795 {
11796 /* Get the "Normal" font. Either the name saved by
11797 * hl_set_font_name() or from the font ID. */
11798 font = gui.norm_font;
11799 name = hl_get_font_name();
11800 }
11801 else
11802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011804 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11805 return;
11806 font = gui_mch_get_font(name, FALSE);
11807 if (font == NOFONT)
11808 return; /* Invalid font name, return empty string. */
11809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011812 gui_mch_free_font(font);
11813 }
11814#endif
11815}
11816
11817/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011818 * "getfperm({fname})" function
11819 */
11820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011822 typval_T *argvars;
11823 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011824{
11825 char_u *fname;
11826 struct stat st;
11827 char_u *perm = NULL;
11828 char_u flags[] = "rwx";
11829 int i;
11830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011834 if (mch_stat((char *)fname, &st) >= 0)
11835 {
11836 perm = vim_strsave((char_u *)"---------");
11837 if (perm != NULL)
11838 {
11839 for (i = 0; i < 9; i++)
11840 {
11841 if (st.st_mode & (1 << (8 - i)))
11842 perm[i] = flags[i % 3];
11843 }
11844 }
11845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011847}
11848
11849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * "getfsize({fname})" function
11851 */
11852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011854 typval_T *argvars;
11855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
11857 char_u *fname;
11858 struct stat st;
11859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863
11864 if (mch_stat((char *)fname, &st) >= 0)
11865 {
11866 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011871
11872 /* non-perfect check for overflow */
11873 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11874 rettv->vval.v_number = -2;
11875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 }
11877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879}
11880
11881/*
11882 * "getftime({fname})" function
11883 */
11884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 typval_T *argvars;
11887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888{
11889 char_u *fname;
11890 struct stat st;
11891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893
11894 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898}
11899
11900/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011901 * "getftype({fname})" function
11902 */
11903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011907{
11908 char_u *fname;
11909 struct stat st;
11910 char_u *type = NULL;
11911 char *t;
11912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011916 if (mch_lstat((char *)fname, &st) >= 0)
11917 {
11918#ifdef S_ISREG
11919 if (S_ISREG(st.st_mode))
11920 t = "file";
11921 else if (S_ISDIR(st.st_mode))
11922 t = "dir";
11923# ifdef S_ISLNK
11924 else if (S_ISLNK(st.st_mode))
11925 t = "link";
11926# endif
11927# ifdef S_ISBLK
11928 else if (S_ISBLK(st.st_mode))
11929 t = "bdev";
11930# endif
11931# ifdef S_ISCHR
11932 else if (S_ISCHR(st.st_mode))
11933 t = "cdev";
11934# endif
11935# ifdef S_ISFIFO
11936 else if (S_ISFIFO(st.st_mode))
11937 t = "fifo";
11938# endif
11939# ifdef S_ISSOCK
11940 else if (S_ISSOCK(st.st_mode))
11941 t = "fifo";
11942# endif
11943 else
11944 t = "other";
11945#else
11946# ifdef S_IFMT
11947 switch (st.st_mode & S_IFMT)
11948 {
11949 case S_IFREG: t = "file"; break;
11950 case S_IFDIR: t = "dir"; break;
11951# ifdef S_IFLNK
11952 case S_IFLNK: t = "link"; break;
11953# endif
11954# ifdef S_IFBLK
11955 case S_IFBLK: t = "bdev"; break;
11956# endif
11957# ifdef S_IFCHR
11958 case S_IFCHR: t = "cdev"; break;
11959# endif
11960# ifdef S_IFIFO
11961 case S_IFIFO: t = "fifo"; break;
11962# endif
11963# ifdef S_IFSOCK
11964 case S_IFSOCK: t = "socket"; break;
11965# endif
11966 default: t = "other";
11967 }
11968# else
11969 if (mch_isdir(fname))
11970 t = "dir";
11971 else
11972 t = "file";
11973# endif
11974#endif
11975 type = vim_strsave((char_u *)t);
11976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011978}
11979
11980/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011981 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011982 */
11983 static void
11984f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 typval_T *argvars;
11986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011987{
11988 linenr_T lnum;
11989 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011990 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011991
11992 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011993 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011994 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011995 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011996 retlist = FALSE;
11997 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011998 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011999 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012000 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012001 retlist = TRUE;
12002 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012003
Bram Moolenaar342337a2005-07-21 21:11:17 +000012004 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005}
12006
12007/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012008 * "getmatches()" function
12009 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012010 static void
12011f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012012 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012013 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012014{
12015#ifdef FEAT_SEARCH_EXTRA
12016 dict_T *dict;
12017 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012018 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012019
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012020 if (rettv_list_alloc(rettv) == OK)
12021 {
12022 while (cur != NULL)
12023 {
12024 dict = dict_alloc();
12025 if (dict == NULL)
12026 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012027 if (cur->match.regprog == NULL)
12028 {
12029 /* match added with matchaddpos() */
12030 for (i = 0; i < MAXPOSMATCH; ++i)
12031 {
12032 llpos_T *llpos;
12033 char buf[6];
12034 list_T *l;
12035
12036 llpos = &cur->pos.pos[i];
12037 if (llpos->lnum == 0)
12038 break;
12039 l = list_alloc();
12040 if (l == NULL)
12041 break;
12042 list_append_number(l, (varnumber_T)llpos->lnum);
12043 if (llpos->col > 0)
12044 {
12045 list_append_number(l, (varnumber_T)llpos->col);
12046 list_append_number(l, (varnumber_T)llpos->len);
12047 }
12048 sprintf(buf, "pos%d", i + 1);
12049 dict_add_list(dict, buf, l);
12050 }
12051 }
12052 else
12053 {
12054 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12055 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012056 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012057 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12058 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012059# ifdef FEAT_CONCEAL
12060 if (cur->conceal_char)
12061 {
12062 char_u buf[MB_MAXBYTES + 1];
12063
12064 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12065 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12066 }
12067# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012068 list_append_dict(rettv->vval.v_list, dict);
12069 cur = cur->next;
12070 }
12071 }
12072#endif
12073}
12074
12075/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012076 * "getpid()" function
12077 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012078 static void
12079f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012080 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012081 typval_T *rettv;
12082{
12083 rettv->vval.v_number = mch_get_pid();
12084}
12085
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012086static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12087
12088/*
12089 * "getcurpos()" function
12090 */
12091 static void
12092f_getcurpos(argvars, rettv)
12093 typval_T *argvars;
12094 typval_T *rettv;
12095{
12096 getpos_both(argvars, rettv, TRUE);
12097}
12098
Bram Moolenaar18081e32008-02-20 19:11:07 +000012099/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012100 * "getpos(string)" function
12101 */
12102 static void
12103f_getpos(argvars, rettv)
12104 typval_T *argvars;
12105 typval_T *rettv;
12106{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012107 getpos_both(argvars, rettv, FALSE);
12108}
12109
12110 static void
12111getpos_both(argvars, rettv, getcurpos)
12112 typval_T *argvars;
12113 typval_T *rettv;
12114 int getcurpos;
12115{
Bram Moolenaara5525202006-03-02 22:52:09 +000012116 pos_T *fp;
12117 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012118 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012119
12120 if (rettv_list_alloc(rettv) == OK)
12121 {
12122 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012123 if (getcurpos)
12124 fp = &curwin->w_cursor;
12125 else
12126 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012127 if (fnum != -1)
12128 list_append_number(l, (varnumber_T)fnum);
12129 else
12130 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012131 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12132 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012133 list_append_number(l, (fp != NULL)
12134 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012135 : (varnumber_T)0);
12136 list_append_number(l,
12137#ifdef FEAT_VIRTUALEDIT
12138 (fp != NULL) ? (varnumber_T)fp->coladd :
12139#endif
12140 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012141 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012142 list_append_number(l, curwin->w_curswant == MAXCOL ?
12143 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012144 }
12145 else
12146 rettv->vval.v_number = FALSE;
12147}
12148
12149/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012150 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012151 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012152 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012153f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012154 typval_T *argvars UNUSED;
12155 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012156{
12157#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012158 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012159#endif
12160
Bram Moolenaar2641f772005-03-25 21:58:17 +000012161#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012162 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012163 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012164 wp = NULL;
12165 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12166 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012167 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012168 if (wp == NULL)
12169 return;
12170 }
12171
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012172 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012173 }
12174#endif
12175}
12176
12177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 * "getreg()" function
12179 */
12180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012182 typval_T *argvars;
12183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185 char_u *strregname;
12186 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012187 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012188 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012191 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012193 strregname = get_tv_string_chk(&argvars[0]);
12194 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012195 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012198 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12199 return_list = get_tv_number_chk(&argvars[2], &error);
12200 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012203 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012204
12205 if (error)
12206 return;
12207
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 regname = (strregname == NULL ? '"' : *strregname);
12209 if (regname == 0)
12210 regname = '"';
12211
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012212 if (return_list)
12213 {
12214 rettv->v_type = VAR_LIST;
12215 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12216 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012217 if (rettv->vval.v_list != NULL)
12218 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012219 }
12220 else
12221 {
12222 rettv->v_type = VAR_STRING;
12223 rettv->vval.v_string = get_reg_contents(regname,
12224 arg2 ? GREG_EXPR_SRC : 0);
12225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226}
12227
12228/*
12229 * "getregtype()" function
12230 */
12231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 typval_T *argvars;
12234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
12236 char_u *strregname;
12237 int regname;
12238 char_u buf[NUMBUFLEN + 2];
12239 long reglen = 0;
12240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012241 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012242 {
12243 strregname = get_tv_string_chk(&argvars[0]);
12244 if (strregname == NULL) /* type error; errmsg already given */
12245 {
12246 rettv->v_type = VAR_STRING;
12247 rettv->vval.v_string = NULL;
12248 return;
12249 }
12250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 else
12252 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012253 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254
12255 regname = (strregname == NULL ? '"' : *strregname);
12256 if (regname == 0)
12257 regname = '"';
12258
12259 buf[0] = NUL;
12260 buf[1] = NUL;
12261 switch (get_reg_type(regname, &reglen))
12262 {
12263 case MLINE: buf[0] = 'V'; break;
12264 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 case MBLOCK:
12266 buf[0] = Ctrl_V;
12267 sprintf((char *)buf + 1, "%ld", reglen + 1);
12268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 rettv->v_type = VAR_STRING;
12271 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272}
12273
12274/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012275 * "gettabvar()" function
12276 */
12277 static void
12278f_gettabvar(argvars, rettv)
12279 typval_T *argvars;
12280 typval_T *rettv;
12281{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012282 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012283 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012284 dictitem_T *v;
12285 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012286 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012287
12288 rettv->v_type = VAR_STRING;
12289 rettv->vval.v_string = NULL;
12290
12291 varname = get_tv_string_chk(&argvars[1]);
12292 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12293 if (tp != NULL && varname != NULL)
12294 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012295 /* Set tp to be our tabpage, temporarily. Also set the window to the
12296 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012297 if (switch_win(&oldcurwin, &oldtabpage,
12298 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012299 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012300 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012301 /* look up the variable */
12302 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12303 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12304 if (v != NULL)
12305 {
12306 copy_tv(&v->di_tv, rettv);
12307 done = TRUE;
12308 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012309 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012310
12311 /* restore previous notion of curwin */
12312 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012313 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012314
12315 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012316 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012317}
12318
12319/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012320 * "gettabwinvar()" function
12321 */
12322 static void
12323f_gettabwinvar(argvars, rettv)
12324 typval_T *argvars;
12325 typval_T *rettv;
12326{
12327 getwinvar(argvars, rettv, 1);
12328}
12329
12330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 * "getwinposx()" function
12332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339#ifdef FEAT_GUI
12340 if (gui.in_use)
12341 {
12342 int x, y;
12343
12344 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 }
12347#endif
12348}
12349
12350/*
12351 * "getwinposy()" function
12352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012355 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359#ifdef FEAT_GUI
12360 if (gui.in_use)
12361 {
12362 int x, y;
12363
12364 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 }
12367#endif
12368}
12369
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012370/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012371 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012372 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012373 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012374find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012375 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012376 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012377{
12378#ifdef FEAT_WINDOWS
12379 win_T *wp;
12380#endif
12381 int nr;
12382
12383 nr = get_tv_number_chk(vp, NULL);
12384
12385#ifdef FEAT_WINDOWS
12386 if (nr < 0)
12387 return NULL;
12388 if (nr == 0)
12389 return curwin;
12390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012391 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12392 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012393 if (--nr <= 0)
12394 break;
12395 return wp;
12396#else
12397 if (nr == 0 || nr == 1)
12398 return curwin;
12399 return NULL;
12400#endif
12401}
12402
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403/*
12404 * "getwinvar()" function
12405 */
12406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *argvars;
12409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012411 getwinvar(argvars, rettv, 0);
12412}
12413
12414/*
12415 * getwinvar() and gettabwinvar()
12416 */
12417 static void
12418getwinvar(argvars, rettv, off)
12419 typval_T *argvars;
12420 typval_T *rettv;
12421 int off; /* 1 for gettabwinvar() */
12422{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012423 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012425 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012426 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012427 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012428#ifdef FEAT_WINDOWS
12429 win_T *oldcurwin;
12430 tabpage_T *oldtabpage;
12431 int need_switch_win;
12432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012434#ifdef FEAT_WINDOWS
12435 if (off == 1)
12436 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12437 else
12438 tp = curtab;
12439#endif
12440 win = find_win_by_nr(&argvars[off], tp);
12441 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012442 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012444 rettv->v_type = VAR_STRING;
12445 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446
12447 if (win != NULL && varname != NULL)
12448 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012449#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012450 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012451 * otherwise the window is not valid. Only do this when needed,
12452 * autocommands get blocked. */
12453 need_switch_win = !(tp == curtab && win == curwin);
12454 if (!need_switch_win
12455 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12456#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012457 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012458 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012459 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012460 if (get_option_tv(&varname, rettv, 1) == OK)
12461 done = TRUE;
12462 }
12463 else
12464 {
12465 /* Look up the variable. */
12466 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12467 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12468 varname, FALSE);
12469 if (v != NULL)
12470 {
12471 copy_tv(&v->di_tv, rettv);
12472 done = TRUE;
12473 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012476
Bram Moolenaarba117c22015-09-29 16:53:22 +020012477#ifdef FEAT_WINDOWS
12478 if (need_switch_win)
12479 /* restore previous notion of curwin */
12480 restore_win(oldcurwin, oldtabpage, TRUE);
12481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 }
12483
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012484 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12485 /* use the default return value */
12486 copy_tv(&argvars[off + 2], rettv);
12487
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 --emsg_off;
12489}
12490
12491/*
12492 * "glob()" function
12493 */
12494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 typval_T *argvars;
12497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012499 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012501 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012503 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012504 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012505 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012506 if (argvars[1].v_type != VAR_UNKNOWN)
12507 {
12508 if (get_tv_number_chk(&argvars[1], &error))
12509 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012511 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012512 if (get_tv_number_chk(&argvars[2], &error))
12513 {
12514 rettv->v_type = VAR_LIST;
12515 rettv->vval.v_list = NULL;
12516 }
12517 if (argvars[3].v_type != VAR_UNKNOWN
12518 && get_tv_number_chk(&argvars[3], &error))
12519 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012520 }
12521 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012522 if (!error)
12523 {
12524 ExpandInit(&xpc);
12525 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012526 if (p_wic)
12527 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012528 if (rettv->v_type == VAR_STRING)
12529 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012530 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012531 else if (rettv_list_alloc(rettv) != FAIL)
12532 {
12533 int i;
12534
12535 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12536 NULL, options, WILD_ALL_KEEP);
12537 for (i = 0; i < xpc.xp_numfiles; i++)
12538 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12539
12540 ExpandCleanup(&xpc);
12541 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012542 }
12543 else
12544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545}
12546
12547/*
12548 * "globpath()" function
12549 */
12550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012552 typval_T *argvars;
12553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012555 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012557 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012558 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012559 garray_T ga;
12560 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012562 /* When the optional second argument is non-zero, don't remove matches
12563 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012565 if (argvars[2].v_type != VAR_UNKNOWN)
12566 {
12567 if (get_tv_number_chk(&argvars[2], &error))
12568 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012569 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012570 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012571 if (get_tv_number_chk(&argvars[3], &error))
12572 {
12573 rettv->v_type = VAR_LIST;
12574 rettv->vval.v_list = NULL;
12575 }
12576 if (argvars[4].v_type != VAR_UNKNOWN
12577 && get_tv_number_chk(&argvars[4], &error))
12578 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012579 }
12580 }
12581 if (file != NULL && !error)
12582 {
12583 ga_init2(&ga, (int)sizeof(char_u *), 10);
12584 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12585 if (rettv->v_type == VAR_STRING)
12586 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12587 else if (rettv_list_alloc(rettv) != FAIL)
12588 for (i = 0; i < ga.ga_len; ++i)
12589 list_append_string(rettv->vval.v_list,
12590 ((char_u **)(ga.ga_data))[i], -1);
12591 ga_clear_strings(&ga);
12592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012593 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595}
12596
12597/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012598 * "glob2regpat()" function
12599 */
12600 static void
12601f_glob2regpat(argvars, rettv)
12602 typval_T *argvars;
12603 typval_T *rettv;
12604{
12605 char_u *pat = get_tv_string_chk(&argvars[0]);
12606
12607 rettv->v_type = VAR_STRING;
12608 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12609}
12610
12611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 * "has()" function
12613 */
12614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012616 typval_T *argvars;
12617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618{
12619 int i;
12620 char_u *name;
12621 int n = FALSE;
12622 static char *(has_list[]) =
12623 {
12624#ifdef AMIGA
12625 "amiga",
12626# ifdef FEAT_ARP
12627 "arp",
12628# endif
12629#endif
12630#ifdef __BEOS__
12631 "beos",
12632#endif
12633#ifdef MSDOS
12634# ifdef DJGPP
12635 "dos32",
12636# else
12637 "dos16",
12638# endif
12639#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012640#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 "mac",
12642#endif
12643#if defined(MACOS_X_UNIX)
12644 "macunix",
12645#endif
12646#ifdef OS2
12647 "os2",
12648#endif
12649#ifdef __QNX__
12650 "qnx",
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef UNIX
12653 "unix",
12654#endif
12655#ifdef VMS
12656 "vms",
12657#endif
12658#ifdef WIN16
12659 "win16",
12660#endif
12661#ifdef WIN32
12662 "win32",
12663#endif
12664#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12665 "win32unix",
12666#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012667#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 "win64",
12669#endif
12670#ifdef EBCDIC
12671 "ebcdic",
12672#endif
12673#ifndef CASE_INSENSITIVE_FILENAME
12674 "fname_case",
12675#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012676#ifdef HAVE_ACL
12677 "acl",
12678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679#ifdef FEAT_ARABIC
12680 "arabic",
12681#endif
12682#ifdef FEAT_AUTOCMD
12683 "autocmd",
12684#endif
12685#ifdef FEAT_BEVAL
12686 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012687# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12688 "balloon_multiline",
12689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690#endif
12691#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12692 "builtin_terms",
12693# ifdef ALL_BUILTIN_TCAPS
12694 "all_builtin_terms",
12695# endif
12696#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012697#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12698 || defined(FEAT_GUI_W32) \
12699 || defined(FEAT_GUI_MOTIF))
12700 "browsefilter",
12701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702#ifdef FEAT_BYTEOFF
12703 "byte_offset",
12704#endif
12705#ifdef FEAT_CINDENT
12706 "cindent",
12707#endif
12708#ifdef FEAT_CLIENTSERVER
12709 "clientserver",
12710#endif
12711#ifdef FEAT_CLIPBOARD
12712 "clipboard",
12713#endif
12714#ifdef FEAT_CMDL_COMPL
12715 "cmdline_compl",
12716#endif
12717#ifdef FEAT_CMDHIST
12718 "cmdline_hist",
12719#endif
12720#ifdef FEAT_COMMENTS
12721 "comments",
12722#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012723#ifdef FEAT_CONCEAL
12724 "conceal",
12725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726#ifdef FEAT_CRYPT
12727 "cryptv",
12728#endif
12729#ifdef FEAT_CSCOPE
12730 "cscope",
12731#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012732#ifdef FEAT_CURSORBIND
12733 "cursorbind",
12734#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012735#ifdef CURSOR_SHAPE
12736 "cursorshape",
12737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738#ifdef DEBUG
12739 "debug",
12740#endif
12741#ifdef FEAT_CON_DIALOG
12742 "dialog_con",
12743#endif
12744#ifdef FEAT_GUI_DIALOG
12745 "dialog_gui",
12746#endif
12747#ifdef FEAT_DIFF
12748 "diff",
12749#endif
12750#ifdef FEAT_DIGRAPHS
12751 "digraphs",
12752#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012753#ifdef FEAT_DIRECTX
12754 "directx",
12755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756#ifdef FEAT_DND
12757 "dnd",
12758#endif
12759#ifdef FEAT_EMACS_TAGS
12760 "emacs_tags",
12761#endif
12762 "eval", /* always present, of course! */
12763#ifdef FEAT_EX_EXTRA
12764 "ex_extra",
12765#endif
12766#ifdef FEAT_SEARCH_EXTRA
12767 "extra_search",
12768#endif
12769#ifdef FEAT_FKMAP
12770 "farsi",
12771#endif
12772#ifdef FEAT_SEARCHPATH
12773 "file_in_path",
12774#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012775#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012776 "filterpipe",
12777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778#ifdef FEAT_FIND_ID
12779 "find_in_path",
12780#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012781#ifdef FEAT_FLOAT
12782 "float",
12783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784#ifdef FEAT_FOLDING
12785 "folding",
12786#endif
12787#ifdef FEAT_FOOTER
12788 "footer",
12789#endif
12790#if !defined(USE_SYSTEM) && defined(UNIX)
12791 "fork",
12792#endif
12793#ifdef FEAT_GETTEXT
12794 "gettext",
12795#endif
12796#ifdef FEAT_GUI
12797 "gui",
12798#endif
12799#ifdef FEAT_GUI_ATHENA
12800# ifdef FEAT_GUI_NEXTAW
12801 "gui_neXtaw",
12802# else
12803 "gui_athena",
12804# endif
12805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806#ifdef FEAT_GUI_GTK
12807 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012810#ifdef FEAT_GUI_GNOME
12811 "gui_gnome",
12812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813#ifdef FEAT_GUI_MAC
12814 "gui_mac",
12815#endif
12816#ifdef FEAT_GUI_MOTIF
12817 "gui_motif",
12818#endif
12819#ifdef FEAT_GUI_PHOTON
12820 "gui_photon",
12821#endif
12822#ifdef FEAT_GUI_W16
12823 "gui_win16",
12824#endif
12825#ifdef FEAT_GUI_W32
12826 "gui_win32",
12827#endif
12828#ifdef FEAT_HANGULIN
12829 "hangul_input",
12830#endif
12831#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12832 "iconv",
12833#endif
12834#ifdef FEAT_INS_EXPAND
12835 "insert_expand",
12836#endif
12837#ifdef FEAT_JUMPLIST
12838 "jumplist",
12839#endif
12840#ifdef FEAT_KEYMAP
12841 "keymap",
12842#endif
12843#ifdef FEAT_LANGMAP
12844 "langmap",
12845#endif
12846#ifdef FEAT_LIBCALL
12847 "libcall",
12848#endif
12849#ifdef FEAT_LINEBREAK
12850 "linebreak",
12851#endif
12852#ifdef FEAT_LISP
12853 "lispindent",
12854#endif
12855#ifdef FEAT_LISTCMDS
12856 "listcmds",
12857#endif
12858#ifdef FEAT_LOCALMAP
12859 "localmap",
12860#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012861#ifdef FEAT_LUA
12862# ifndef DYNAMIC_LUA
12863 "lua",
12864# endif
12865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866#ifdef FEAT_MENU
12867 "menu",
12868#endif
12869#ifdef FEAT_SESSION
12870 "mksession",
12871#endif
12872#ifdef FEAT_MODIFY_FNAME
12873 "modify_fname",
12874#endif
12875#ifdef FEAT_MOUSE
12876 "mouse",
12877#endif
12878#ifdef FEAT_MOUSESHAPE
12879 "mouseshape",
12880#endif
12881#if defined(UNIX) || defined(VMS)
12882# ifdef FEAT_MOUSE_DEC
12883 "mouse_dec",
12884# endif
12885# ifdef FEAT_MOUSE_GPM
12886 "mouse_gpm",
12887# endif
12888# ifdef FEAT_MOUSE_JSB
12889 "mouse_jsbterm",
12890# endif
12891# ifdef FEAT_MOUSE_NET
12892 "mouse_netterm",
12893# endif
12894# ifdef FEAT_MOUSE_PTERM
12895 "mouse_pterm",
12896# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012897# ifdef FEAT_MOUSE_SGR
12898 "mouse_sgr",
12899# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012900# ifdef FEAT_SYSMOUSE
12901 "mouse_sysmouse",
12902# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012903# ifdef FEAT_MOUSE_URXVT
12904 "mouse_urxvt",
12905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906# ifdef FEAT_MOUSE_XTERM
12907 "mouse_xterm",
12908# endif
12909#endif
12910#ifdef FEAT_MBYTE
12911 "multi_byte",
12912#endif
12913#ifdef FEAT_MBYTE_IME
12914 "multi_byte_ime",
12915#endif
12916#ifdef FEAT_MULTI_LANG
12917 "multi_lang",
12918#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012919#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012920#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012921 "mzscheme",
12922#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924#ifdef FEAT_OLE
12925 "ole",
12926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927#ifdef FEAT_PATH_EXTRA
12928 "path_extra",
12929#endif
12930#ifdef FEAT_PERL
12931#ifndef DYNAMIC_PERL
12932 "perl",
12933#endif
12934#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012935#ifdef FEAT_PERSISTENT_UNDO
12936 "persistent_undo",
12937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938#ifdef FEAT_PYTHON
12939#ifndef DYNAMIC_PYTHON
12940 "python",
12941#endif
12942#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012943#ifdef FEAT_PYTHON3
12944#ifndef DYNAMIC_PYTHON3
12945 "python3",
12946#endif
12947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948#ifdef FEAT_POSTSCRIPT
12949 "postscript",
12950#endif
12951#ifdef FEAT_PRINTER
12952 "printer",
12953#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012954#ifdef FEAT_PROFILE
12955 "profile",
12956#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012957#ifdef FEAT_RELTIME
12958 "reltime",
12959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960#ifdef FEAT_QUICKFIX
12961 "quickfix",
12962#endif
12963#ifdef FEAT_RIGHTLEFT
12964 "rightleft",
12965#endif
12966#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12967 "ruby",
12968#endif
12969#ifdef FEAT_SCROLLBIND
12970 "scrollbind",
12971#endif
12972#ifdef FEAT_CMDL_INFO
12973 "showcmd",
12974 "cmdline_info",
12975#endif
12976#ifdef FEAT_SIGNS
12977 "signs",
12978#endif
12979#ifdef FEAT_SMARTINDENT
12980 "smartindent",
12981#endif
12982#ifdef FEAT_SNIFF
12983 "sniff",
12984#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012985#ifdef STARTUPTIME
12986 "startuptime",
12987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988#ifdef FEAT_STL_OPT
12989 "statusline",
12990#endif
12991#ifdef FEAT_SUN_WORKSHOP
12992 "sun_workshop",
12993#endif
12994#ifdef FEAT_NETBEANS_INTG
12995 "netbeans_intg",
12996#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012997#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012998 "spell",
12999#endif
13000#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001 "syntax",
13002#endif
13003#if defined(USE_SYSTEM) || !defined(UNIX)
13004 "system",
13005#endif
13006#ifdef FEAT_TAG_BINS
13007 "tag_binary",
13008#endif
13009#ifdef FEAT_TAG_OLDSTATIC
13010 "tag_old_static",
13011#endif
13012#ifdef FEAT_TAG_ANYWHITE
13013 "tag_any_white",
13014#endif
13015#ifdef FEAT_TCL
13016# ifndef DYNAMIC_TCL
13017 "tcl",
13018# endif
13019#endif
13020#ifdef TERMINFO
13021 "terminfo",
13022#endif
13023#ifdef FEAT_TERMRESPONSE
13024 "termresponse",
13025#endif
13026#ifdef FEAT_TEXTOBJ
13027 "textobjects",
13028#endif
13029#ifdef HAVE_TGETENT
13030 "tgetent",
13031#endif
13032#ifdef FEAT_TITLE
13033 "title",
13034#endif
13035#ifdef FEAT_TOOLBAR
13036 "toolbar",
13037#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013038#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13039 "unnamedplus",
13040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041#ifdef FEAT_USR_CMDS
13042 "user-commands", /* was accidentally included in 5.4 */
13043 "user_commands",
13044#endif
13045#ifdef FEAT_VIMINFO
13046 "viminfo",
13047#endif
13048#ifdef FEAT_VERTSPLIT
13049 "vertsplit",
13050#endif
13051#ifdef FEAT_VIRTUALEDIT
13052 "virtualedit",
13053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef FEAT_VISUALEXTRA
13056 "visualextra",
13057#endif
13058#ifdef FEAT_VREPLACE
13059 "vreplace",
13060#endif
13061#ifdef FEAT_WILDIGN
13062 "wildignore",
13063#endif
13064#ifdef FEAT_WILDMENU
13065 "wildmenu",
13066#endif
13067#ifdef FEAT_WINDOWS
13068 "windows",
13069#endif
13070#ifdef FEAT_WAK
13071 "winaltkeys",
13072#endif
13073#ifdef FEAT_WRITEBACKUP
13074 "writebackup",
13075#endif
13076#ifdef FEAT_XIM
13077 "xim",
13078#endif
13079#ifdef FEAT_XFONTSET
13080 "xfontset",
13081#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013082#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013083 "xpm",
13084 "xpm_w32", /* for backward compatibility */
13085#else
13086# if defined(HAVE_XPM)
13087 "xpm",
13088# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090#ifdef USE_XSMP
13091 "xsmp",
13092#endif
13093#ifdef USE_XSMP_INTERACT
13094 "xsmp_interact",
13095#endif
13096#ifdef FEAT_XCLIPBOARD
13097 "xterm_clipboard",
13098#endif
13099#ifdef FEAT_XTERM_SAVE
13100 "xterm_save",
13101#endif
13102#if defined(UNIX) && defined(FEAT_X11)
13103 "X11",
13104#endif
13105 NULL
13106 };
13107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 for (i = 0; has_list[i] != NULL; ++i)
13110 if (STRICMP(name, has_list[i]) == 0)
13111 {
13112 n = TRUE;
13113 break;
13114 }
13115
13116 if (n == FALSE)
13117 {
13118 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013119 {
13120 if (name[5] == '-'
13121 && STRLEN(name) > 11
13122 && vim_isdigit(name[6])
13123 && vim_isdigit(name[8])
13124 && vim_isdigit(name[10]))
13125 {
13126 int major = atoi((char *)name + 6);
13127 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013128
13129 /* Expect "patch-9.9.01234". */
13130 n = (major < VIM_VERSION_MAJOR
13131 || (major == VIM_VERSION_MAJOR
13132 && (minor < VIM_VERSION_MINOR
13133 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013134 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013135 }
13136 else
13137 n = has_patch(atoi((char *)name + 5));
13138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 else if (STRICMP(name, "vim_starting") == 0)
13140 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013141#ifdef FEAT_MBYTE
13142 else if (STRICMP(name, "multi_byte_encoding") == 0)
13143 n = has_mbyte;
13144#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013145#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13146 else if (STRICMP(name, "balloon_multiline") == 0)
13147 n = multiline_balloon_available();
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149#ifdef DYNAMIC_TCL
13150 else if (STRICMP(name, "tcl") == 0)
13151 n = tcl_enabled(FALSE);
13152#endif
13153#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13154 else if (STRICMP(name, "iconv") == 0)
13155 n = iconv_enabled(FALSE);
13156#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013157#ifdef DYNAMIC_LUA
13158 else if (STRICMP(name, "lua") == 0)
13159 n = lua_enabled(FALSE);
13160#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013161#ifdef DYNAMIC_MZSCHEME
13162 else if (STRICMP(name, "mzscheme") == 0)
13163 n = mzscheme_enabled(FALSE);
13164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165#ifdef DYNAMIC_RUBY
13166 else if (STRICMP(name, "ruby") == 0)
13167 n = ruby_enabled(FALSE);
13168#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013169#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170#ifdef DYNAMIC_PYTHON
13171 else if (STRICMP(name, "python") == 0)
13172 n = python_enabled(FALSE);
13173#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013174#endif
13175#ifdef FEAT_PYTHON3
13176#ifdef DYNAMIC_PYTHON3
13177 else if (STRICMP(name, "python3") == 0)
13178 n = python3_enabled(FALSE);
13179#endif
13180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181#ifdef DYNAMIC_PERL
13182 else if (STRICMP(name, "perl") == 0)
13183 n = perl_enabled(FALSE);
13184#endif
13185#ifdef FEAT_GUI
13186 else if (STRICMP(name, "gui_running") == 0)
13187 n = (gui.in_use || gui.starting);
13188# ifdef FEAT_GUI_W32
13189 else if (STRICMP(name, "gui_win32s") == 0)
13190 n = gui_is_win32s();
13191# endif
13192# ifdef FEAT_BROWSE
13193 else if (STRICMP(name, "browse") == 0)
13194 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13195# endif
13196#endif
13197#ifdef FEAT_SYN_HL
13198 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013199 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200#endif
13201#if defined(WIN3264)
13202 else if (STRICMP(name, "win95") == 0)
13203 n = mch_windows95();
13204#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013205#ifdef FEAT_NETBEANS_INTG
13206 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013207 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 }
13210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212}
13213
13214/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013215 * "has_key()" function
13216 */
13217 static void
13218f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013219 typval_T *argvars;
13220 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013221{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013222 if (argvars[0].v_type != VAR_DICT)
13223 {
13224 EMSG(_(e_dictreq));
13225 return;
13226 }
13227 if (argvars[0].vval.v_dict == NULL)
13228 return;
13229
13230 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013231 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013232}
13233
13234/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013235 * "haslocaldir()" function
13236 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013237 static void
13238f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013239 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013240 typval_T *rettv;
13241{
13242 rettv->vval.v_number = (curwin->w_localdir != NULL);
13243}
13244
13245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 * "hasmapto()" function
13247 */
13248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *argvars;
13251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
13253 char_u *name;
13254 char_u *mode;
13255 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013256 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 mode = (char_u *)"nvo";
13261 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013264 if (argvars[2].v_type != VAR_UNKNOWN)
13265 abbr = get_tv_number(&argvars[2]);
13266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267
Bram Moolenaar2c932302006-03-18 21:42:09 +000013268 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272}
13273
13274/*
13275 * "histadd()" function
13276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281{
13282#ifdef FEAT_CMDHIST
13283 int histype;
13284 char_u *str;
13285 char_u buf[NUMBUFLEN];
13286#endif
13287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 if (check_restricted() || check_secure())
13290 return;
13291#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13293 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 if (histype >= 0)
13295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 if (*str != NUL)
13298 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013299 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 return;
13303 }
13304 }
13305#endif
13306}
13307
13308/*
13309 * "histdel()" function
13310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013313 typval_T *argvars UNUSED;
13314 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315{
13316#ifdef FEAT_CMDHIST
13317 int n;
13318 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13322 if (str == NULL)
13323 n = 0;
13324 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013327 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013329 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 else
13332 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013333 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 get_tv_string_buf(&argvars[1], buf));
13335 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336#endif
13337}
13338
13339/*
13340 * "histget()" function
13341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346{
13347#ifdef FEAT_CMDHIST
13348 int type;
13349 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013350 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13353 if (str == NULL)
13354 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013356 {
13357 type = get_histtype(str);
13358 if (argvars[1].v_type == VAR_UNKNOWN)
13359 idx = get_history_idx(type);
13360 else
13361 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13362 /* -1 on type error */
13363 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013366 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369}
13370
13371/*
13372 * "histnr()" function
13373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013376 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378{
13379 int i;
13380
13381#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 char_u *history = get_tv_string_chk(&argvars[0]);
13383
13384 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 if (i >= HIST_CMD && i < HIST_COUNT)
13386 i = get_history_idx(i);
13387 else
13388#endif
13389 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391}
13392
13393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 * "highlightID(name)" function
13395 */
13396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402}
13403
13404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013405 * "highlight_exists()" function
13406 */
13407 static void
13408f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013411{
13412 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13413}
13414
13415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 * "hostname()" function
13417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419f_hostname(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 char_u hostname[256];
13424
13425 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426 rettv->v_type = VAR_STRING;
13427 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428}
13429
13430/*
13431 * iconv() function
13432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437{
13438#ifdef FEAT_MBYTE
13439 char_u buf1[NUMBUFLEN];
13440 char_u buf2[NUMBUFLEN];
13441 char_u *from, *to, *str;
13442 vimconv_T vimconv;
13443#endif
13444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445 rettv->v_type = VAR_STRING;
13446 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447
13448#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 str = get_tv_string(&argvars[0]);
13450 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13451 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 vimconv.vc_type = CONV_NONE;
13453 convert_setup(&vimconv, from, to);
13454
13455 /* If the encodings are equal, no conversion needed. */
13456 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460
13461 convert_setup(&vimconv, NULL, NULL);
13462 vim_free(from);
13463 vim_free(to);
13464#endif
13465}
13466
13467/*
13468 * "indent()" function
13469 */
13470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013471f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013472 typval_T *argvars;
13473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474{
13475 linenr_T lnum;
13476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482}
13483
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013484/*
13485 * "index()" function
13486 */
13487 static void
13488f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013489 typval_T *argvars;
13490 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013491{
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 list_T *l;
13493 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013494 long idx = 0;
13495 int ic = FALSE;
13496
13497 rettv->vval.v_number = -1;
13498 if (argvars[0].v_type != VAR_LIST)
13499 {
13500 EMSG(_(e_listreq));
13501 return;
13502 }
13503 l = argvars[0].vval.v_list;
13504 if (l != NULL)
13505 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013506 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013507 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 int error = FALSE;
13510
Bram Moolenaar758711c2005-02-02 23:11:38 +000013511 /* Start at specified item. Use the cached index that list_find()
13512 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013514 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013515 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013516 ic = get_tv_number_chk(&argvars[3], &error);
13517 if (error)
13518 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013520
Bram Moolenaar758711c2005-02-02 23:11:38 +000013521 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013522 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013523 {
13524 rettv->vval.v_number = idx;
13525 break;
13526 }
13527 }
13528}
13529
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530static int inputsecret_flag = 0;
13531
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013532static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13533
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013535 * This function is used by f_input() and f_inputdialog() functions. The third
13536 * argument to f_input() specifies the type of completion to use at the
13537 * prompt. The third argument to f_inputdialog() specifies the value to return
13538 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 */
13540 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013541get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013542 typval_T *argvars;
13543 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013544 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 char_u *p = NULL;
13548 int c;
13549 char_u buf[NUMBUFLEN];
13550 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013551 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013552 int xp_type = EXPAND_NOTHING;
13553 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013556 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
13558#ifdef NO_CONSOLE_INPUT
13559 /* While starting up, there is no place to enter text. */
13560 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562#endif
13563
13564 cmd_silent = FALSE; /* Want to see the prompt. */
13565 if (prompt != NULL)
13566 {
13567 /* Only the part of the message after the last NL is considered as
13568 * prompt for the command line */
13569 p = vim_strrchr(prompt, '\n');
13570 if (p == NULL)
13571 p = prompt;
13572 else
13573 {
13574 ++p;
13575 c = *p;
13576 *p = NUL;
13577 msg_start();
13578 msg_clr_eos();
13579 msg_puts_attr(prompt, echo_attr);
13580 msg_didout = FALSE;
13581 msg_starthere();
13582 *p = c;
13583 }
13584 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 if (argvars[1].v_type != VAR_UNKNOWN)
13587 {
13588 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13589 if (defstr != NULL)
13590 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013592 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013593 {
13594 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013595 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013596 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013597
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013598 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013599 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013600
Bram Moolenaar4463f292005-09-25 22:20:24 +000013601 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13602 if (xp_name == NULL)
13603 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013604
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013605 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013606
Bram Moolenaar4463f292005-09-25 22:20:24 +000013607 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13608 &xp_arg) == FAIL)
13609 return;
13610 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013611 }
13612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013614 {
13615# ifdef FEAT_EX_EXTRA
13616 int save_ex_normal_busy = ex_normal_busy;
13617 ex_normal_busy = 0;
13618# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013620 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13621 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013622# ifdef FEAT_EX_EXTRA
13623 ex_normal_busy = save_ex_normal_busy;
13624# endif
13625 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013626 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013627 && argvars[1].v_type != VAR_UNKNOWN
13628 && argvars[2].v_type != VAR_UNKNOWN)
13629 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13630 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013631
13632 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 /* since the user typed this, no need to wait for return */
13635 need_wait_return = FALSE;
13636 msg_didout = FALSE;
13637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 cmd_silent = cmd_silent_save;
13639}
13640
13641/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013642 * "input()" function
13643 * Also handles inputsecret() when inputsecret is set.
13644 */
13645 static void
13646f_input(argvars, rettv)
13647 typval_T *argvars;
13648 typval_T *rettv;
13649{
13650 get_user_input(argvars, rettv, FALSE);
13651}
13652
13653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 * "inputdialog()" function
13655 */
13656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660{
13661#if defined(FEAT_GUI_TEXTDIALOG)
13662 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13663 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13664 {
13665 char_u *message;
13666 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 message = get_tv_string_chk(&argvars[0]);
13670 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013671 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013672 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 else
13674 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013675 if (message != NULL && defstr != NULL
13676 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013677 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 else
13680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013681 if (message != NULL && defstr != NULL
13682 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013683 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684 rettv->vval.v_string = vim_strsave(
13685 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 }
13691 else
13692#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013693 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694}
13695
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013696/*
13697 * "inputlist()" function
13698 */
13699 static void
13700f_inputlist(argvars, rettv)
13701 typval_T *argvars;
13702 typval_T *rettv;
13703{
13704 listitem_T *li;
13705 int selected;
13706 int mouse_used;
13707
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013708#ifdef NO_CONSOLE_INPUT
13709 /* While starting up, there is no place to enter text. */
13710 if (no_console_input())
13711 return;
13712#endif
13713 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13714 {
13715 EMSG2(_(e_listarg), "inputlist()");
13716 return;
13717 }
13718
13719 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013720 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013721 lines_left = Rows; /* avoid more prompt */
13722 msg_scroll = TRUE;
13723 msg_clr_eos();
13724
13725 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13726 {
13727 msg_puts(get_tv_string(&li->li_tv));
13728 msg_putchar('\n');
13729 }
13730
13731 /* Ask for choice. */
13732 selected = prompt_for_number(&mouse_used);
13733 if (mouse_used)
13734 selected -= lines_left;
13735
13736 rettv->vval.v_number = selected;
13737}
13738
13739
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13741
13742/*
13743 * "inputrestore()" function
13744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749{
13750 if (ga_userinput.ga_len > 0)
13751 {
13752 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13754 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013755 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 }
13757 else if (p_verbose > 1)
13758 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013759 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 }
13762}
13763
13764/*
13765 * "inputsave()" function
13766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013769 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013772 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 if (ga_grow(&ga_userinput, 1) == OK)
13774 {
13775 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13776 + ga_userinput.ga_len);
13777 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013778 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 }
13780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782}
13783
13784/*
13785 * "inputsecret()" function
13786 */
13787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013789 typval_T *argvars;
13790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791{
13792 ++cmdline_star;
13793 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 --cmdline_star;
13796 --inputsecret_flag;
13797}
13798
13799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013800 * "insert()" function
13801 */
13802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 typval_T *argvars;
13805 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013806{
13807 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013808 listitem_T *item;
13809 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013810 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013811
13812 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013814 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013815 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013816 {
13817 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 before = get_tv_number_chk(&argvars[2], &error);
13819 if (error)
13820 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013821
Bram Moolenaar758711c2005-02-02 23:11:38 +000013822 if (before == l->lv_len)
13823 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013824 else
13825 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013826 item = list_find(l, before);
13827 if (item == NULL)
13828 {
13829 EMSGN(_(e_listidx), before);
13830 l = NULL;
13831 }
13832 }
13833 if (l != NULL)
13834 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013835 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013836 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013837 }
13838 }
13839}
13840
13841/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013842 * "invert(expr)" function
13843 */
13844 static void
13845f_invert(argvars, rettv)
13846 typval_T *argvars;
13847 typval_T *rettv;
13848{
13849 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13850}
13851
13852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 * "isdirectory()" function
13854 */
13855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013856f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013857 typval_T *argvars;
13858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013860 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861}
13862
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013863/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013864 * "islocked()" function
13865 */
13866 static void
13867f_islocked(argvars, rettv)
13868 typval_T *argvars;
13869 typval_T *rettv;
13870{
13871 lval_T lv;
13872 char_u *end;
13873 dictitem_T *di;
13874
13875 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013876 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13877 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013878 if (end != NULL && lv.ll_name != NULL)
13879 {
13880 if (*end != NUL)
13881 EMSG(_(e_trailing));
13882 else
13883 {
13884 if (lv.ll_tv == NULL)
13885 {
13886 if (check_changedtick(lv.ll_name))
13887 rettv->vval.v_number = 1; /* always locked */
13888 else
13889 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013890 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013891 if (di != NULL)
13892 {
13893 /* Consider a variable locked when:
13894 * 1. the variable itself is locked
13895 * 2. the value of the variable is locked.
13896 * 3. the List or Dict value is locked.
13897 */
13898 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13899 || tv_islocked(&di->di_tv));
13900 }
13901 }
13902 }
13903 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013904 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013905 else if (lv.ll_newkey != NULL)
13906 EMSG2(_(e_dictkey), lv.ll_newkey);
13907 else if (lv.ll_list != NULL)
13908 /* List item. */
13909 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13910 else
13911 /* Dictionary item. */
13912 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13913 }
13914 }
13915
13916 clear_lval(&lv);
13917}
13918
Bram Moolenaar33570922005-01-25 22:26:29 +000013919static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013920
13921/*
13922 * Turn a dict into a list:
13923 * "what" == 0: list of keys
13924 * "what" == 1: list of values
13925 * "what" == 2: list of items
13926 */
13927 static void
13928dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013929 typval_T *argvars;
13930 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013931 int what;
13932{
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 list_T *l2;
13934 dictitem_T *di;
13935 hashitem_T *hi;
13936 listitem_T *li;
13937 listitem_T *li2;
13938 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013939 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013940
Bram Moolenaar8c711452005-01-14 21:53:12 +000013941 if (argvars[0].v_type != VAR_DICT)
13942 {
13943 EMSG(_(e_dictreq));
13944 return;
13945 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013946 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013947 return;
13948
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013949 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013950 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013951
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013952 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013953 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013955 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013957 --todo;
13958 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013959
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013960 li = listitem_alloc();
13961 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013962 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013963 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013964
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013965 if (what == 0)
13966 {
13967 /* keys() */
13968 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013969 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013970 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13971 }
13972 else if (what == 1)
13973 {
13974 /* values() */
13975 copy_tv(&di->di_tv, &li->li_tv);
13976 }
13977 else
13978 {
13979 /* items() */
13980 l2 = list_alloc();
13981 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013982 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013983 li->li_tv.vval.v_list = l2;
13984 if (l2 == NULL)
13985 break;
13986 ++l2->lv_refcount;
13987
13988 li2 = listitem_alloc();
13989 if (li2 == NULL)
13990 break;
13991 list_append(l2, li2);
13992 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013993 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013994 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13995
13996 li2 = listitem_alloc();
13997 if (li2 == NULL)
13998 break;
13999 list_append(l2, li2);
14000 copy_tv(&di->di_tv, &li2->li_tv);
14001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014002 }
14003 }
14004}
14005
14006/*
14007 * "items(dict)" function
14008 */
14009 static void
14010f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014011 typval_T *argvars;
14012 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014013{
14014 dict_list(argvars, rettv, 2);
14015}
14016
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014018 * "join()" function
14019 */
14020 static void
14021f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 typval_T *argvars;
14023 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014024{
14025 garray_T ga;
14026 char_u *sep;
14027
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014028 if (argvars[0].v_type != VAR_LIST)
14029 {
14030 EMSG(_(e_listreq));
14031 return;
14032 }
14033 if (argvars[0].vval.v_list == NULL)
14034 return;
14035 if (argvars[1].v_type == VAR_UNKNOWN)
14036 sep = (char_u *)" ";
14037 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014038 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014039
14040 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041
14042 if (sep != NULL)
14043 {
14044 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014045 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 ga_append(&ga, NUL);
14047 rettv->vval.v_string = (char_u *)ga.ga_data;
14048 }
14049 else
14050 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014051}
14052
14053/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014054 * "keys()" function
14055 */
14056 static void
14057f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *argvars;
14059 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014060{
14061 dict_list(argvars, rettv, 0);
14062}
14063
14064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 * "last_buffer_nr()" function.
14066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014069 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071{
14072 int n = 0;
14073 buf_T *buf;
14074
14075 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14076 if (n < buf->b_fnum)
14077 n = buf->b_fnum;
14078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080}
14081
14082/*
14083 * "len()" function
14084 */
14085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *argvars;
14088 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014089{
14090 switch (argvars[0].v_type)
14091 {
14092 case VAR_STRING:
14093 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014094 rettv->vval.v_number = (varnumber_T)STRLEN(
14095 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014096 break;
14097 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014099 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014100 case VAR_DICT:
14101 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14102 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014103 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014104 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014105 break;
14106 }
14107}
14108
Bram Moolenaar33570922005-01-25 22:26:29 +000014109static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014110
14111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 typval_T *argvars;
14114 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014115 int type;
14116{
14117#ifdef FEAT_LIBCALL
14118 char_u *string_in;
14119 char_u **string_result;
14120 int nr_result;
14121#endif
14122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014124 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014126
14127 if (check_restricted() || check_secure())
14128 return;
14129
14130#ifdef FEAT_LIBCALL
14131 /* The first two args must be strings, otherwise its meaningless */
14132 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14133 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014134 string_in = NULL;
14135 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014136 string_in = argvars[2].vval.v_string;
14137 if (type == VAR_NUMBER)
14138 string_result = NULL;
14139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014141 if (mch_libcall(argvars[0].vval.v_string,
14142 argvars[1].vval.v_string,
14143 string_in,
14144 argvars[2].vval.v_number,
14145 string_result,
14146 &nr_result) == OK
14147 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014149 }
14150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151}
14152
14153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154 * "libcall()" function
14155 */
14156 static void
14157f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *argvars;
14159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014160{
14161 libcall_common(argvars, rettv, VAR_STRING);
14162}
14163
14164/*
14165 * "libcallnr()" function
14166 */
14167 static void
14168f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *argvars;
14170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014171{
14172 libcall_common(argvars, rettv, VAR_NUMBER);
14173}
14174
14175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 * "line(string)" function
14177 */
14178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014180 typval_T *argvars;
14181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182{
14183 linenr_T lnum = 0;
14184 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014185 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014187 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 if (fp != NULL)
14189 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191}
14192
14193/*
14194 * "line2byte(lnum)" function
14195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014197f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200{
14201#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203#else
14204 linenr_T lnum;
14205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14211 if (rettv->vval.v_number >= 0)
14212 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213#endif
14214}
14215
14216/*
14217 * "lispindent(lnum)" function
14218 */
14219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014220f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223{
14224#ifdef FEAT_LISP
14225 pos_T pos;
14226 linenr_T lnum;
14227
14228 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14231 {
14232 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 curwin->w_cursor = pos;
14235 }
14236 else
14237#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239}
14240
14241/*
14242 * "localtime()" function
14243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014246 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250}
14251
Bram Moolenaar33570922005-01-25 22:26:29 +000014252static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253
14254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014256 typval_T *argvars;
14257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 int exact;
14259{
14260 char_u *keys;
14261 char_u *which;
14262 char_u buf[NUMBUFLEN];
14263 char_u *keys_buf = NULL;
14264 char_u *rhs;
14265 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014266 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014267 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014268 mapblock_T *mp;
14269 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270
14271 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->v_type = VAR_STRING;
14273 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 if (*keys == NUL)
14277 return;
14278
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014281 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014282 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014283 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014284 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014285 if (argvars[3].v_type != VAR_UNKNOWN)
14286 get_dict = get_tv_number(&argvars[3]);
14287 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 else
14290 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 if (which == NULL)
14292 return;
14293
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 mode = get_map_mode(&which, 0);
14295
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014296 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014297 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014299
14300 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014302 /* Return a string. */
14303 if (rhs != NULL)
14304 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305
Bram Moolenaarbd743252010-10-20 21:23:33 +020014306 }
14307 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14308 {
14309 /* Return a dictionary. */
14310 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14311 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14312 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
Bram Moolenaarbd743252010-10-20 21:23:33 +020014314 dict_add_nr_str(dict, "lhs", 0L, lhs);
14315 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14316 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14317 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14318 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14319 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14320 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014321 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014322 dict_add_nr_str(dict, "mode", 0L, mapmode);
14323
14324 vim_free(lhs);
14325 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 }
14327}
14328
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329#ifdef FEAT_FLOAT
14330/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014331 * "log()" function
14332 */
14333 static void
14334f_log(argvars, rettv)
14335 typval_T *argvars;
14336 typval_T *rettv;
14337{
14338 float_T f;
14339
14340 rettv->v_type = VAR_FLOAT;
14341 if (get_float_arg(argvars, &f) == OK)
14342 rettv->vval.v_float = log(f);
14343 else
14344 rettv->vval.v_float = 0.0;
14345}
14346
14347/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014348 * "log10()" function
14349 */
14350 static void
14351f_log10(argvars, rettv)
14352 typval_T *argvars;
14353 typval_T *rettv;
14354{
14355 float_T f;
14356
14357 rettv->v_type = VAR_FLOAT;
14358 if (get_float_arg(argvars, &f) == OK)
14359 rettv->vval.v_float = log10(f);
14360 else
14361 rettv->vval.v_float = 0.0;
14362}
14363#endif
14364
Bram Moolenaar1dced572012-04-05 16:54:08 +020014365#ifdef FEAT_LUA
14366/*
14367 * "luaeval()" function
14368 */
14369 static void
14370f_luaeval(argvars, rettv)
14371 typval_T *argvars;
14372 typval_T *rettv;
14373{
14374 char_u *str;
14375 char_u buf[NUMBUFLEN];
14376
14377 str = get_tv_string_buf(&argvars[0], buf);
14378 do_luaeval(str, argvars + 1, rettv);
14379}
14380#endif
14381
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014383 * "map()" function
14384 */
14385 static void
14386f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014387 typval_T *argvars;
14388 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014389{
14390 filter_map(argvars, rettv, TRUE);
14391}
14392
14393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014394 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 */
14396 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014398 typval_T *argvars;
14399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014401 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
14404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014405 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 */
14407 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014409 typval_T *argvars;
14410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014412 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413}
14414
Bram Moolenaar33570922005-01-25 22:26:29 +000014415static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416
14417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014419 typval_T *argvars;
14420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 int type;
14422{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014423 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014424 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014425 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 char_u *pat;
14427 regmatch_T regmatch;
14428 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014429 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 char_u *save_cpo;
14431 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014432 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014433 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014434 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 list_T *l = NULL;
14436 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014437 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014438 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439
14440 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14441 save_cpo = p_cpo;
14442 p_cpo = (char_u *)"";
14443
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014444 rettv->vval.v_number = -1;
14445 if (type == 3)
14446 {
14447 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014449 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014450 }
14451 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 rettv->v_type = VAR_STRING;
14454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014457 if (argvars[0].v_type == VAR_LIST)
14458 {
14459 if ((l = argvars[0].vval.v_list) == NULL)
14460 goto theend;
14461 li = l->lv_first;
14462 }
14463 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014464 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014465 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014466 len = (long)STRLEN(str);
14467 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014469 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14470 if (pat == NULL)
14471 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014473 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014475 int error = FALSE;
14476
14477 start = get_tv_number_chk(&argvars[2], &error);
14478 if (error)
14479 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 if (l != NULL)
14481 {
14482 li = list_find(l, start);
14483 if (li == NULL)
14484 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014485 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014486 }
14487 else
14488 {
14489 if (start < 0)
14490 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014491 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014492 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014493 /* When "count" argument is there ignore matches before "start",
14494 * otherwise skip part of the string. Differs when pattern is "^"
14495 * or "\<". */
14496 if (argvars[3].v_type != VAR_UNKNOWN)
14497 startcol = start;
14498 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014499 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014500 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014501 len -= start;
14502 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014503 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014504
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014505 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 nth = get_tv_number_chk(&argvars[3], &error);
14507 if (error)
14508 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 }
14510
14511 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14512 if (regmatch.regprog != NULL)
14513 {
14514 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014515
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014516 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014517 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014518 if (l != NULL)
14519 {
14520 if (li == NULL)
14521 {
14522 match = FALSE;
14523 break;
14524 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014525 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014526 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014527 if (str == NULL)
14528 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014529 }
14530
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014531 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014532
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014533 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014534 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014535 if (l == NULL && !match)
14536 break;
14537
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014538 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014539 if (l != NULL)
14540 {
14541 li = li->li_next;
14542 ++idx;
14543 }
14544 else
14545 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014546#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014547 startcol = (colnr_T)(regmatch.startp[0]
14548 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014549#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014550 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014551#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014552 if (startcol > (colnr_T)len
14553 || str + startcol <= regmatch.startp[0])
14554 {
14555 match = FALSE;
14556 break;
14557 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014558 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014559 }
14560
14561 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014563 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014564 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565 int i;
14566
14567 /* return list with matched string and submatches */
14568 for (i = 0; i < NSUBEXP; ++i)
14569 {
14570 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014571 {
14572 if (list_append_string(rettv->vval.v_list,
14573 (char_u *)"", 0) == FAIL)
14574 break;
14575 }
14576 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014577 regmatch.startp[i],
14578 (int)(regmatch.endp[i] - regmatch.startp[i]))
14579 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014580 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014581 }
14582 }
14583 else if (type == 2)
14584 {
14585 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014586 if (l != NULL)
14587 copy_tv(&li->li_tv, rettv);
14588 else
14589 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014591 }
14592 else if (l != NULL)
14593 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 else
14595 {
14596 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 (varnumber_T)(regmatch.startp[0] - str);
14599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014600 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014602 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 }
14604 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014605 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 }
14607
14608theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014609 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 p_cpo = save_cpo;
14611}
14612
14613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614 * "match()" function
14615 */
14616 static void
14617f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014618 typval_T *argvars;
14619 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620{
14621 find_some_match(argvars, rettv, 1);
14622}
14623
14624/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014625 * "matchadd()" function
14626 */
14627 static void
14628f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014629 typval_T *argvars UNUSED;
14630 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014631{
14632#ifdef FEAT_SEARCH_EXTRA
14633 char_u buf[NUMBUFLEN];
14634 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14635 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14636 int prio = 10; /* default priority */
14637 int id = -1;
14638 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014639 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014640
14641 rettv->vval.v_number = -1;
14642
14643 if (grp == NULL || pat == NULL)
14644 return;
14645 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014646 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014647 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014648 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014649 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014650 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014651 if (argvars[4].v_type != VAR_UNKNOWN)
14652 {
14653 if (argvars[4].v_type != VAR_DICT)
14654 {
14655 EMSG(_(e_dictreq));
14656 return;
14657 }
14658 if (dict_find(argvars[4].vval.v_dict,
14659 (char_u *)"conceal", -1) != NULL)
14660 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14661 (char_u *)"conceal", FALSE);
14662 }
14663 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014664 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014665 if (error == TRUE)
14666 return;
14667 if (id >= 1 && id <= 3)
14668 {
14669 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14670 return;
14671 }
14672
Bram Moolenaar6561d522015-07-21 15:48:27 +020014673 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14674 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014675#endif
14676}
14677
14678/*
14679 * "matchaddpos()" function
14680 */
14681 static void
14682f_matchaddpos(argvars, rettv)
14683 typval_T *argvars UNUSED;
14684 typval_T *rettv UNUSED;
14685{
14686#ifdef FEAT_SEARCH_EXTRA
14687 char_u buf[NUMBUFLEN];
14688 char_u *group;
14689 int prio = 10;
14690 int id = -1;
14691 int error = FALSE;
14692 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014693 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014694
14695 rettv->vval.v_number = -1;
14696
14697 group = get_tv_string_buf_chk(&argvars[0], buf);
14698 if (group == NULL)
14699 return;
14700
14701 if (argvars[1].v_type != VAR_LIST)
14702 {
14703 EMSG2(_(e_listarg), "matchaddpos()");
14704 return;
14705 }
14706 l = argvars[1].vval.v_list;
14707 if (l == NULL)
14708 return;
14709
14710 if (argvars[2].v_type != VAR_UNKNOWN)
14711 {
14712 prio = get_tv_number_chk(&argvars[2], &error);
14713 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014714 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014715 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014716 if (argvars[4].v_type != VAR_UNKNOWN)
14717 {
14718 if (argvars[4].v_type != VAR_DICT)
14719 {
14720 EMSG(_(e_dictreq));
14721 return;
14722 }
14723 if (dict_find(argvars[4].vval.v_dict,
14724 (char_u *)"conceal", -1) != NULL)
14725 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14726 (char_u *)"conceal", FALSE);
14727 }
14728 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014729 }
14730 if (error == TRUE)
14731 return;
14732
14733 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14734 if (id == 1 || id == 2)
14735 {
14736 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14737 return;
14738 }
14739
Bram Moolenaar6561d522015-07-21 15:48:27 +020014740 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14741 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014742#endif
14743}
14744
14745/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014746 * "matcharg()" function
14747 */
14748 static void
14749f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014750 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014751 typval_T *rettv;
14752{
14753 if (rettv_list_alloc(rettv) == OK)
14754 {
14755#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014756 int id = get_tv_number(&argvars[0]);
14757 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014758
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014759 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014760 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014761 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14762 {
14763 list_append_string(rettv->vval.v_list,
14764 syn_id2name(m->hlg_id), -1);
14765 list_append_string(rettv->vval.v_list, m->pattern, -1);
14766 }
14767 else
14768 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014769 list_append_string(rettv->vval.v_list, NULL, -1);
14770 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014771 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014772 }
14773#endif
14774 }
14775}
14776
14777/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014778 * "matchdelete()" function
14779 */
14780 static void
14781f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014782 typval_T *argvars UNUSED;
14783 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014784{
14785#ifdef FEAT_SEARCH_EXTRA
14786 rettv->vval.v_number = match_delete(curwin,
14787 (int)get_tv_number(&argvars[0]), TRUE);
14788#endif
14789}
14790
14791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014792 * "matchend()" function
14793 */
14794 static void
14795f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014796 typval_T *argvars;
14797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798{
14799 find_some_match(argvars, rettv, 0);
14800}
14801
14802/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014803 * "matchlist()" function
14804 */
14805 static void
14806f_matchlist(argvars, rettv)
14807 typval_T *argvars;
14808 typval_T *rettv;
14809{
14810 find_some_match(argvars, rettv, 3);
14811}
14812
14813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014814 * "matchstr()" function
14815 */
14816 static void
14817f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014818 typval_T *argvars;
14819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014820{
14821 find_some_match(argvars, rettv, 2);
14822}
14823
Bram Moolenaar33570922005-01-25 22:26:29 +000014824static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014825
14826 static void
14827max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014828 typval_T *argvars;
14829 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014830 int domax;
14831{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014832 long n = 0;
14833 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014834 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014835
14836 if (argvars[0].v_type == VAR_LIST)
14837 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014838 list_T *l;
14839 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014840
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014841 l = argvars[0].vval.v_list;
14842 if (l != NULL)
14843 {
14844 li = l->lv_first;
14845 if (li != NULL)
14846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014848 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014849 {
14850 li = li->li_next;
14851 if (li == NULL)
14852 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014853 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014854 if (domax ? i > n : i < n)
14855 n = i;
14856 }
14857 }
14858 }
14859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014860 else if (argvars[0].v_type == VAR_DICT)
14861 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014862 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014863 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014864 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014865 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014866
14867 d = argvars[0].vval.v_dict;
14868 if (d != NULL)
14869 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014870 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014871 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014873 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014874 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014875 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014876 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014877 if (first)
14878 {
14879 n = i;
14880 first = FALSE;
14881 }
14882 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014883 n = i;
14884 }
14885 }
14886 }
14887 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014888 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014889 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014890 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014891}
14892
14893/*
14894 * "max()" function
14895 */
14896 static void
14897f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014898 typval_T *argvars;
14899 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014900{
14901 max_min(argvars, rettv, TRUE);
14902}
14903
14904/*
14905 * "min()" function
14906 */
14907 static void
14908f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 typval_T *argvars;
14910 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014911{
14912 max_min(argvars, rettv, FALSE);
14913}
14914
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014915static int mkdir_recurse __ARGS((char_u *dir, int prot));
14916
14917/*
14918 * Create the directory in which "dir" is located, and higher levels when
14919 * needed.
14920 */
14921 static int
14922mkdir_recurse(dir, prot)
14923 char_u *dir;
14924 int prot;
14925{
14926 char_u *p;
14927 char_u *updir;
14928 int r = FAIL;
14929
14930 /* Get end of directory name in "dir".
14931 * We're done when it's "/" or "c:/". */
14932 p = gettail_sep(dir);
14933 if (p <= get_past_head(dir))
14934 return OK;
14935
14936 /* If the directory exists we're done. Otherwise: create it.*/
14937 updir = vim_strnsave(dir, (int)(p - dir));
14938 if (updir == NULL)
14939 return FAIL;
14940 if (mch_isdir(updir))
14941 r = OK;
14942 else if (mkdir_recurse(updir, prot) == OK)
14943 r = vim_mkdir_emsg(updir, prot);
14944 vim_free(updir);
14945 return r;
14946}
14947
14948#ifdef vim_mkdir
14949/*
14950 * "mkdir()" function
14951 */
14952 static void
14953f_mkdir(argvars, rettv)
14954 typval_T *argvars;
14955 typval_T *rettv;
14956{
14957 char_u *dir;
14958 char_u buf[NUMBUFLEN];
14959 int prot = 0755;
14960
14961 rettv->vval.v_number = FAIL;
14962 if (check_restricted() || check_secure())
14963 return;
14964
14965 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014966 if (*dir == NUL)
14967 rettv->vval.v_number = FAIL;
14968 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014969 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014970 if (*gettail(dir) == NUL)
14971 /* remove trailing slashes */
14972 *gettail_sep(dir) = NUL;
14973
14974 if (argvars[1].v_type != VAR_UNKNOWN)
14975 {
14976 if (argvars[2].v_type != VAR_UNKNOWN)
14977 prot = get_tv_number_chk(&argvars[2], NULL);
14978 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14979 mkdir_recurse(dir, prot);
14980 }
14981 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014982 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014983}
14984#endif
14985
Bram Moolenaar0d660222005-01-07 21:51:51 +000014986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 * "mode()" function
14988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014991 typval_T *argvars;
14992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014994 char_u buf[3];
14995
14996 buf[1] = NUL;
14997 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 if (VIsual_active)
15000 {
15001 if (VIsual_select)
15002 buf[0] = VIsual_mode + 's' - 'v';
15003 else
15004 buf[0] = VIsual_mode;
15005 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015006 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015007 || State == CONFIRM)
15008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015010 if (State == ASKMORE)
15011 buf[1] = 'm';
15012 else if (State == CONFIRM)
15013 buf[1] = '?';
15014 }
15015 else if (State == EXTERNCMD)
15016 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 else if (State & INSERT)
15018 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015019#ifdef FEAT_VREPLACE
15020 if (State & VREPLACE_FLAG)
15021 {
15022 buf[0] = 'R';
15023 buf[1] = 'v';
15024 }
15025 else
15026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 if (State & REPLACE_FLAG)
15028 buf[0] = 'R';
15029 else
15030 buf[0] = 'i';
15031 }
15032 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015035 if (exmode_active)
15036 buf[1] = 'v';
15037 }
15038 else if (exmode_active)
15039 {
15040 buf[0] = 'c';
15041 buf[1] = 'e';
15042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015046 if (finish_op)
15047 buf[1] = 'o';
15048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015050 /* Clear out the minor mode when the argument is not a non-zero number or
15051 * non-empty string. */
15052 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015053 buf[1] = NUL;
15054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 rettv->vval.v_string = vim_strsave(buf);
15056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057}
15058
Bram Moolenaar429fa852013-04-15 12:27:36 +020015059#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015060/*
15061 * "mzeval()" function
15062 */
15063 static void
15064f_mzeval(argvars, rettv)
15065 typval_T *argvars;
15066 typval_T *rettv;
15067{
15068 char_u *str;
15069 char_u buf[NUMBUFLEN];
15070
15071 str = get_tv_string_buf(&argvars[0], buf);
15072 do_mzeval(str, rettv);
15073}
Bram Moolenaar75676462013-01-30 14:55:42 +010015074
15075 void
15076mzscheme_call_vim(name, args, rettv)
15077 char_u *name;
15078 typval_T *args;
15079 typval_T *rettv;
15080{
15081 typval_T argvars[3];
15082
15083 argvars[0].v_type = VAR_STRING;
15084 argvars[0].vval.v_string = name;
15085 copy_tv(args, &argvars[1]);
15086 argvars[2].v_type = VAR_UNKNOWN;
15087 f_call(argvars, rettv);
15088 clear_tv(&argvars[1]);
15089}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015090#endif
15091
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 * "nextnonblank()" function
15094 */
15095 static void
15096f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015097 typval_T *argvars;
15098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099{
15100 linenr_T lnum;
15101
15102 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015104 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 {
15106 lnum = 0;
15107 break;
15108 }
15109 if (*skipwhite(ml_get(lnum)) != NUL)
15110 break;
15111 }
15112 rettv->vval.v_number = lnum;
15113}
15114
15115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 * "nr2char()" function
15117 */
15118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015120 typval_T *argvars;
15121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122{
15123 char_u buf[NUMBUFLEN];
15124
15125#ifdef FEAT_MBYTE
15126 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015127 {
15128 int utf8 = 0;
15129
15130 if (argvars[1].v_type != VAR_UNKNOWN)
15131 utf8 = get_tv_number_chk(&argvars[1], NULL);
15132 if (utf8)
15133 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15134 else
15135 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 else
15138#endif
15139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 buf[1] = NUL;
15142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 rettv->v_type = VAR_STRING;
15144 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015145}
15146
15147/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015148 * "or(expr, expr)" function
15149 */
15150 static void
15151f_or(argvars, rettv)
15152 typval_T *argvars;
15153 typval_T *rettv;
15154{
15155 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15156 | get_tv_number_chk(&argvars[1], NULL);
15157}
15158
15159/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015160 * "pathshorten()" function
15161 */
15162 static void
15163f_pathshorten(argvars, rettv)
15164 typval_T *argvars;
15165 typval_T *rettv;
15166{
15167 char_u *p;
15168
15169 rettv->v_type = VAR_STRING;
15170 p = get_tv_string_chk(&argvars[0]);
15171 if (p == NULL)
15172 rettv->vval.v_string = NULL;
15173 else
15174 {
15175 p = vim_strsave(p);
15176 rettv->vval.v_string = p;
15177 if (p != NULL)
15178 shorten_dir(p);
15179 }
15180}
15181
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015182#ifdef FEAT_FLOAT
15183/*
15184 * "pow()" function
15185 */
15186 static void
15187f_pow(argvars, rettv)
15188 typval_T *argvars;
15189 typval_T *rettv;
15190{
15191 float_T fx, fy;
15192
15193 rettv->v_type = VAR_FLOAT;
15194 if (get_float_arg(argvars, &fx) == OK
15195 && get_float_arg(&argvars[1], &fy) == OK)
15196 rettv->vval.v_float = pow(fx, fy);
15197 else
15198 rettv->vval.v_float = 0.0;
15199}
15200#endif
15201
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203 * "prevnonblank()" function
15204 */
15205 static void
15206f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *argvars;
15208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209{
15210 linenr_T lnum;
15211
15212 lnum = get_tv_lnum(argvars);
15213 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15214 lnum = 0;
15215 else
15216 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15217 --lnum;
15218 rettv->vval.v_number = lnum;
15219}
15220
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015221#ifdef HAVE_STDARG_H
15222/* This dummy va_list is here because:
15223 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15224 * - locally in the function results in a "used before set" warning
15225 * - using va_start() to initialize it gives "function with fixed args" error */
15226static va_list ap;
15227#endif
15228
Bram Moolenaar8c711452005-01-14 21:53:12 +000015229/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015230 * "printf()" function
15231 */
15232 static void
15233f_printf(argvars, rettv)
15234 typval_T *argvars;
15235 typval_T *rettv;
15236{
15237 rettv->v_type = VAR_STRING;
15238 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015239#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015240 {
15241 char_u buf[NUMBUFLEN];
15242 int len;
15243 char_u *s;
15244 int saved_did_emsg = did_emsg;
15245 char *fmt;
15246
15247 /* Get the required length, allocate the buffer and do it for real. */
15248 did_emsg = FALSE;
15249 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015250 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015251 if (!did_emsg)
15252 {
15253 s = alloc(len + 1);
15254 if (s != NULL)
15255 {
15256 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015257 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015258 }
15259 }
15260 did_emsg |= saved_did_emsg;
15261 }
15262#endif
15263}
15264
15265/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015266 * "pumvisible()" function
15267 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015268 static void
15269f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015270 typval_T *argvars UNUSED;
15271 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015272{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015273#ifdef FEAT_INS_EXPAND
15274 if (pum_visible())
15275 rettv->vval.v_number = 1;
15276#endif
15277}
15278
Bram Moolenaardb913952012-06-29 12:54:53 +020015279#ifdef FEAT_PYTHON3
15280/*
15281 * "py3eval()" function
15282 */
15283 static void
15284f_py3eval(argvars, rettv)
15285 typval_T *argvars;
15286 typval_T *rettv;
15287{
15288 char_u *str;
15289 char_u buf[NUMBUFLEN];
15290
15291 str = get_tv_string_buf(&argvars[0], buf);
15292 do_py3eval(str, rettv);
15293}
15294#endif
15295
15296#ifdef FEAT_PYTHON
15297/*
15298 * "pyeval()" function
15299 */
15300 static void
15301f_pyeval(argvars, rettv)
15302 typval_T *argvars;
15303 typval_T *rettv;
15304{
15305 char_u *str;
15306 char_u buf[NUMBUFLEN];
15307
15308 str = get_tv_string_buf(&argvars[0], buf);
15309 do_pyeval(str, rettv);
15310}
15311#endif
15312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015314 * "range()" function
15315 */
15316 static void
15317f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 typval_T *argvars;
15319 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320{
15321 long start;
15322 long end;
15323 long stride = 1;
15324 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015328 if (argvars[1].v_type == VAR_UNKNOWN)
15329 {
15330 end = start - 1;
15331 start = 0;
15332 }
15333 else
15334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015335 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015336 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015337 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015338 }
15339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015340 if (error)
15341 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015342 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015343 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015344 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015345 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015346 else
15347 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015348 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015349 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015350 if (list_append_number(rettv->vval.v_list,
15351 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015352 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015353 }
15354}
15355
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015356/*
15357 * "readfile()" function
15358 */
15359 static void
15360f_readfile(argvars, rettv)
15361 typval_T *argvars;
15362 typval_T *rettv;
15363{
15364 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015365 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 char_u *fname;
15367 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015368 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15369 int io_size = sizeof(buf);
15370 int readlen; /* size of last fread() */
15371 char_u *prev = NULL; /* previously read bytes, if any */
15372 long prevlen = 0; /* length of data in prev */
15373 long prevsize = 0; /* size of prev buffer */
15374 long maxline = MAXLNUM;
15375 long cnt = 0;
15376 char_u *p; /* position in buf */
15377 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015379 if (argvars[1].v_type != VAR_UNKNOWN)
15380 {
15381 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15382 binary = TRUE;
15383 if (argvars[2].v_type != VAR_UNKNOWN)
15384 maxline = get_tv_number(&argvars[2]);
15385 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015386
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015387 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015388 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015389
15390 /* Always open the file in binary mode, library functions have a mind of
15391 * their own about CR-LF conversion. */
15392 fname = get_tv_string(&argvars[0]);
15393 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15394 {
15395 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15396 return;
15397 }
15398
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015399 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015400 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015401 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015402
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015403 /* This for loop processes what was read, but is also entered at end
15404 * of file so that either:
15405 * - an incomplete line gets written
15406 * - a "binary" file gets an empty line at the end if it ends in a
15407 * newline. */
15408 for (p = buf, start = buf;
15409 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15410 ++p)
15411 {
15412 if (*p == '\n' || readlen <= 0)
15413 {
15414 listitem_T *li;
15415 char_u *s = NULL;
15416 long_u len = p - start;
15417
15418 /* Finished a line. Remove CRs before NL. */
15419 if (readlen > 0 && !binary)
15420 {
15421 while (len > 0 && start[len - 1] == '\r')
15422 --len;
15423 /* removal may cross back to the "prev" string */
15424 if (len == 0)
15425 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15426 --prevlen;
15427 }
15428 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015429 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015430 else
15431 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015432 /* Change "prev" buffer to be the right size. This way
15433 * the bytes are only copied once, and very long lines are
15434 * allocated only once. */
15435 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015436 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015437 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015438 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015439 prev = NULL; /* the list will own the string */
15440 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015441 }
15442 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015443 if (s == NULL)
15444 {
15445 do_outofmem_msg((long_u) prevlen + len + 1);
15446 failed = TRUE;
15447 break;
15448 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015449
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015450 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015451 {
15452 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015453 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015454 break;
15455 }
15456 li->li_tv.v_type = VAR_STRING;
15457 li->li_tv.v_lock = 0;
15458 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015459 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015460
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015461 start = p + 1; /* step over newline */
15462 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015463 break;
15464 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015465 else if (*p == NUL)
15466 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015467#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015468 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15469 * when finding the BF and check the previous two bytes. */
15470 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015471 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015472 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15473 * + 1, these may be in the "prev" string. */
15474 char_u back1 = p >= buf + 1 ? p[-1]
15475 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15476 char_u back2 = p >= buf + 2 ? p[-2]
15477 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15478 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015479
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015480 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015481 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015482 char_u *dest = p - 2;
15483
15484 /* Usually a BOM is at the beginning of a file, and so at
15485 * the beginning of a line; then we can just step over it.
15486 */
15487 if (start == dest)
15488 start = p + 1;
15489 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015490 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015491 /* have to shuffle buf to close gap */
15492 int adjust_prevlen = 0;
15493
15494 if (dest < buf)
15495 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015496 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015497 dest = buf;
15498 }
15499 if (readlen > p - buf + 1)
15500 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15501 readlen -= 3 - adjust_prevlen;
15502 prevlen -= adjust_prevlen;
15503 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015504 }
15505 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015506 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015507#endif
15508 } /* for */
15509
15510 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15511 break;
15512 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015513 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 /* There's part of a line in buf, store it in "prev". */
15515 if (p - start + prevlen >= prevsize)
15516 {
15517 /* need bigger "prev" buffer */
15518 char_u *newprev;
15519
15520 /* A common use case is ordinary text files and "prev" gets a
15521 * fragment of a line, so the first allocation is made
15522 * small, to avoid repeatedly 'allocing' large and
15523 * 'reallocing' small. */
15524 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015525 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015526 else
15527 {
15528 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015529 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015530 prevsize = grow50pc > growmin ? grow50pc : growmin;
15531 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015532 newprev = prev == NULL ? alloc(prevsize)
15533 : vim_realloc(prev, prevsize);
15534 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015535 {
15536 do_outofmem_msg((long_u)prevsize);
15537 failed = TRUE;
15538 break;
15539 }
15540 prev = newprev;
15541 }
15542 /* Add the line part to end of "prev". */
15543 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015544 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015546 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015547
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015548 /*
15549 * For a negative line count use only the lines at the end of the file,
15550 * free the rest.
15551 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015552 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015553 while (cnt > -maxline)
15554 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015555 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015556 --cnt;
15557 }
15558
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015559 if (failed)
15560 {
15561 list_free(rettv->vval.v_list, TRUE);
15562 /* readfile doc says an empty list is returned on error */
15563 rettv->vval.v_list = list_alloc();
15564 }
15565
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015566 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015567 fclose(fd);
15568}
15569
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015570#if defined(FEAT_RELTIME)
15571static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15572
15573/*
15574 * Convert a List to proftime_T.
15575 * Return FAIL when there is something wrong.
15576 */
15577 static int
15578list2proftime(arg, tm)
15579 typval_T *arg;
15580 proftime_T *tm;
15581{
15582 long n1, n2;
15583 int error = FALSE;
15584
15585 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15586 || arg->vval.v_list->lv_len != 2)
15587 return FAIL;
15588 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15589 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15590# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015591 tm->HighPart = n1;
15592 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015593# else
15594 tm->tv_sec = n1;
15595 tm->tv_usec = n2;
15596# endif
15597 return error ? FAIL : OK;
15598}
15599#endif /* FEAT_RELTIME */
15600
15601/*
15602 * "reltime()" function
15603 */
15604 static void
15605f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015606 typval_T *argvars UNUSED;
15607 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015608{
15609#ifdef FEAT_RELTIME
15610 proftime_T res;
15611 proftime_T start;
15612
15613 if (argvars[0].v_type == VAR_UNKNOWN)
15614 {
15615 /* No arguments: get current time. */
15616 profile_start(&res);
15617 }
15618 else if (argvars[1].v_type == VAR_UNKNOWN)
15619 {
15620 if (list2proftime(&argvars[0], &res) == FAIL)
15621 return;
15622 profile_end(&res);
15623 }
15624 else
15625 {
15626 /* Two arguments: compute the difference. */
15627 if (list2proftime(&argvars[0], &start) == FAIL
15628 || list2proftime(&argvars[1], &res) == FAIL)
15629 return;
15630 profile_sub(&res, &start);
15631 }
15632
15633 if (rettv_list_alloc(rettv) == OK)
15634 {
15635 long n1, n2;
15636
15637# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015638 n1 = res.HighPart;
15639 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015640# else
15641 n1 = res.tv_sec;
15642 n2 = res.tv_usec;
15643# endif
15644 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15645 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15646 }
15647#endif
15648}
15649
15650/*
15651 * "reltimestr()" function
15652 */
15653 static void
15654f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015655 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015656 typval_T *rettv;
15657{
15658#ifdef FEAT_RELTIME
15659 proftime_T tm;
15660#endif
15661
15662 rettv->v_type = VAR_STRING;
15663 rettv->vval.v_string = NULL;
15664#ifdef FEAT_RELTIME
15665 if (list2proftime(&argvars[0], &tm) == OK)
15666 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15667#endif
15668}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015669
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15671static void make_connection __ARGS((void));
15672static int check_connection __ARGS((void));
15673
15674 static void
15675make_connection()
15676{
15677 if (X_DISPLAY == NULL
15678# ifdef FEAT_GUI
15679 && !gui.in_use
15680# endif
15681 )
15682 {
15683 x_force_connect = TRUE;
15684 setup_term_clip();
15685 x_force_connect = FALSE;
15686 }
15687}
15688
15689 static int
15690check_connection()
15691{
15692 make_connection();
15693 if (X_DISPLAY == NULL)
15694 {
15695 EMSG(_("E240: No connection to Vim server"));
15696 return FAIL;
15697 }
15698 return OK;
15699}
15700#endif
15701
15702#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015703static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704
15705 static void
15706remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015707 typval_T *argvars;
15708 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015709 int expr;
15710{
15711 char_u *server_name;
15712 char_u *keys;
15713 char_u *r = NULL;
15714 char_u buf[NUMBUFLEN];
15715# ifdef WIN32
15716 HWND w;
15717# else
15718 Window w;
15719# endif
15720
15721 if (check_restricted() || check_secure())
15722 return;
15723
15724# ifdef FEAT_X11
15725 if (check_connection() == FAIL)
15726 return;
15727# endif
15728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 server_name = get_tv_string_chk(&argvars[0]);
15730 if (server_name == NULL)
15731 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732 keys = get_tv_string_buf(&argvars[1], buf);
15733# ifdef WIN32
15734 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15735# else
15736 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15737 < 0)
15738# endif
15739 {
15740 if (r != NULL)
15741 EMSG(r); /* sending worked but evaluation failed */
15742 else
15743 EMSG2(_("E241: Unable to send to %s"), server_name);
15744 return;
15745 }
15746
15747 rettv->vval.v_string = r;
15748
15749 if (argvars[2].v_type != VAR_UNKNOWN)
15750 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015751 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015752 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015753 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015755 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 v.di_tv.v_type = VAR_STRING;
15757 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015758 idvar = get_tv_string_chk(&argvars[2]);
15759 if (idvar != NULL)
15760 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015761 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 }
15763}
15764#endif
15765
15766/*
15767 * "remote_expr()" function
15768 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769 static void
15770f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015771 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773{
15774 rettv->v_type = VAR_STRING;
15775 rettv->vval.v_string = NULL;
15776#ifdef FEAT_CLIENTSERVER
15777 remote_common(argvars, rettv, TRUE);
15778#endif
15779}
15780
15781/*
15782 * "remote_foreground()" function
15783 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784 static void
15785f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015786 typval_T *argvars UNUSED;
15787 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789#ifdef FEAT_CLIENTSERVER
15790# ifdef WIN32
15791 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015792 {
15793 char_u *server_name = get_tv_string_chk(&argvars[0]);
15794
15795 if (server_name != NULL)
15796 serverForeground(server_name);
15797 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798# else
15799 /* Send a foreground() expression to the server. */
15800 argvars[1].v_type = VAR_STRING;
15801 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15802 argvars[2].v_type = VAR_UNKNOWN;
15803 remote_common(argvars, rettv, TRUE);
15804 vim_free(argvars[1].vval.v_string);
15805# endif
15806#endif
15807}
15808
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809 static void
15810f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015811 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813{
15814#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816 char_u *s = NULL;
15817# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015818 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015820 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821
15822 if (check_restricted() || check_secure())
15823 {
15824 rettv->vval.v_number = -1;
15825 return;
15826 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015827 serverid = get_tv_string_chk(&argvars[0]);
15828 if (serverid == NULL)
15829 {
15830 rettv->vval.v_number = -1;
15831 return; /* type error; errmsg already given */
15832 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015834 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835 if (n == 0)
15836 rettv->vval.v_number = -1;
15837 else
15838 {
15839 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15840 rettv->vval.v_number = (s != NULL);
15841 }
15842# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843 if (check_connection() == FAIL)
15844 return;
15845
15846 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848# endif
15849
15850 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015852 char_u *retvar;
15853
Bram Moolenaar33570922005-01-25 22:26:29 +000015854 v.di_tv.v_type = VAR_STRING;
15855 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015856 retvar = get_tv_string_chk(&argvars[1]);
15857 if (retvar != NULL)
15858 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015859 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015860 }
15861#else
15862 rettv->vval.v_number = -1;
15863#endif
15864}
15865
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866 static void
15867f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015868 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015869 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870{
15871 char_u *r = NULL;
15872
15873#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 char_u *serverid = get_tv_string_chk(&argvars[0]);
15875
15876 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877 {
15878# ifdef WIN32
15879 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015880 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015881
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015882 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015883 if (n != 0)
15884 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15885 if (r == NULL)
15886# else
15887 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015888 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889# endif
15890 EMSG(_("E277: Unable to read a server reply"));
15891 }
15892#endif
15893 rettv->v_type = VAR_STRING;
15894 rettv->vval.v_string = r;
15895}
15896
15897/*
15898 * "remote_send()" function
15899 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015900 static void
15901f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015904{
15905 rettv->v_type = VAR_STRING;
15906 rettv->vval.v_string = NULL;
15907#ifdef FEAT_CLIENTSERVER
15908 remote_common(argvars, rettv, FALSE);
15909#endif
15910}
15911
15912/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015913 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015914 */
15915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015916f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015917 typval_T *argvars;
15918 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919{
Bram Moolenaar33570922005-01-25 22:26:29 +000015920 list_T *l;
15921 listitem_T *item, *item2;
15922 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015923 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015924 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015925 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015926 dict_T *d;
15927 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015928 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015929
Bram Moolenaar8c711452005-01-14 21:53:12 +000015930 if (argvars[0].v_type == VAR_DICT)
15931 {
15932 if (argvars[2].v_type != VAR_UNKNOWN)
15933 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015934 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015935 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015937 key = get_tv_string_chk(&argvars[1]);
15938 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015940 di = dict_find(d, key, -1);
15941 if (di == NULL)
15942 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015943 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15944 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 {
15946 *rettv = di->di_tv;
15947 init_tv(&di->di_tv);
15948 dictitem_remove(d, di);
15949 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015951 }
15952 }
15953 else if (argvars[0].v_type != VAR_LIST)
15954 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015955 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015956 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015958 int error = FALSE;
15959
15960 idx = get_tv_number_chk(&argvars[1], &error);
15961 if (error)
15962 ; /* type error: do nothing, errmsg already given */
15963 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015964 EMSGN(_(e_listidx), idx);
15965 else
15966 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015967 if (argvars[2].v_type == VAR_UNKNOWN)
15968 {
15969 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015970 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015971 *rettv = item->li_tv;
15972 vim_free(item);
15973 }
15974 else
15975 {
15976 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 end = get_tv_number_chk(&argvars[2], &error);
15978 if (error)
15979 ; /* type error: do nothing */
15980 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015981 EMSGN(_(e_listidx), end);
15982 else
15983 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015984 int cnt = 0;
15985
15986 for (li = item; li != NULL; li = li->li_next)
15987 {
15988 ++cnt;
15989 if (li == item2)
15990 break;
15991 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015992 if (li == NULL) /* didn't find "item2" after "item" */
15993 EMSG(_(e_invrange));
15994 else
15995 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015996 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015997 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015998 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015999 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016000 l->lv_first = item;
16001 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016002 item->li_prev = NULL;
16003 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016004 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016005 }
16006 }
16007 }
16008 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016009 }
16010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011}
16012
16013/*
16014 * "rename({from}, {to})" function
16015 */
16016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 typval_T *argvars;
16019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020{
16021 char_u buf[NUMBUFLEN];
16022
16023 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016026 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16027 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028}
16029
16030/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016031 * "repeat()" function
16032 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016033 static void
16034f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016035 typval_T *argvars;
16036 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016037{
16038 char_u *p;
16039 int n;
16040 int slen;
16041 int len;
16042 char_u *r;
16043 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016044
16045 n = get_tv_number(&argvars[1]);
16046 if (argvars[0].v_type == VAR_LIST)
16047 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016048 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016049 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016050 if (list_extend(rettv->vval.v_list,
16051 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016052 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016053 }
16054 else
16055 {
16056 p = get_tv_string(&argvars[0]);
16057 rettv->v_type = VAR_STRING;
16058 rettv->vval.v_string = NULL;
16059
16060 slen = (int)STRLEN(p);
16061 len = slen * n;
16062 if (len <= 0)
16063 return;
16064
16065 r = alloc(len + 1);
16066 if (r != NULL)
16067 {
16068 for (i = 0; i < n; i++)
16069 mch_memmove(r + i * slen, p, (size_t)slen);
16070 r[len] = NUL;
16071 }
16072
16073 rettv->vval.v_string = r;
16074 }
16075}
16076
16077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 * "resolve()" function
16079 */
16080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 typval_T *argvars;
16083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084{
16085 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016086#ifdef HAVE_READLINK
16087 char_u *buf = NULL;
16088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016090 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091#ifdef FEAT_SHORTCUT
16092 {
16093 char_u *v = NULL;
16094
16095 v = mch_resolve_shortcut(p);
16096 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 }
16101#else
16102# ifdef HAVE_READLINK
16103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 char_u *cpy;
16105 int len;
16106 char_u *remain = NULL;
16107 char_u *q;
16108 int is_relative_to_current = FALSE;
16109 int has_trailing_pathsep = FALSE;
16110 int limit = 100;
16111
16112 p = vim_strsave(p);
16113
16114 if (p[0] == '.' && (vim_ispathsep(p[1])
16115 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16116 is_relative_to_current = TRUE;
16117
16118 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016119 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016122 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124
16125 q = getnextcomp(p);
16126 if (*q != NUL)
16127 {
16128 /* Separate the first path component in "p", and keep the
16129 * remainder (beginning with the path separator). */
16130 remain = vim_strsave(q - 1);
16131 q[-1] = NUL;
16132 }
16133
Bram Moolenaard9462e32011-04-11 21:35:11 +020016134 buf = alloc(MAXPATHL + 1);
16135 if (buf == NULL)
16136 goto fail;
16137
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 for (;;)
16139 {
16140 for (;;)
16141 {
16142 len = readlink((char *)p, (char *)buf, MAXPATHL);
16143 if (len <= 0)
16144 break;
16145 buf[len] = NUL;
16146
16147 if (limit-- == 0)
16148 {
16149 vim_free(p);
16150 vim_free(remain);
16151 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016152 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 goto fail;
16154 }
16155
16156 /* Ensure that the result will have a trailing path separator
16157 * if the argument has one. */
16158 if (remain == NULL && has_trailing_pathsep)
16159 add_pathsep(buf);
16160
16161 /* Separate the first path component in the link value and
16162 * concatenate the remainders. */
16163 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16164 if (*q != NUL)
16165 {
16166 if (remain == NULL)
16167 remain = vim_strsave(q - 1);
16168 else
16169 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016170 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 if (cpy != NULL)
16172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 vim_free(remain);
16174 remain = cpy;
16175 }
16176 }
16177 q[-1] = NUL;
16178 }
16179
16180 q = gettail(p);
16181 if (q > p && *q == NUL)
16182 {
16183 /* Ignore trailing path separator. */
16184 q[-1] = NUL;
16185 q = gettail(p);
16186 }
16187 if (q > p && !mch_isFullName(buf))
16188 {
16189 /* symlink is relative to directory of argument */
16190 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16191 if (cpy != NULL)
16192 {
16193 STRCPY(cpy, p);
16194 STRCPY(gettail(cpy), buf);
16195 vim_free(p);
16196 p = cpy;
16197 }
16198 }
16199 else
16200 {
16201 vim_free(p);
16202 p = vim_strsave(buf);
16203 }
16204 }
16205
16206 if (remain == NULL)
16207 break;
16208
16209 /* Append the first path component of "remain" to "p". */
16210 q = getnextcomp(remain + 1);
16211 len = q - remain - (*q != NUL);
16212 cpy = vim_strnsave(p, STRLEN(p) + len);
16213 if (cpy != NULL)
16214 {
16215 STRNCAT(cpy, remain, len);
16216 vim_free(p);
16217 p = cpy;
16218 }
16219 /* Shorten "remain". */
16220 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016221 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 else
16223 {
16224 vim_free(remain);
16225 remain = NULL;
16226 }
16227 }
16228
16229 /* If the result is a relative path name, make it explicitly relative to
16230 * the current directory if and only if the argument had this form. */
16231 if (!vim_ispathsep(*p))
16232 {
16233 if (is_relative_to_current
16234 && *p != NUL
16235 && !(p[0] == '.'
16236 && (p[1] == NUL
16237 || vim_ispathsep(p[1])
16238 || (p[1] == '.'
16239 && (p[2] == NUL
16240 || vim_ispathsep(p[2]))))))
16241 {
16242 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016243 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 if (cpy != NULL)
16245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 vim_free(p);
16247 p = cpy;
16248 }
16249 }
16250 else if (!is_relative_to_current)
16251 {
16252 /* Strip leading "./". */
16253 q = p;
16254 while (q[0] == '.' && vim_ispathsep(q[1]))
16255 q += 2;
16256 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016257 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 }
16259 }
16260
16261 /* Ensure that the result will have no trailing path separator
16262 * if the argument had none. But keep "/" or "//". */
16263 if (!has_trailing_pathsep)
16264 {
16265 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016266 if (after_pathsep(p, q))
16267 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 }
16269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 }
16272# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274# endif
16275#endif
16276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278
16279#ifdef HAVE_READLINK
16280fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016281 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016283 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284}
16285
16286/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 */
16289 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 typval_T *argvars;
16292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293{
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 list_T *l;
16295 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297 if (argvars[0].v_type != VAR_LIST)
16298 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016299 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016300 && !tv_check_lock(l->lv_lock,
16301 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016302 {
16303 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016304 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016305 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 while (li != NULL)
16307 {
16308 ni = li->li_prev;
16309 list_append(l, li);
16310 li = ni;
16311 }
16312 rettv->vval.v_list = l;
16313 rettv->v_type = VAR_LIST;
16314 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016315 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317}
16318
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016319#define SP_NOMOVE 0x01 /* don't move cursor */
16320#define SP_REPEAT 0x02 /* repeat to find outer pair */
16321#define SP_RETCOUNT 0x04 /* return matchcount */
16322#define SP_SETPCMARK 0x08 /* set previous context mark */
16323#define SP_START 0x10 /* accept match at start position */
16324#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16325#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016326
Bram Moolenaar33570922005-01-25 22:26:29 +000016327static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328
16329/*
16330 * Get flags for a search function.
16331 * Possibly sets "p_ws".
16332 * Returns BACKWARD, FORWARD or zero (for an error).
16333 */
16334 static int
16335get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 int *flagsp;
16338{
16339 int dir = FORWARD;
16340 char_u *flags;
16341 char_u nbuf[NUMBUFLEN];
16342 int mask;
16343
16344 if (varp->v_type != VAR_UNKNOWN)
16345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016346 flags = get_tv_string_buf_chk(varp, nbuf);
16347 if (flags == NULL)
16348 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 while (*flags != NUL)
16350 {
16351 switch (*flags)
16352 {
16353 case 'b': dir = BACKWARD; break;
16354 case 'w': p_ws = TRUE; break;
16355 case 'W': p_ws = FALSE; break;
16356 default: mask = 0;
16357 if (flagsp != NULL)
16358 switch (*flags)
16359 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016360 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016361 case 'e': mask = SP_END; break;
16362 case 'm': mask = SP_RETCOUNT; break;
16363 case 'n': mask = SP_NOMOVE; break;
16364 case 'p': mask = SP_SUBPAT; break;
16365 case 'r': mask = SP_REPEAT; break;
16366 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367 }
16368 if (mask == 0)
16369 {
16370 EMSG2(_(e_invarg2), flags);
16371 dir = 0;
16372 }
16373 else
16374 *flagsp |= mask;
16375 }
16376 if (dir == 0)
16377 break;
16378 ++flags;
16379 }
16380 }
16381 return dir;
16382}
16383
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016385 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016388search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016389 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016390 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016391 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016393 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 char_u *pat;
16395 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016396 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 int save_p_ws = p_ws;
16398 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016399 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016400 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016401 proftime_T tm;
16402#ifdef FEAT_RELTIME
16403 long time_limit = 0;
16404#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016405 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016406 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016408 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016409 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016410 if (dir == 0)
16411 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016412 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016413 if (flags & SP_START)
16414 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016415 if (flags & SP_END)
16416 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016417
Bram Moolenaar76929292008-01-06 19:07:36 +000016418 /* Optional arguments: line number to stop searching and timeout. */
16419 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016420 {
16421 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16422 if (lnum_stop < 0)
16423 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016424#ifdef FEAT_RELTIME
16425 if (argvars[3].v_type != VAR_UNKNOWN)
16426 {
16427 time_limit = get_tv_number_chk(&argvars[3], NULL);
16428 if (time_limit < 0)
16429 goto theend;
16430 }
16431#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016432 }
16433
Bram Moolenaar76929292008-01-06 19:07:36 +000016434#ifdef FEAT_RELTIME
16435 /* Set the time limit, if there is one. */
16436 profile_setlimit(time_limit, &tm);
16437#endif
16438
Bram Moolenaar231334e2005-07-25 20:46:57 +000016439 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016440 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016441 * Check to make sure only those flags are set.
16442 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16443 * flags cannot be set. Check for that condition also.
16444 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016445 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016446 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016449 goto theend;
16450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016452 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016453 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016454 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016455 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016457 if (flags & SP_SUBPAT)
16458 retval = subpatnum;
16459 else
16460 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016461 if (flags & SP_SETPCMARK)
16462 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016464 if (match_pos != NULL)
16465 {
16466 /* Store the match cursor position */
16467 match_pos->lnum = pos.lnum;
16468 match_pos->col = pos.col + 1;
16469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 /* "/$" will put the cursor after the end of the line, may need to
16471 * correct that here */
16472 check_cursor();
16473 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016474
16475 /* If 'n' flag is used: restore cursor position. */
16476 if (flags & SP_NOMOVE)
16477 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016478 else
16479 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016480theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016482
16483 return retval;
16484}
16485
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016486#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016487
16488/*
16489 * round() is not in C90, use ceil() or floor() instead.
16490 */
16491 float_T
16492vim_round(f)
16493 float_T f;
16494{
16495 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16496}
16497
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016498/*
16499 * "round({float})" function
16500 */
16501 static void
16502f_round(argvars, rettv)
16503 typval_T *argvars;
16504 typval_T *rettv;
16505{
16506 float_T f;
16507
16508 rettv->v_type = VAR_FLOAT;
16509 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016510 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016511 else
16512 rettv->vval.v_float = 0.0;
16513}
16514#endif
16515
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016516/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016517 * "screenattr()" function
16518 */
16519 static void
16520f_screenattr(argvars, rettv)
16521 typval_T *argvars UNUSED;
16522 typval_T *rettv;
16523{
16524 int row;
16525 int col;
16526 int c;
16527
16528 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16529 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16530 if (row < 0 || row >= screen_Rows
16531 || col < 0 || col >= screen_Columns)
16532 c = -1;
16533 else
16534 c = ScreenAttrs[LineOffset[row] + col];
16535 rettv->vval.v_number = c;
16536}
16537
16538/*
16539 * "screenchar()" function
16540 */
16541 static void
16542f_screenchar(argvars, rettv)
16543 typval_T *argvars UNUSED;
16544 typval_T *rettv;
16545{
16546 int row;
16547 int col;
16548 int off;
16549 int c;
16550
16551 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16552 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16553 if (row < 0 || row >= screen_Rows
16554 || col < 0 || col >= screen_Columns)
16555 c = -1;
16556 else
16557 {
16558 off = LineOffset[row] + col;
16559#ifdef FEAT_MBYTE
16560 if (enc_utf8 && ScreenLinesUC[off] != 0)
16561 c = ScreenLinesUC[off];
16562 else
16563#endif
16564 c = ScreenLines[off];
16565 }
16566 rettv->vval.v_number = c;
16567}
16568
16569/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016570 * "screencol()" function
16571 *
16572 * First column is 1 to be consistent with virtcol().
16573 */
16574 static void
16575f_screencol(argvars, rettv)
16576 typval_T *argvars UNUSED;
16577 typval_T *rettv;
16578{
16579 rettv->vval.v_number = screen_screencol() + 1;
16580}
16581
16582/*
16583 * "screenrow()" function
16584 */
16585 static void
16586f_screenrow(argvars, rettv)
16587 typval_T *argvars UNUSED;
16588 typval_T *rettv;
16589{
16590 rettv->vval.v_number = screen_screenrow() + 1;
16591}
16592
16593/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016594 * "search()" function
16595 */
16596 static void
16597f_search(argvars, rettv)
16598 typval_T *argvars;
16599 typval_T *rettv;
16600{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016601 int flags = 0;
16602
16603 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604}
16605
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016607 * "searchdecl()" function
16608 */
16609 static void
16610f_searchdecl(argvars, rettv)
16611 typval_T *argvars;
16612 typval_T *rettv;
16613{
16614 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016615 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016616 int error = FALSE;
16617 char_u *name;
16618
16619 rettv->vval.v_number = 1; /* default: FAIL */
16620
16621 name = get_tv_string_chk(&argvars[0]);
16622 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016623 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016624 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016625 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16626 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16627 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016628 if (!error && name != NULL)
16629 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016630 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016631}
16632
16633/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016634 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016636 static int
16637searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016638 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016639 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640{
16641 char_u *spat, *mpat, *epat;
16642 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 int dir;
16645 int flags = 0;
16646 char_u nbuf1[NUMBUFLEN];
16647 char_u nbuf2[NUMBUFLEN];
16648 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016649 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016650 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016651 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016654 spat = get_tv_string_chk(&argvars[0]);
16655 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16656 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16657 if (spat == NULL || mpat == NULL || epat == NULL)
16658 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 /* Handle the optional fourth argument: flags */
16661 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016662 if (dir == 0)
16663 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016664
16665 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016666 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16667 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016668 if ((flags & (SP_END | SP_SUBPAT)) != 0
16669 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016670 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016671 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016672 goto theend;
16673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016675 /* Using 'r' implies 'W', otherwise it doesn't work. */
16676 if (flags & SP_REPEAT)
16677 p_ws = FALSE;
16678
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016679 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016680 if (argvars[3].v_type == VAR_UNKNOWN
16681 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 skip = (char_u *)"";
16683 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016685 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016686 if (argvars[5].v_type != VAR_UNKNOWN)
16687 {
16688 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16689 if (lnum_stop < 0)
16690 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016691#ifdef FEAT_RELTIME
16692 if (argvars[6].v_type != VAR_UNKNOWN)
16693 {
16694 time_limit = get_tv_number_chk(&argvars[6], NULL);
16695 if (time_limit < 0)
16696 goto theend;
16697 }
16698#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016699 }
16700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016701 if (skip == NULL)
16702 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016704 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016705 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016706
16707theend:
16708 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016709
16710 return retval;
16711}
16712
16713/*
16714 * "searchpair()" function
16715 */
16716 static void
16717f_searchpair(argvars, rettv)
16718 typval_T *argvars;
16719 typval_T *rettv;
16720{
16721 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16722}
16723
16724/*
16725 * "searchpairpos()" function
16726 */
16727 static void
16728f_searchpairpos(argvars, rettv)
16729 typval_T *argvars;
16730 typval_T *rettv;
16731{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016732 pos_T match_pos;
16733 int lnum = 0;
16734 int col = 0;
16735
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016736 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016737 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016738
16739 if (searchpair_cmn(argvars, &match_pos) > 0)
16740 {
16741 lnum = match_pos.lnum;
16742 col = match_pos.col;
16743 }
16744
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016745 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16746 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016747}
16748
16749/*
16750 * Search for a start/middle/end thing.
16751 * Used by searchpair(), see its documentation for the details.
16752 * Returns 0 or -1 for no match,
16753 */
16754 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016755do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16756 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016757 char_u *spat; /* start pattern */
16758 char_u *mpat; /* middle pattern */
16759 char_u *epat; /* end pattern */
16760 int dir; /* BACKWARD or FORWARD */
16761 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016762 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016763 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016764 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016765 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016766{
16767 char_u *save_cpo;
16768 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16769 long retval = 0;
16770 pos_T pos;
16771 pos_T firstpos;
16772 pos_T foundpos;
16773 pos_T save_cursor;
16774 pos_T save_pos;
16775 int n;
16776 int r;
16777 int nest = 1;
16778 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016779 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016780 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016781
16782 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16783 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016784 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016785
Bram Moolenaar76929292008-01-06 19:07:36 +000016786#ifdef FEAT_RELTIME
16787 /* Set the time limit, if there is one. */
16788 profile_setlimit(time_limit, &tm);
16789#endif
16790
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016791 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16792 * start/middle/end (pat3, for the top pair). */
16793 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16794 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16795 if (pat2 == NULL || pat3 == NULL)
16796 goto theend;
16797 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16798 if (*mpat == NUL)
16799 STRCPY(pat3, pat2);
16800 else
16801 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16802 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016803 if (flags & SP_START)
16804 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016805
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 save_cursor = curwin->w_cursor;
16807 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016808 clearpos(&firstpos);
16809 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 pat = pat3;
16811 for (;;)
16812 {
16813 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016814 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16816 /* didn't find it or found the first match again: FAIL */
16817 break;
16818
16819 if (firstpos.lnum == 0)
16820 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016821 if (equalpos(pos, foundpos))
16822 {
16823 /* Found the same position again. Can happen with a pattern that
16824 * has "\zs" at the end and searching backwards. Advance one
16825 * character and try again. */
16826 if (dir == BACKWARD)
16827 decl(&pos);
16828 else
16829 incl(&pos);
16830 }
16831 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016833 /* clear the start flag to avoid getting stuck here */
16834 options &= ~SEARCH_START;
16835
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 /* If the skip pattern matches, ignore this match. */
16837 if (*skip != NUL)
16838 {
16839 save_pos = curwin->w_cursor;
16840 curwin->w_cursor = pos;
16841 r = eval_to_bool(skip, &err, NULL, FALSE);
16842 curwin->w_cursor = save_pos;
16843 if (err)
16844 {
16845 /* Evaluating {skip} caused an error, break here. */
16846 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016847 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 break;
16849 }
16850 if (r)
16851 continue;
16852 }
16853
16854 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16855 {
16856 /* Found end when searching backwards or start when searching
16857 * forward: nested pair. */
16858 ++nest;
16859 pat = pat2; /* nested, don't search for middle */
16860 }
16861 else
16862 {
16863 /* Found end when searching forward or start when searching
16864 * backward: end of (nested) pair; or found middle in outer pair. */
16865 if (--nest == 1)
16866 pat = pat3; /* outer level, search for middle */
16867 }
16868
16869 if (nest == 0)
16870 {
16871 /* Found the match: return matchcount or line number. */
16872 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016873 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016875 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016876 if (flags & SP_SETPCMARK)
16877 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 curwin->w_cursor = pos;
16879 if (!(flags & SP_REPEAT))
16880 break;
16881 nest = 1; /* search for next unmatched */
16882 }
16883 }
16884
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016885 if (match_pos != NULL)
16886 {
16887 /* Store the match cursor position */
16888 match_pos->lnum = curwin->w_cursor.lnum;
16889 match_pos->col = curwin->w_cursor.col + 1;
16890 }
16891
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016893 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 curwin->w_cursor = save_cursor;
16895
16896theend:
16897 vim_free(pat2);
16898 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016899 if (p_cpo == empty_option)
16900 p_cpo = save_cpo;
16901 else
16902 /* Darn, evaluating the {skip} expression changed the value. */
16903 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016904
16905 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906}
16907
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016908/*
16909 * "searchpos()" function
16910 */
16911 static void
16912f_searchpos(argvars, rettv)
16913 typval_T *argvars;
16914 typval_T *rettv;
16915{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016916 pos_T match_pos;
16917 int lnum = 0;
16918 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016919 int n;
16920 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016921
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016922 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016923 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016924
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016925 n = search_cmn(argvars, &match_pos, &flags);
16926 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016927 {
16928 lnum = match_pos.lnum;
16929 col = match_pos.col;
16930 }
16931
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016932 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16933 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016934 if (flags & SP_SUBPAT)
16935 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016936}
16937
16938
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 static void
16940f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016941 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944#ifdef FEAT_CLIENTSERVER
16945 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 char_u *server = get_tv_string_chk(&argvars[0]);
16947 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 if (server == NULL || reply == NULL)
16951 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 if (check_restricted() || check_secure())
16953 return;
16954# ifdef FEAT_X11
16955 if (check_connection() == FAIL)
16956 return;
16957# endif
16958
16959 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961 EMSG(_("E258: Unable to send to client"));
16962 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 rettv->vval.v_number = 0;
16965#else
16966 rettv->vval.v_number = -1;
16967#endif
16968}
16969
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 static void
16971f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016972 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974{
16975 char_u *r = NULL;
16976
16977#ifdef FEAT_CLIENTSERVER
16978# ifdef WIN32
16979 r = serverGetVimNames();
16980# else
16981 make_connection();
16982 if (X_DISPLAY != NULL)
16983 r = serverGetVimNames(X_DISPLAY);
16984# endif
16985#endif
16986 rettv->v_type = VAR_STRING;
16987 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988}
16989
16990/*
16991 * "setbufvar()" function
16992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016994f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016996 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997{
16998 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 char_u nbuf[NUMBUFLEN];
17003
17004 if (check_restricted() || check_secure())
17005 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17007 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017008 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 varp = &argvars[2];
17010
17011 if (buf != NULL && varname != NULL && varp != NULL)
17012 {
17013 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015
17016 if (*varname == '&')
17017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 long numval;
17019 char_u *strval;
17020 int error = FALSE;
17021
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017023 numval = get_tv_number_chk(varp, &error);
17024 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017025 if (!error && strval != NULL)
17026 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 }
17028 else
17029 {
17030 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17031 if (bufvarname != NULL)
17032 {
17033 STRCPY(bufvarname, "b:");
17034 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017035 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 vim_free(bufvarname);
17037 }
17038 }
17039
17040 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043}
17044
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017045 static void
17046f_setcharsearch(argvars, rettv)
17047 typval_T *argvars;
17048 typval_T *rettv UNUSED;
17049{
17050 dict_T *d;
17051 dictitem_T *di;
17052 char_u *csearch;
17053
17054 if (argvars[0].v_type != VAR_DICT)
17055 {
17056 EMSG(_(e_dictreq));
17057 return;
17058 }
17059
17060 if ((d = argvars[0].vval.v_dict) != NULL)
17061 {
17062 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17063 if (csearch != NULL)
17064 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017065#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017066 if (enc_utf8)
17067 {
17068 int pcc[MAX_MCO];
17069 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017070
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017071 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17072 }
17073 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017074#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017075 set_last_csearch(PTR2CHAR(csearch),
17076 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017077 }
17078
17079 di = dict_find(d, (char_u *)"forward", -1);
17080 if (di != NULL)
17081 set_csearch_direction(get_tv_number(&di->di_tv)
17082 ? FORWARD : BACKWARD);
17083
17084 di = dict_find(d, (char_u *)"until", -1);
17085 if (di != NULL)
17086 set_csearch_until(!!get_tv_number(&di->di_tv));
17087 }
17088}
17089
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090/*
17091 * "setcmdpos()" function
17092 */
17093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017094f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 typval_T *argvars;
17096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017098 int pos = (int)get_tv_number(&argvars[0]) - 1;
17099
17100 if (pos >= 0)
17101 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102}
17103
17104/*
17105 * "setline()" function
17106 */
17107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017108f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017109 typval_T *argvars;
17110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111{
17112 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017113 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017114 list_T *l = NULL;
17115 listitem_T *li = NULL;
17116 long added = 0;
17117 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017119 lnum = get_tv_lnum(&argvars[0]);
17120 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122 l = argvars[1].vval.v_list;
17123 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017125 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017127
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017128 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017129 for (;;)
17130 {
17131 if (l != NULL)
17132 {
17133 /* list argument, get next string */
17134 if (li == NULL)
17135 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 li = li->li_next;
17138 }
17139
17140 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017142 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017143
17144 /* When coming here from Insert mode, sync undo, so that this can be
17145 * undone separately from what was previously inserted. */
17146 if (u_sync_once == 2)
17147 {
17148 u_sync_once = 1; /* notify that u_sync() was called */
17149 u_sync(TRUE);
17150 }
17151
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017152 if (lnum <= curbuf->b_ml.ml_line_count)
17153 {
17154 /* existing line, replace it */
17155 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17156 {
17157 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017158 if (lnum == curwin->w_cursor.lnum)
17159 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017160 rettv->vval.v_number = 0; /* OK */
17161 }
17162 }
17163 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17164 {
17165 /* lnum is one past the last line, append the line */
17166 ++added;
17167 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17168 rettv->vval.v_number = 0; /* OK */
17169 }
17170
17171 if (l == NULL) /* only one string argument */
17172 break;
17173 ++lnum;
17174 }
17175
17176 if (added > 0)
17177 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178}
17179
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017180static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17181
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017183 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017184 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017185 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017186set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017187 win_T *wp UNUSED;
17188 typval_T *list_arg UNUSED;
17189 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017190 typval_T *rettv;
17191{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017192#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017193 char_u *act;
17194 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017195#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017196
Bram Moolenaar2641f772005-03-25 21:58:17 +000017197 rettv->vval.v_number = -1;
17198
17199#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017200 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017201 EMSG(_(e_listreq));
17202 else
17203 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017204 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017205
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017206 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017207 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017208 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 if (act == NULL)
17210 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017211 if (*act == 'a' || *act == 'r')
17212 action = *act;
17213 }
17214
Bram Moolenaar81484f42012-12-05 15:16:47 +010017215 if (l != NULL && set_errorlist(wp, l, action,
17216 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017217 rettv->vval.v_number = 0;
17218 }
17219#endif
17220}
17221
17222/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017223 * "setloclist()" function
17224 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017225 static void
17226f_setloclist(argvars, rettv)
17227 typval_T *argvars;
17228 typval_T *rettv;
17229{
17230 win_T *win;
17231
17232 rettv->vval.v_number = -1;
17233
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017234 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017235 if (win != NULL)
17236 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17237}
17238
17239/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017240 * "setmatches()" function
17241 */
17242 static void
17243f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017244 typval_T *argvars UNUSED;
17245 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017246{
17247#ifdef FEAT_SEARCH_EXTRA
17248 list_T *l;
17249 listitem_T *li;
17250 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017251 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017252
17253 rettv->vval.v_number = -1;
17254 if (argvars[0].v_type != VAR_LIST)
17255 {
17256 EMSG(_(e_listreq));
17257 return;
17258 }
17259 if ((l = argvars[0].vval.v_list) != NULL)
17260 {
17261
17262 /* To some extent make sure that we are dealing with a list from
17263 * "getmatches()". */
17264 li = l->lv_first;
17265 while (li != NULL)
17266 {
17267 if (li->li_tv.v_type != VAR_DICT
17268 || (d = li->li_tv.vval.v_dict) == NULL)
17269 {
17270 EMSG(_(e_invarg));
17271 return;
17272 }
17273 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017274 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17275 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017276 && dict_find(d, (char_u *)"priority", -1) != NULL
17277 && dict_find(d, (char_u *)"id", -1) != NULL))
17278 {
17279 EMSG(_(e_invarg));
17280 return;
17281 }
17282 li = li->li_next;
17283 }
17284
17285 clear_matches(curwin);
17286 li = l->lv_first;
17287 while (li != NULL)
17288 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017289 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017290 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017291 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017292 char_u *group;
17293 int priority;
17294 int id;
17295 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017296
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017297 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017298 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17299 {
17300 if (s == NULL)
17301 {
17302 s = list_alloc();
17303 if (s == NULL)
17304 return;
17305 }
17306
17307 /* match from matchaddpos() */
17308 for (i = 1; i < 9; i++)
17309 {
17310 sprintf((char *)buf, (char *)"pos%d", i);
17311 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17312 {
17313 if (di->di_tv.v_type != VAR_LIST)
17314 return;
17315
17316 list_append_tv(s, &di->di_tv);
17317 s->lv_refcount++;
17318 }
17319 else
17320 break;
17321 }
17322 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017323
17324 group = get_dict_string(d, (char_u *)"group", FALSE);
17325 priority = (int)get_dict_number(d, (char_u *)"priority");
17326 id = (int)get_dict_number(d, (char_u *)"id");
17327 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17328 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17329 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017330 if (i == 0)
17331 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017332 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017333 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017334 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017335 }
17336 else
17337 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017338 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017339 list_unref(s);
17340 s = NULL;
17341 }
17342
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017343 li = li->li_next;
17344 }
17345 rettv->vval.v_number = 0;
17346 }
17347#endif
17348}
17349
17350/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017351 * "setpos()" function
17352 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017353 static void
17354f_setpos(argvars, rettv)
17355 typval_T *argvars;
17356 typval_T *rettv;
17357{
17358 pos_T pos;
17359 int fnum;
17360 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017361 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017362
Bram Moolenaar08250432008-02-13 11:42:46 +000017363 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017364 name = get_tv_string_chk(argvars);
17365 if (name != NULL)
17366 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017367 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017368 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017369 if (--pos.col < 0)
17370 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017371 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017372 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017373 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017374 if (fnum == curbuf->b_fnum)
17375 {
17376 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017377 if (curswant >= 0)
17378 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017379 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017380 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017381 }
17382 else
17383 EMSG(_(e_invarg));
17384 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017385 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17386 {
17387 /* set mark */
17388 if (setmark_pos(name[1], &pos, fnum) == OK)
17389 rettv->vval.v_number = 0;
17390 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017391 else
17392 EMSG(_(e_invarg));
17393 }
17394 }
17395}
17396
17397/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017398 * "setqflist()" function
17399 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017400 static void
17401f_setqflist(argvars, rettv)
17402 typval_T *argvars;
17403 typval_T *rettv;
17404{
17405 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17406}
17407
17408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 * "setreg()" function
17410 */
17411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017413 typval_T *argvars;
17414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415{
17416 int regname;
17417 char_u *strregname;
17418 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 int append;
17421 char_u yank_type;
17422 long block_len;
17423
17424 block_len = -1;
17425 yank_type = MAUTO;
17426 append = FALSE;
17427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017428 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017429 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017431 if (strregname == NULL)
17432 return; /* type error; errmsg already given */
17433 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 if (regname == 0 || regname == '@')
17435 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017437 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017439 stropt = get_tv_string_chk(&argvars[2]);
17440 if (stropt == NULL)
17441 return; /* type error */
17442 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 switch (*stropt)
17444 {
17445 case 'a': case 'A': /* append */
17446 append = TRUE;
17447 break;
17448 case 'v': case 'c': /* character-wise selection */
17449 yank_type = MCHAR;
17450 break;
17451 case 'V': case 'l': /* line-wise selection */
17452 yank_type = MLINE;
17453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 case 'b': case Ctrl_V: /* block-wise selection */
17455 yank_type = MBLOCK;
17456 if (VIM_ISDIGIT(stropt[1]))
17457 {
17458 ++stropt;
17459 block_len = getdigits(&stropt) - 1;
17460 --stropt;
17461 }
17462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 }
17464 }
17465
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017466 if (argvars[1].v_type == VAR_LIST)
17467 {
17468 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017469 char_u **allocval;
17470 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017471 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017472 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017473 int len = argvars[1].vval.v_list->lv_len;
17474 listitem_T *li;
17475
Bram Moolenaar7d647822014-04-05 21:28:56 +020017476 /* First half: use for pointers to result lines; second half: use for
17477 * pointers to allocated copies. */
17478 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017479 if (lstval == NULL)
17480 return;
17481 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017482 allocval = lstval + len + 2;
17483 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017484
17485 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17486 li = li->li_next)
17487 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017488 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017489 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017490 goto free_lstval;
17491 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017492 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017493 /* Need to make a copy, next get_tv_string_buf_chk() will
17494 * overwrite the string. */
17495 strval = vim_strsave(buf);
17496 if (strval == NULL)
17497 goto free_lstval;
17498 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017499 }
17500 *curval++ = strval;
17501 }
17502 *curval++ = NULL;
17503
17504 write_reg_contents_lst(regname, lstval, -1,
17505 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017506free_lstval:
17507 while (curallocval > allocval)
17508 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017509 vim_free(lstval);
17510 }
17511 else
17512 {
17513 strval = get_tv_string_chk(&argvars[1]);
17514 if (strval == NULL)
17515 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520}
17521
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017522/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017523 * "settabvar()" function
17524 */
17525 static void
17526f_settabvar(argvars, rettv)
17527 typval_T *argvars;
17528 typval_T *rettv;
17529{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017530#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017532 tabpage_T *tp;
17533#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017534 char_u *varname, *tabvarname;
17535 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017536
17537 rettv->vval.v_number = 0;
17538
17539 if (check_restricted() || check_secure())
17540 return;
17541
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017542#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017543 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017544#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017545 varname = get_tv_string_chk(&argvars[1]);
17546 varp = &argvars[2];
17547
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017548 if (varname != NULL && varp != NULL
17549#ifdef FEAT_WINDOWS
17550 && tp != NULL
17551#endif
17552 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017553 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017554#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017555 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017556 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017557#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017558
17559 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17560 if (tabvarname != NULL)
17561 {
17562 STRCPY(tabvarname, "t:");
17563 STRCPY(tabvarname + 2, varname);
17564 set_var(tabvarname, varp, TRUE);
17565 vim_free(tabvarname);
17566 }
17567
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017568#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017569 /* Restore current tabpage */
17570 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017571 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017572#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017573 }
17574}
17575
17576/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017577 * "settabwinvar()" function
17578 */
17579 static void
17580f_settabwinvar(argvars, rettv)
17581 typval_T *argvars;
17582 typval_T *rettv;
17583{
17584 setwinvar(argvars, rettv, 1);
17585}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586
17587/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017588 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017591f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 typval_T *argvars;
17593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017595 setwinvar(argvars, rettv, 0);
17596}
17597
17598/*
17599 * "setwinvar()" and "settabwinvar()" functions
17600 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017601
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017602 static void
17603setwinvar(argvars, rettv, off)
17604 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017605 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017606 int off;
17607{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 win_T *win;
17609#ifdef FEAT_WINDOWS
17610 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017611 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017612 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613#endif
17614 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017615 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017617 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
17619 if (check_restricted() || check_secure())
17620 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017621
17622#ifdef FEAT_WINDOWS
17623 if (off == 1)
17624 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17625 else
17626 tp = curtab;
17627#endif
17628 win = find_win_by_nr(&argvars[off], tp);
17629 varname = get_tv_string_chk(&argvars[off + 1]);
17630 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
17632 if (win != NULL && varname != NULL && varp != NULL)
17633 {
17634#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017635 need_switch_win = !(tp == curtab && win == curwin);
17636 if (!need_switch_win
17637 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017640 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017642 long numval;
17643 char_u *strval;
17644 int error = FALSE;
17645
17646 ++varname;
17647 numval = get_tv_number_chk(varp, &error);
17648 strval = get_tv_string_buf_chk(varp, nbuf);
17649 if (!error && strval != NULL)
17650 set_option_value(varname, numval, strval, OPT_LOCAL);
17651 }
17652 else
17653 {
17654 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17655 if (winvarname != NULL)
17656 {
17657 STRCPY(winvarname, "w:");
17658 STRCPY(winvarname + 2, varname);
17659 set_var(winvarname, varp, TRUE);
17660 vim_free(winvarname);
17661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 }
17663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017665 if (need_switch_win)
17666 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667#endif
17668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669}
17670
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017671#ifdef FEAT_CRYPT
17672/*
17673 * "sha256({string})" function
17674 */
17675 static void
17676f_sha256(argvars, rettv)
17677 typval_T *argvars;
17678 typval_T *rettv;
17679{
17680 char_u *p;
17681
17682 p = get_tv_string(&argvars[0]);
17683 rettv->vval.v_string = vim_strsave(
17684 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17685 rettv->v_type = VAR_STRING;
17686}
17687#endif /* FEAT_CRYPT */
17688
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017690 * "shellescape({string})" function
17691 */
17692 static void
17693f_shellescape(argvars, rettv)
17694 typval_T *argvars;
17695 typval_T *rettv;
17696{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017697 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017698 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017699 rettv->v_type = VAR_STRING;
17700}
17701
17702/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017703 * shiftwidth() function
17704 */
17705 static void
17706f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017707 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017708 typval_T *rettv;
17709{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017710 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017711}
17712
17713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017714 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 */
17716 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017717f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 typval_T *argvars;
17719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017721 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722
Bram Moolenaar0d660222005-01-07 21:51:51 +000017723 p = get_tv_string(&argvars[0]);
17724 rettv->vval.v_string = vim_strsave(p);
17725 simplify_filename(rettv->vval.v_string); /* simplify in place */
17726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727}
17728
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017729#ifdef FEAT_FLOAT
17730/*
17731 * "sin()" function
17732 */
17733 static void
17734f_sin(argvars, rettv)
17735 typval_T *argvars;
17736 typval_T *rettv;
17737{
17738 float_T f;
17739
17740 rettv->v_type = VAR_FLOAT;
17741 if (get_float_arg(argvars, &f) == OK)
17742 rettv->vval.v_float = sin(f);
17743 else
17744 rettv->vval.v_float = 0.0;
17745}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017746
17747/*
17748 * "sinh()" function
17749 */
17750 static void
17751f_sinh(argvars, rettv)
17752 typval_T *argvars;
17753 typval_T *rettv;
17754{
17755 float_T f;
17756
17757 rettv->v_type = VAR_FLOAT;
17758 if (get_float_arg(argvars, &f) == OK)
17759 rettv->vval.v_float = sinh(f);
17760 else
17761 rettv->vval.v_float = 0.0;
17762}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017763#endif
17764
Bram Moolenaar0d660222005-01-07 21:51:51 +000017765static int
17766#ifdef __BORLANDC__
17767 _RTLENTRYF
17768#endif
17769 item_compare __ARGS((const void *s1, const void *s2));
17770static int
17771#ifdef __BORLANDC__
17772 _RTLENTRYF
17773#endif
17774 item_compare2 __ARGS((const void *s1, const void *s2));
17775
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017776/* struct used in the array that's given to qsort() */
17777typedef struct
17778{
17779 listitem_T *item;
17780 int idx;
17781} sortItem_T;
17782
Bram Moolenaar0d660222005-01-07 21:51:51 +000017783static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017784static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017785static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017786static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017788static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017789static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790#define ITEM_COMPARE_FAIL 999
17791
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017793 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017795 static int
17796#ifdef __BORLANDC__
17797_RTLENTRYF
17798#endif
17799item_compare(s1, s2)
17800 const void *s1;
17801 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017803 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017804 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017805 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017806 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017807 int res;
17808 char_u numbuf1[NUMBUFLEN];
17809 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017811 si1 = (sortItem_T *)s1;
17812 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017813 tv1 = &si1->item->li_tv;
17814 tv2 = &si2->item->li_tv;
17815 /* tv2string() puts quotes around a string and allocates memory. Don't do
17816 * that for string variables. Use a single quote when comparing with a
17817 * non-string to do what the docs promise. */
17818 if (tv1->v_type == VAR_STRING)
17819 {
17820 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17821 p1 = (char_u *)"'";
17822 else
17823 p1 = tv1->vval.v_string;
17824 }
17825 else
17826 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17827 if (tv2->v_type == VAR_STRING)
17828 {
17829 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17830 p2 = (char_u *)"'";
17831 else
17832 p2 = tv2->vval.v_string;
17833 }
17834 else
17835 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017836 if (p1 == NULL)
17837 p1 = (char_u *)"";
17838 if (p2 == NULL)
17839 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017840 if (!item_compare_numeric)
17841 {
17842 if (item_compare_ic)
17843 res = STRICMP(p1, p2);
17844 else
17845 res = STRCMP(p1, p2);
17846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017848 {
17849 double n1, n2;
17850 n1 = strtod((char *)p1, (char **)&p1);
17851 n2 = strtod((char *)p2, (char **)&p2);
17852 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17853 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017854
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017855 /* When the result would be zero, compare the item indexes. Makes the
17856 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017857 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017858 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017859
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860 vim_free(tofree1);
17861 vim_free(tofree2);
17862 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863}
17864
17865 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866#ifdef __BORLANDC__
17867_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017869item_compare2(s1, s2)
17870 const void *s1;
17871 const void *s2;
17872{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017873 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017876 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017879 /* shortcut after failure in previous call; compare all items equal */
17880 if (item_compare_func_err)
17881 return 0;
17882
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017883 si1 = (sortItem_T *)s1;
17884 si2 = (sortItem_T *)s2;
17885
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017886 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017887 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017888 copy_tv(&si1->item->li_tv, &argv[0]);
17889 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017890
17891 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017892 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017893 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17894 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 clear_tv(&argv[0]);
17896 clear_tv(&argv[1]);
17897
17898 if (res == FAIL)
17899 res = ITEM_COMPARE_FAIL;
17900 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017901 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17902 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017903 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017904 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017905
17906 /* When the result would be zero, compare the pointers themselves. Makes
17907 * the sort stable. */
17908 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017909 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017910
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911 return res;
17912}
17913
17914/*
17915 * "sort({list})" function
17916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017918do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 typval_T *argvars;
17920 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017921 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922{
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 list_T *l;
17924 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017925 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 long len;
17927 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017930 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 else
17932 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017933 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017934 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017935 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17936 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017937 return;
17938 rettv->vval.v_list = l;
17939 rettv->v_type = VAR_LIST;
17940 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942 len = list_len(l);
17943 if (len <= 1)
17944 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945
Bram Moolenaar0d660222005-01-07 21:51:51 +000017946 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017947 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017949 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017950 if (argvars[1].v_type != VAR_UNKNOWN)
17951 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017952 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017954 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017955 else
17956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017957 int error = FALSE;
17958
17959 i = get_tv_number_chk(&argvars[1], &error);
17960 if (error)
17961 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017962 if (i == 1)
17963 item_compare_ic = TRUE;
17964 else
17965 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017966 if (item_compare_func != NULL)
17967 {
17968 if (STRCMP(item_compare_func, "n") == 0)
17969 {
17970 item_compare_func = NULL;
17971 item_compare_numeric = TRUE;
17972 }
17973 else if (STRCMP(item_compare_func, "i") == 0)
17974 {
17975 item_compare_func = NULL;
17976 item_compare_ic = TRUE;
17977 }
17978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017979 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017980
17981 if (argvars[2].v_type != VAR_UNKNOWN)
17982 {
17983 /* optional third argument: {dict} */
17984 if (argvars[2].v_type != VAR_DICT)
17985 {
17986 EMSG(_(e_dictreq));
17987 return;
17988 }
17989 item_compare_selfdict = argvars[2].vval.v_dict;
17990 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992
Bram Moolenaar0d660222005-01-07 21:51:51 +000017993 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017994 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995 if (ptrs == NULL)
17996 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997
Bram Moolenaar327aa022014-03-25 18:24:23 +010017998 i = 0;
17999 if (sort)
18000 {
18001 /* sort(): ptrs will be the list to sort */
18002 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018003 {
18004 ptrs[i].item = li;
18005 ptrs[i].idx = i;
18006 ++i;
18007 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018008
18009 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018010 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018011 /* test the compare function */
18012 if (item_compare_func != NULL
18013 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018015 EMSG(_("E702: Sort compare function failed"));
18016 else
18017 {
18018 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018019 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018020 item_compare_func == NULL ? item_compare : item_compare2);
18021
18022 if (!item_compare_func_err)
18023 {
18024 /* Clear the List and append the items in sorted order. */
18025 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18026 l->lv_len = 0;
18027 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018028 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018029 }
18030 }
18031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018034 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18035
18036 /* f_uniq(): ptrs will be a stack of items to remove */
18037 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018038 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018039 item_compare_func_ptr = item_compare_func
18040 ? item_compare2 : item_compare;
18041
18042 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18043 li = li->li_next)
18044 {
18045 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18046 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018047 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018048 if (item_compare_func_err)
18049 {
18050 EMSG(_("E882: Uniq compare function failed"));
18051 break;
18052 }
18053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018055 if (!item_compare_func_err)
18056 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018057 while (--i >= 0)
18058 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018059 li = ptrs[i].item->li_next;
18060 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018061 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018062 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018063 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018064 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018065 list_fix_watch(l, li);
18066 listitem_free(li);
18067 l->lv_len--;
18068 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018069 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018070 }
18071
18072 vim_free(ptrs);
18073 }
18074}
18075
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018076/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018077 * "sort({list})" function
18078 */
18079 static void
18080f_sort(argvars, rettv)
18081 typval_T *argvars;
18082 typval_T *rettv;
18083{
18084 do_sort_uniq(argvars, rettv, TRUE);
18085}
18086
18087/*
18088 * "uniq({list})" function
18089 */
18090 static void
18091f_uniq(argvars, rettv)
18092 typval_T *argvars;
18093 typval_T *rettv;
18094{
18095 do_sort_uniq(argvars, rettv, FALSE);
18096}
18097
18098/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018099 * "soundfold({word})" function
18100 */
18101 static void
18102f_soundfold(argvars, rettv)
18103 typval_T *argvars;
18104 typval_T *rettv;
18105{
18106 char_u *s;
18107
18108 rettv->v_type = VAR_STRING;
18109 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018110#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018111 rettv->vval.v_string = eval_soundfold(s);
18112#else
18113 rettv->vval.v_string = vim_strsave(s);
18114#endif
18115}
18116
18117/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018118 * "spellbadword()" function
18119 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018120 static void
18121f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018122 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018123 typval_T *rettv;
18124{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018125 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018126 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018127 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018128
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018129 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018130 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018131
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018132#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018133 if (argvars[0].v_type == VAR_UNKNOWN)
18134 {
18135 /* Find the start and length of the badly spelled word. */
18136 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18137 if (len != 0)
18138 word = ml_get_cursor();
18139 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018140 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018141 {
18142 char_u *str = get_tv_string_chk(&argvars[0]);
18143 int capcol = -1;
18144
18145 if (str != NULL)
18146 {
18147 /* Check the argument for spelling. */
18148 while (*str != NUL)
18149 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018150 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018151 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018152 {
18153 word = str;
18154 break;
18155 }
18156 str += len;
18157 }
18158 }
18159 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018160#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018161
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018162 list_append_string(rettv->vval.v_list, word, len);
18163 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018164 attr == HLF_SPB ? "bad" :
18165 attr == HLF_SPR ? "rare" :
18166 attr == HLF_SPL ? "local" :
18167 attr == HLF_SPC ? "caps" :
18168 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018169}
18170
18171/*
18172 * "spellsuggest()" function
18173 */
18174 static void
18175f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018176 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018177 typval_T *rettv;
18178{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018179#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018180 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018181 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018182 int maxcount;
18183 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018184 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018185 listitem_T *li;
18186 int need_capital = FALSE;
18187#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018189 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018190 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018191
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018192#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018193 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018194 {
18195 str = get_tv_string(&argvars[0]);
18196 if (argvars[1].v_type != VAR_UNKNOWN)
18197 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018198 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018199 if (maxcount <= 0)
18200 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018201 if (argvars[2].v_type != VAR_UNKNOWN)
18202 {
18203 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18204 if (typeerr)
18205 return;
18206 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018207 }
18208 else
18209 maxcount = 25;
18210
Bram Moolenaar4770d092006-01-12 23:22:24 +000018211 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018212
18213 for (i = 0; i < ga.ga_len; ++i)
18214 {
18215 str = ((char_u **)ga.ga_data)[i];
18216
18217 li = listitem_alloc();
18218 if (li == NULL)
18219 vim_free(str);
18220 else
18221 {
18222 li->li_tv.v_type = VAR_STRING;
18223 li->li_tv.v_lock = 0;
18224 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018225 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018226 }
18227 }
18228 ga_clear(&ga);
18229 }
18230#endif
18231}
18232
Bram Moolenaar0d660222005-01-07 21:51:51 +000018233 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018234f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018235 typval_T *argvars;
18236 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237{
18238 char_u *str;
18239 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018240 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018241 regmatch_T regmatch;
18242 char_u patbuf[NUMBUFLEN];
18243 char_u *save_cpo;
18244 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018246 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018247 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248
18249 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18250 save_cpo = p_cpo;
18251 p_cpo = (char_u *)"";
18252
18253 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018254 if (argvars[1].v_type != VAR_UNKNOWN)
18255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018256 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18257 if (pat == NULL)
18258 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018259 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018260 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018261 }
18262 if (pat == NULL || *pat == NUL)
18263 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018265 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018267 if (typeerr)
18268 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18271 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018274 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018275 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018276 if (*str == NUL)
18277 match = FALSE; /* empty item at the end */
18278 else
18279 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280 if (match)
18281 end = regmatch.startp[0];
18282 else
18283 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018284 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18285 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018286 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018287 if (list_append_string(rettv->vval.v_list, str,
18288 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 }
18291 if (!match)
18292 break;
18293 /* Advance to just after the match. */
18294 if (regmatch.endp[0] > str)
18295 col = 0;
18296 else
18297 {
18298 /* Don't get stuck at the same match. */
18299#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018300 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018301#else
18302 col = 1;
18303#endif
18304 }
18305 str = regmatch.endp[0];
18306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307
Bram Moolenaar473de612013-06-08 18:19:48 +020018308 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312}
18313
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018314#ifdef FEAT_FLOAT
18315/*
18316 * "sqrt()" function
18317 */
18318 static void
18319f_sqrt(argvars, rettv)
18320 typval_T *argvars;
18321 typval_T *rettv;
18322{
18323 float_T f;
18324
18325 rettv->v_type = VAR_FLOAT;
18326 if (get_float_arg(argvars, &f) == OK)
18327 rettv->vval.v_float = sqrt(f);
18328 else
18329 rettv->vval.v_float = 0.0;
18330}
18331
18332/*
18333 * "str2float()" function
18334 */
18335 static void
18336f_str2float(argvars, rettv)
18337 typval_T *argvars;
18338 typval_T *rettv;
18339{
18340 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18341
18342 if (*p == '+')
18343 p = skipwhite(p + 1);
18344 (void)string2float(p, &rettv->vval.v_float);
18345 rettv->v_type = VAR_FLOAT;
18346}
18347#endif
18348
Bram Moolenaar2c932302006-03-18 21:42:09 +000018349/*
18350 * "str2nr()" function
18351 */
18352 static void
18353f_str2nr(argvars, rettv)
18354 typval_T *argvars;
18355 typval_T *rettv;
18356{
18357 int base = 10;
18358 char_u *p;
18359 long n;
18360
18361 if (argvars[1].v_type != VAR_UNKNOWN)
18362 {
18363 base = get_tv_number(&argvars[1]);
18364 if (base != 8 && base != 10 && base != 16)
18365 {
18366 EMSG(_(e_invarg));
18367 return;
18368 }
18369 }
18370
18371 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018372 if (*p == '+')
18373 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018374 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018375 rettv->vval.v_number = n;
18376}
18377
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378#ifdef HAVE_STRFTIME
18379/*
18380 * "strftime({format}[, {time}])" function
18381 */
18382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018384 typval_T *argvars;
18385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
18387 char_u result_buf[256];
18388 struct tm *curtime;
18389 time_t seconds;
18390 char_u *p;
18391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018395 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 seconds = time(NULL);
18397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018398 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 curtime = localtime(&seconds);
18400 /* MSVC returns NULL for an invalid value of seconds. */
18401 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 else
18404 {
18405# ifdef FEAT_MBYTE
18406 vimconv_T conv;
18407 char_u *enc;
18408
18409 conv.vc_type = CONV_NONE;
18410 enc = enc_locale();
18411 convert_setup(&conv, p_enc, enc);
18412 if (conv.vc_type != CONV_NONE)
18413 p = string_convert(&conv, p, NULL);
18414# endif
18415 if (p != NULL)
18416 (void)strftime((char *)result_buf, sizeof(result_buf),
18417 (char *)p, curtime);
18418 else
18419 result_buf[0] = NUL;
18420
18421# ifdef FEAT_MBYTE
18422 if (conv.vc_type != CONV_NONE)
18423 vim_free(p);
18424 convert_setup(&conv, enc, p_enc);
18425 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 else
18428# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430
18431# ifdef FEAT_MBYTE
18432 /* Release conversion descriptors */
18433 convert_setup(&conv, NULL, NULL);
18434 vim_free(enc);
18435# endif
18436 }
18437}
18438#endif
18439
18440/*
18441 * "stridx()" function
18442 */
18443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018444f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018445 typval_T *argvars;
18446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447{
18448 char_u buf[NUMBUFLEN];
18449 char_u *needle;
18450 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018453 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018455 needle = get_tv_string_chk(&argvars[1]);
18456 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018458 if (needle == NULL || haystack == NULL)
18459 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460
Bram Moolenaar33570922005-01-25 22:26:29 +000018461 if (argvars[2].v_type != VAR_UNKNOWN)
18462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018463 int error = FALSE;
18464
18465 start_idx = get_tv_number_chk(&argvars[2], &error);
18466 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018467 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018468 if (start_idx >= 0)
18469 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018470 }
18471
18472 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18473 if (pos != NULL)
18474 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475}
18476
18477/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018478 * "string()" function
18479 */
18480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018482 typval_T *argvars;
18483 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018484{
18485 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018486 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018488 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018489 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018490 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018491 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018492 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493}
18494
18495/*
18496 * "strlen()" function
18497 */
18498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018499f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018500 typval_T *argvars;
18501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018503 rettv->vval.v_number = (varnumber_T)(STRLEN(
18504 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505}
18506
18507/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018508 * "strchars()" function
18509 */
18510 static void
18511f_strchars(argvars, rettv)
18512 typval_T *argvars;
18513 typval_T *rettv;
18514{
18515 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018516 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018517#ifdef FEAT_MBYTE
18518 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018519 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018520#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018521
18522 if (argvars[1].v_type != VAR_UNKNOWN)
18523 skipcc = get_tv_number_chk(&argvars[1], NULL);
18524 if (skipcc < 0 || skipcc > 1)
18525 EMSG(_(e_invarg));
18526 else
18527 {
18528#ifdef FEAT_MBYTE
18529 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18530 while (*s != NUL)
18531 {
18532 func_mb_ptr2char_adv(&s);
18533 ++len;
18534 }
18535 rettv->vval.v_number = len;
18536#else
18537 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18538#endif
18539 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018540}
18541
18542/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018543 * "strdisplaywidth()" function
18544 */
18545 static void
18546f_strdisplaywidth(argvars, rettv)
18547 typval_T *argvars;
18548 typval_T *rettv;
18549{
18550 char_u *s = get_tv_string(&argvars[0]);
18551 int col = 0;
18552
18553 if (argvars[1].v_type != VAR_UNKNOWN)
18554 col = get_tv_number(&argvars[1]);
18555
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018556 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018557}
18558
18559/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018560 * "strwidth()" function
18561 */
18562 static void
18563f_strwidth(argvars, rettv)
18564 typval_T *argvars;
18565 typval_T *rettv;
18566{
18567 char_u *s = get_tv_string(&argvars[0]);
18568
18569 rettv->vval.v_number = (varnumber_T)(
18570#ifdef FEAT_MBYTE
18571 mb_string2cells(s, -1)
18572#else
18573 STRLEN(s)
18574#endif
18575 );
18576}
18577
18578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 * "strpart()" function
18580 */
18581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 typval_T *argvars;
18584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585{
18586 char_u *p;
18587 int n;
18588 int len;
18589 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018590 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 slen = (int)STRLEN(p);
18594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018595 n = get_tv_number_chk(&argvars[1], &error);
18596 if (error)
18597 len = 0;
18598 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 else
18601 len = slen - n; /* default len: all bytes that are available. */
18602
18603 /*
18604 * Only return the overlap between the specified part and the actual
18605 * string.
18606 */
18607 if (n < 0)
18608 {
18609 len += n;
18610 n = 0;
18611 }
18612 else if (n > slen)
18613 n = slen;
18614 if (len < 0)
18615 len = 0;
18616 else if (n + len > slen)
18617 len = slen - n;
18618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018619 rettv->v_type = VAR_STRING;
18620 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621}
18622
18623/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018624 * "strridx()" function
18625 */
18626 static void
18627f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 typval_T *argvars;
18629 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018630{
18631 char_u buf[NUMBUFLEN];
18632 char_u *needle;
18633 char_u *haystack;
18634 char_u *rest;
18635 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018636 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018638 needle = get_tv_string_chk(&argvars[1]);
18639 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640
18641 rettv->vval.v_number = -1;
18642 if (needle == NULL || haystack == NULL)
18643 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018644
18645 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018646 if (argvars[2].v_type != VAR_UNKNOWN)
18647 {
18648 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018650 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018651 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018652 }
18653 else
18654 end_idx = haystack_len;
18655
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018657 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018659 lastmatch = haystack + end_idx;
18660 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018662 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663 for (rest = haystack; *rest != '\0'; ++rest)
18664 {
18665 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018666 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018667 break;
18668 lastmatch = rest;
18669 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018670 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018671
18672 if (lastmatch == NULL)
18673 rettv->vval.v_number = -1;
18674 else
18675 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18676}
18677
18678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 * "strtrans()" function
18680 */
18681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 typval_T *argvars;
18684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018686 rettv->v_type = VAR_STRING;
18687 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688}
18689
18690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691 * "submatch()" function
18692 */
18693 static void
18694f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018695 typval_T *argvars;
18696 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018697{
Bram Moolenaar41571762014-04-02 19:00:58 +020018698 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018699 int no;
18700 int retList = 0;
18701
18702 no = (int)get_tv_number_chk(&argvars[0], &error);
18703 if (error)
18704 return;
18705 error = FALSE;
18706 if (argvars[1].v_type != VAR_UNKNOWN)
18707 retList = get_tv_number_chk(&argvars[1], &error);
18708 if (error)
18709 return;
18710
18711 if (retList == 0)
18712 {
18713 rettv->v_type = VAR_STRING;
18714 rettv->vval.v_string = reg_submatch(no);
18715 }
18716 else
18717 {
18718 rettv->v_type = VAR_LIST;
18719 rettv->vval.v_list = reg_submatch_list(no);
18720 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018721}
18722
18723/*
18724 * "substitute()" function
18725 */
18726 static void
18727f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018728 typval_T *argvars;
18729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018730{
18731 char_u patbuf[NUMBUFLEN];
18732 char_u subbuf[NUMBUFLEN];
18733 char_u flagsbuf[NUMBUFLEN];
18734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018735 char_u *str = get_tv_string_chk(&argvars[0]);
18736 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18737 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18738 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18739
Bram Moolenaar0d660222005-01-07 21:51:51 +000018740 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018741 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18742 rettv->vval.v_string = NULL;
18743 else
18744 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018745}
18746
18747/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018748 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754{
18755 int id = 0;
18756#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018757 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 long col;
18759 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018760 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018762 lnum = get_tv_lnum(argvars); /* -1 on type error */
18763 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18764 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018766 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018767 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018768 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769#endif
18770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772}
18773
18774/*
18775 * "synIDattr(id, what [, mode])" function
18776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018779 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781{
18782 char_u *p = NULL;
18783#ifdef FEAT_SYN_HL
18784 int id;
18785 char_u *what;
18786 char_u *mode;
18787 char_u modebuf[NUMBUFLEN];
18788 int modec;
18789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 id = get_tv_number(&argvars[0]);
18791 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018792 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018794 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018796 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 modec = 0; /* replace invalid with current */
18798 }
18799 else
18800 {
18801#ifdef FEAT_GUI
18802 if (gui.in_use)
18803 modec = 'g';
18804 else
18805#endif
18806 if (t_colors > 1)
18807 modec = 'c';
18808 else
18809 modec = 't';
18810 }
18811
18812
18813 switch (TOLOWER_ASC(what[0]))
18814 {
18815 case 'b':
18816 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18817 p = highlight_color(id, what, modec);
18818 else /* bold */
18819 p = highlight_has_attr(id, HL_BOLD, modec);
18820 break;
18821
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018822 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 p = highlight_color(id, what, modec);
18824 break;
18825
18826 case 'i':
18827 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18828 p = highlight_has_attr(id, HL_INVERSE, modec);
18829 else /* italic */
18830 p = highlight_has_attr(id, HL_ITALIC, modec);
18831 break;
18832
18833 case 'n': /* name */
18834 p = get_highlight_name(NULL, id - 1);
18835 break;
18836
18837 case 'r': /* reverse */
18838 p = highlight_has_attr(id, HL_INVERSE, modec);
18839 break;
18840
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018841 case 's':
18842 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18843 p = highlight_color(id, what, modec);
18844 else /* standout */
18845 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 break;
18847
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018848 case 'u':
18849 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18850 /* underline */
18851 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18852 else
18853 /* undercurl */
18854 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 break;
18856 }
18857
18858 if (p != NULL)
18859 p = vim_strsave(p);
18860#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018861 rettv->v_type = VAR_STRING;
18862 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863}
18864
18865/*
18866 * "synIDtrans(id)" function
18867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018869f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018870 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872{
18873 int id;
18874
18875#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018876 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877
18878 if (id > 0)
18879 id = syn_get_final_id(id);
18880 else
18881#endif
18882 id = 0;
18883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018884 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885}
18886
18887/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018888 * "synconcealed(lnum, col)" function
18889 */
18890 static void
18891f_synconcealed(argvars, rettv)
18892 typval_T *argvars UNUSED;
18893 typval_T *rettv;
18894{
18895#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18896 long lnum;
18897 long col;
18898 int syntax_flags = 0;
18899 int cchar;
18900 int matchid = 0;
18901 char_u str[NUMBUFLEN];
18902#endif
18903
18904 rettv->v_type = VAR_LIST;
18905 rettv->vval.v_list = NULL;
18906
18907#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18908 lnum = get_tv_lnum(argvars); /* -1 on type error */
18909 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18910
18911 vim_memset(str, NUL, sizeof(str));
18912
18913 if (rettv_list_alloc(rettv) != FAIL)
18914 {
18915 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18916 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18917 && curwin->w_p_cole > 0)
18918 {
18919 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18920 syntax_flags = get_syntax_info(&matchid);
18921
18922 /* get the conceal character */
18923 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18924 {
18925 cchar = syn_get_sub_char();
18926 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18927 cchar = lcs_conceal;
18928 if (cchar != NUL)
18929 {
18930# ifdef FEAT_MBYTE
18931 if (has_mbyte)
18932 (*mb_char2bytes)(cchar, str);
18933 else
18934# endif
18935 str[0] = cchar;
18936 }
18937 }
18938 }
18939
18940 list_append_number(rettv->vval.v_list,
18941 (syntax_flags & HL_CONCEAL) != 0);
18942 /* -1 to auto-determine strlen */
18943 list_append_string(rettv->vval.v_list, str, -1);
18944 list_append_number(rettv->vval.v_list, matchid);
18945 }
18946#endif
18947}
18948
18949/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018950 * "synstack(lnum, col)" function
18951 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018952 static void
18953f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018954 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018955 typval_T *rettv;
18956{
18957#ifdef FEAT_SYN_HL
18958 long lnum;
18959 long col;
18960 int i;
18961 int id;
18962#endif
18963
18964 rettv->v_type = VAR_LIST;
18965 rettv->vval.v_list = NULL;
18966
18967#ifdef FEAT_SYN_HL
18968 lnum = get_tv_lnum(argvars); /* -1 on type error */
18969 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18970
18971 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018972 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018973 && rettv_list_alloc(rettv) != FAIL)
18974 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018975 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018976 for (i = 0; ; ++i)
18977 {
18978 id = syn_get_stack_item(i);
18979 if (id < 0)
18980 break;
18981 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18982 break;
18983 }
18984 }
18985#endif
18986}
18987
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018989get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018990 typval_T *argvars;
18991 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018992 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018994 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018996 char_u *infile = NULL;
18997 char_u buf[NUMBUFLEN];
18998 int err = FALSE;
18999 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019000 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019001 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019003 rettv->v_type = VAR_STRING;
19004 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019005 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019006 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019007
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019008 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019009 {
19010 /*
19011 * Write the string to a temp file, to be used for input of the shell
19012 * command.
19013 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019014 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019015 {
19016 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019017 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019018 }
19019
19020 fd = mch_fopen((char *)infile, WRITEBIN);
19021 if (fd == NULL)
19022 {
19023 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019024 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019025 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019026 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019027 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019028 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19029 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019030 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019031 else
19032 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019033 size_t len;
19034
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019035 p = get_tv_string_buf_chk(&argvars[1], buf);
19036 if (p == NULL)
19037 {
19038 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019039 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019040 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019041 len = STRLEN(p);
19042 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019043 err = TRUE;
19044 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019045 if (fclose(fd) != 0)
19046 err = TRUE;
19047 if (err)
19048 {
19049 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019050 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019051 }
19052 }
19053
Bram Moolenaar52a72462014-08-29 15:53:52 +020019054 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19055 * echoes typeahead, that messes up the display. */
19056 if (!msg_silent)
19057 flags += SHELL_COOKED;
19058
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019061 int len;
19062 listitem_T *li;
19063 char_u *s = NULL;
19064 char_u *start;
19065 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019066 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar52a72462014-08-29 15:53:52 +020019068 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019069 if (res == NULL)
19070 goto errret;
19071
19072 list = list_alloc();
19073 if (list == NULL)
19074 goto errret;
19075
19076 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019078 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019079 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019080 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019081 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019082
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019083 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019084 if (s == NULL)
19085 goto errret;
19086
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019087 for (p = s; start < end; ++p, ++start)
19088 *p = *start == NUL ? NL : *start;
19089 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019090
19091 li = listitem_alloc();
19092 if (li == NULL)
19093 {
19094 vim_free(s);
19095 goto errret;
19096 }
19097 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019098 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019099 li->li_tv.vval.v_string = s;
19100 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019102
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019103 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019104 rettv->v_type = VAR_LIST;
19105 rettv->vval.v_list = list;
19106 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019108 else
19109 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019110 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019111#ifdef USE_CR
19112 /* translate <CR> into <NL> */
19113 if (res != NULL)
19114 {
19115 char_u *s;
19116
19117 for (s = res; *s; ++s)
19118 {
19119 if (*s == CAR)
19120 *s = NL;
19121 }
19122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123#else
19124# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019125 /* translate <CR><NL> into <NL> */
19126 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019128 char_u *s, *d;
19129
19130 d = res;
19131 for (s = res; *s; ++s)
19132 {
19133 if (s[0] == CAR && s[1] == NL)
19134 ++s;
19135 *d++ = *s;
19136 }
19137 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139# endif
19140#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019141 rettv->vval.v_string = res;
19142 res = NULL;
19143 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019144
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019145errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019146 if (infile != NULL)
19147 {
19148 mch_remove(infile);
19149 vim_free(infile);
19150 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019151 if (res != NULL)
19152 vim_free(res);
19153 if (list != NULL)
19154 list_free(list, TRUE);
19155}
19156
19157/*
19158 * "system()" function
19159 */
19160 static void
19161f_system(argvars, rettv)
19162 typval_T *argvars;
19163 typval_T *rettv;
19164{
19165 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19166}
19167
19168/*
19169 * "systemlist()" function
19170 */
19171 static void
19172f_systemlist(argvars, rettv)
19173 typval_T *argvars;
19174 typval_T *rettv;
19175{
19176 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177}
19178
19179/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019180 * "tabpagebuflist()" function
19181 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019182 static void
19183f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019184 typval_T *argvars UNUSED;
19185 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019186{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019187#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019188 tabpage_T *tp;
19189 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019190
19191 if (argvars[0].v_type == VAR_UNKNOWN)
19192 wp = firstwin;
19193 else
19194 {
19195 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19196 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019197 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019198 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019199 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019200 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019201 for (; wp != NULL; wp = wp->w_next)
19202 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019203 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019204 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019205 }
19206#endif
19207}
19208
19209
19210/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019211 * "tabpagenr()" function
19212 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019213 static void
19214f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019215 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019216 typval_T *rettv;
19217{
19218 int nr = 1;
19219#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019220 char_u *arg;
19221
19222 if (argvars[0].v_type != VAR_UNKNOWN)
19223 {
19224 arg = get_tv_string_chk(&argvars[0]);
19225 nr = 0;
19226 if (arg != NULL)
19227 {
19228 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019229 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019230 else
19231 EMSG2(_(e_invexpr2), arg);
19232 }
19233 }
19234 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019235 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019236#endif
19237 rettv->vval.v_number = nr;
19238}
19239
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019240
19241#ifdef FEAT_WINDOWS
19242static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19243
19244/*
19245 * Common code for tabpagewinnr() and winnr().
19246 */
19247 static int
19248get_winnr(tp, argvar)
19249 tabpage_T *tp;
19250 typval_T *argvar;
19251{
19252 win_T *twin;
19253 int nr = 1;
19254 win_T *wp;
19255 char_u *arg;
19256
19257 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19258 if (argvar->v_type != VAR_UNKNOWN)
19259 {
19260 arg = get_tv_string_chk(argvar);
19261 if (arg == NULL)
19262 nr = 0; /* type error; errmsg already given */
19263 else if (STRCMP(arg, "$") == 0)
19264 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19265 else if (STRCMP(arg, "#") == 0)
19266 {
19267 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19268 if (twin == NULL)
19269 nr = 0;
19270 }
19271 else
19272 {
19273 EMSG2(_(e_invexpr2), arg);
19274 nr = 0;
19275 }
19276 }
19277
19278 if (nr > 0)
19279 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19280 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019281 {
19282 if (wp == NULL)
19283 {
19284 /* didn't find it in this tabpage */
19285 nr = 0;
19286 break;
19287 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019288 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019289 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019290 return nr;
19291}
19292#endif
19293
19294/*
19295 * "tabpagewinnr()" function
19296 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019297 static void
19298f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019299 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019300 typval_T *rettv;
19301{
19302 int nr = 1;
19303#ifdef FEAT_WINDOWS
19304 tabpage_T *tp;
19305
19306 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19307 if (tp == NULL)
19308 nr = 0;
19309 else
19310 nr = get_winnr(tp, &argvars[1]);
19311#endif
19312 rettv->vval.v_number = nr;
19313}
19314
19315
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019316/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019317 * "tagfiles()" function
19318 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019319 static void
19320f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019321 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019322 typval_T *rettv;
19323{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019324 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019325 tagname_T tn;
19326 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019327
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019328 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019329 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019330 fname = alloc(MAXPATHL);
19331 if (fname == NULL)
19332 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019333
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019334 for (first = TRUE; ; first = FALSE)
19335 if (get_tagfname(&tn, first, fname) == FAIL
19336 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019337 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019338 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019339 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019340}
19341
19342/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019343 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019344 */
19345 static void
19346f_taglist(argvars, rettv)
19347 typval_T *argvars;
19348 typval_T *rettv;
19349{
19350 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019351
19352 tag_pattern = get_tv_string(&argvars[0]);
19353
19354 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019355 if (*tag_pattern == NUL)
19356 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019357
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019358 if (rettv_list_alloc(rettv) == OK)
19359 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019360}
19361
19362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 * "tempname()" function
19364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369{
19370 static int x = 'A';
19371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019372 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019373 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
19375 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19376 * names. Skip 'I' and 'O', they are used for shell redirection. */
19377 do
19378 {
19379 if (x == 'Z')
19380 x = '0';
19381 else if (x == '9')
19382 x = 'A';
19383 else
19384 {
19385#ifdef EBCDIC
19386 if (x == 'I')
19387 x = 'J';
19388 else if (x == 'R')
19389 x = 'S';
19390 else
19391#endif
19392 ++x;
19393 }
19394 } while (x == 'I' || x == 'O');
19395}
19396
19397/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019398 * "test(list)" function: Just checking the walls...
19399 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019400 static void
19401f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019402 typval_T *argvars UNUSED;
19403 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019404{
19405 /* Used for unit testing. Change the code below to your liking. */
19406#if 0
19407 listitem_T *li;
19408 list_T *l;
19409 char_u *bad, *good;
19410
19411 if (argvars[0].v_type != VAR_LIST)
19412 return;
19413 l = argvars[0].vval.v_list;
19414 if (l == NULL)
19415 return;
19416 li = l->lv_first;
19417 if (li == NULL)
19418 return;
19419 bad = get_tv_string(&li->li_tv);
19420 li = li->li_next;
19421 if (li == NULL)
19422 return;
19423 good = get_tv_string(&li->li_tv);
19424 rettv->vval.v_number = test_edit_score(bad, good);
19425#endif
19426}
19427
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019428#ifdef FEAT_FLOAT
19429/*
19430 * "tan()" function
19431 */
19432 static void
19433f_tan(argvars, rettv)
19434 typval_T *argvars;
19435 typval_T *rettv;
19436{
19437 float_T f;
19438
19439 rettv->v_type = VAR_FLOAT;
19440 if (get_float_arg(argvars, &f) == OK)
19441 rettv->vval.v_float = tan(f);
19442 else
19443 rettv->vval.v_float = 0.0;
19444}
19445
19446/*
19447 * "tanh()" function
19448 */
19449 static void
19450f_tanh(argvars, rettv)
19451 typval_T *argvars;
19452 typval_T *rettv;
19453{
19454 float_T f;
19455
19456 rettv->v_type = VAR_FLOAT;
19457 if (get_float_arg(argvars, &f) == OK)
19458 rettv->vval.v_float = tanh(f);
19459 else
19460 rettv->vval.v_float = 0.0;
19461}
19462#endif
19463
Bram Moolenaard52d9742005-08-21 22:20:28 +000019464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 * "tolower(string)" function
19466 */
19467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019468f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019469 typval_T *argvars;
19470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471{
19472 char_u *p;
19473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019474 p = vim_strsave(get_tv_string(&argvars[0]));
19475 rettv->v_type = VAR_STRING;
19476 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477
19478 if (p != NULL)
19479 while (*p != NUL)
19480 {
19481#ifdef FEAT_MBYTE
19482 int l;
19483
19484 if (enc_utf8)
19485 {
19486 int c, lc;
19487
19488 c = utf_ptr2char(p);
19489 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019490 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 /* TODO: reallocate string when byte count changes. */
19492 if (utf_char2len(lc) == l)
19493 utf_char2bytes(lc, p);
19494 p += l;
19495 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019496 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 p += l; /* skip multi-byte character */
19498 else
19499#endif
19500 {
19501 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19502 ++p;
19503 }
19504 }
19505}
19506
19507/*
19508 * "toupper(string)" function
19509 */
19510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 typval_T *argvars;
19513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019516 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517}
19518
19519/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019520 * "tr(string, fromstr, tostr)" function
19521 */
19522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 typval_T *argvars;
19525 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019526{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019527 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019528 char_u *fromstr;
19529 char_u *tostr;
19530 char_u *p;
19531#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019532 int inlen;
19533 int fromlen;
19534 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019535 int idx;
19536 char_u *cpstr;
19537 int cplen;
19538 int first = TRUE;
19539#endif
19540 char_u buf[NUMBUFLEN];
19541 char_u buf2[NUMBUFLEN];
19542 garray_T ga;
19543
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019544 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019545 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19546 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019547
19548 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019549 rettv->v_type = VAR_STRING;
19550 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019551 if (fromstr == NULL || tostr == NULL)
19552 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019553 ga_init2(&ga, (int)sizeof(char), 80);
19554
19555#ifdef FEAT_MBYTE
19556 if (!has_mbyte)
19557#endif
19558 /* not multi-byte: fromstr and tostr must be the same length */
19559 if (STRLEN(fromstr) != STRLEN(tostr))
19560 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019561#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019562error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019563#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019564 EMSG2(_(e_invarg2), fromstr);
19565 ga_clear(&ga);
19566 return;
19567 }
19568
19569 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019570 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019571 {
19572#ifdef FEAT_MBYTE
19573 if (has_mbyte)
19574 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019575 inlen = (*mb_ptr2len)(in_str);
19576 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019577 cplen = inlen;
19578 idx = 0;
19579 for (p = fromstr; *p != NUL; p += fromlen)
19580 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019581 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019582 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019583 {
19584 for (p = tostr; *p != NUL; p += tolen)
19585 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019586 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019587 if (idx-- == 0)
19588 {
19589 cplen = tolen;
19590 cpstr = p;
19591 break;
19592 }
19593 }
19594 if (*p == NUL) /* tostr is shorter than fromstr */
19595 goto error;
19596 break;
19597 }
19598 ++idx;
19599 }
19600
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019601 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019602 {
19603 /* Check that fromstr and tostr have the same number of
19604 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019605 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019606 first = FALSE;
19607 for (p = tostr; *p != NUL; p += tolen)
19608 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019609 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019610 --idx;
19611 }
19612 if (idx != 0)
19613 goto error;
19614 }
19615
Bram Moolenaarcde88542015-08-11 19:14:00 +020019616 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019617 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019618 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019619
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019620 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019621 }
19622 else
19623#endif
19624 {
19625 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019626 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019627 if (p != NULL)
19628 ga_append(&ga, tostr[p - fromstr]);
19629 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019630 ga_append(&ga, *in_str);
19631 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019632 }
19633 }
19634
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019635 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019636 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019637 ga_append(&ga, NUL);
19638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019639 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019640}
19641
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019642#ifdef FEAT_FLOAT
19643/*
19644 * "trunc({float})" function
19645 */
19646 static void
19647f_trunc(argvars, rettv)
19648 typval_T *argvars;
19649 typval_T *rettv;
19650{
19651 float_T f;
19652
19653 rettv->v_type = VAR_FLOAT;
19654 if (get_float_arg(argvars, &f) == OK)
19655 /* trunc() is not in C90, use floor() or ceil() instead. */
19656 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19657 else
19658 rettv->vval.v_float = 0.0;
19659}
19660#endif
19661
Bram Moolenaar8299df92004-07-10 09:47:34 +000019662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 * "type(expr)" function
19664 */
19665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 typval_T *argvars;
19668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019670 int n;
19671
19672 switch (argvars[0].v_type)
19673 {
19674 case VAR_NUMBER: n = 0; break;
19675 case VAR_STRING: n = 1; break;
19676 case VAR_FUNC: n = 2; break;
19677 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019678 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019679#ifdef FEAT_FLOAT
19680 case VAR_FLOAT: n = 5; break;
19681#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019682 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19683 }
19684 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685}
19686
19687/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019688 * "undofile(name)" function
19689 */
19690 static void
19691f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019692 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019693 typval_T *rettv;
19694{
19695 rettv->v_type = VAR_STRING;
19696#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019697 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019698 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019699
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019700 if (*fname == NUL)
19701 {
19702 /* If there is no file name there will be no undo file. */
19703 rettv->vval.v_string = NULL;
19704 }
19705 else
19706 {
19707 char_u *ffname = FullName_save(fname, FALSE);
19708
19709 if (ffname != NULL)
19710 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19711 vim_free(ffname);
19712 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019713 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019714#else
19715 rettv->vval.v_string = NULL;
19716#endif
19717}
19718
19719/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019720 * "undotree()" function
19721 */
19722 static void
19723f_undotree(argvars, rettv)
19724 typval_T *argvars UNUSED;
19725 typval_T *rettv;
19726{
19727 if (rettv_dict_alloc(rettv) == OK)
19728 {
19729 dict_T *dict = rettv->vval.v_dict;
19730 list_T *list;
19731
Bram Moolenaar730cde92010-06-27 05:18:54 +020019732 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019733 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019734 dict_add_nr_str(dict, "save_last",
19735 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019736 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19737 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019738 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019739
19740 list = list_alloc();
19741 if (list != NULL)
19742 {
19743 u_eval_tree(curbuf->b_u_oldhead, list);
19744 dict_add_list(dict, "entries", list);
19745 }
19746 }
19747}
19748
19749/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019750 * "values(dict)" function
19751 */
19752 static void
19753f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019754 typval_T *argvars;
19755 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019756{
19757 dict_list(argvars, rettv, 1);
19758}
19759
19760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 * "virtcol(string)" function
19762 */
19763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 typval_T *argvars;
19766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
19768 colnr_T vcol = 0;
19769 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019770 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019772 fp = var2fpos(&argvars[0], FALSE, &fnum);
19773 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19774 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 {
19776 getvvcol(curwin, fp, NULL, NULL, &vcol);
19777 ++vcol;
19778 }
19779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781}
19782
19783/*
19784 * "visualmode()" function
19785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019787f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019788 typval_T *argvars UNUSED;
19789 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 char_u str[2];
19792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 str[0] = curbuf->b_visual_mode_eval;
19795 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797
19798 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019799 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801}
19802
19803/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019804 * "wildmenumode()" function
19805 */
19806 static void
19807f_wildmenumode(argvars, rettv)
19808 typval_T *argvars UNUSED;
19809 typval_T *rettv UNUSED;
19810{
19811#ifdef FEAT_WILDMENU
19812 if (wild_menu_showing)
19813 rettv->vval.v_number = 1;
19814#endif
19815}
19816
19817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 * "winbufnr(nr)" function
19819 */
19820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 typval_T *argvars;
19823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824{
19825 win_T *wp;
19826
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019827 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832}
19833
19834/*
19835 * "wincol()" function
19836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841{
19842 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844}
19845
19846/*
19847 * "winheight(nr)" function
19848 */
19849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019850f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019851 typval_T *argvars;
19852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853{
19854 win_T *wp;
19855
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019856 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019860 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861}
19862
19863/*
19864 * "winline()" function
19865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019867f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019868 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870{
19871 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019872 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873}
19874
19875/*
19876 * "winnr()" function
19877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019879f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019880 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882{
19883 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019884
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019886 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019888 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889}
19890
19891/*
19892 * "winrestcmd()" function
19893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898{
19899#ifdef FEAT_WINDOWS
19900 win_T *wp;
19901 int winnr = 1;
19902 garray_T ga;
19903 char_u buf[50];
19904
19905 ga_init2(&ga, (int)sizeof(char), 70);
19906 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19907 {
19908 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19909 ga_concat(&ga, buf);
19910# ifdef FEAT_VERTSPLIT
19911 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19912 ga_concat(&ga, buf);
19913# endif
19914 ++winnr;
19915 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019916 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019918 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019922 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923}
19924
19925/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019926 * "winrestview()" function
19927 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019928 static void
19929f_winrestview(argvars, rettv)
19930 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019931 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019932{
19933 dict_T *dict;
19934
19935 if (argvars[0].v_type != VAR_DICT
19936 || (dict = argvars[0].vval.v_dict) == NULL)
19937 EMSG(_(e_invarg));
19938 else
19939 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019940 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19941 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19942 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19943 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019944#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019945 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19946 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019947#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019948 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19949 {
19950 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19951 curwin->w_set_curswant = FALSE;
19952 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019953
Bram Moolenaar82c25852014-05-28 16:47:16 +020019954 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19955 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019956#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019957 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19958 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019959#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019960 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19961 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19962 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19963 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019964
19965 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019966 win_new_height(curwin, curwin->w_height);
19967# ifdef FEAT_VERTSPLIT
19968 win_new_width(curwin, W_WIDTH(curwin));
19969# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019970 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019971
Bram Moolenaarb851a962014-10-31 15:45:52 +010019972 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019973 curwin->w_topline = 1;
19974 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19975 curwin->w_topline = curbuf->b_ml.ml_line_count;
19976#ifdef FEAT_DIFF
19977 check_topfill(curwin, TRUE);
19978#endif
19979 }
19980}
19981
19982/*
19983 * "winsaveview()" function
19984 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019985 static void
19986f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019987 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019988 typval_T *rettv;
19989{
19990 dict_T *dict;
19991
Bram Moolenaara800b422010-06-27 01:15:55 +020019992 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019993 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019994 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019995
19996 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19997 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19998#ifdef FEAT_VIRTUALEDIT
19999 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20000#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020001 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020002 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20003
20004 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20005#ifdef FEAT_DIFF
20006 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20007#endif
20008 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20009 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20010}
20011
20012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 * "winwidth(nr)" function
20014 */
20015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020017 typval_T *argvars;
20018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019{
20020 win_T *wp;
20021
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020022 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 else
20026#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020027 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030#endif
20031}
20032
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020034 * Write list of strings to file
20035 */
20036 static int
20037write_list(fd, list, binary)
20038 FILE *fd;
20039 list_T *list;
20040 int binary;
20041{
20042 listitem_T *li;
20043 int c;
20044 int ret = OK;
20045 char_u *s;
20046
20047 for (li = list->lv_first; li != NULL; li = li->li_next)
20048 {
20049 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20050 {
20051 if (*s == '\n')
20052 c = putc(NUL, fd);
20053 else
20054 c = putc(*s, fd);
20055 if (c == EOF)
20056 {
20057 ret = FAIL;
20058 break;
20059 }
20060 }
20061 if (!binary || li->li_next != NULL)
20062 if (putc('\n', fd) == EOF)
20063 {
20064 ret = FAIL;
20065 break;
20066 }
20067 if (ret == FAIL)
20068 {
20069 EMSG(_(e_write));
20070 break;
20071 }
20072 }
20073 return ret;
20074}
20075
20076/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020077 * "writefile()" function
20078 */
20079 static void
20080f_writefile(argvars, rettv)
20081 typval_T *argvars;
20082 typval_T *rettv;
20083{
20084 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020085 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020086 char_u *fname;
20087 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020088 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020089
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020090 if (check_restricted() || check_secure())
20091 return;
20092
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020093 if (argvars[0].v_type != VAR_LIST)
20094 {
20095 EMSG2(_(e_listarg), "writefile()");
20096 return;
20097 }
20098 if (argvars[0].vval.v_list == NULL)
20099 return;
20100
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020101 if (argvars[2].v_type != VAR_UNKNOWN)
20102 {
20103 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20104 binary = TRUE;
20105 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20106 append = TRUE;
20107 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020108
20109 /* Always open the file in binary mode, library functions have a mind of
20110 * their own about CR-LF conversion. */
20111 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020112 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20113 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020114 {
20115 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20116 ret = -1;
20117 }
20118 else
20119 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020120 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20121 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020122 fclose(fd);
20123 }
20124
20125 rettv->vval.v_number = ret;
20126}
20127
20128/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020129 * "xor(expr, expr)" function
20130 */
20131 static void
20132f_xor(argvars, rettv)
20133 typval_T *argvars;
20134 typval_T *rettv;
20135{
20136 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20137 ^ get_tv_number_chk(&argvars[1], NULL);
20138}
20139
20140
20141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020143 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 */
20145 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020146var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020148 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020149 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020151 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020153 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154
Bram Moolenaara5525202006-03-02 22:52:09 +000020155 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020156 if (varp->v_type == VAR_LIST)
20157 {
20158 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020159 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020160 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020161 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020162
20163 l = varp->vval.v_list;
20164 if (l == NULL)
20165 return NULL;
20166
20167 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020168 pos.lnum = list_find_nr(l, 0L, &error);
20169 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020170 return NULL; /* invalid line number */
20171
20172 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020173 pos.col = list_find_nr(l, 1L, &error);
20174 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020175 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020176 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020177
20178 /* We accept "$" for the column number: last column. */
20179 li = list_find(l, 1L);
20180 if (li != NULL && li->li_tv.v_type == VAR_STRING
20181 && li->li_tv.vval.v_string != NULL
20182 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20183 pos.col = len + 1;
20184
Bram Moolenaara5525202006-03-02 22:52:09 +000020185 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020186 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020187 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020188 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020189
Bram Moolenaara5525202006-03-02 22:52:09 +000020190#ifdef FEAT_VIRTUALEDIT
20191 /* Get the virtual offset. Defaults to zero. */
20192 pos.coladd = list_find_nr(l, 2L, &error);
20193 if (error)
20194 pos.coladd = 0;
20195#endif
20196
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020197 return &pos;
20198 }
20199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020200 name = get_tv_string_chk(varp);
20201 if (name == NULL)
20202 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020203 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020205 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20206 {
20207 if (VIsual_active)
20208 return &VIsual;
20209 return &curwin->w_cursor;
20210 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020211 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020213 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20215 return NULL;
20216 return pp;
20217 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020218
20219#ifdef FEAT_VIRTUALEDIT
20220 pos.coladd = 0;
20221#endif
20222
Bram Moolenaar477933c2007-07-17 14:32:23 +000020223 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020224 {
20225 pos.col = 0;
20226 if (name[1] == '0') /* "w0": first visible line */
20227 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020228 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020229 pos.lnum = curwin->w_topline;
20230 return &pos;
20231 }
20232 else if (name[1] == '$') /* "w$": last visible line */
20233 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020234 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020235 pos.lnum = curwin->w_botline - 1;
20236 return &pos;
20237 }
20238 }
20239 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020241 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 {
20243 pos.lnum = curbuf->b_ml.ml_line_count;
20244 pos.col = 0;
20245 }
20246 else
20247 {
20248 pos.lnum = curwin->w_cursor.lnum;
20249 pos.col = (colnr_T)STRLEN(ml_get_curline());
20250 }
20251 return &pos;
20252 }
20253 return NULL;
20254}
20255
20256/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020257 * Convert list in "arg" into a position and optional file number.
20258 * When "fnump" is NULL there is no file number, only 3 items.
20259 * Note that the column is passed on as-is, the caller may want to decrement
20260 * it to use 1 for the first column.
20261 * Return FAIL when conversion is not possible, doesn't check the position for
20262 * validity.
20263 */
20264 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020265list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020266 typval_T *arg;
20267 pos_T *posp;
20268 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020269 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020270{
20271 list_T *l = arg->vval.v_list;
20272 long i = 0;
20273 long n;
20274
Bram Moolenaar493c1782014-05-28 14:34:46 +020020275 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20276 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020277 if (arg->v_type != VAR_LIST
20278 || l == NULL
20279 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020280 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020281 return FAIL;
20282
20283 if (fnump != NULL)
20284 {
20285 n = list_find_nr(l, i++, NULL); /* fnum */
20286 if (n < 0)
20287 return FAIL;
20288 if (n == 0)
20289 n = curbuf->b_fnum; /* current buffer */
20290 *fnump = n;
20291 }
20292
20293 n = list_find_nr(l, i++, NULL); /* lnum */
20294 if (n < 0)
20295 return FAIL;
20296 posp->lnum = n;
20297
20298 n = list_find_nr(l, i++, NULL); /* col */
20299 if (n < 0)
20300 return FAIL;
20301 posp->col = n;
20302
20303#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020304 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020305 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020306 posp->coladd = 0;
20307 else
20308 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020309#endif
20310
Bram Moolenaar493c1782014-05-28 14:34:46 +020020311 if (curswantp != NULL)
20312 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20313
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020314 return OK;
20315}
20316
20317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 * Get the length of an environment variable name.
20319 * Advance "arg" to the first character after the name.
20320 * Return 0 for error.
20321 */
20322 static int
20323get_env_len(arg)
20324 char_u **arg;
20325{
20326 char_u *p;
20327 int len;
20328
20329 for (p = *arg; vim_isIDc(*p); ++p)
20330 ;
20331 if (p == *arg) /* no name found */
20332 return 0;
20333
20334 len = (int)(p - *arg);
20335 *arg = p;
20336 return len;
20337}
20338
20339/*
20340 * Get the length of the name of a function or internal variable.
20341 * "arg" is advanced to the first non-white character after the name.
20342 * Return 0 if something is wrong.
20343 */
20344 static int
20345get_id_len(arg)
20346 char_u **arg;
20347{
20348 char_u *p;
20349 int len;
20350
20351 /* Find the end of the name. */
20352 for (p = *arg; eval_isnamec(*p); ++p)
20353 ;
20354 if (p == *arg) /* no name found */
20355 return 0;
20356
20357 len = (int)(p - *arg);
20358 *arg = skipwhite(p);
20359
20360 return len;
20361}
20362
20363/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020364 * Get the length of the name of a variable or function.
20365 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020367 * Return -1 if curly braces expansion failed.
20368 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 * If the name contains 'magic' {}'s, expand them and return the
20370 * expanded name in an allocated string via 'alias' - caller must free.
20371 */
20372 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020373get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 char_u **arg;
20375 char_u **alias;
20376 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020377 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378{
20379 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 char_u *p;
20381 char_u *expr_start;
20382 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383
20384 *alias = NULL; /* default to no alias */
20385
20386 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20387 && (*arg)[2] == (int)KE_SNR)
20388 {
20389 /* hard coded <SNR>, already translated */
20390 *arg += 3;
20391 return get_id_len(arg) + 3;
20392 }
20393 len = eval_fname_script(*arg);
20394 if (len > 0)
20395 {
20396 /* literal "<SID>", "s:" or "<SNR>" */
20397 *arg += len;
20398 }
20399
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020403 p = find_name_end(*arg, &expr_start, &expr_end,
20404 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 if (expr_start != NULL)
20406 {
20407 char_u *temp_string;
20408
20409 if (!evaluate)
20410 {
20411 len += (int)(p - *arg);
20412 *arg = skipwhite(p);
20413 return len;
20414 }
20415
20416 /*
20417 * Include any <SID> etc in the expanded string:
20418 * Thus the -len here.
20419 */
20420 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20421 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020422 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 *alias = temp_string;
20424 *arg = skipwhite(p);
20425 return (int)STRLEN(temp_string);
20426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427
20428 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020429 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 EMSG2(_(e_invexpr2), *arg);
20431
20432 return len;
20433}
20434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020435/*
20436 * Find the end of a variable or function name, taking care of magic braces.
20437 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20438 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020439 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020440 * Return a pointer to just after the name. Equal to "arg" if there is no
20441 * valid name.
20442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020444find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 char_u *arg;
20446 char_u **expr_start;
20447 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020448 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 int mb_nest = 0;
20451 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 char_u *p;
20453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020456 *expr_start = NULL;
20457 *expr_end = NULL;
20458 }
20459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020460 /* Quick check for valid starting character. */
20461 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20462 return arg;
20463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020464 for (p = arg; *p != NUL
20465 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020466 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020467 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020468 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020469 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020470 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020471 if (*p == '\'')
20472 {
20473 /* skip over 'string' to avoid counting [ and ] inside it. */
20474 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20475 ;
20476 if (*p == NUL)
20477 break;
20478 }
20479 else if (*p == '"')
20480 {
20481 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20482 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20483 if (*p == '\\' && p[1] != NUL)
20484 ++p;
20485 if (*p == NUL)
20486 break;
20487 }
20488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020489 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 if (*p == '[')
20492 ++br_nest;
20493 else if (*p == ']')
20494 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020497 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020499 if (*p == '{')
20500 {
20501 mb_nest++;
20502 if (expr_start != NULL && *expr_start == NULL)
20503 *expr_start = p;
20504 }
20505 else if (*p == '}')
20506 {
20507 mb_nest--;
20508 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20509 *expr_end = p;
20510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 }
20513
20514 return p;
20515}
20516
20517/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020518 * Expands out the 'magic' {}'s in a variable/function name.
20519 * Note that this can call itself recursively, to deal with
20520 * constructs like foo{bar}{baz}{bam}
20521 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20522 * "in_start" ^
20523 * "expr_start" ^
20524 * "expr_end" ^
20525 * "in_end" ^
20526 *
20527 * Returns a new allocated string, which the caller must free.
20528 * Returns NULL for failure.
20529 */
20530 static char_u *
20531make_expanded_name(in_start, expr_start, expr_end, in_end)
20532 char_u *in_start;
20533 char_u *expr_start;
20534 char_u *expr_end;
20535 char_u *in_end;
20536{
20537 char_u c1;
20538 char_u *retval = NULL;
20539 char_u *temp_result;
20540 char_u *nextcmd = NULL;
20541
20542 if (expr_end == NULL || in_end == NULL)
20543 return NULL;
20544 *expr_start = NUL;
20545 *expr_end = NUL;
20546 c1 = *in_end;
20547 *in_end = NUL;
20548
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020549 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020550 if (temp_result != NULL && nextcmd == NULL)
20551 {
20552 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20553 + (in_end - expr_end) + 1));
20554 if (retval != NULL)
20555 {
20556 STRCPY(retval, in_start);
20557 STRCAT(retval, temp_result);
20558 STRCAT(retval, expr_end + 1);
20559 }
20560 }
20561 vim_free(temp_result);
20562
20563 *in_end = c1; /* put char back for error messages */
20564 *expr_start = '{';
20565 *expr_end = '}';
20566
20567 if (retval != NULL)
20568 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020569 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020570 if (expr_start != NULL)
20571 {
20572 /* Further expansion! */
20573 temp_result = make_expanded_name(retval, expr_start,
20574 expr_end, temp_result);
20575 vim_free(retval);
20576 retval = temp_result;
20577 }
20578 }
20579
20580 return retval;
20581}
20582
20583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020585 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 */
20587 static int
20588eval_isnamec(c)
20589 int c;
20590{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020591 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20592}
20593
20594/*
20595 * Return TRUE if character "c" can be used as the first character in a
20596 * variable or function name (excluding '{' and '}').
20597 */
20598 static int
20599eval_isnamec1(c)
20600 int c;
20601{
20602 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603}
20604
20605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 * Set number v: variable to "val".
20607 */
20608 void
20609set_vim_var_nr(idx, val)
20610 int idx;
20611 long val;
20612{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020613 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614}
20615
20616/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020617 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 */
20619 long
20620get_vim_var_nr(idx)
20621 int idx;
20622{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020623 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624}
20625
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020626/*
20627 * Get string v: variable value. Uses a static buffer, can only be used once.
20628 */
20629 char_u *
20630get_vim_var_str(idx)
20631 int idx;
20632{
20633 return get_tv_string(&vimvars[idx].vv_tv);
20634}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020635
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020637 * Get List v: variable value. Caller must take care of reference count when
20638 * needed.
20639 */
20640 list_T *
20641get_vim_var_list(idx)
20642 int idx;
20643{
20644 return vimvars[idx].vv_list;
20645}
20646
20647/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020648 * Set v:char to character "c".
20649 */
20650 void
20651set_vim_var_char(c)
20652 int c;
20653{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020654 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020655
20656#ifdef FEAT_MBYTE
20657 if (has_mbyte)
20658 buf[(*mb_char2bytes)(c, buf)] = NUL;
20659 else
20660#endif
20661 {
20662 buf[0] = c;
20663 buf[1] = NUL;
20664 }
20665 set_vim_var_string(VV_CHAR, buf, -1);
20666}
20667
20668/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020669 * Set v:count to "count" and v:count1 to "count1".
20670 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 */
20672 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020673set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 long count;
20675 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020676 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020678 if (set_prevcount)
20679 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020680 vimvars[VV_COUNT].vv_nr = count;
20681 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682}
20683
20684/*
20685 * Set string v: variable to a copy of "val".
20686 */
20687 void
20688set_vim_var_string(idx, val, len)
20689 int idx;
20690 char_u *val;
20691 int len; /* length of "val" to use or -1 (whole string) */
20692{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020693 /* Need to do this (at least) once, since we can't initialize a union.
20694 * Will always be invoked when "v:progname" is set. */
20695 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20696
Bram Moolenaare9a41262005-01-15 22:18:47 +000020697 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020699 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020701 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020703 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704}
20705
20706/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020707 * Set List v: variable to "val".
20708 */
20709 void
20710set_vim_var_list(idx, val)
20711 int idx;
20712 list_T *val;
20713{
20714 list_unref(vimvars[idx].vv_list);
20715 vimvars[idx].vv_list = val;
20716 if (val != NULL)
20717 ++val->lv_refcount;
20718}
20719
20720/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020721 * Set Dictionary v: variable to "val".
20722 */
20723 void
20724set_vim_var_dict(idx, val)
20725 int idx;
20726 dict_T *val;
20727{
20728 int todo;
20729 hashitem_T *hi;
20730
20731 dict_unref(vimvars[idx].vv_dict);
20732 vimvars[idx].vv_dict = val;
20733 if (val != NULL)
20734 {
20735 ++val->dv_refcount;
20736
20737 /* Set readonly */
20738 todo = (int)val->dv_hashtab.ht_used;
20739 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20740 {
20741 if (HASHITEM_EMPTY(hi))
20742 continue;
20743 --todo;
20744 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20745 }
20746 }
20747}
20748
20749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 * Set v:register if needed.
20751 */
20752 void
20753set_reg_var(c)
20754 int c;
20755{
20756 char_u regname;
20757
20758 if (c == 0 || c == ' ')
20759 regname = '"';
20760 else
20761 regname = c;
20762 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020763 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 set_vim_var_string(VV_REG, &regname, 1);
20765}
20766
20767/*
20768 * Get or set v:exception. If "oldval" == NULL, return the current value.
20769 * Otherwise, restore the value to "oldval" and return NULL.
20770 * Must always be called in pairs to save and restore v:exception! Does not
20771 * take care of memory allocations.
20772 */
20773 char_u *
20774v_exception(oldval)
20775 char_u *oldval;
20776{
20777 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020778 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779
Bram Moolenaare9a41262005-01-15 22:18:47 +000020780 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 return NULL;
20782}
20783
20784/*
20785 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20786 * Otherwise, restore the value to "oldval" and return NULL.
20787 * Must always be called in pairs to save and restore v:throwpoint! Does not
20788 * take care of memory allocations.
20789 */
20790 char_u *
20791v_throwpoint(oldval)
20792 char_u *oldval;
20793{
20794 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020795 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796
Bram Moolenaare9a41262005-01-15 22:18:47 +000020797 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 return NULL;
20799}
20800
20801#if defined(FEAT_AUTOCMD) || defined(PROTO)
20802/*
20803 * Set v:cmdarg.
20804 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20805 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20806 * Must always be called in pairs!
20807 */
20808 char_u *
20809set_cmdarg(eap, oldarg)
20810 exarg_T *eap;
20811 char_u *oldarg;
20812{
20813 char_u *oldval;
20814 char_u *newval;
20815 unsigned len;
20816
Bram Moolenaare9a41262005-01-15 22:18:47 +000020817 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020818 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020820 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020821 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020822 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 }
20824
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020825 if (eap->force_bin == FORCE_BIN)
20826 len = 6;
20827 else if (eap->force_bin == FORCE_NOBIN)
20828 len = 8;
20829 else
20830 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020831
20832 if (eap->read_edit)
20833 len += 7;
20834
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020835 if (eap->force_ff != 0)
20836 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20837# ifdef FEAT_MBYTE
20838 if (eap->force_enc != 0)
20839 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020840 if (eap->bad_char != 0)
20841 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020842# endif
20843
20844 newval = alloc(len + 1);
20845 if (newval == NULL)
20846 return NULL;
20847
20848 if (eap->force_bin == FORCE_BIN)
20849 sprintf((char *)newval, " ++bin");
20850 else if (eap->force_bin == FORCE_NOBIN)
20851 sprintf((char *)newval, " ++nobin");
20852 else
20853 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020854
20855 if (eap->read_edit)
20856 STRCAT(newval, " ++edit");
20857
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020858 if (eap->force_ff != 0)
20859 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20860 eap->cmd + eap->force_ff);
20861# ifdef FEAT_MBYTE
20862 if (eap->force_enc != 0)
20863 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20864 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020865 if (eap->bad_char == BAD_KEEP)
20866 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20867 else if (eap->bad_char == BAD_DROP)
20868 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20869 else if (eap->bad_char != 0)
20870 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020871# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020872 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020873 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874}
20875#endif
20876
20877/*
20878 * Get the value of internal variable "name".
20879 * Return OK or FAIL.
20880 */
20881 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020882get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 char_u *name;
20884 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020885 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020886 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020887 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020888 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889{
20890 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020891 typval_T *tv = NULL;
20892 typval_T atv;
20893 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895
20896 /* truncate the name, so that we can use strcmp() */
20897 cc = name[len];
20898 name[len] = NUL;
20899
20900 /*
20901 * Check for "b:changedtick".
20902 */
20903 if (STRCMP(name, "b:changedtick") == 0)
20904 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020905 atv.v_type = VAR_NUMBER;
20906 atv.vval.v_number = curbuf->b_changedtick;
20907 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 }
20909
20910 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 * Check for user-defined variables.
20912 */
20913 else
20914 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020915 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020917 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020918 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020919 if (dip != NULL)
20920 *dip = v;
20921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 }
20923
Bram Moolenaare9a41262005-01-15 22:18:47 +000020924 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020926 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020927 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 ret = FAIL;
20929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020930 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020931 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932
20933 name[len] = cc;
20934
20935 return ret;
20936}
20937
20938/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020939 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20940 * Also handle function call with Funcref variable: func(expr)
20941 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20942 */
20943 static int
20944handle_subscript(arg, rettv, evaluate, verbose)
20945 char_u **arg;
20946 typval_T *rettv;
20947 int evaluate; /* do more than finding the end */
20948 int verbose; /* give error messages */
20949{
20950 int ret = OK;
20951 dict_T *selfdict = NULL;
20952 char_u *s;
20953 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020954 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020955
20956 while (ret == OK
20957 && (**arg == '['
20958 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020959 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020960 && !vim_iswhite(*(*arg - 1)))
20961 {
20962 if (**arg == '(')
20963 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020964 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020965 if (evaluate)
20966 {
20967 functv = *rettv;
20968 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020969
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020970 /* Invoke the function. Recursive! */
20971 s = functv.vval.v_string;
20972 }
20973 else
20974 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020975 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020976 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20977 &len, evaluate, selfdict);
20978
20979 /* Clear the funcref afterwards, so that deleting it while
20980 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020981 if (evaluate)
20982 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020983
20984 /* Stop the expression evaluation when immediately aborting on
20985 * error, or when an interrupt occurred or an exception was thrown
20986 * but not caught. */
20987 if (aborting())
20988 {
20989 if (ret == OK)
20990 clear_tv(rettv);
20991 ret = FAIL;
20992 }
20993 dict_unref(selfdict);
20994 selfdict = NULL;
20995 }
20996 else /* **arg == '[' || **arg == '.' */
20997 {
20998 dict_unref(selfdict);
20999 if (rettv->v_type == VAR_DICT)
21000 {
21001 selfdict = rettv->vval.v_dict;
21002 if (selfdict != NULL)
21003 ++selfdict->dv_refcount;
21004 }
21005 else
21006 selfdict = NULL;
21007 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21008 {
21009 clear_tv(rettv);
21010 ret = FAIL;
21011 }
21012 }
21013 }
21014 dict_unref(selfdict);
21015 return ret;
21016}
21017
21018/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021019 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021020 * value).
21021 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021022 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021024{
Bram Moolenaar33570922005-01-25 22:26:29 +000021025 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021026}
21027
21028/*
21029 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 * The string "s" must have been allocated, it is consumed.
21031 * Return NULL for out of memory, the variable otherwise.
21032 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 char_u *s;
21036{
Bram Moolenaar33570922005-01-25 22:26:29 +000021037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021039 rettv = alloc_tv();
21040 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021042 rettv->v_type = VAR_STRING;
21043 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044 }
21045 else
21046 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021047 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048}
21049
21050/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021051 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021053 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021054free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021055 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056{
21057 if (varp != NULL)
21058 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021059 switch (varp->v_type)
21060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021061 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021062 func_unref(varp->vval.v_string);
21063 /*FALLTHROUGH*/
21064 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021065 vim_free(varp->vval.v_string);
21066 break;
21067 case VAR_LIST:
21068 list_unref(varp->vval.v_list);
21069 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021070 case VAR_DICT:
21071 dict_unref(varp->vval.v_dict);
21072 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021073 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021074#ifdef FEAT_FLOAT
21075 case VAR_FLOAT:
21076#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021077 case VAR_UNKNOWN:
21078 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021079 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021080 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021081 break;
21082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 vim_free(varp);
21084 }
21085}
21086
21087/*
21088 * Free the memory for a variable value and set the value to NULL or 0.
21089 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021090 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021091clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093{
21094 if (varp != NULL)
21095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021096 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021098 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021099 func_unref(varp->vval.v_string);
21100 /*FALLTHROUGH*/
21101 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021102 vim_free(varp->vval.v_string);
21103 varp->vval.v_string = NULL;
21104 break;
21105 case VAR_LIST:
21106 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021107 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021108 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021109 case VAR_DICT:
21110 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021111 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021112 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021113 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021114 varp->vval.v_number = 0;
21115 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021116#ifdef FEAT_FLOAT
21117 case VAR_FLOAT:
21118 varp->vval.v_float = 0.0;
21119 break;
21120#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021121 case VAR_UNKNOWN:
21122 break;
21123 default:
21124 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021126 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 }
21128}
21129
21130/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021131 * Set the value of a variable to NULL without freeing items.
21132 */
21133 static void
21134init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021136{
21137 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021138 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021139}
21140
21141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 * Get the number value of a variable.
21143 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021144 * For incompatible types, return 0.
21145 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21146 * caller of incompatible types: it sets *denote to TRUE if "denote"
21147 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 */
21149 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021150get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021151 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021153 int error = FALSE;
21154
21155 return get_tv_number_chk(varp, &error); /* return 0L on error */
21156}
21157
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021158 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021159get_tv_number_chk(varp, denote)
21160 typval_T *varp;
21161 int *denote;
21162{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021163 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021165 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021167 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021168 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021169#ifdef FEAT_FLOAT
21170 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021171 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021172 break;
21173#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021174 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021175 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021176 break;
21177 case VAR_STRING:
21178 if (varp->vval.v_string != NULL)
21179 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021180 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021181 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021182 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021183 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021184 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021185 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021186 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021187 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021188 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021189 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021190 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021192 if (denote == NULL) /* useful for values that must be unsigned */
21193 n = -1;
21194 else
21195 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021196 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197}
21198
21199/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021200 * Get the lnum from the first argument.
21201 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021202 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 */
21204 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021205get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021206 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207{
Bram Moolenaar33570922005-01-25 22:26:29 +000021208 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 linenr_T lnum;
21210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021211 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 if (lnum == 0) /* no valid number, try using line() */
21213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 rettv.v_type = VAR_NUMBER;
21215 f_line(argvars, &rettv);
21216 lnum = rettv.vval.v_number;
21217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 }
21219 return lnum;
21220}
21221
21222/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021223 * Get the lnum from the first argument.
21224 * Also accepts "$", then "buf" is used.
21225 * Returns 0 on error.
21226 */
21227 static linenr_T
21228get_tv_lnum_buf(argvars, buf)
21229 typval_T *argvars;
21230 buf_T *buf;
21231{
21232 if (argvars[0].v_type == VAR_STRING
21233 && argvars[0].vval.v_string != NULL
21234 && argvars[0].vval.v_string[0] == '$'
21235 && buf != NULL)
21236 return buf->b_ml.ml_line_count;
21237 return get_tv_number_chk(&argvars[0], NULL);
21238}
21239
21240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 * Get the string value of a variable.
21242 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021243 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21244 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 * If the String variable has never been set, return an empty string.
21246 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021247 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21248 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 */
21250 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021251get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021252 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253{
21254 static char_u mybuf[NUMBUFLEN];
21255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021256 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257}
21258
21259 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021260get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021261 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021262 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021264 char_u *res = get_tv_string_buf_chk(varp, buf);
21265
21266 return res != NULL ? res : (char_u *)"";
21267}
21268
Bram Moolenaar7d647822014-04-05 21:28:56 +020021269/*
21270 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21271 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021272 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021273get_tv_string_chk(varp)
21274 typval_T *varp;
21275{
21276 static char_u mybuf[NUMBUFLEN];
21277
21278 return get_tv_string_buf_chk(varp, mybuf);
21279}
21280
21281 static char_u *
21282get_tv_string_buf_chk(varp, buf)
21283 typval_T *varp;
21284 char_u *buf;
21285{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021288 case VAR_NUMBER:
21289 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21290 return buf;
21291 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021292 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021293 break;
21294 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021295 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021296 break;
21297 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021298 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021299 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021300#ifdef FEAT_FLOAT
21301 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021302 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021303 break;
21304#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305 case VAR_STRING:
21306 if (varp->vval.v_string != NULL)
21307 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021308 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021310 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021313 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314}
21315
21316/*
21317 * Find variable "name" in the list of variables.
21318 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021319 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021320 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021324find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021327 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021330 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331
Bram Moolenaara7043832005-01-21 11:56:39 +000021332 ht = find_var_ht(name, &varname);
21333 if (htp != NULL)
21334 *htp = ht;
21335 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021337 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338}
21339
21340/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021341 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021342 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021344 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021345find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021346 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021347 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021348 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021349 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021350{
Bram Moolenaar33570922005-01-25 22:26:29 +000021351 hashitem_T *hi;
21352
21353 if (*varname == NUL)
21354 {
21355 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021356 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021357 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021358 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021359 case 'g': return &globvars_var;
21360 case 'v': return &vimvars_var;
21361 case 'b': return &curbuf->b_bufvar;
21362 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021363#ifdef FEAT_WINDOWS
21364 case 't': return &curtab->tp_winvar;
21365#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021366 case 'l': return current_funccal == NULL
21367 ? NULL : &current_funccal->l_vars_var;
21368 case 'a': return current_funccal == NULL
21369 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021370 }
21371 return NULL;
21372 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021373
21374 hi = hash_find(ht, varname);
21375 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021376 {
21377 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021378 * worked find the variable again. Don't auto-load a script if it was
21379 * loaded already, otherwise it would be loaded every time when
21380 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021381 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021382 {
21383 /* Note: script_autoload() may make "hi" invalid. It must either
21384 * be obtained again or not used. */
21385 if (!script_autoload(varname, FALSE) || aborting())
21386 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021387 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021388 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021389 if (HASHITEM_EMPTY(hi))
21390 return NULL;
21391 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021392 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021393}
21394
21395/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021396 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021397 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021398 * Set "varname" to the start of name without ':'.
21399 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021400 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021401find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 char_u *name;
21403 char_u **varname;
21404{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021405 hashitem_T *hi;
21406
Bram Moolenaar73627d02015-08-11 15:46:09 +020021407 if (name[0] == NUL)
21408 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 if (name[1] != ':')
21410 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021411 /* The name must not start with a colon or #. */
21412 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 return NULL;
21414 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021415
21416 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021417 hi = hash_find(&compat_hashtab, name);
21418 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021419 return &compat_hashtab;
21420
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021422 return &globvarht; /* global variable */
21423 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 }
21425 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021426 if (*name == 'g') /* global variable */
21427 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021428 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21429 */
21430 if (vim_strchr(name + 2, ':') != NULL
21431 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021432 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021434 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021436 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021437#ifdef FEAT_WINDOWS
21438 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021439 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021440#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 if (*name == 'v') /* v: variable */
21442 return &vimvarht;
21443 if (*name == 'a' && current_funccal != NULL) /* function argument */
21444 return &current_funccal->l_avars.dv_hashtab;
21445 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21446 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 if (*name == 's' /* script variable */
21448 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21449 return &SCRIPT_VARS(current_SID);
21450 return NULL;
21451}
21452
21453/*
21454 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021455 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 * Returns NULL when it doesn't exist.
21457 */
21458 char_u *
21459get_var_value(name)
21460 char_u *name;
21461{
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021464 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 if (v == NULL)
21466 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468}
21469
21470/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021471 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 * sourcing this script and when executing functions defined in the script.
21473 */
21474 void
21475new_script_vars(id)
21476 scid_T id;
21477{
Bram Moolenaara7043832005-01-21 11:56:39 +000021478 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021479 hashtab_T *ht;
21480 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021481
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21483 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021484 /* Re-allocating ga_data means that an ht_array pointing to
21485 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021487 for (i = 1; i <= ga_scripts.ga_len; ++i)
21488 {
21489 ht = &SCRIPT_VARS(i);
21490 if (ht->ht_mask == HT_INIT_SIZE - 1)
21491 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021492 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021494 }
21495
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 while (ga_scripts.ga_len < id)
21497 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021498 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021499 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021500 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 }
21503 }
21504}
21505
21506/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21508 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 */
21510 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021511init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 dict_T *dict;
21513 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021514 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515{
Bram Moolenaar33570922005-01-25 22:26:29 +000021516 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021517 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021518 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021519 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021520 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 dict_var->di_tv.vval.v_dict = dict;
21522 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021523 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021524 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21525 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526}
21527
21528/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021529 * Unreference a dictionary initialized by init_var_dict().
21530 */
21531 void
21532unref_var_dict(dict)
21533 dict_T *dict;
21534{
21535 /* Now the dict needs to be freed if no one else is using it, go back to
21536 * normal reference counting. */
21537 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21538 dict_unref(dict);
21539}
21540
21541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 * Frees all allocated variables and the value they contain.
21544 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 */
21546 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021547vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021548 hashtab_T *ht;
21549{
21550 vars_clear_ext(ht, TRUE);
21551}
21552
21553/*
21554 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21555 */
21556 static void
21557vars_clear_ext(ht, free_val)
21558 hashtab_T *ht;
21559 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560{
Bram Moolenaara7043832005-01-21 11:56:39 +000021561 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021562 hashitem_T *hi;
21563 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564
Bram Moolenaar33570922005-01-25 22:26:29 +000021565 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021566 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021567 for (hi = ht->ht_array; todo > 0; ++hi)
21568 {
21569 if (!HASHITEM_EMPTY(hi))
21570 {
21571 --todo;
21572
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021574 * ht_array might change then. hash_clear() takes care of it
21575 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021576 v = HI2DI(hi);
21577 if (free_val)
21578 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021579 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021580 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021581 }
21582 }
21583 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021584 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585}
21586
Bram Moolenaara7043832005-01-21 11:56:39 +000021587/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 * Delete a variable from hashtab "ht" at item "hi".
21589 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021592delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021593 hashtab_T *ht;
21594 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595{
Bram Moolenaar33570922005-01-25 22:26:29 +000021596 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021597
21598 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 clear_tv(&di->di_tv);
21600 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601}
21602
21603/*
21604 * List the value of one internal variable.
21605 */
21606 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021607list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021608 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021610 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021612 char_u *tofree;
21613 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021614 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021615
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021616 current_copyID += COPYID_INC;
21617 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021618 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021619 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621}
21622
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021624list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 char_u *prefix;
21626 char_u *name;
21627 int type;
21628 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021629 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630{
Bram Moolenaar31859182007-08-14 20:41:13 +000021631 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21632 msg_start();
21633 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 if (name != NULL) /* "a:" vars don't have a name stored */
21635 msg_puts(name);
21636 msg_putchar(' ');
21637 msg_advance(22);
21638 if (type == VAR_NUMBER)
21639 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640 else if (type == VAR_FUNC)
21641 msg_putchar('*');
21642 else if (type == VAR_LIST)
21643 {
21644 msg_putchar('[');
21645 if (*string == '[')
21646 ++string;
21647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021648 else if (type == VAR_DICT)
21649 {
21650 msg_putchar('{');
21651 if (*string == '{')
21652 ++string;
21653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 else
21655 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021656
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021658
21659 if (type == VAR_FUNC)
21660 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021661 if (*first)
21662 {
21663 msg_clr_eos();
21664 *first = FALSE;
21665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666}
21667
21668/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021669 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 * If the variable already exists, the value is updated.
21671 * Otherwise the variable is created.
21672 */
21673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021674set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021676 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021677 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678{
Bram Moolenaar33570922005-01-25 22:26:29 +000021679 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021681 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021683 ht = find_var_ht(name, &varname);
21684 if (ht == NULL || *varname == NUL)
21685 {
21686 EMSG2(_(e_illvar), name);
21687 return;
21688 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021689 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021690
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021691 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21692 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021693
Bram Moolenaar33570922005-01-25 22:26:29 +000021694 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021696 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021697 if (var_check_ro(v->di_flags, name, FALSE)
21698 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021699 return;
21700 if (v->di_tv.v_type != tv->v_type
21701 && !((v->di_tv.v_type == VAR_STRING
21702 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021704 || tv->v_type == VAR_NUMBER))
21705#ifdef FEAT_FLOAT
21706 && !((v->di_tv.v_type == VAR_NUMBER
21707 || v->di_tv.v_type == VAR_FLOAT)
21708 && (tv->v_type == VAR_NUMBER
21709 || tv->v_type == VAR_FLOAT))
21710#endif
21711 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021712 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021713 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021714 return;
21715 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021716
21717 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021718 * Handle setting internal v: variables separately where needed to
21719 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 */
21721 if (ht == &vimvarht)
21722 {
21723 if (v->di_tv.v_type == VAR_STRING)
21724 {
21725 vim_free(v->di_tv.vval.v_string);
21726 if (copy || tv->v_type != VAR_STRING)
21727 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21728 else
21729 {
21730 /* Take over the string to avoid an extra alloc/free. */
21731 v->di_tv.vval.v_string = tv->vval.v_string;
21732 tv->vval.v_string = NULL;
21733 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021734 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021735 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021736 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021737 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021739 if (STRCMP(varname, "searchforward") == 0)
21740 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021741#ifdef FEAT_SEARCH_EXTRA
21742 else if (STRCMP(varname, "hlsearch") == 0)
21743 {
21744 no_hlsearch = !v->di_tv.vval.v_number;
21745 redraw_all_later(SOME_VALID);
21746 }
21747#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021748 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021749 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021750 else if (v->di_tv.v_type != tv->v_type)
21751 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 }
21753
21754 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 }
21756 else /* add a new variable */
21757 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021758 /* Can't add "v:" variable. */
21759 if (ht == &vimvarht)
21760 {
21761 EMSG2(_(e_illvar), name);
21762 return;
21763 }
21764
Bram Moolenaar92124a32005-06-17 22:03:40 +000021765 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021766 if (!valid_varname(varname))
21767 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021768
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021769 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21770 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021771 if (v == NULL)
21772 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021773 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021776 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 return;
21778 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021779 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021781
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021782 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021784 else
21785 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021787 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021788 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790}
21791
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021792/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021793 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 * Also give an error message.
21795 */
21796 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021797var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 int flags;
21799 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021800 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021801{
21802 if (flags & DI_FLAGS_RO)
21803 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021804 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 return TRUE;
21806 }
21807 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21808 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021809 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 return TRUE;
21811 }
21812 return FALSE;
21813}
21814
21815/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021816 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21817 * Also give an error message.
21818 */
21819 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021820var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021821 int flags;
21822 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021823 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021824{
21825 if (flags & DI_FLAGS_FIX)
21826 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021827 EMSG2(_("E795: Cannot delete variable %s"),
21828 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021829 return TRUE;
21830 }
21831 return FALSE;
21832}
21833
21834/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021835 * Check if a funcref is assigned to a valid variable name.
21836 * Return TRUE and give an error if not.
21837 */
21838 static int
21839var_check_func_name(name, new_var)
21840 char_u *name; /* points to start of variable name */
21841 int new_var; /* TRUE when creating the variable */
21842{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021843 /* Allow for w: b: s: and t:. */
21844 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021845 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21846 ? name[2] : name[0]))
21847 {
21848 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21849 name);
21850 return TRUE;
21851 }
21852 /* Don't allow hiding a function. When "v" is not NULL we might be
21853 * assigning another function to the same var, the type is checked
21854 * below. */
21855 if (new_var && function_exists(name))
21856 {
21857 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21858 name);
21859 return TRUE;
21860 }
21861 return FALSE;
21862}
21863
21864/*
21865 * Check if a variable name is valid.
21866 * Return FALSE and give an error if not.
21867 */
21868 static int
21869valid_varname(varname)
21870 char_u *varname;
21871{
21872 char_u *p;
21873
21874 for (p = varname; *p != NUL; ++p)
21875 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21876 && *p != AUTOLOAD_CHAR)
21877 {
21878 EMSG2(_(e_illvar), varname);
21879 return FALSE;
21880 }
21881 return TRUE;
21882}
21883
21884/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021885 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021886 * Also give an error message, using "name" or _("name") when use_gettext is
21887 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021888 */
21889 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021890tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021891 int lock;
21892 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021893 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021894{
21895 if (lock & VAR_LOCKED)
21896 {
21897 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021898 name == NULL ? (char_u *)_("Unknown")
21899 : use_gettext ? (char_u *)_(name)
21900 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021901 return TRUE;
21902 }
21903 if (lock & VAR_FIXED)
21904 {
21905 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021906 name == NULL ? (char_u *)_("Unknown")
21907 : use_gettext ? (char_u *)_(name)
21908 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021909 return TRUE;
21910 }
21911 return FALSE;
21912}
21913
21914/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021917 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021918 * It is OK for "from" and "to" to point to the same item. This is used to
21919 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021920 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021921 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021922copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 typval_T *from;
21924 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021926 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021927 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928 switch (from->v_type)
21929 {
21930 case VAR_NUMBER:
21931 to->vval.v_number = from->vval.v_number;
21932 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021933#ifdef FEAT_FLOAT
21934 case VAR_FLOAT:
21935 to->vval.v_float = from->vval.v_float;
21936 break;
21937#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021938 case VAR_STRING:
21939 case VAR_FUNC:
21940 if (from->vval.v_string == NULL)
21941 to->vval.v_string = NULL;
21942 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021944 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021945 if (from->v_type == VAR_FUNC)
21946 func_ref(to->vval.v_string);
21947 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 break;
21949 case VAR_LIST:
21950 if (from->vval.v_list == NULL)
21951 to->vval.v_list = NULL;
21952 else
21953 {
21954 to->vval.v_list = from->vval.v_list;
21955 ++to->vval.v_list->lv_refcount;
21956 }
21957 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021958 case VAR_DICT:
21959 if (from->vval.v_dict == NULL)
21960 to->vval.v_dict = NULL;
21961 else
21962 {
21963 to->vval.v_dict = from->vval.v_dict;
21964 ++to->vval.v_dict->dv_refcount;
21965 }
21966 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021967 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021968 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021969 break;
21970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971}
21972
21973/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021974 * Make a copy of an item.
21975 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021976 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21977 * reference to an already copied list/dict can be used.
21978 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021979 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021980 static int
21981item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 typval_T *from;
21983 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021984 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021985 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021986{
21987 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021988 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021989
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021991 {
21992 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021993 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021994 }
21995 ++recurse;
21996
21997 switch (from->v_type)
21998 {
21999 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022000#ifdef FEAT_FLOAT
22001 case VAR_FLOAT:
22002#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022003 case VAR_STRING:
22004 case VAR_FUNC:
22005 copy_tv(from, to);
22006 break;
22007 case VAR_LIST:
22008 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022009 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022010 if (from->vval.v_list == NULL)
22011 to->vval.v_list = NULL;
22012 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22013 {
22014 /* use the copy made earlier */
22015 to->vval.v_list = from->vval.v_list->lv_copylist;
22016 ++to->vval.v_list->lv_refcount;
22017 }
22018 else
22019 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22020 if (to->vval.v_list == NULL)
22021 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022022 break;
22023 case VAR_DICT:
22024 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022025 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022026 if (from->vval.v_dict == NULL)
22027 to->vval.v_dict = NULL;
22028 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22029 {
22030 /* use the copy made earlier */
22031 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22032 ++to->vval.v_dict->dv_refcount;
22033 }
22034 else
22035 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22036 if (to->vval.v_dict == NULL)
22037 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022038 break;
22039 default:
22040 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022041 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022042 }
22043 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022044 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022045}
22046
22047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 * ":echo expr1 ..." print each argument separated with a space, add a
22049 * newline at the end.
22050 * ":echon expr1 ..." print each argument plain.
22051 */
22052 void
22053ex_echo(eap)
22054 exarg_T *eap;
22055{
22056 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022057 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022058 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 char_u *p;
22060 int needclr = TRUE;
22061 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022062 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063
22064 if (eap->skip)
22065 ++emsg_skip;
22066 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22067 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022068 /* If eval1() causes an error message the text from the command may
22069 * still need to be cleared. E.g., "echo 22,44". */
22070 need_clr_eos = needclr;
22071
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022073 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 {
22075 /*
22076 * Report the invalid expression unless the expression evaluation
22077 * has been cancelled due to an aborting error, an interrupt, or an
22078 * exception.
22079 */
22080 if (!aborting())
22081 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022082 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 break;
22084 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022085 need_clr_eos = FALSE;
22086
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 if (!eap->skip)
22088 {
22089 if (atstart)
22090 {
22091 atstart = FALSE;
22092 /* Call msg_start() after eval1(), evaluating the expression
22093 * may cause a message to appear. */
22094 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022095 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022096 /* Mark the saved text as finishing the line, so that what
22097 * follows is displayed on a new line when scrolling back
22098 * at the more prompt. */
22099 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 }
22103 else if (eap->cmdidx == CMD_echo)
22104 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022105 current_copyID += COPYID_INC;
22106 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022107 if (p != NULL)
22108 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022110 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022112 if (*p != TAB && needclr)
22113 {
22114 /* remove any text still there from the command */
22115 msg_clr_eos();
22116 needclr = FALSE;
22117 }
22118 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 }
22120 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022121 {
22122#ifdef FEAT_MBYTE
22123 if (has_mbyte)
22124 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022125 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022126
22127 (void)msg_outtrans_len_attr(p, i, echo_attr);
22128 p += i - 1;
22129 }
22130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022132 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022135 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022137 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 arg = skipwhite(arg);
22139 }
22140 eap->nextcmd = check_nextcmd(arg);
22141
22142 if (eap->skip)
22143 --emsg_skip;
22144 else
22145 {
22146 /* remove text that may still be there from the command */
22147 if (needclr)
22148 msg_clr_eos();
22149 if (eap->cmdidx == CMD_echo)
22150 msg_end();
22151 }
22152}
22153
22154/*
22155 * ":echohl {name}".
22156 */
22157 void
22158ex_echohl(eap)
22159 exarg_T *eap;
22160{
22161 int id;
22162
22163 id = syn_name2id(eap->arg);
22164 if (id == 0)
22165 echo_attr = 0;
22166 else
22167 echo_attr = syn_id2attr(id);
22168}
22169
22170/*
22171 * ":execute expr1 ..." execute the result of an expression.
22172 * ":echomsg expr1 ..." Print a message
22173 * ":echoerr expr1 ..." Print an error
22174 * Each gets spaces around each argument and a newline at the end for
22175 * echo commands
22176 */
22177 void
22178ex_execute(eap)
22179 exarg_T *eap;
22180{
22181 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 int ret = OK;
22184 char_u *p;
22185 garray_T ga;
22186 int len;
22187 int save_did_emsg;
22188
22189 ga_init2(&ga, 1, 80);
22190
22191 if (eap->skip)
22192 ++emsg_skip;
22193 while (*arg != NUL && *arg != '|' && *arg != '\n')
22194 {
22195 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022196 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 {
22198 /*
22199 * Report the invalid expression unless the expression evaluation
22200 * has been cancelled due to an aborting error, an interrupt, or an
22201 * exception.
22202 */
22203 if (!aborting())
22204 EMSG2(_(e_invexpr2), p);
22205 ret = FAIL;
22206 break;
22207 }
22208
22209 if (!eap->skip)
22210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022211 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 len = (int)STRLEN(p);
22213 if (ga_grow(&ga, len + 2) == FAIL)
22214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 ret = FAIL;
22217 break;
22218 }
22219 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 ga.ga_len += len;
22223 }
22224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022225 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 arg = skipwhite(arg);
22227 }
22228
22229 if (ret != FAIL && ga.ga_data != NULL)
22230 {
22231 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022234 out_flush();
22235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 else if (eap->cmdidx == CMD_echoerr)
22237 {
22238 /* We don't want to abort following commands, restore did_emsg. */
22239 save_did_emsg = did_emsg;
22240 EMSG((char_u *)ga.ga_data);
22241 if (!force_abort)
22242 did_emsg = save_did_emsg;
22243 }
22244 else if (eap->cmdidx == CMD_execute)
22245 do_cmdline((char_u *)ga.ga_data,
22246 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22247 }
22248
22249 ga_clear(&ga);
22250
22251 if (eap->skip)
22252 --emsg_skip;
22253
22254 eap->nextcmd = check_nextcmd(arg);
22255}
22256
22257/*
22258 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22259 * "arg" points to the "&" or '+' when called, to "option" when returning.
22260 * Returns NULL when no option name found. Otherwise pointer to the char
22261 * after the option name.
22262 */
22263 static char_u *
22264find_option_end(arg, opt_flags)
22265 char_u **arg;
22266 int *opt_flags;
22267{
22268 char_u *p = *arg;
22269
22270 ++p;
22271 if (*p == 'g' && p[1] == ':')
22272 {
22273 *opt_flags = OPT_GLOBAL;
22274 p += 2;
22275 }
22276 else if (*p == 'l' && p[1] == ':')
22277 {
22278 *opt_flags = OPT_LOCAL;
22279 p += 2;
22280 }
22281 else
22282 *opt_flags = 0;
22283
22284 if (!ASCII_ISALPHA(*p))
22285 return NULL;
22286 *arg = p;
22287
22288 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22289 p += 4; /* termcap option */
22290 else
22291 while (ASCII_ISALPHA(*p))
22292 ++p;
22293 return p;
22294}
22295
22296/*
22297 * ":function"
22298 */
22299 void
22300ex_function(eap)
22301 exarg_T *eap;
22302{
22303 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022304 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 int j;
22306 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022308 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 char_u *name = NULL;
22310 char_u *p;
22311 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022312 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 garray_T newargs;
22314 garray_T newlines;
22315 int varargs = FALSE;
22316 int mustend = FALSE;
22317 int flags = 0;
22318 ufunc_T *fp;
22319 int indent;
22320 int nesting;
22321 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022322 dictitem_T *v;
22323 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022324 static int func_nr = 0; /* number for nameless function */
22325 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022326 hashtab_T *ht;
22327 int todo;
22328 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022329 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330
22331 /*
22332 * ":function" without argument: list functions.
22333 */
22334 if (ends_excmd(*eap->arg))
22335 {
22336 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022337 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022338 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022340 {
22341 if (!HASHITEM_EMPTY(hi))
22342 {
22343 --todo;
22344 fp = HI2UF(hi);
22345 if (!isdigit(*fp->uf_name))
22346 list_func_head(fp, FALSE);
22347 }
22348 }
22349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 eap->nextcmd = check_nextcmd(eap->arg);
22351 return;
22352 }
22353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022355 * ":function /pat": list functions matching pattern.
22356 */
22357 if (*eap->arg == '/')
22358 {
22359 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22360 if (!eap->skip)
22361 {
22362 regmatch_T regmatch;
22363
22364 c = *p;
22365 *p = NUL;
22366 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22367 *p = c;
22368 if (regmatch.regprog != NULL)
22369 {
22370 regmatch.rm_ic = p_ic;
22371
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022372 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022373 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22374 {
22375 if (!HASHITEM_EMPTY(hi))
22376 {
22377 --todo;
22378 fp = HI2UF(hi);
22379 if (!isdigit(*fp->uf_name)
22380 && vim_regexec(&regmatch, fp->uf_name, 0))
22381 list_func_head(fp, FALSE);
22382 }
22383 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022384 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022385 }
22386 }
22387 if (*p == '/')
22388 ++p;
22389 eap->nextcmd = check_nextcmd(p);
22390 return;
22391 }
22392
22393 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022394 * Get the function name. There are these situations:
22395 * func normal function name
22396 * "name" == func, "fudi.fd_dict" == NULL
22397 * dict.func new dictionary entry
22398 * "name" == NULL, "fudi.fd_dict" set,
22399 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22400 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022401 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022402 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22403 * dict.func existing dict entry that's not a Funcref
22404 * "name" == NULL, "fudi.fd_dict" set,
22405 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022406 * s:func script-local function name
22407 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022410 name = trans_function_name(&p, eap->skip, 0, &fudi);
22411 paren = (vim_strchr(p, '(') != NULL);
22412 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 {
22414 /*
22415 * Return on an invalid expression in braces, unless the expression
22416 * evaluation has been cancelled due to an aborting error, an
22417 * interrupt, or an exception.
22418 */
22419 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022421 if (!eap->skip && fudi.fd_newkey != NULL)
22422 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 else
22427 eap->skip = TRUE;
22428 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022429
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 /* An error in a function call during evaluation of an expression in magic
22431 * braces should not cause the function not to be defined. */
22432 saved_did_emsg = did_emsg;
22433 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434
22435 /*
22436 * ":function func" with only function name: list function.
22437 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022438 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 {
22440 if (!ends_excmd(*skipwhite(p)))
22441 {
22442 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022443 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 }
22445 eap->nextcmd = check_nextcmd(p);
22446 if (eap->nextcmd != NULL)
22447 *p = NUL;
22448 if (!eap->skip && !got_int)
22449 {
22450 fp = find_func(name);
22451 if (fp != NULL)
22452 {
22453 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022454 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022456 if (FUNCLINE(fp, j) == NULL)
22457 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 msg_putchar('\n');
22459 msg_outnum((long)(j + 1));
22460 if (j < 9)
22461 msg_putchar(' ');
22462 if (j < 99)
22463 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022464 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 out_flush(); /* show a line at a time */
22466 ui_breakcheck();
22467 }
22468 if (!got_int)
22469 {
22470 msg_putchar('\n');
22471 msg_puts((char_u *)" endfunction");
22472 }
22473 }
22474 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022475 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022477 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 }
22479
22480 /*
22481 * ":function name(arg1, arg2)" Define function.
22482 */
22483 p = skipwhite(p);
22484 if (*p != '(')
22485 {
22486 if (!eap->skip)
22487 {
22488 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 }
22491 /* attempt to continue by skipping some text */
22492 if (vim_strchr(p, '(') != NULL)
22493 p = vim_strchr(p, '(');
22494 }
22495 p = skipwhite(p + 1);
22496
22497 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22498 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22499
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022500 if (!eap->skip)
22501 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022502 /* Check the name of the function. Unless it's a dictionary function
22503 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022504 if (name != NULL)
22505 arg = name;
22506 else
22507 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022508 if (arg != NULL && (fudi.fd_di == NULL
22509 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022510 {
22511 if (*arg == K_SPECIAL)
22512 j = 3;
22513 else
22514 j = 0;
22515 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22516 : eval_isnamec(arg[j])))
22517 ++j;
22518 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022519 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022520 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022521 /* Disallow using the g: dict. */
22522 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22523 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022524 }
22525
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 /*
22527 * Isolate the arguments: "arg1, arg2, ...)"
22528 */
22529 while (*p != ')')
22530 {
22531 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22532 {
22533 varargs = TRUE;
22534 p += 3;
22535 mustend = TRUE;
22536 }
22537 else
22538 {
22539 arg = p;
22540 while (ASCII_ISALNUM(*p) || *p == '_')
22541 ++p;
22542 if (arg == p || isdigit(*arg)
22543 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22544 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22545 {
22546 if (!eap->skip)
22547 EMSG2(_("E125: Illegal argument: %s"), arg);
22548 break;
22549 }
22550 if (ga_grow(&newargs, 1) == FAIL)
22551 goto erret;
22552 c = *p;
22553 *p = NUL;
22554 arg = vim_strsave(arg);
22555 if (arg == NULL)
22556 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022557
22558 /* Check for duplicate argument name. */
22559 for (i = 0; i < newargs.ga_len; ++i)
22560 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22561 {
22562 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022563 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022564 goto erret;
22565 }
22566
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22568 *p = c;
22569 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 if (*p == ',')
22571 ++p;
22572 else
22573 mustend = TRUE;
22574 }
22575 p = skipwhite(p);
22576 if (mustend && *p != ')')
22577 {
22578 if (!eap->skip)
22579 EMSG2(_(e_invarg2), eap->arg);
22580 break;
22581 }
22582 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022583 if (*p != ')')
22584 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 ++p; /* skip the ')' */
22586
Bram Moolenaare9a41262005-01-15 22:18:47 +000022587 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 for (;;)
22589 {
22590 p = skipwhite(p);
22591 if (STRNCMP(p, "range", 5) == 0)
22592 {
22593 flags |= FC_RANGE;
22594 p += 5;
22595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022596 else if (STRNCMP(p, "dict", 4) == 0)
22597 {
22598 flags |= FC_DICT;
22599 p += 4;
22600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 else if (STRNCMP(p, "abort", 5) == 0)
22602 {
22603 flags |= FC_ABORT;
22604 p += 5;
22605 }
22606 else
22607 break;
22608 }
22609
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022610 /* When there is a line break use what follows for the function body.
22611 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22612 if (*p == '\n')
22613 line_arg = p + 1;
22614 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 EMSG(_(e_trailing));
22616
22617 /*
22618 * Read the body of the function, until ":endfunction" is found.
22619 */
22620 if (KeyTyped)
22621 {
22622 /* Check if the function already exists, don't let the user type the
22623 * whole function before telling him it doesn't work! For a script we
22624 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022625 if (!eap->skip && !eap->forceit)
22626 {
22627 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22628 EMSG(_(e_funcdict));
22629 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022630 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022633 if (!eap->skip && did_emsg)
22634 goto erret;
22635
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 msg_putchar('\n'); /* don't overwrite the function name */
22637 cmdline_row = msg_row;
22638 }
22639
22640 indent = 2;
22641 nesting = 0;
22642 for (;;)
22643 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022644 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022645 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022646 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022647 saved_wait_return = FALSE;
22648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022650 sourcing_lnum_off = sourcing_lnum;
22651
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022652 if (line_arg != NULL)
22653 {
22654 /* Use eap->arg, split up in parts by line breaks. */
22655 theline = line_arg;
22656 p = vim_strchr(theline, '\n');
22657 if (p == NULL)
22658 line_arg += STRLEN(line_arg);
22659 else
22660 {
22661 *p = NUL;
22662 line_arg = p + 1;
22663 }
22664 }
22665 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 theline = getcmdline(':', 0L, indent);
22667 else
22668 theline = eap->getline(':', eap->cookie, indent);
22669 if (KeyTyped)
22670 lines_left = Rows - 1;
22671 if (theline == NULL)
22672 {
22673 EMSG(_("E126: Missing :endfunction"));
22674 goto erret;
22675 }
22676
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022677 /* Detect line continuation: sourcing_lnum increased more than one. */
22678 if (sourcing_lnum > sourcing_lnum_off + 1)
22679 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22680 else
22681 sourcing_lnum_off = 0;
22682
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 if (skip_until != NULL)
22684 {
22685 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22686 * don't check for ":endfunc". */
22687 if (STRCMP(theline, skip_until) == 0)
22688 {
22689 vim_free(skip_until);
22690 skip_until = NULL;
22691 }
22692 }
22693 else
22694 {
22695 /* skip ':' and blanks*/
22696 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22697 ;
22698
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022699 /* Check for "endfunction". */
22700 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022702 if (line_arg == NULL)
22703 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 break;
22705 }
22706
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022707 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 * at "end". */
22709 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22710 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022711 else if (STRNCMP(p, "if", 2) == 0
22712 || STRNCMP(p, "wh", 2) == 0
22713 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 || STRNCMP(p, "try", 3) == 0)
22715 indent += 2;
22716
22717 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022718 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022720 if (*p == '!')
22721 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022723 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22724 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022726 ++nesting;
22727 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 }
22729 }
22730
22731 /* Check for ":append" or ":insert". */
22732 p = skip_range(p, NULL);
22733 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22734 || (p[0] == 'i'
22735 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22736 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22737 skip_until = vim_strsave((char_u *)".");
22738
22739 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22740 arg = skipwhite(skiptowhite(p));
22741 if (arg[0] == '<' && arg[1] =='<'
22742 && ((p[0] == 'p' && p[1] == 'y'
22743 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22744 || (p[0] == 'p' && p[1] == 'e'
22745 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22746 || (p[0] == 't' && p[1] == 'c'
22747 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022748 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22749 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22751 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022752 || (p[0] == 'm' && p[1] == 'z'
22753 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 ))
22755 {
22756 /* ":python <<" continues until a dot, like ":append" */
22757 p = skipwhite(arg + 2);
22758 if (*p == NUL)
22759 skip_until = vim_strsave((char_u *)".");
22760 else
22761 skip_until = vim_strsave(p);
22762 }
22763 }
22764
22765 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022766 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022767 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022768 if (line_arg == NULL)
22769 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022771 }
22772
22773 /* Copy the line to newly allocated memory. get_one_sourceline()
22774 * allocates 250 bytes per line, this saves 80% on average. The cost
22775 * is an extra alloc/free. */
22776 p = vim_strsave(theline);
22777 if (p != NULL)
22778 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022779 if (line_arg == NULL)
22780 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022781 theline = p;
22782 }
22783
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022784 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22785
22786 /* Add NULL lines for continuation lines, so that the line count is
22787 * equal to the index in the growarray. */
22788 while (sourcing_lnum_off-- > 0)
22789 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022790
22791 /* Check for end of eap->arg. */
22792 if (line_arg != NULL && *line_arg == NUL)
22793 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 }
22795
22796 /* Don't define the function when skipping commands or when an error was
22797 * detected. */
22798 if (eap->skip || did_emsg)
22799 goto erret;
22800
22801 /*
22802 * If there are no errors, add the function
22803 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022804 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022805 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022806 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022808 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022809 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022810 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022811 goto erret;
22812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022813
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022814 fp = find_func(name);
22815 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022817 if (!eap->forceit)
22818 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022819 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022820 goto erret;
22821 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022822 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022823 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022824 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022825 name);
22826 goto erret;
22827 }
22828 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022829 ga_clear_strings(&(fp->uf_args));
22830 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022831 vim_free(name);
22832 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 }
22835 else
22836 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022837 char numbuf[20];
22838
22839 fp = NULL;
22840 if (fudi.fd_newkey == NULL && !eap->forceit)
22841 {
22842 EMSG(_(e_funcdict));
22843 goto erret;
22844 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022845 if (fudi.fd_di == NULL)
22846 {
22847 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022848 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022849 goto erret;
22850 }
22851 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022852 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022853 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022854
22855 /* Give the function a sequential number. Can only be used with a
22856 * Funcref! */
22857 vim_free(name);
22858 sprintf(numbuf, "%d", ++func_nr);
22859 name = vim_strsave((char_u *)numbuf);
22860 if (name == NULL)
22861 goto erret;
22862 }
22863
22864 if (fp == NULL)
22865 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022866 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022867 {
22868 int slen, plen;
22869 char_u *scriptname;
22870
22871 /* Check that the autoload name matches the script name. */
22872 j = FAIL;
22873 if (sourcing_name != NULL)
22874 {
22875 scriptname = autoload_name(name);
22876 if (scriptname != NULL)
22877 {
22878 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022879 plen = (int)STRLEN(p);
22880 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022881 if (slen > plen && fnamecmp(p,
22882 sourcing_name + slen - plen) == 0)
22883 j = OK;
22884 vim_free(scriptname);
22885 }
22886 }
22887 if (j == FAIL)
22888 {
22889 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22890 goto erret;
22891 }
22892 }
22893
22894 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 if (fp == NULL)
22896 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022897
22898 if (fudi.fd_dict != NULL)
22899 {
22900 if (fudi.fd_di == NULL)
22901 {
22902 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022903 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022904 if (fudi.fd_di == NULL)
22905 {
22906 vim_free(fp);
22907 goto erret;
22908 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022909 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22910 {
22911 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022912 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022913 goto erret;
22914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022915 }
22916 else
22917 /* overwrite existing dict entry */
22918 clear_tv(&fudi.fd_di->di_tv);
22919 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022920 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022921 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022922 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022923
22924 /* behave like "dict" was used */
22925 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022926 }
22927
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022929 STRCPY(fp->uf_name, name);
22930 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022932 fp->uf_args = newargs;
22933 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022934#ifdef FEAT_PROFILE
22935 fp->uf_tml_count = NULL;
22936 fp->uf_tml_total = NULL;
22937 fp->uf_tml_self = NULL;
22938 fp->uf_profiling = FALSE;
22939 if (prof_def_func())
22940 func_do_profile(fp);
22941#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022942 fp->uf_varargs = varargs;
22943 fp->uf_flags = flags;
22944 fp->uf_calls = 0;
22945 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947
22948erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 ga_clear_strings(&newargs);
22950 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022951ret_free:
22952 vim_free(skip_until);
22953 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022956 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957}
22958
22959/*
22960 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022961 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022963 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022964 * TFN_INT: internal function name OK
22965 * TFN_QUIET: be quiet
22966 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 * Advances "pp" to just after the function name (if no error).
22968 */
22969 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022970trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 char_u **pp;
22972 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022973 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022976 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 char_u *start;
22978 char_u *end;
22979 int lead;
22980 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022983
22984 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022985 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022987
22988 /* Check for hard coded <SNR>: already translated function ID (from a user
22989 * command). */
22990 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22991 && (*pp)[2] == (int)KE_SNR)
22992 {
22993 *pp += 3;
22994 len = get_id_len(pp) + 3;
22995 return vim_strnsave(start, len);
22996 }
22997
22998 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22999 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023001 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023003
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023004 /* Note that TFN_ flags use the same values as GLV_ flags. */
23005 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023006 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023007 if (end == start)
23008 {
23009 if (!skip)
23010 EMSG(_("E129: Function name required"));
23011 goto theend;
23012 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023013 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023014 {
23015 /*
23016 * Report an invalid expression in braces, unless the expression
23017 * evaluation has been cancelled due to an aborting error, an
23018 * interrupt, or an exception.
23019 */
23020 if (!aborting())
23021 {
23022 if (end != NULL)
23023 EMSG2(_(e_invarg2), start);
23024 }
23025 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023026 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023027 goto theend;
23028 }
23029
23030 if (lv.ll_tv != NULL)
23031 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023032 if (fdp != NULL)
23033 {
23034 fdp->fd_dict = lv.ll_dict;
23035 fdp->fd_newkey = lv.ll_newkey;
23036 lv.ll_newkey = NULL;
23037 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023039 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23040 {
23041 name = vim_strsave(lv.ll_tv->vval.v_string);
23042 *pp = end;
23043 }
23044 else
23045 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023046 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23047 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023048 EMSG(_(e_funcref));
23049 else
23050 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023051 name = NULL;
23052 }
23053 goto theend;
23054 }
23055
23056 if (lv.ll_name == NULL)
23057 {
23058 /* Error found, but continue after the function name. */
23059 *pp = end;
23060 goto theend;
23061 }
23062
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023063 /* Check if the name is a Funcref. If so, use the value. */
23064 if (lv.ll_exp_name != NULL)
23065 {
23066 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023067 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023068 if (name == lv.ll_exp_name)
23069 name = NULL;
23070 }
23071 else
23072 {
23073 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023074 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023075 if (name == *pp)
23076 name = NULL;
23077 }
23078 if (name != NULL)
23079 {
23080 name = vim_strsave(name);
23081 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023082 if (STRNCMP(name, "<SNR>", 5) == 0)
23083 {
23084 /* Change "<SNR>" to the byte sequence. */
23085 name[0] = K_SPECIAL;
23086 name[1] = KS_EXTRA;
23087 name[2] = (int)KE_SNR;
23088 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23089 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023090 goto theend;
23091 }
23092
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023093 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023094 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023095 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023096 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23097 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23098 {
23099 /* When there was "s:" already or the name expanded to get a
23100 * leading "s:" then remove it. */
23101 lv.ll_name += 2;
23102 len -= 2;
23103 lead = 2;
23104 }
23105 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023106 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023107 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023108 /* skip over "s:" and "g:" */
23109 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023110 lv.ll_name += 2;
23111 len = (int)(end - lv.ll_name);
23112 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023113
23114 /*
23115 * Copy the function name to allocated memory.
23116 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23117 * Accept <SNR>123_name() outside a script.
23118 */
23119 if (skip)
23120 lead = 0; /* do nothing */
23121 else if (lead > 0)
23122 {
23123 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023124 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23125 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023126 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023127 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023128 if (current_SID <= 0)
23129 {
23130 EMSG(_(e_usingsid));
23131 goto theend;
23132 }
23133 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23134 lead += (int)STRLEN(sid_buf);
23135 }
23136 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023137 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023138 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023139 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023140 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023141 goto theend;
23142 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023143 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023144 {
23145 char_u *cp = vim_strchr(lv.ll_name, ':');
23146
23147 if (cp != NULL && cp < end)
23148 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023149 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023150 goto theend;
23151 }
23152 }
23153
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023154 name = alloc((unsigned)(len + lead + 1));
23155 if (name != NULL)
23156 {
23157 if (lead > 0)
23158 {
23159 name[0] = K_SPECIAL;
23160 name[1] = KS_EXTRA;
23161 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023162 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023163 STRCPY(name + 3, sid_buf);
23164 }
23165 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023166 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023167 }
23168 *pp = end;
23169
23170theend:
23171 clear_lval(&lv);
23172 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173}
23174
23175/*
23176 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23177 * Return 2 if "p" starts with "s:".
23178 * Return 0 otherwise.
23179 */
23180 static int
23181eval_fname_script(p)
23182 char_u *p;
23183{
23184 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23185 || STRNICMP(p + 1, "SNR>", 4) == 0))
23186 return 5;
23187 if (p[0] == 's' && p[1] == ':')
23188 return 2;
23189 return 0;
23190}
23191
23192/*
23193 * Return TRUE if "p" starts with "<SID>" or "s:".
23194 * Only works if eval_fname_script() returned non-zero for "p"!
23195 */
23196 static int
23197eval_fname_sid(p)
23198 char_u *p;
23199{
23200 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23201}
23202
23203/*
23204 * List the head of the function: "name(arg1, arg2)".
23205 */
23206 static void
23207list_func_head(fp, indent)
23208 ufunc_T *fp;
23209 int indent;
23210{
23211 int j;
23212
23213 msg_start();
23214 if (indent)
23215 MSG_PUTS(" ");
23216 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023217 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 {
23219 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023220 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 }
23222 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023223 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023225 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 {
23227 if (j)
23228 MSG_PUTS(", ");
23229 msg_puts(FUNCARG(fp, j));
23230 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023231 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232 {
23233 if (j)
23234 MSG_PUTS(", ");
23235 MSG_PUTS("...");
23236 }
23237 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023238 if (fp->uf_flags & FC_ABORT)
23239 MSG_PUTS(" abort");
23240 if (fp->uf_flags & FC_RANGE)
23241 MSG_PUTS(" range");
23242 if (fp->uf_flags & FC_DICT)
23243 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023244 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023245 if (p_verbose > 0)
23246 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247}
23248
23249/*
23250 * Find a function by name, return pointer to it in ufuncs.
23251 * Return NULL for unknown function.
23252 */
23253 static ufunc_T *
23254find_func(name)
23255 char_u *name;
23256{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023257 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023259 hi = hash_find(&func_hashtab, name);
23260 if (!HASHITEM_EMPTY(hi))
23261 return HI2UF(hi);
23262 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263}
23264
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023265#if defined(EXITFREE) || defined(PROTO)
23266 void
23267free_all_functions()
23268{
23269 hashitem_T *hi;
23270
23271 /* Need to start all over every time, because func_free() may change the
23272 * hash table. */
23273 while (func_hashtab.ht_used > 0)
23274 for (hi = func_hashtab.ht_array; ; ++hi)
23275 if (!HASHITEM_EMPTY(hi))
23276 {
23277 func_free(HI2UF(hi));
23278 break;
23279 }
23280}
23281#endif
23282
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023283 int
23284translated_function_exists(name)
23285 char_u *name;
23286{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023287 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023288 return find_internal_func(name) >= 0;
23289 return find_func(name) != NULL;
23290}
23291
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023292/*
23293 * Return TRUE if a function "name" exists.
23294 */
23295 static int
23296function_exists(name)
23297 char_u *name;
23298{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023299 char_u *nm = name;
23300 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023301 int n = FALSE;
23302
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023303 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23304 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023305 nm = skipwhite(nm);
23306
23307 /* Only accept "funcname", "funcname ", "funcname (..." and
23308 * "funcname(...", not "funcname!...". */
23309 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023310 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023311 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023312 return n;
23313}
23314
Bram Moolenaara1544c02013-05-30 12:35:52 +020023315 char_u *
23316get_expanded_name(name, check)
23317 char_u *name;
23318 int check;
23319{
23320 char_u *nm = name;
23321 char_u *p;
23322
23323 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23324
23325 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023326 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023327 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023328
Bram Moolenaara1544c02013-05-30 12:35:52 +020023329 vim_free(p);
23330 return NULL;
23331}
23332
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023333/*
23334 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023335 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23336 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023337 */
23338 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023339builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023340 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023341 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023342{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023343 char_u *p;
23344
23345 if (!ASCII_ISLOWER(name[0]))
23346 return FALSE;
23347 p = vim_strchr(name, AUTOLOAD_CHAR);
23348 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023349}
23350
Bram Moolenaar05159a02005-02-26 23:04:13 +000023351#if defined(FEAT_PROFILE) || defined(PROTO)
23352/*
23353 * Start profiling function "fp".
23354 */
23355 static void
23356func_do_profile(fp)
23357 ufunc_T *fp;
23358{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023359 int len = fp->uf_lines.ga_len;
23360
23361 if (len == 0)
23362 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023363 fp->uf_tm_count = 0;
23364 profile_zero(&fp->uf_tm_self);
23365 profile_zero(&fp->uf_tm_total);
23366 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023367 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023368 if (fp->uf_tml_total == NULL)
23369 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023370 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023371 if (fp->uf_tml_self == NULL)
23372 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023373 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023374 fp->uf_tml_idx = -1;
23375 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23376 || fp->uf_tml_self == NULL)
23377 return; /* out of memory */
23378
23379 fp->uf_profiling = TRUE;
23380}
23381
23382/*
23383 * Dump the profiling results for all functions in file "fd".
23384 */
23385 void
23386func_dump_profile(fd)
23387 FILE *fd;
23388{
23389 hashitem_T *hi;
23390 int todo;
23391 ufunc_T *fp;
23392 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023393 ufunc_T **sorttab;
23394 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023395
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023396 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023397 if (todo == 0)
23398 return; /* nothing to dump */
23399
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023400 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023401
Bram Moolenaar05159a02005-02-26 23:04:13 +000023402 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23403 {
23404 if (!HASHITEM_EMPTY(hi))
23405 {
23406 --todo;
23407 fp = HI2UF(hi);
23408 if (fp->uf_profiling)
23409 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023410 if (sorttab != NULL)
23411 sorttab[st_len++] = fp;
23412
Bram Moolenaar05159a02005-02-26 23:04:13 +000023413 if (fp->uf_name[0] == K_SPECIAL)
23414 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23415 else
23416 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23417 if (fp->uf_tm_count == 1)
23418 fprintf(fd, "Called 1 time\n");
23419 else
23420 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23421 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23422 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23423 fprintf(fd, "\n");
23424 fprintf(fd, "count total (s) self (s)\n");
23425
23426 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23427 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023428 if (FUNCLINE(fp, i) == NULL)
23429 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023430 prof_func_line(fd, fp->uf_tml_count[i],
23431 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023432 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23433 }
23434 fprintf(fd, "\n");
23435 }
23436 }
23437 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023438
23439 if (sorttab != NULL && st_len > 0)
23440 {
23441 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23442 prof_total_cmp);
23443 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23444 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23445 prof_self_cmp);
23446 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23447 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023448
23449 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023450}
Bram Moolenaar73830342005-02-28 22:48:19 +000023451
23452 static void
23453prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23454 FILE *fd;
23455 ufunc_T **sorttab;
23456 int st_len;
23457 char *title;
23458 int prefer_self; /* when equal print only self time */
23459{
23460 int i;
23461 ufunc_T *fp;
23462
23463 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23464 fprintf(fd, "count total (s) self (s) function\n");
23465 for (i = 0; i < 20 && i < st_len; ++i)
23466 {
23467 fp = sorttab[i];
23468 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23469 prefer_self);
23470 if (fp->uf_name[0] == K_SPECIAL)
23471 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23472 else
23473 fprintf(fd, " %s()\n", fp->uf_name);
23474 }
23475 fprintf(fd, "\n");
23476}
23477
23478/*
23479 * Print the count and times for one function or function line.
23480 */
23481 static void
23482prof_func_line(fd, count, total, self, prefer_self)
23483 FILE *fd;
23484 int count;
23485 proftime_T *total;
23486 proftime_T *self;
23487 int prefer_self; /* when equal print only self time */
23488{
23489 if (count > 0)
23490 {
23491 fprintf(fd, "%5d ", count);
23492 if (prefer_self && profile_equal(total, self))
23493 fprintf(fd, " ");
23494 else
23495 fprintf(fd, "%s ", profile_msg(total));
23496 if (!prefer_self && profile_equal(total, self))
23497 fprintf(fd, " ");
23498 else
23499 fprintf(fd, "%s ", profile_msg(self));
23500 }
23501 else
23502 fprintf(fd, " ");
23503}
23504
23505/*
23506 * Compare function for total time sorting.
23507 */
23508 static int
23509#ifdef __BORLANDC__
23510_RTLENTRYF
23511#endif
23512prof_total_cmp(s1, s2)
23513 const void *s1;
23514 const void *s2;
23515{
23516 ufunc_T *p1, *p2;
23517
23518 p1 = *(ufunc_T **)s1;
23519 p2 = *(ufunc_T **)s2;
23520 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23521}
23522
23523/*
23524 * Compare function for self time sorting.
23525 */
23526 static int
23527#ifdef __BORLANDC__
23528_RTLENTRYF
23529#endif
23530prof_self_cmp(s1, s2)
23531 const void *s1;
23532 const void *s2;
23533{
23534 ufunc_T *p1, *p2;
23535
23536 p1 = *(ufunc_T **)s1;
23537 p2 = *(ufunc_T **)s2;
23538 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23539}
23540
Bram Moolenaar05159a02005-02-26 23:04:13 +000023541#endif
23542
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023543/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023544 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023545 * Return TRUE if a package was loaded.
23546 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023547 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023548script_autoload(name, reload)
23549 char_u *name;
23550 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023551{
23552 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023553 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023554 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023555 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023556
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023557 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023558 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023559 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023560 return FALSE;
23561
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023562 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023563
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023564 /* Find the name in the list of previously loaded package names. Skip
23565 * "autoload/", it's always the same. */
23566 for (i = 0; i < ga_loaded.ga_len; ++i)
23567 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23568 break;
23569 if (!reload && i < ga_loaded.ga_len)
23570 ret = FALSE; /* was loaded already */
23571 else
23572 {
23573 /* Remember the name if it wasn't loaded already. */
23574 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23575 {
23576 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23577 tofree = NULL;
23578 }
23579
23580 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023581 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023582 ret = TRUE;
23583 }
23584
23585 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023586 return ret;
23587}
23588
23589/*
23590 * Return the autoload script name for a function or variable name.
23591 * Returns NULL when out of memory.
23592 */
23593 static char_u *
23594autoload_name(name)
23595 char_u *name;
23596{
23597 char_u *p;
23598 char_u *scriptname;
23599
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023600 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023601 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23602 if (scriptname == NULL)
23603 return FALSE;
23604 STRCPY(scriptname, "autoload/");
23605 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023606 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023607 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023608 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023609 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023610 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023611}
23612
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23614
23615/*
23616 * Function given to ExpandGeneric() to obtain the list of user defined
23617 * function names.
23618 */
23619 char_u *
23620get_user_func_name(xp, idx)
23621 expand_T *xp;
23622 int idx;
23623{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023624 static long_u done;
23625 static hashitem_T *hi;
23626 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627
23628 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023630 done = 0;
23631 hi = func_hashtab.ht_array;
23632 }
23633 if (done < func_hashtab.ht_used)
23634 {
23635 if (done++ > 0)
23636 ++hi;
23637 while (HASHITEM_EMPTY(hi))
23638 ++hi;
23639 fp = HI2UF(hi);
23640
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023641 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023642 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023643
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023644 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23645 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646
23647 cat_func_name(IObuff, fp);
23648 if (xp->xp_context != EXPAND_USER_FUNC)
23649 {
23650 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023651 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 STRCAT(IObuff, ")");
23653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 return IObuff;
23655 }
23656 return NULL;
23657}
23658
23659#endif /* FEAT_CMDL_COMPL */
23660
23661/*
23662 * Copy the function name of "fp" to buffer "buf".
23663 * "buf" must be able to hold the function name plus three bytes.
23664 * Takes care of script-local function names.
23665 */
23666 static void
23667cat_func_name(buf, fp)
23668 char_u *buf;
23669 ufunc_T *fp;
23670{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023671 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 {
23673 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023674 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 }
23676 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023677 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678}
23679
23680/*
23681 * ":delfunction {name}"
23682 */
23683 void
23684ex_delfunction(eap)
23685 exarg_T *eap;
23686{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023687 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688 char_u *p;
23689 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023690 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691
23692 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023693 name = trans_function_name(&p, eap->skip, 0, &fudi);
23694 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023696 {
23697 if (fudi.fd_dict != NULL && !eap->skip)
23698 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 if (!ends_excmd(*skipwhite(p)))
23702 {
23703 vim_free(name);
23704 EMSG(_(e_trailing));
23705 return;
23706 }
23707 eap->nextcmd = check_nextcmd(p);
23708 if (eap->nextcmd != NULL)
23709 *p = NUL;
23710
23711 if (!eap->skip)
23712 fp = find_func(name);
23713 vim_free(name);
23714
23715 if (!eap->skip)
23716 {
23717 if (fp == NULL)
23718 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023719 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 return;
23721 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023722 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 {
23724 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23725 return;
23726 }
23727
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023730 /* Delete the dict item that refers to the function, it will
23731 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023732 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023734 else
23735 func_free(fp);
23736 }
23737}
23738
23739/*
23740 * Free a function and remove it from the list of functions.
23741 */
23742 static void
23743func_free(fp)
23744 ufunc_T *fp;
23745{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023746 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023747
23748 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023749 ga_clear_strings(&(fp->uf_args));
23750 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023751#ifdef FEAT_PROFILE
23752 vim_free(fp->uf_tml_count);
23753 vim_free(fp->uf_tml_total);
23754 vim_free(fp->uf_tml_self);
23755#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023756
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023757 /* remove the function from the function hashtable */
23758 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23759 if (HASHITEM_EMPTY(hi))
23760 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023761 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023762 hash_remove(&func_hashtab, hi);
23763
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023764 vim_free(fp);
23765}
23766
23767/*
23768 * Unreference a Function: decrement the reference count and free it when it
23769 * becomes zero. Only for numbered functions.
23770 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023771 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023772func_unref(name)
23773 char_u *name;
23774{
23775 ufunc_T *fp;
23776
23777 if (name != NULL && isdigit(*name))
23778 {
23779 fp = find_func(name);
23780 if (fp == NULL)
23781 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023782 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023783 {
23784 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023785 * when "uf_calls" becomes zero. */
23786 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023787 func_free(fp);
23788 }
23789 }
23790}
23791
23792/*
23793 * Count a reference to a Function.
23794 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023795 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023796func_ref(name)
23797 char_u *name;
23798{
23799 ufunc_T *fp;
23800
23801 if (name != NULL && isdigit(*name))
23802 {
23803 fp = find_func(name);
23804 if (fp == NULL)
23805 EMSG2(_(e_intern2), "func_ref()");
23806 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023807 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 }
23809}
23810
23811/*
23812 * Call a user function.
23813 */
23814 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023815call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 ufunc_T *fp; /* pointer to function */
23817 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023818 typval_T *argvars; /* arguments */
23819 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 linenr_T firstline; /* first line of range */
23821 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023822 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823{
Bram Moolenaar33570922005-01-25 22:26:29 +000023824 char_u *save_sourcing_name;
23825 linenr_T save_sourcing_lnum;
23826 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023827 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023828 int save_did_emsg;
23829 static int depth = 0;
23830 dictitem_T *v;
23831 int fixvar_idx = 0; /* index in fixvar[] */
23832 int i;
23833 int ai;
23834 char_u numbuf[NUMBUFLEN];
23835 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023836 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023837#ifdef FEAT_PROFILE
23838 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023839 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841
23842 /* If depth of calling is getting too high, don't execute the function */
23843 if (depth >= p_mfd)
23844 {
23845 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023846 rettv->v_type = VAR_NUMBER;
23847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 return;
23849 }
23850 ++depth;
23851
23852 line_breakcheck(); /* check for CTRL-C hit */
23853
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023854 fc = (funccall_T *)alloc(sizeof(funccall_T));
23855 fc->caller = current_funccal;
23856 current_funccal = fc;
23857 fc->func = fp;
23858 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023859 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023860 fc->linenr = 0;
23861 fc->returned = FALSE;
23862 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023864 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23865 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866
Bram Moolenaar33570922005-01-25 22:26:29 +000023867 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023868 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023869 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23870 * each argument variable and saves a lot of time.
23871 */
23872 /*
23873 * Init l: variables.
23874 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023875 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023876 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023877 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023878 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23879 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023880 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023881 name = v->di_key;
23882 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023883 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023884 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023885 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023886 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023887 v->di_tv.vval.v_dict = selfdict;
23888 ++selfdict->dv_refcount;
23889 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023890
Bram Moolenaar33570922005-01-25 22:26:29 +000023891 /*
23892 * Init a: variables.
23893 * Set a:0 to "argcount".
23894 * Set a:000 to a list with room for the "..." arguments.
23895 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023896 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023897 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023898 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023899 /* Use "name" to avoid a warning from some compiler that checks the
23900 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023901 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023902 name = v->di_key;
23903 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023904 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023905 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023906 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023907 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023908 v->di_tv.vval.v_list = &fc->l_varlist;
23909 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23910 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23911 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023912
23913 /*
23914 * Set a:firstline to "firstline" and a:lastline to "lastline".
23915 * Set a:name to named arguments.
23916 * Set a:N to the "..." arguments.
23917 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023918 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023919 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023920 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023921 (varnumber_T)lastline);
23922 for (i = 0; i < argcount; ++i)
23923 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023924 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023925 if (ai < 0)
23926 /* named argument a:name */
23927 name = FUNCARG(fp, i);
23928 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023929 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023930 /* "..." argument a:1, a:2, etc. */
23931 sprintf((char *)numbuf, "%d", ai + 1);
23932 name = numbuf;
23933 }
23934 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23935 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023936 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023937 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23938 }
23939 else
23940 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023941 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23942 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023943 if (v == NULL)
23944 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023945 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023946 }
23947 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023948 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023949
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023950 /* Note: the values are copied directly to avoid alloc/free.
23951 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023952 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023953 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023954
23955 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23956 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023957 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23958 fc->l_listitems[ai].li_tv = argvars[i];
23959 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023960 }
23961 }
23962
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 /* Don't redraw while executing the function. */
23964 ++RedrawingDisabled;
23965 save_sourcing_name = sourcing_name;
23966 save_sourcing_lnum = sourcing_lnum;
23967 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023968 /* need space for function name + ("function " + 3) or "[number]" */
23969 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23970 + STRLEN(fp->uf_name) + 20;
23971 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 if (sourcing_name != NULL)
23973 {
23974 if (save_sourcing_name != NULL
23975 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023976 sprintf((char *)sourcing_name, "%s[%d]..",
23977 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 else
23979 STRCPY(sourcing_name, "function ");
23980 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23981
23982 if (p_verbose >= 12)
23983 {
23984 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023985 verbose_enter_scroll();
23986
Bram Moolenaar555b2802005-05-19 21:08:39 +000023987 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 if (p_verbose >= 14)
23989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023991 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023992 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023993 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994
23995 msg_puts((char_u *)"(");
23996 for (i = 0; i < argcount; ++i)
23997 {
23998 if (i > 0)
23999 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024000 if (argvars[i].v_type == VAR_NUMBER)
24001 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 else
24003 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024004 /* Do not want errors such as E724 here. */
24005 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024006 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024007 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024008 if (s != NULL)
24009 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024010 if (vim_strsize(s) > MSG_BUF_CLEN)
24011 {
24012 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24013 s = buf;
24014 }
24015 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024016 vim_free(tofree);
24017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 }
24019 }
24020 msg_puts((char_u *)")");
24021 }
24022 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024023
24024 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 --no_wait_return;
24026 }
24027 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024028#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024029 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024030 {
24031 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24032 func_do_profile(fp);
24033 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024034 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024035 {
24036 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024037 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024038 profile_zero(&fp->uf_tm_children);
24039 }
24040 script_prof_save(&wait_start);
24041 }
24042#endif
24043
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024045 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 save_did_emsg = did_emsg;
24047 did_emsg = FALSE;
24048
24049 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024050 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24052
24053 --RedrawingDisabled;
24054
24055 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024056 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024058 clear_tv(rettv);
24059 rettv->v_type = VAR_NUMBER;
24060 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 }
24062
Bram Moolenaar05159a02005-02-26 23:04:13 +000024063#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024064 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024065 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024066 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024067 profile_end(&call_start);
24068 profile_sub_wait(&wait_start, &call_start);
24069 profile_add(&fp->uf_tm_total, &call_start);
24070 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024071 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024072 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024073 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24074 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024075 }
24076 }
24077#endif
24078
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 /* when being verbose, mention the return value */
24080 if (p_verbose >= 12)
24081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024083 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024086 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024087 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024088 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024089 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024092 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024093 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024094 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024095 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024096
Bram Moolenaar555b2802005-05-19 21:08:39 +000024097 /* The value may be very long. Skip the middle part, so that we
24098 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024099 * truncate it at the end. Don't want errors such as E724 here. */
24100 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024101 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024102 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024103 if (s != NULL)
24104 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024105 if (vim_strsize(s) > MSG_BUF_CLEN)
24106 {
24107 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24108 s = buf;
24109 }
24110 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024111 vim_free(tofree);
24112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 }
24114 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024115
24116 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 --no_wait_return;
24118 }
24119
24120 vim_free(sourcing_name);
24121 sourcing_name = save_sourcing_name;
24122 sourcing_lnum = save_sourcing_lnum;
24123 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024124#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024125 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024126 script_prof_restore(&wait_start);
24127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128
24129 if (p_verbose >= 12 && sourcing_name != NULL)
24130 {
24131 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024132 verbose_enter_scroll();
24133
Bram Moolenaar555b2802005-05-19 21:08:39 +000024134 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024136
24137 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 --no_wait_return;
24139 }
24140
24141 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024142 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024144
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024145 /* If the a:000 list and the l: and a: dicts are not referenced we can
24146 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024147 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24148 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24149 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24150 {
24151 free_funccal(fc, FALSE);
24152 }
24153 else
24154 {
24155 hashitem_T *hi;
24156 listitem_T *li;
24157 int todo;
24158
24159 /* "fc" is still in use. This can happen when returning "a:000" or
24160 * assigning "l:" to a global variable.
24161 * Link "fc" in the list for garbage collection later. */
24162 fc->caller = previous_funccal;
24163 previous_funccal = fc;
24164
24165 /* Make a copy of the a: variables, since we didn't do that above. */
24166 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24167 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24168 {
24169 if (!HASHITEM_EMPTY(hi))
24170 {
24171 --todo;
24172 v = HI2DI(hi);
24173 copy_tv(&v->di_tv, &v->di_tv);
24174 }
24175 }
24176
24177 /* Make a copy of the a:000 items, since we didn't do that above. */
24178 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24179 copy_tv(&li->li_tv, &li->li_tv);
24180 }
24181}
24182
24183/*
24184 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024185 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024186 */
24187 static int
24188can_free_funccal(fc, copyID)
24189 funccall_T *fc;
24190 int copyID;
24191{
24192 return (fc->l_varlist.lv_copyID != copyID
24193 && fc->l_vars.dv_copyID != copyID
24194 && fc->l_avars.dv_copyID != copyID);
24195}
24196
24197/*
24198 * Free "fc" and what it contains.
24199 */
24200 static void
24201free_funccal(fc, free_val)
24202 funccall_T *fc;
24203 int free_val; /* a: vars were allocated */
24204{
24205 listitem_T *li;
24206
24207 /* The a: variables typevals may not have been allocated, only free the
24208 * allocated variables. */
24209 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24210
24211 /* free all l: variables */
24212 vars_clear(&fc->l_vars.dv_hashtab);
24213
24214 /* Free the a:000 variables if they were allocated. */
24215 if (free_val)
24216 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24217 clear_tv(&li->li_tv);
24218
24219 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220}
24221
24222/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 * Add a number variable "name" to dict "dp" with value "nr".
24224 */
24225 static void
24226add_nr_var(dp, v, name, nr)
24227 dict_T *dp;
24228 dictitem_T *v;
24229 char *name;
24230 varnumber_T nr;
24231{
24232 STRCPY(v->di_key, name);
24233 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24234 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24235 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024236 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 v->di_tv.vval.v_number = nr;
24238}
24239
24240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 * ":return [expr]"
24242 */
24243 void
24244ex_return(eap)
24245 exarg_T *eap;
24246{
24247 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024248 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 int returning = FALSE;
24250
24251 if (current_funccal == NULL)
24252 {
24253 EMSG(_("E133: :return not inside a function"));
24254 return;
24255 }
24256
24257 if (eap->skip)
24258 ++emsg_skip;
24259
24260 eap->nextcmd = NULL;
24261 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024262 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 {
24264 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024265 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024267 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 }
24269 /* It's safer to return also on error. */
24270 else if (!eap->skip)
24271 {
24272 /*
24273 * Return unless the expression evaluation has been cancelled due to an
24274 * aborting error, an interrupt, or an exception.
24275 */
24276 if (!aborting())
24277 returning = do_return(eap, FALSE, TRUE, NULL);
24278 }
24279
24280 /* When skipping or the return gets pending, advance to the next command
24281 * in this line (!returning). Otherwise, ignore the rest of the line.
24282 * Following lines will be ignored by get_func_line(). */
24283 if (returning)
24284 eap->nextcmd = NULL;
24285 else if (eap->nextcmd == NULL) /* no argument */
24286 eap->nextcmd = check_nextcmd(arg);
24287
24288 if (eap->skip)
24289 --emsg_skip;
24290}
24291
24292/*
24293 * Return from a function. Possibly makes the return pending. Also called
24294 * for a pending return at the ":endtry" or after returning from an extra
24295 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024296 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024297 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 * FALSE when the return gets pending.
24299 */
24300 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024301do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 exarg_T *eap;
24303 int reanimate;
24304 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024305 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306{
24307 int idx;
24308 struct condstack *cstack = eap->cstack;
24309
24310 if (reanimate)
24311 /* Undo the return. */
24312 current_funccal->returned = FALSE;
24313
24314 /*
24315 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24316 * not in its finally clause (which then is to be executed next) is found.
24317 * In this case, make the ":return" pending for execution at the ":endtry".
24318 * Otherwise, return normally.
24319 */
24320 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24321 if (idx >= 0)
24322 {
24323 cstack->cs_pending[idx] = CSTP_RETURN;
24324
24325 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024326 /* A pending return again gets pending. "rettv" points to an
24327 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024329 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 else
24331 {
24332 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024333 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024335 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024337 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 {
24339 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024340 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024341 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 else
24343 EMSG(_(e_outofmem));
24344 }
24345 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024346 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347
24348 if (reanimate)
24349 {
24350 /* The pending return value could be overwritten by a ":return"
24351 * without argument in a finally clause; reset the default
24352 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024353 current_funccal->rettv->v_type = VAR_NUMBER;
24354 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 }
24356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024357 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 }
24359 else
24360 {
24361 current_funccal->returned = TRUE;
24362
24363 /* If the return is carried out now, store the return value. For
24364 * a return immediately after reanimation, the value is already
24365 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024366 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024368 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024369 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024371 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 }
24373 }
24374
24375 return idx < 0;
24376}
24377
24378/*
24379 * Free the variable with a pending return value.
24380 */
24381 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024382discard_pending_return(rettv)
24383 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384{
Bram Moolenaar33570922005-01-25 22:26:29 +000024385 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386}
24387
24388/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024389 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390 * is an allocated string. Used by report_pending() for verbose messages.
24391 */
24392 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024393get_return_cmd(rettv)
24394 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024396 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024397 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024398 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024400 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024401 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024402 if (s == NULL)
24403 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024404
24405 STRCPY(IObuff, ":return ");
24406 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24407 if (STRLEN(s) + 8 >= IOSIZE)
24408 STRCPY(IObuff + IOSIZE - 4, "...");
24409 vim_free(tofree);
24410 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411}
24412
24413/*
24414 * Get next function line.
24415 * Called by do_cmdline() to get the next line.
24416 * Returns allocated string, or NULL for end of function.
24417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 char_u *
24419get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024420 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024422 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423{
Bram Moolenaar33570922005-01-25 22:26:29 +000024424 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024425 ufunc_T *fp = fcp->func;
24426 char_u *retval;
24427 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428
24429 /* If breakpoints have been added/deleted need to check for it. */
24430 if (fcp->dbg_tick != debug_tick)
24431 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024432 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433 sourcing_lnum);
24434 fcp->dbg_tick = debug_tick;
24435 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024436#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024437 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024438 func_line_end(cookie);
24439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440
Bram Moolenaar05159a02005-02-26 23:04:13 +000024441 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024442 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24443 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 retval = NULL;
24445 else
24446 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024447 /* Skip NULL lines (continuation lines). */
24448 while (fcp->linenr < gap->ga_len
24449 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24450 ++fcp->linenr;
24451 if (fcp->linenr >= gap->ga_len)
24452 retval = NULL;
24453 else
24454 {
24455 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24456 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024457#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024458 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024459 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024460#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 }
24463
24464 /* Did we encounter a breakpoint? */
24465 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24466 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024467 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024469 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 sourcing_lnum);
24471 fcp->dbg_tick = debug_tick;
24472 }
24473
24474 return retval;
24475}
24476
Bram Moolenaar05159a02005-02-26 23:04:13 +000024477#if defined(FEAT_PROFILE) || defined(PROTO)
24478/*
24479 * Called when starting to read a function line.
24480 * "sourcing_lnum" must be correct!
24481 * When skipping lines it may not actually be executed, but we won't find out
24482 * until later and we need to store the time now.
24483 */
24484 void
24485func_line_start(cookie)
24486 void *cookie;
24487{
24488 funccall_T *fcp = (funccall_T *)cookie;
24489 ufunc_T *fp = fcp->func;
24490
24491 if (fp->uf_profiling && sourcing_lnum >= 1
24492 && sourcing_lnum <= fp->uf_lines.ga_len)
24493 {
24494 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024495 /* Skip continuation lines. */
24496 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24497 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024498 fp->uf_tml_execed = FALSE;
24499 profile_start(&fp->uf_tml_start);
24500 profile_zero(&fp->uf_tml_children);
24501 profile_get_wait(&fp->uf_tml_wait);
24502 }
24503}
24504
24505/*
24506 * Called when actually executing a function line.
24507 */
24508 void
24509func_line_exec(cookie)
24510 void *cookie;
24511{
24512 funccall_T *fcp = (funccall_T *)cookie;
24513 ufunc_T *fp = fcp->func;
24514
24515 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24516 fp->uf_tml_execed = TRUE;
24517}
24518
24519/*
24520 * Called when done with a function line.
24521 */
24522 void
24523func_line_end(cookie)
24524 void *cookie;
24525{
24526 funccall_T *fcp = (funccall_T *)cookie;
24527 ufunc_T *fp = fcp->func;
24528
24529 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24530 {
24531 if (fp->uf_tml_execed)
24532 {
24533 ++fp->uf_tml_count[fp->uf_tml_idx];
24534 profile_end(&fp->uf_tml_start);
24535 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024536 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024537 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24538 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024539 }
24540 fp->uf_tml_idx = -1;
24541 }
24542}
24543#endif
24544
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545/*
24546 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024547 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 */
24549 int
24550func_has_ended(cookie)
24551 void *cookie;
24552{
Bram Moolenaar33570922005-01-25 22:26:29 +000024553 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554
24555 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24556 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024557 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 || fcp->returned);
24559}
24560
24561/*
24562 * return TRUE if cookie indicates a function which "abort"s on errors.
24563 */
24564 int
24565func_has_abort(cookie)
24566 void *cookie;
24567{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024568 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569}
24570
24571#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24572typedef enum
24573{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024574 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24575 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24576 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577} var_flavour_T;
24578
24579static var_flavour_T var_flavour __ARGS((char_u *varname));
24580
24581 static var_flavour_T
24582var_flavour(varname)
24583 char_u *varname;
24584{
24585 char_u *p = varname;
24586
24587 if (ASCII_ISUPPER(*p))
24588 {
24589 while (*(++p))
24590 if (ASCII_ISLOWER(*p))
24591 return VAR_FLAVOUR_SESSION;
24592 return VAR_FLAVOUR_VIMINFO;
24593 }
24594 else
24595 return VAR_FLAVOUR_DEFAULT;
24596}
24597#endif
24598
24599#if defined(FEAT_VIMINFO) || defined(PROTO)
24600/*
24601 * Restore global vars that start with a capital from the viminfo file
24602 */
24603 int
24604read_viminfo_varlist(virp, writing)
24605 vir_T *virp;
24606 int writing;
24607{
24608 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024609 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024610 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611
24612 if (!writing && (find_viminfo_parameter('!') != NULL))
24613 {
24614 tab = vim_strchr(virp->vir_line + 1, '\t');
24615 if (tab != NULL)
24616 {
24617 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024618 switch (*tab)
24619 {
24620 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024621#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024622 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024623#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024624 case 'D': type = VAR_DICT; break;
24625 case 'L': type = VAR_LIST; break;
24626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627
24628 tab = vim_strchr(tab, '\t');
24629 if (tab != NULL)
24630 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024631 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024632 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024633 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024635#ifdef FEAT_FLOAT
24636 else if (type == VAR_FLOAT)
24637 (void)string2float(tab + 1, &tv.vval.v_float);
24638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024640 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024641 if (type == VAR_DICT || type == VAR_LIST)
24642 {
24643 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24644
24645 if (etv == NULL)
24646 /* Failed to parse back the dict or list, use it as a
24647 * string. */
24648 tv.v_type = VAR_STRING;
24649 else
24650 {
24651 vim_free(tv.vval.v_string);
24652 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024653 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024654 }
24655 }
24656
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024657 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024658
24659 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024660 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024661 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24662 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 }
24664 }
24665 }
24666
24667 return viminfo_readline(virp);
24668}
24669
24670/*
24671 * Write global vars that start with a capital to the viminfo file
24672 */
24673 void
24674write_viminfo_varlist(fp)
24675 FILE *fp;
24676{
Bram Moolenaar33570922005-01-25 22:26:29 +000024677 hashitem_T *hi;
24678 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024679 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024680 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024681 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024682 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024683 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684
24685 if (find_viminfo_parameter('!') == NULL)
24686 return;
24687
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024688 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024690 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024691 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024693 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024695 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024696 this_var = HI2DI(hi);
24697 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024698 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024699 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024700 {
24701 case VAR_STRING: s = "STR"; break;
24702 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024703#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024704 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024705#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024706 case VAR_DICT: s = "DIC"; break;
24707 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024708 default: continue;
24709 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024710 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024711 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024712 if (p != NULL)
24713 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024714 vim_free(tofree);
24715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 }
24717 }
24718}
24719#endif
24720
24721#if defined(FEAT_SESSION) || defined(PROTO)
24722 int
24723store_session_globals(fd)
24724 FILE *fd;
24725{
Bram Moolenaar33570922005-01-25 22:26:29 +000024726 hashitem_T *hi;
24727 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024728 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 char_u *p, *t;
24730
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024731 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024732 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024734 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024736 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024737 this_var = HI2DI(hi);
24738 if ((this_var->di_tv.v_type == VAR_NUMBER
24739 || this_var->di_tv.v_type == VAR_STRING)
24740 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024741 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024742 /* Escape special characters with a backslash. Turn a LF and
24743 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024744 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024745 (char_u *)"\\\"\n\r");
24746 if (p == NULL) /* out of memory */
24747 break;
24748 for (t = p; *t != NUL; ++t)
24749 if (*t == '\n')
24750 *t = 'n';
24751 else if (*t == '\r')
24752 *t = 'r';
24753 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024754 this_var->di_key,
24755 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24756 : ' ',
24757 p,
24758 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24759 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024760 || put_eol(fd) == FAIL)
24761 {
24762 vim_free(p);
24763 return FAIL;
24764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 vim_free(p);
24766 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024767#ifdef FEAT_FLOAT
24768 else if (this_var->di_tv.v_type == VAR_FLOAT
24769 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24770 {
24771 float_T f = this_var->di_tv.vval.v_float;
24772 int sign = ' ';
24773
24774 if (f < 0)
24775 {
24776 f = -f;
24777 sign = '-';
24778 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024779 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024780 this_var->di_key, sign, f) < 0)
24781 || put_eol(fd) == FAIL)
24782 return FAIL;
24783 }
24784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 }
24786 }
24787 return OK;
24788}
24789#endif
24790
Bram Moolenaar661b1822005-07-28 22:36:45 +000024791/*
24792 * Display script name where an item was last set.
24793 * Should only be invoked when 'verbose' is non-zero.
24794 */
24795 void
24796last_set_msg(scriptID)
24797 scid_T scriptID;
24798{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024799 char_u *p;
24800
Bram Moolenaar661b1822005-07-28 22:36:45 +000024801 if (scriptID != 0)
24802 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024803 p = home_replace_save(NULL, get_scriptname(scriptID));
24804 if (p != NULL)
24805 {
24806 verbose_enter();
24807 MSG_PUTS(_("\n\tLast set from "));
24808 MSG_PUTS(p);
24809 vim_free(p);
24810 verbose_leave();
24811 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024812 }
24813}
24814
Bram Moolenaard812df62008-11-09 12:46:09 +000024815/*
24816 * List v:oldfiles in a nice way.
24817 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024818 void
24819ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024820 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024821{
24822 list_T *l = vimvars[VV_OLDFILES].vv_list;
24823 listitem_T *li;
24824 int nr = 0;
24825
24826 if (l == NULL)
24827 msg((char_u *)_("No old files"));
24828 else
24829 {
24830 msg_start();
24831 msg_scroll = TRUE;
24832 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24833 {
24834 msg_outnum((long)++nr);
24835 MSG_PUTS(": ");
24836 msg_outtrans(get_tv_string(&li->li_tv));
24837 msg_putchar('\n');
24838 out_flush(); /* output one line at a time */
24839 ui_breakcheck();
24840 }
24841 /* Assume "got_int" was set to truncate the listing. */
24842 got_int = FALSE;
24843
24844#ifdef FEAT_BROWSE_CMD
24845 if (cmdmod.browse)
24846 {
24847 quit_more = FALSE;
24848 nr = prompt_for_number(FALSE);
24849 msg_starthere();
24850 if (nr > 0)
24851 {
24852 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24853 (long)nr);
24854
24855 if (p != NULL)
24856 {
24857 p = expand_env_save(p);
24858 eap->arg = p;
24859 eap->cmdidx = CMD_edit;
24860 cmdmod.browse = FALSE;
24861 do_exedit(eap, NULL);
24862 vim_free(p);
24863 }
24864 }
24865 }
24866#endif
24867 }
24868}
24869
Bram Moolenaar53744302015-07-17 17:38:22 +020024870/* reset v:option_new, v:option_old and v:option_type */
24871 void
24872reset_v_option_vars()
24873{
24874 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24875 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24876 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24877}
24878
24879
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880#endif /* FEAT_EVAL */
24881
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024883#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884
24885#ifdef WIN3264
24886/*
24887 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24888 */
24889static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24890static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24891static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24892
24893/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024894 * Get the short path (8.3) for the filename in "fnamep".
24895 * Only works for a valid file name.
24896 * When the path gets longer "fnamep" is changed and the allocated buffer
24897 * is put in "bufp".
24898 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24899 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 */
24901 static int
24902get_short_pathname(fnamep, bufp, fnamelen)
24903 char_u **fnamep;
24904 char_u **bufp;
24905 int *fnamelen;
24906{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024907 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 char_u *newbuf;
24909
24910 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 l = GetShortPathName(*fnamep, *fnamep, len);
24912 if (l > len - 1)
24913 {
24914 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024915 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 newbuf = vim_strnsave(*fnamep, l);
24917 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024918 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919
24920 vim_free(*bufp);
24921 *fnamep = *bufp = newbuf;
24922
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024923 /* Really should always succeed, as the buffer is big enough. */
24924 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 }
24926
24927 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024928 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929}
24930
24931/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024932 * Get the short path (8.3) for the filename in "fname". The converted
24933 * path is returned in "bufp".
24934 *
24935 * Some of the directories specified in "fname" may not exist. This function
24936 * will shorten the existing directories at the beginning of the path and then
24937 * append the remaining non-existing path.
24938 *
24939 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024940 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024941 * bufp - Pointer to an allocated buffer for the filename.
24942 * fnamelen - Length of the filename pointed to by fname
24943 *
24944 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945 */
24946 static int
24947shortpath_for_invalid_fname(fname, bufp, fnamelen)
24948 char_u **fname;
24949 char_u **bufp;
24950 int *fnamelen;
24951{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024952 char_u *short_fname, *save_fname, *pbuf_unused;
24953 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024955 int old_len, len;
24956 int new_len, sfx_len;
24957 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958
24959 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024960 old_len = *fnamelen;
24961 save_fname = vim_strnsave(*fname, old_len);
24962 pbuf_unused = NULL;
24963 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024965 endp = save_fname + old_len - 1; /* Find the end of the copy */
24966 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024968 /*
24969 * Try shortening the supplied path till it succeeds by removing one
24970 * directory at a time from the tail of the path.
24971 */
24972 len = 0;
24973 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024975 /* go back one path-separator */
24976 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24977 --endp;
24978 if (endp <= save_fname)
24979 break; /* processed the complete path */
24980
24981 /*
24982 * Replace the path separator with a NUL and try to shorten the
24983 * resulting path.
24984 */
24985 ch = *endp;
24986 *endp = 0;
24987 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024988 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024989 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24990 {
24991 retval = FAIL;
24992 goto theend;
24993 }
24994 *endp = ch; /* preserve the string */
24995
24996 if (len > 0)
24997 break; /* successfully shortened the path */
24998
24999 /* failed to shorten the path. Skip the path separator */
25000 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 }
25002
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025003 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025005 /*
25006 * Succeeded in shortening the path. Now concatenate the shortened
25007 * path with the remaining path at the tail.
25008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025010 /* Compute the length of the new path. */
25011 sfx_len = (int)(save_endp - endp) + 1;
25012 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025014 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025016 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025018 /* There is not enough space in the currently allocated string,
25019 * copy it to a buffer big enough. */
25020 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025022 {
25023 retval = FAIL;
25024 goto theend;
25025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026 }
25027 else
25028 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025029 /* Transfer short_fname to the main buffer (it's big enough),
25030 * unless get_short_pathname() did its work in-place. */
25031 *fname = *bufp = save_fname;
25032 if (short_fname != save_fname)
25033 vim_strncpy(save_fname, short_fname, len);
25034 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025036
25037 /* concat the not-shortened part of the path */
25038 vim_strncpy(*fname + len, endp, sfx_len);
25039 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025041
25042theend:
25043 vim_free(pbuf_unused);
25044 vim_free(save_fname);
25045
25046 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047}
25048
25049/*
25050 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025051 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 */
25053 static int
25054shortpath_for_partial(fnamep, bufp, fnamelen)
25055 char_u **fnamep;
25056 char_u **bufp;
25057 int *fnamelen;
25058{
25059 int sepcount, len, tflen;
25060 char_u *p;
25061 char_u *pbuf, *tfname;
25062 int hasTilde;
25063
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025064 /* Count up the path separators from the RHS.. so we know which part
25065 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025067 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 if (vim_ispathsep(*p))
25069 ++sepcount;
25070
25071 /* Need full path first (use expand_env() to remove a "~/") */
25072 hasTilde = (**fnamep == '~');
25073 if (hasTilde)
25074 pbuf = tfname = expand_env_save(*fnamep);
25075 else
25076 pbuf = tfname = FullName_save(*fnamep, FALSE);
25077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025078 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025080 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25081 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082
25083 if (len == 0)
25084 {
25085 /* Don't have a valid filename, so shorten the rest of the
25086 * path if we can. This CAN give us invalid 8.3 filenames, but
25087 * there's not a lot of point in guessing what it might be.
25088 */
25089 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025090 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25091 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 }
25093
25094 /* Count the paths backward to find the beginning of the desired string. */
25095 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025096 {
25097#ifdef FEAT_MBYTE
25098 if (has_mbyte)
25099 p -= mb_head_off(tfname, p);
25100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 if (vim_ispathsep(*p))
25102 {
25103 if (sepcount == 0 || (hasTilde && sepcount == 1))
25104 break;
25105 else
25106 sepcount --;
25107 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 if (hasTilde)
25110 {
25111 --p;
25112 if (p >= tfname)
25113 *p = '~';
25114 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 }
25117 else
25118 ++p;
25119
25120 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25121 vim_free(*bufp);
25122 *fnamelen = (int)STRLEN(p);
25123 *bufp = pbuf;
25124 *fnamep = p;
25125
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025126 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025127}
25128#endif /* WIN3264 */
25129
25130/*
25131 * Adjust a filename, according to a string of modifiers.
25132 * *fnamep must be NUL terminated when called. When returning, the length is
25133 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025134 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 * When there is an error, *fnamep is set to NULL.
25136 */
25137 int
25138modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25139 char_u *src; /* string with modifiers */
25140 int *usedlen; /* characters after src that are used */
25141 char_u **fnamep; /* file name so far */
25142 char_u **bufp; /* buffer for allocated file name or NULL */
25143 int *fnamelen; /* length of fnamep */
25144{
25145 int valid = 0;
25146 char_u *tail;
25147 char_u *s, *p, *pbuf;
25148 char_u dirname[MAXPATHL];
25149 int c;
25150 int has_fullname = 0;
25151#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025152 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 int has_shortname = 0;
25154#endif
25155
25156repeat:
25157 /* ":p" - full path/file_name */
25158 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25159 {
25160 has_fullname = 1;
25161
25162 valid |= VALID_PATH;
25163 *usedlen += 2;
25164
25165 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25166 if ((*fnamep)[0] == '~'
25167#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25168 && ((*fnamep)[1] == '/'
25169# ifdef BACKSLASH_IN_FILENAME
25170 || (*fnamep)[1] == '\\'
25171# endif
25172 || (*fnamep)[1] == NUL)
25173
25174#endif
25175 )
25176 {
25177 *fnamep = expand_env_save(*fnamep);
25178 vim_free(*bufp); /* free any allocated file name */
25179 *bufp = *fnamep;
25180 if (*fnamep == NULL)
25181 return -1;
25182 }
25183
25184 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025185 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186 {
25187 if (vim_ispathsep(*p)
25188 && p[1] == '.'
25189 && (p[2] == NUL
25190 || vim_ispathsep(p[2])
25191 || (p[2] == '.'
25192 && (p[3] == NUL || vim_ispathsep(p[3])))))
25193 break;
25194 }
25195
25196 /* FullName_save() is slow, don't use it when not needed. */
25197 if (*p != NUL || !vim_isAbsName(*fnamep))
25198 {
25199 *fnamep = FullName_save(*fnamep, *p != NUL);
25200 vim_free(*bufp); /* free any allocated file name */
25201 *bufp = *fnamep;
25202 if (*fnamep == NULL)
25203 return -1;
25204 }
25205
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025206#ifdef WIN3264
25207# if _WIN32_WINNT >= 0x0500
25208 if (vim_strchr(*fnamep, '~') != NULL)
25209 {
25210 /* Expand 8.3 filename to full path. Needed to make sure the same
25211 * file does not have two different names.
25212 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25213 p = alloc(_MAX_PATH + 1);
25214 if (p != NULL)
25215 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025216 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025217 {
25218 vim_free(*bufp);
25219 *bufp = *fnamep = p;
25220 }
25221 else
25222 vim_free(p);
25223 }
25224 }
25225# endif
25226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 /* Append a path separator to a directory. */
25228 if (mch_isdir(*fnamep))
25229 {
25230 /* Make room for one or two extra characters. */
25231 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25232 vim_free(*bufp); /* free any allocated file name */
25233 *bufp = *fnamep;
25234 if (*fnamep == NULL)
25235 return -1;
25236 add_pathsep(*fnamep);
25237 }
25238 }
25239
25240 /* ":." - path relative to the current directory */
25241 /* ":~" - path relative to the home directory */
25242 /* ":8" - shortname path - postponed till after */
25243 while (src[*usedlen] == ':'
25244 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25245 {
25246 *usedlen += 2;
25247 if (c == '8')
25248 {
25249#ifdef WIN3264
25250 has_shortname = 1; /* Postpone this. */
25251#endif
25252 continue;
25253 }
25254 pbuf = NULL;
25255 /* Need full path first (use expand_env() to remove a "~/") */
25256 if (!has_fullname)
25257 {
25258 if (c == '.' && **fnamep == '~')
25259 p = pbuf = expand_env_save(*fnamep);
25260 else
25261 p = pbuf = FullName_save(*fnamep, FALSE);
25262 }
25263 else
25264 p = *fnamep;
25265
25266 has_fullname = 0;
25267
25268 if (p != NULL)
25269 {
25270 if (c == '.')
25271 {
25272 mch_dirname(dirname, MAXPATHL);
25273 s = shorten_fname(p, dirname);
25274 if (s != NULL)
25275 {
25276 *fnamep = s;
25277 if (pbuf != NULL)
25278 {
25279 vim_free(*bufp); /* free any allocated file name */
25280 *bufp = pbuf;
25281 pbuf = NULL;
25282 }
25283 }
25284 }
25285 else
25286 {
25287 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25288 /* Only replace it when it starts with '~' */
25289 if (*dirname == '~')
25290 {
25291 s = vim_strsave(dirname);
25292 if (s != NULL)
25293 {
25294 *fnamep = s;
25295 vim_free(*bufp);
25296 *bufp = s;
25297 }
25298 }
25299 }
25300 vim_free(pbuf);
25301 }
25302 }
25303
25304 tail = gettail(*fnamep);
25305 *fnamelen = (int)STRLEN(*fnamep);
25306
25307 /* ":h" - head, remove "/file_name", can be repeated */
25308 /* Don't remove the first "/" or "c:\" */
25309 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25310 {
25311 valid |= VALID_HEAD;
25312 *usedlen += 2;
25313 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025314 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025315 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 *fnamelen = (int)(tail - *fnamep);
25317#ifdef VMS
25318 if (*fnamelen > 0)
25319 *fnamelen += 1; /* the path separator is part of the path */
25320#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025321 if (*fnamelen == 0)
25322 {
25323 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25324 p = vim_strsave((char_u *)".");
25325 if (p == NULL)
25326 return -1;
25327 vim_free(*bufp);
25328 *bufp = *fnamep = tail = p;
25329 *fnamelen = 1;
25330 }
25331 else
25332 {
25333 while (tail > s && !after_pathsep(s, tail))
25334 mb_ptr_back(*fnamep, tail);
25335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 }
25337
25338 /* ":8" - shortname */
25339 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25340 {
25341 *usedlen += 2;
25342#ifdef WIN3264
25343 has_shortname = 1;
25344#endif
25345 }
25346
25347#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025348 /*
25349 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 */
25351 if (has_shortname)
25352 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025353 /* Copy the string if it is shortened by :h and when it wasn't copied
25354 * yet, because we are going to change it in place. Avoids changing
25355 * the buffer name for "%:8". */
25356 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 {
25358 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025359 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 return -1;
25361 vim_free(*bufp);
25362 *bufp = *fnamep = p;
25363 }
25364
25365 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025366 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 if (!has_fullname && !vim_isAbsName(*fnamep))
25368 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025369 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 return -1;
25371 }
25372 else
25373 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025374 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375
Bram Moolenaardc935552011-08-17 15:23:23 +020025376 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025378 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 return -1;
25380
25381 if (l == 0)
25382 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025383 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025385 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386 return -1;
25387 }
25388 *fnamelen = l;
25389 }
25390 }
25391#endif /* WIN3264 */
25392
25393 /* ":t" - tail, just the basename */
25394 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25395 {
25396 *usedlen += 2;
25397 *fnamelen -= (int)(tail - *fnamep);
25398 *fnamep = tail;
25399 }
25400
25401 /* ":e" - extension, can be repeated */
25402 /* ":r" - root, without extension, can be repeated */
25403 while (src[*usedlen] == ':'
25404 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25405 {
25406 /* find a '.' in the tail:
25407 * - for second :e: before the current fname
25408 * - otherwise: The last '.'
25409 */
25410 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25411 s = *fnamep - 2;
25412 else
25413 s = *fnamep + *fnamelen - 1;
25414 for ( ; s > tail; --s)
25415 if (s[0] == '.')
25416 break;
25417 if (src[*usedlen + 1] == 'e') /* :e */
25418 {
25419 if (s > tail)
25420 {
25421 *fnamelen += (int)(*fnamep - (s + 1));
25422 *fnamep = s + 1;
25423#ifdef VMS
25424 /* cut version from the extension */
25425 s = *fnamep + *fnamelen - 1;
25426 for ( ; s > *fnamep; --s)
25427 if (s[0] == ';')
25428 break;
25429 if (s > *fnamep)
25430 *fnamelen = s - *fnamep;
25431#endif
25432 }
25433 else if (*fnamep <= tail)
25434 *fnamelen = 0;
25435 }
25436 else /* :r */
25437 {
25438 if (s > tail) /* remove one extension */
25439 *fnamelen = (int)(s - *fnamep);
25440 }
25441 *usedlen += 2;
25442 }
25443
25444 /* ":s?pat?foo?" - substitute */
25445 /* ":gs?pat?foo?" - global substitute */
25446 if (src[*usedlen] == ':'
25447 && (src[*usedlen + 1] == 's'
25448 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25449 {
25450 char_u *str;
25451 char_u *pat;
25452 char_u *sub;
25453 int sep;
25454 char_u *flags;
25455 int didit = FALSE;
25456
25457 flags = (char_u *)"";
25458 s = src + *usedlen + 2;
25459 if (src[*usedlen + 1] == 'g')
25460 {
25461 flags = (char_u *)"g";
25462 ++s;
25463 }
25464
25465 sep = *s++;
25466 if (sep)
25467 {
25468 /* find end of pattern */
25469 p = vim_strchr(s, sep);
25470 if (p != NULL)
25471 {
25472 pat = vim_strnsave(s, (int)(p - s));
25473 if (pat != NULL)
25474 {
25475 s = p + 1;
25476 /* find end of substitution */
25477 p = vim_strchr(s, sep);
25478 if (p != NULL)
25479 {
25480 sub = vim_strnsave(s, (int)(p - s));
25481 str = vim_strnsave(*fnamep, *fnamelen);
25482 if (sub != NULL && str != NULL)
25483 {
25484 *usedlen = (int)(p + 1 - src);
25485 s = do_string_sub(str, pat, sub, flags);
25486 if (s != NULL)
25487 {
25488 *fnamep = s;
25489 *fnamelen = (int)STRLEN(s);
25490 vim_free(*bufp);
25491 *bufp = s;
25492 didit = TRUE;
25493 }
25494 }
25495 vim_free(sub);
25496 vim_free(str);
25497 }
25498 vim_free(pat);
25499 }
25500 }
25501 /* after using ":s", repeat all the modifiers */
25502 if (didit)
25503 goto repeat;
25504 }
25505 }
25506
Bram Moolenaar26df0922014-02-23 23:39:13 +010025507 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25508 {
25509 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25510 if (p == NULL)
25511 return -1;
25512 vim_free(*bufp);
25513 *bufp = *fnamep = p;
25514 *fnamelen = (int)STRLEN(p);
25515 *usedlen += 2;
25516 }
25517
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 return valid;
25519}
25520
25521/*
25522 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25523 * "flags" can be "g" to do a global substitute.
25524 * Returns an allocated string, NULL for error.
25525 */
25526 char_u *
25527do_string_sub(str, pat, sub, flags)
25528 char_u *str;
25529 char_u *pat;
25530 char_u *sub;
25531 char_u *flags;
25532{
25533 int sublen;
25534 regmatch_T regmatch;
25535 int i;
25536 int do_all;
25537 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025538 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 garray_T ga;
25540 char_u *ret;
25541 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025542 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543
25544 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25545 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025546 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547
25548 ga_init2(&ga, 1, 200);
25549
25550 do_all = (flags[0] == 'g');
25551
25552 regmatch.rm_ic = p_ic;
25553 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25554 if (regmatch.regprog != NULL)
25555 {
25556 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025557 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25559 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025560 /* Skip empty match except for first match. */
25561 if (regmatch.startp[0] == regmatch.endp[0])
25562 {
25563 if (zero_width == regmatch.startp[0])
25564 {
25565 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025566 i = MB_PTR2LEN(tail);
25567 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25568 (size_t)i);
25569 ga.ga_len += i;
25570 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025571 continue;
25572 }
25573 zero_width = regmatch.startp[0];
25574 }
25575
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 /*
25577 * Get some space for a temporary buffer to do the substitution
25578 * into. It will contain:
25579 * - The text up to where the match is.
25580 * - The substituted text.
25581 * - The text after the match.
25582 */
25583 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025584 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25586 {
25587 ga_clear(&ga);
25588 break;
25589 }
25590
25591 /* copy the text up to where the match is */
25592 i = (int)(regmatch.startp[0] - tail);
25593 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25594 /* add the substituted text */
25595 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25596 + ga.ga_len + i, TRUE, TRUE, FALSE);
25597 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025598 tail = regmatch.endp[0];
25599 if (*tail == NUL)
25600 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601 if (!do_all)
25602 break;
25603 }
25604
25605 if (ga.ga_data != NULL)
25606 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25607
Bram Moolenaar473de612013-06-08 18:19:48 +020025608 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 }
25610
25611 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25612 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025613 if (p_cpo == empty_option)
25614 p_cpo = save_cpo;
25615 else
25616 /* Darn, evaluating {sub} expression changed the value. */
25617 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
25619 return ret;
25620}
25621
25622#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */