blob: f9df76350c3c0356a8844948e501b1bd7fed087d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000367};
368
369/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370#define vv_type vv_di.di_tv.v_type
371#define vv_nr vv_di.di_tv.vval.v_number
372#define vv_float vv_di.di_tv.vval.v_float
373#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000374#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200377static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000378#define vimvarht vimvardict.dv_hashtab
379
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
381static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
383static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
384static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
386static void list_glob_vars __ARGS((int *first));
387static void list_buf_vars __ARGS((int *first));
388static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_vim_vars __ARGS((int *first));
393static void list_script_vars __ARGS((int *first));
394static void list_func_vars __ARGS((int *first));
395static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
397static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100398static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void clear_lval __ARGS((lval_T *lp));
400static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
401static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
403static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
404static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
405static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
406static void item_lock __ARGS((typval_T *tv, int deep, int lock));
407static int tv_islocked __ARGS((typval_T *tv));
408
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
410static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000415static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
416static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000418static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000423static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100425static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
426static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
427static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000428static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
432static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100435static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000437static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200438static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
440static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000448#ifdef FEAT_FLOAT
449static int string2float __ARGS((char_u *text, float_T *value));
450#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
452static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100453static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200455static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000456static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000457static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#ifdef FEAT_FLOAT
460static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100464static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200468static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200471static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100484static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100486static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200505static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
510static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200520static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200522#ifdef FEAT_FLOAT
523static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
524#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000538static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000547static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000549static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000555static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200556static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100578static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
805static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000806static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000809static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002260 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
2811 else if (var_check_ro(lp->ll_di->di_flags, name))
2812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002930 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 {
2932 if (tv_op(&tv, rettv, op) == OK)
2933 set_var(lp->ll_name, &tv, FALSE);
2934 clear_tv(&tv);
2935 }
2936 }
2937 else
2938 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 else if (tv_check_lock(lp->ll_newkey == NULL
2943 ? lp->ll_tv->v_lock
2944 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2945 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 else if (lp->ll_range)
2947 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002948 listitem_T *ll_li = lp->ll_li;
2949 int ll_n1 = lp->ll_n1;
2950
2951 /*
2952 * Check whether any of the list items is locked
2953 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002954 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002955 {
2956 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
2957 return;
2958 ri = ri->li_next;
2959 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2960 break;
2961 ll_li = ll_li->li_next;
2962 ++ll_n1;
2963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /*
2966 * Assign the List values to the list items.
2967 */
2968 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 if (op != NULL && *op != '=')
2971 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2972 else
2973 {
2974 clear_tv(&lp->ll_li->li_tv);
2975 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 ri = ri->li_next;
2978 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2979 break;
2980 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002983 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 ri = NULL;
2986 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 lp->ll_li = lp->ll_li->li_next;
2990 ++lp->ll_n1;
2991 }
2992 if (ri != NULL)
2993 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 else if (lp->ll_empty2
2995 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 : lp->ll_n1 != lp->ll_n2)
2997 EMSG(_("E711: List value has not enough items"));
2998 }
2999 else
3000 {
3001 /*
3002 * Assign to a List or Dictionary item.
3003 */
3004 if (lp->ll_newkey != NULL)
3005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 {
3008 EMSG2(_(e_letwrong), op);
3009 return;
3010 }
3011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003013 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 if (di == NULL)
3015 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3017 {
3018 vim_free(di);
3019 return;
3020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (op != NULL && *op != '=')
3024 {
3025 tv_op(lp->ll_tv, rettv, op);
3026 return;
3027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /*
3032 * Assign the value to the variable or list item.
3033 */
3034 if (copy)
3035 copy_tv(rettv, lp->ll_tv);
3036 else
3037 {
3038 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003039 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 }
3042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043}
3044
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3047 * Returns OK or FAIL.
3048 */
3049 static int
3050tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 typval_T *tv1;
3052 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 char_u *op;
3054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
3059 /* Can't do anything with a Funcref or a Dict on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3061 {
3062 switch (tv1->v_type)
3063 {
3064 case VAR_DICT:
3065 case VAR_FUNC:
3066 break;
3067
3068 case VAR_LIST:
3069 if (*op != '+' || tv2->v_type != VAR_LIST)
3070 break;
3071 /* List += List */
3072 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3073 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3074 return OK;
3075
3076 case VAR_NUMBER:
3077 case VAR_STRING:
3078 if (tv2->v_type == VAR_LIST)
3079 break;
3080 if (*op == '+' || *op == '-')
3081 {
3082 /* nr += nr or nr -= nr*/
3083 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#ifdef FEAT_FLOAT
3085 if (tv2->v_type == VAR_FLOAT)
3086 {
3087 float_T f = n;
3088
3089 if (*op == '+')
3090 f += tv2->vval.v_float;
3091 else
3092 f -= tv2->vval.v_float;
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_FLOAT;
3095 tv1->vval.v_float = f;
3096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#endif
3099 {
3100 if (*op == '+')
3101 n += get_tv_number(tv2);
3102 else
3103 n -= get_tv_number(tv2);
3104 clear_tv(tv1);
3105 tv1->v_type = VAR_NUMBER;
3106 tv1->vval.v_number = n;
3107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 }
3109 else
3110 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (tv2->v_type == VAR_FLOAT)
3112 break;
3113
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 /* str .= str */
3115 s = get_tv_string(tv1);
3116 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_STRING;
3119 tv1->vval.v_string = s;
3120 }
3121 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122
3123#ifdef FEAT_FLOAT
3124 case VAR_FLOAT:
3125 {
3126 float_T f;
3127
3128 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3129 && tv2->v_type != VAR_NUMBER
3130 && tv2->v_type != VAR_STRING))
3131 break;
3132 if (tv2->v_type == VAR_FLOAT)
3133 f = tv2->vval.v_float;
3134 else
3135 f = get_tv_number(tv2);
3136 if (*op == '+')
3137 tv1->vval.v_float += f;
3138 else
3139 tv1->vval.v_float -= f;
3140 }
3141 return OK;
3142#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 }
3144 }
3145
3146 EMSG2(_(e_letwrong), op);
3147 return FAIL;
3148}
3149
3150/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 * Add a watcher to a list.
3152 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003153 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 list_T *l;
3156 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172
3173 lwp = &l->lv_watch;
3174 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3175 {
3176 if (lw == lwrem)
3177 {
3178 *lwp = lw->lw_next;
3179 break;
3180 }
3181 lwp = &lw->lw_next;
3182 }
3183}
3184
3185/*
3186 * Just before removing an item from a list: advance watchers to the next
3187 * item.
3188 */
3189 static void
3190list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 list_T *l;
3192 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 if (lw->lw_item == item)
3198 lw->lw_item = item->li_next;
3199}
3200
3201/*
3202 * Evaluate the expression used in a ":for var in expr" command.
3203 * "arg" points to "var".
3204 * Set "*errp" to TRUE for an error, FALSE otherwise;
3205 * Return a pointer that holds the info. Null when there is an error.
3206 */
3207 void *
3208eval_for_line(arg, errp, nextcmdp, skip)
3209 char_u *arg;
3210 int *errp;
3211 char_u **nextcmdp;
3212 int skip;
3213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 typval_T tv;
3217 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 *errp = TRUE; /* default: there is an error */
3220
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (fi == NULL)
3223 return NULL;
3224
3225 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3226 if (expr == NULL)
3227 return fi;
3228
3229 expr = skipwhite(expr);
3230 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3231 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003232 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 return fi;
3234 }
3235
3236 if (skip)
3237 ++emsg_skip;
3238 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3239 {
3240 *errp = FALSE;
3241 if (!skip)
3242 {
3243 l = tv.vval.v_list;
3244 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 clear_tv(&tv);
3248 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 else
3250 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003251 /* No need to increment the refcount, it's already set for the
3252 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 fi->fi_list = l;
3254 list_add_watch(l, &fi->fi_lw);
3255 fi->fi_lw.lw_item = l->lv_first;
3256 }
3257 }
3258 }
3259 if (skip)
3260 --emsg_skip;
3261
3262 return fi;
3263}
3264
3265/*
3266 * Use the first item in a ":for" list. Advance to the next.
3267 * Assign the values to the variable (list). "arg" points to the first one.
3268 * Return TRUE when a valid item was found, FALSE when at end of list or
3269 * something wrong.
3270 */
3271 int
3272next_for_item(fi_void, arg)
3273 void *fi_void;
3274 char_u *arg;
3275{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003276 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 item = fi->fi_lw.lw_item;
3281 if (item == NULL)
3282 result = FALSE;
3283 else
3284 {
3285 fi->fi_lw.lw_item = item->li_next;
3286 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3287 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3288 }
3289 return result;
3290}
3291
3292/*
3293 * Free the structure used to store info used by ":for".
3294 */
3295 void
3296free_for_info(fi_void)
3297 void *fi_void;
3298{
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003301 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003304 list_unref(fi->fi_list);
3305 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 vim_free(fi);
3307}
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3310
3311 void
3312set_context_for_expression(xp, arg, cmdidx)
3313 expand_T *xp;
3314 char_u *arg;
3315 cmdidx_T cmdidx;
3316{
3317 int got_eq = FALSE;
3318 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 if (cmdidx == CMD_let)
3322 {
3323 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003324 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 {
3326 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003327 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 {
3329 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 if (vim_iswhite(*p))
3332 break;
3333 }
3334 return;
3335 }
3336 }
3337 else
3338 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3339 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 while ((xp->xp_pattern = vim_strpbrk(arg,
3341 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3342 {
3343 c = *xp->xp_pattern;
3344 if (c == '&')
3345 {
3346 c = xp->xp_pattern[1];
3347 if (c == '&')
3348 {
3349 ++xp->xp_pattern;
3350 xp->xp_context = cmdidx != CMD_let || got_eq
3351 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3352 }
3353 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003356 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3357 xp->xp_pattern += 2;
3358
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 else if (c == '$')
3362 {
3363 /* environment variable */
3364 xp->xp_context = EXPAND_ENV_VARS;
3365 }
3366 else if (c == '=')
3367 {
3368 got_eq = TRUE;
3369 xp->xp_context = EXPAND_EXPRESSION;
3370 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003371 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 && xp->xp_context == EXPAND_FUNCTIONS
3373 && vim_strchr(xp->xp_pattern, '(') == NULL)
3374 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003375 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 break;
3377 }
3378 else if (cmdidx != CMD_let || got_eq)
3379 {
3380 if (c == '"') /* string */
3381 {
3382 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3383 if (c == '\\' && xp->xp_pattern[1] != NUL)
3384 ++xp->xp_pattern;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '\'') /* literal string */
3388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3391 /* skip */ ;
3392 xp->xp_context = EXPAND_NOTHING;
3393 }
3394 else if (c == '|')
3395 {
3396 if (xp->xp_pattern[1] == '|')
3397 {
3398 ++xp->xp_pattern;
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
3401 else
3402 xp->xp_context = EXPAND_COMMANDS;
3403 }
3404 else
3405 xp->xp_context = EXPAND_EXPRESSION;
3406 }
3407 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 /* Doesn't look like something valid, expand as an expression
3409 * anyway. */
3410 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 arg = xp->xp_pattern;
3412 if (*arg != NUL)
3413 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3414 /* skip */ ;
3415 }
3416 xp->xp_pattern = arg;
3417}
3418
3419#endif /* FEAT_CMDL_COMPL */
3420
3421/*
3422 * ":1,25call func(arg1, arg2)" function call.
3423 */
3424 void
3425ex_call(eap)
3426 exarg_T *eap;
3427{
3428 char_u *arg = eap->arg;
3429 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 linenr_T lnum;
3435 int doesrange;
3436 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003439 if (eap->skip)
3440 {
3441 /* trans_function_name() doesn't work well when skipping, use eval0()
3442 * instead to skip to any following command, e.g. for:
3443 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003444 ++emsg_skip;
3445 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3446 clear_tv(&rettv);
3447 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003448 return;
3449 }
3450
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003451 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003452 if (fudi.fd_newkey != NULL)
3453 {
3454 /* Still need to give an error message for missing key. */
3455 EMSG2(_(e_dictkey), fudi.fd_newkey);
3456 vim_free(fudi.fd_newkey);
3457 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 if (tofree == NULL)
3459 return;
3460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 /* Increase refcount on dictionary, it could get deleted when evaluating
3462 * the arguments. */
3463 if (fudi.fd_dict != NULL)
3464 ++fudi.fd_dict->dv_refcount;
3465
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003468 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar532c7802005-01-27 14:44:31 +00003470 /* Skip white space to allow ":call func ()". Not good, but required for
3471 * backward compatibility. */
3472 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 if (*startarg != '(')
3476 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003477 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 goto end;
3479 }
3480
3481 /*
3482 * When skipping, evaluate the function once, to find the end of the
3483 * arguments.
3484 * When the function takes a range, this is discovered after the first
3485 * call, and the loop is broken.
3486 */
3487 if (eap->skip)
3488 {
3489 ++emsg_skip;
3490 lnum = eap->line2; /* do it once, also with an invalid range */
3491 }
3492 else
3493 lnum = eap->line1;
3494 for ( ; lnum <= eap->line2; ++lnum)
3495 {
3496 if (!eap->skip && eap->addr_count > 0)
3497 {
3498 curwin->w_cursor.lnum = lnum;
3499 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003500#ifdef FEAT_VIRTUALEDIT
3501 curwin->w_cursor.coladd = 0;
3502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003505 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003506 eap->line1, eap->line2, &doesrange,
3507 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 failed = TRUE;
3510 break;
3511 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003512
3513 /* Handle a function returning a Funcref, Dictionary or List. */
3514 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3515 {
3516 failed = TRUE;
3517 break;
3518 }
3519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (doesrange || eap->skip)
3522 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003525 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003527 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (aborting())
3529 break;
3530 }
3531 if (eap->skip)
3532 --emsg_skip;
3533
3534 if (!failed)
3535 {
3536 /* Check for trailing illegal characters and a following command. */
3537 if (!ends_excmd(*arg))
3538 {
3539 emsg_severe = TRUE;
3540 EMSG(_(e_trailing));
3541 }
3542 else
3543 eap->nextcmd = check_nextcmd(arg);
3544 }
3545
3546end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003547 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003548 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
3551/*
3552 * ":unlet[!] var1 ... " command.
3553 */
3554 void
3555ex_unlet(eap)
3556 exarg_T *eap;
3557{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558 ex_unletlock(eap, eap->arg, 0);
3559}
3560
3561/*
3562 * ":lockvar" and ":unlockvar" commands
3563 */
3564 void
3565ex_lockvar(eap)
3566 exarg_T *eap;
3567{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int deep = 2;
3570
3571 if (eap->forceit)
3572 deep = -1;
3573 else if (vim_isdigit(*arg))
3574 {
3575 deep = getdigits(&arg);
3576 arg = skipwhite(arg);
3577 }
3578
3579 ex_unletlock(eap, arg, deep);
3580}
3581
3582/*
3583 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3584 */
3585 static void
3586ex_unletlock(eap, argstart, deep)
3587 exarg_T *eap;
3588 char_u *argstart;
3589 int deep;
3590{
3591 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 do
3597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003599 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003600 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 if (lv.ll_name == NULL)
3602 error = TRUE; /* error but continue parsing */
3603 if (name_end == NULL || (!vim_iswhite(*name_end)
3604 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (name_end != NULL)
3607 {
3608 emsg_severe = TRUE;
3609 EMSG(_(e_trailing));
3610 }
3611 if (!(eap->skip || error))
3612 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614 }
3615
3616 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 {
3618 if (eap->cmdidx == CMD_unlet)
3619 {
3620 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3621 error = TRUE;
3622 }
3623 else
3624 {
3625 if (do_lock_var(&lv, name_end, deep,
3626 eap->cmdidx == CMD_lockvar) == FAIL)
3627 error = TRUE;
3628 }
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (!eap->skip)
3632 clear_lval(&lv);
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 arg = skipwhite(name_end);
3635 } while (!ends_excmd(*arg));
3636
3637 eap->nextcmd = check_nextcmd(arg);
3638}
3639
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 int forceit;
3645{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 int ret = OK;
3647 int cc;
3648
3649 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 cc = *name_end;
3652 *name_end = NUL;
3653
3654 /* Normal name or expanded name. */
3655 if (check_changedtick(lp->ll_name))
3656 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003661 else if ((lp->ll_list != NULL
3662 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name))
3663 || (lp->ll_dict != NULL
3664 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 else if (lp->ll_range)
3667 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003669 listitem_T *ll_li = lp->ll_li;
3670 int ll_n1 = lp->ll_n1;
3671
3672 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3673 {
3674 li = ll_li->li_next;
3675 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
3676 return FAIL;
3677 ll_li = li;
3678 ++ll_n1;
3679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680
3681 /* Delete a range of List items. */
3682 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3683 {
3684 li = lp->ll_li->li_next;
3685 listitem_remove(lp->ll_list, lp->ll_li);
3686 lp->ll_li = li;
3687 ++lp->ll_n1;
3688 }
3689 }
3690 else
3691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 /* unlet a List item. */
3694 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003696 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003697 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 }
3699
3700 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003701}
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
3704 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
3707 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003708do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 hashtab_T *ht;
3713 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003714 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003715 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003716 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 ht = find_var_ht(name, &varname);
3719 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003721 if (ht == &globvarht)
3722 d = &globvardict;
3723 else if (current_funccal != NULL
3724 && ht == &current_funccal->l_vars.dv_hashtab)
3725 d = &current_funccal->l_vars;
3726 else
3727 {
3728 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3729 d = di->di_tv.vval.v_dict;
3730 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hi = hash_find(ht, varname);
3732 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 di = HI2DI(hi);
3735 if (var_check_fixed(di->di_flags, name)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003736 || var_check_ro(di->di_flags, name)
3737 || tv_check_lock(d->dv_lock, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 return FAIL;
3739 delete_var(ht, hi);
3740 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003743 if (forceit)
3744 return OK;
3745 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return FAIL;
3747}
3748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749/*
3750 * Lock or unlock variable indicated by "lp".
3751 * "deep" is the levels to go (-1 for unlimited);
3752 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3753 */
3754 static int
3755do_lock_var(lp, name_end, deep, lock)
3756 lval_T *lp;
3757 char_u *name_end;
3758 int deep;
3759 int lock;
3760{
3761 int ret = OK;
3762 int cc;
3763 dictitem_T *di;
3764
3765 if (deep == 0) /* nothing to do */
3766 return OK;
3767
3768 if (lp->ll_tv == NULL)
3769 {
3770 cc = *name_end;
3771 *name_end = NUL;
3772
3773 /* Normal name or expanded name. */
3774 if (check_changedtick(lp->ll_name))
3775 ret = FAIL;
3776 else
3777 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003778 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 if (di == NULL)
3780 ret = FAIL;
3781 else
3782 {
3783 if (lock)
3784 di->di_flags |= DI_FLAGS_LOCK;
3785 else
3786 di->di_flags &= ~DI_FLAGS_LOCK;
3787 item_lock(&di->di_tv, deep, lock);
3788 }
3789 }
3790 *name_end = cc;
3791 }
3792 else if (lp->ll_range)
3793 {
3794 listitem_T *li = lp->ll_li;
3795
3796 /* (un)lock a range of List items. */
3797 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3798 {
3799 item_lock(&li->li_tv, deep, lock);
3800 li = li->li_next;
3801 ++lp->ll_n1;
3802 }
3803 }
3804 else if (lp->ll_list != NULL)
3805 /* (un)lock a List item. */
3806 item_lock(&lp->ll_li->li_tv, deep, lock);
3807 else
3808 /* un(lock) a Dictionary item. */
3809 item_lock(&lp->ll_di->di_tv, deep, lock);
3810
3811 return ret;
3812}
3813
3814/*
3815 * Lock or unlock an item. "deep" is nr of levels to go.
3816 */
3817 static void
3818item_lock(tv, deep, lock)
3819 typval_T *tv;
3820 int deep;
3821 int lock;
3822{
3823 static int recurse = 0;
3824 list_T *l;
3825 listitem_T *li;
3826 dict_T *d;
3827 hashitem_T *hi;
3828 int todo;
3829
3830 if (recurse >= DICT_MAXNEST)
3831 {
3832 EMSG(_("E743: variable nested too deep for (un)lock"));
3833 return;
3834 }
3835 if (deep == 0)
3836 return;
3837 ++recurse;
3838
3839 /* lock/unlock the item itself */
3840 if (lock)
3841 tv->v_lock |= VAR_LOCKED;
3842 else
3843 tv->v_lock &= ~VAR_LOCKED;
3844
3845 switch (tv->v_type)
3846 {
3847 case VAR_LIST:
3848 if ((l = tv->vval.v_list) != NULL)
3849 {
3850 if (lock)
3851 l->lv_lock |= VAR_LOCKED;
3852 else
3853 l->lv_lock &= ~VAR_LOCKED;
3854 if (deep < 0 || deep > 1)
3855 /* recursive: lock/unlock the items the List contains */
3856 for (li = l->lv_first; li != NULL; li = li->li_next)
3857 item_lock(&li->li_tv, deep - 1, lock);
3858 }
3859 break;
3860 case VAR_DICT:
3861 if ((d = tv->vval.v_dict) != NULL)
3862 {
3863 if (lock)
3864 d->dv_lock |= VAR_LOCKED;
3865 else
3866 d->dv_lock &= ~VAR_LOCKED;
3867 if (deep < 0 || deep > 1)
3868 {
3869 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003870 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003871 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3872 {
3873 if (!HASHITEM_EMPTY(hi))
3874 {
3875 --todo;
3876 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3877 }
3878 }
3879 }
3880 }
3881 }
3882 --recurse;
3883}
3884
Bram Moolenaara40058a2005-07-11 22:42:07 +00003885/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003886 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3887 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003888 */
3889 static int
3890tv_islocked(tv)
3891 typval_T *tv;
3892{
3893 return (tv->v_lock & VAR_LOCKED)
3894 || (tv->v_type == VAR_LIST
3895 && tv->vval.v_list != NULL
3896 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3897 || (tv->v_type == VAR_DICT
3898 && tv->vval.v_dict != NULL
3899 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3900}
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3903/*
3904 * Delete all "menutrans_" variables.
3905 */
3906 void
3907del_menutrans_vars()
3908{
Bram Moolenaar33570922005-01-25 22:26:29 +00003909 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar33570922005-01-25 22:26:29 +00003912 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003913 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003915 {
3916 if (!HASHITEM_EMPTY(hi))
3917 {
3918 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3920 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 }
3922 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925#endif
3926
3927#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3928
3929/*
3930 * Local string buffer for the next two functions to store a variable name
3931 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3932 * get_user_var_name().
3933 */
3934
3935static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3936
3937static char_u *varnamebuf = NULL;
3938static int varnamebuflen = 0;
3939
3940/*
3941 * Function to concatenate a prefix and a variable name.
3942 */
3943 static char_u *
3944cat_prefix_varname(prefix, name)
3945 int prefix;
3946 char_u *name;
3947{
3948 int len;
3949
3950 len = (int)STRLEN(name) + 3;
3951 if (len > varnamebuflen)
3952 {
3953 vim_free(varnamebuf);
3954 len += 10; /* some additional space */
3955 varnamebuf = alloc(len);
3956 if (varnamebuf == NULL)
3957 {
3958 varnamebuflen = 0;
3959 return NULL;
3960 }
3961 varnamebuflen = len;
3962 }
3963 *varnamebuf = prefix;
3964 varnamebuf[1] = ':';
3965 STRCPY(varnamebuf + 2, name);
3966 return varnamebuf;
3967}
3968
3969/*
3970 * Function given to ExpandGeneric() to obtain the list of user defined
3971 * (global/buffer/window/built-in) variable names.
3972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char_u *
3974get_user_var_name(xp, idx)
3975 expand_T *xp;
3976 int idx;
3977{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003978 static long_u gdone;
3979 static long_u bdone;
3980 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981#ifdef FEAT_WINDOWS
3982 static long_u tdone;
3983#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 static int vidx;
3985 static hashitem_T *hi;
3986 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003989 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003990 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 tdone = 0;
3993#endif
3994 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003995
3996 /* Global variables */
3997 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 else
4002 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 while (HASHITEM_EMPTY(hi))
4004 ++hi;
4005 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4006 return cat_prefix_varname('g', hi->hi_key);
4007 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004009
4010 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004011 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004014 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004016 else
4017 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 while (HASHITEM_EMPTY(hi))
4019 ++hi;
4020 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 return (char_u *)"b:changedtick";
4026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027
4028 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004029 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004032 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004034 else
4035 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 while (HASHITEM_EMPTY(hi))
4037 ++hi;
4038 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004041#ifdef FEAT_WINDOWS
4042 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004043 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004044 if (tdone < ht->ht_used)
4045 {
4046 if (tdone++ == 0)
4047 hi = ht->ht_array;
4048 else
4049 ++hi;
4050 while (HASHITEM_EMPTY(hi))
4051 ++hi;
4052 return cat_prefix_varname('t', hi->hi_key);
4053 }
4054#endif
4055
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 /* v: variables */
4057 if (vidx < VV_LEN)
4058 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 vim_free(varnamebuf);
4061 varnamebuf = NULL;
4062 varnamebuflen = 0;
4063 return NULL;
4064}
4065
4066#endif /* FEAT_CMDL_COMPL */
4067
4068/*
4069 * types for expressions.
4070 */
4071typedef enum
4072{
4073 TYPE_UNKNOWN = 0
4074 , TYPE_EQUAL /* == */
4075 , TYPE_NEQUAL /* != */
4076 , TYPE_GREATER /* > */
4077 , TYPE_GEQUAL /* >= */
4078 , TYPE_SMALLER /* < */
4079 , TYPE_SEQUAL /* <= */
4080 , TYPE_MATCH /* =~ */
4081 , TYPE_NOMATCH /* !~ */
4082} exptype_T;
4083
4084/*
4085 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4088 */
4089
4090/*
4091 * Handle zero level expression.
4092 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004094 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * Return OK or FAIL.
4096 */
4097 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 char_u **nextcmd;
4102 int evaluate;
4103{
4104 int ret;
4105 char_u *p;
4106
4107 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (ret == FAIL || !ends_excmd(*p))
4110 {
4111 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 /*
4114 * Report the invalid expression unless the expression evaluation has
4115 * been cancelled due to an aborting error, an interrupt, or an
4116 * exception.
4117 */
4118 if (!aborting())
4119 EMSG2(_(e_invexpr2), arg);
4120 ret = FAIL;
4121 }
4122 if (nextcmd != NULL)
4123 *nextcmd = check_nextcmd(p);
4124
4125 return ret;
4126}
4127
4128/*
4129 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004130 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 *
4132 * "arg" must point to the first non-white of the expression.
4133 * "arg" is advanced to the next non-white after the recognized expression.
4134 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004135 * Note: "rettv.v_lock" is not set.
4136 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
4145 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /*
4149 * Get the first variable.
4150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 return FAIL;
4153
4154 if ((*arg)[0] == '?')
4155 {
4156 result = FALSE;
4157 if (evaluate)
4158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 int error = FALSE;
4160
4161 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (error)
4165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167
4168 /*
4169 * Get the second variable.
4170 */
4171 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 return FAIL;
4174
4175 /*
4176 * Check for the ":".
4177 */
4178 if ((*arg)[0] != ':')
4179 {
4180 EMSG(_("E109: Missing ':' after '?'"));
4181 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184 }
4185
4186 /*
4187 * Get the third variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
4190 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4191 {
4192 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
4196 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199
4200 return OK;
4201}
4202
4203/*
4204 * Handle first level expression:
4205 * expr2 || expr2 || expr2 logical OR
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long result;
4220 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 /*
4230 * Repeat until there is no following "||".
4231 */
4232 first = TRUE;
4233 result = FALSE;
4234 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4235 {
4236 if (evaluate && first)
4237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first = FALSE;
4244 }
4245
4246 /*
4247 * Get the second variable.
4248 */
4249 *arg = skipwhite(*arg + 2);
4250 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4251 return FAIL;
4252
4253 /*
4254 * Compute the result.
4255 */
4256 if (evaluate && !result)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle second level expression:
4276 * expr3 && expr3 && expr3 logical AND
4277 *
4278 * "arg" must point to the first non-white of the expression.
4279 * "arg" is advanced to the next non-white after the recognized expression.
4280 *
4281 * Return OK or FAIL.
4282 */
4283 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 int evaluate;
4288{
Bram Moolenaar33570922005-01-25 22:26:29 +00004289 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 long result;
4291 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 /*
4295 * Get the first variable.
4296 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299
4300 /*
4301 * Repeat until there is no following "&&".
4302 */
4303 first = TRUE;
4304 result = TRUE;
4305 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4306 {
4307 if (evaluate && first)
4308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (error)
4313 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 first = FALSE;
4315 }
4316
4317 /*
4318 * Get the second variable.
4319 */
4320 *arg = skipwhite(*arg + 2);
4321 if (eval4(arg, &var2, evaluate && result) == FAIL)
4322 return FAIL;
4323
4324 /*
4325 * Compute the result.
4326 */
4327 if (evaluate && result)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 if (evaluate)
4336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 rettv->v_type = VAR_NUMBER;
4338 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 }
4341
4342 return OK;
4343}
4344
4345/*
4346 * Handle third level expression:
4347 * var1 == var2
4348 * var1 =~ var2
4349 * var1 != var2
4350 * var1 !~ var2
4351 * var1 > var2
4352 * var1 >= var2
4353 * var1 < var2
4354 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355 * var1 is var2
4356 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 *
4358 * "arg" must point to the first non-white of the expression.
4359 * "arg" is advanced to the next non-white after the recognized expression.
4360 *
4361 * Return OK or FAIL.
4362 */
4363 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 int evaluate;
4368{
Bram Moolenaar33570922005-01-25 22:26:29 +00004369 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u *p;
4371 int i;
4372 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int len = 2;
4375 long n1, n2;
4376 char_u *s1, *s2;
4377 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4378 regmatch_T regmatch;
4379 int ic;
4380 char_u *save_cpo;
4381
4382 /*
4383 * Get the first variable.
4384 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return FAIL;
4387
4388 p = *arg;
4389 switch (p[0])
4390 {
4391 case '=': if (p[1] == '=')
4392 type = TYPE_EQUAL;
4393 else if (p[1] == '~')
4394 type = TYPE_MATCH;
4395 break;
4396 case '!': if (p[1] == '=')
4397 type = TYPE_NEQUAL;
4398 else if (p[1] == '~')
4399 type = TYPE_NOMATCH;
4400 break;
4401 case '>': if (p[1] != '=')
4402 {
4403 type = TYPE_GREATER;
4404 len = 1;
4405 }
4406 else
4407 type = TYPE_GEQUAL;
4408 break;
4409 case '<': if (p[1] != '=')
4410 {
4411 type = TYPE_SMALLER;
4412 len = 1;
4413 }
4414 else
4415 type = TYPE_SEQUAL;
4416 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 case 'i': if (p[1] == 's')
4418 {
4419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4420 len = 5;
4421 if (!vim_isIDc(p[len]))
4422 {
4423 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4424 type_is = TRUE;
4425 }
4426 }
4427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429
4430 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 */
4433 if (type != TYPE_UNKNOWN)
4434 {
4435 /* extra question mark appended: ignore case */
4436 if (p[len] == '?')
4437 {
4438 ic = TRUE;
4439 ++len;
4440 }
4441 /* extra '#' appended: match case */
4442 else if (p[len] == '#')
4443 {
4444 ic = FALSE;
4445 ++len;
4446 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004447 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 else
4449 ic = p_ic;
4450
4451 /*
4452 * Get the second variable.
4453 */
4454 *arg = skipwhite(p + len);
4455 if (eval5(arg, &var2, evaluate) == FAIL)
4456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 return FAIL;
4459 }
4460
4461 if (evaluate)
4462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 if (type_is && rettv->v_type != var2.v_type)
4464 {
4465 /* For "is" a different type always means FALSE, for "notis"
4466 * it means TRUE. */
4467 n1 = (type == TYPE_NEQUAL);
4468 }
4469 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4470 {
4471 if (type_is)
4472 {
4473 n1 = (rettv->v_type == var2.v_type
4474 && rettv->vval.v_list == var2.vval.v_list);
4475 if (type == TYPE_NEQUAL)
4476 n1 = !n1;
4477 }
4478 else if (rettv->v_type != var2.v_type
4479 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4480 {
4481 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004482 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004484 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 clear_tv(rettv);
4486 clear_tv(&var2);
4487 return FAIL;
4488 }
4489 else
4490 {
4491 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004492 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4493 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 if (type == TYPE_NEQUAL)
4495 n1 = !n1;
4496 }
4497 }
4498
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004499 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4500 {
4501 if (type_is)
4502 {
4503 n1 = (rettv->v_type == var2.v_type
4504 && rettv->vval.v_dict == var2.vval.v_dict);
4505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 else if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
4512 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4513 else
4514 EMSG(_("E736: Invalid operation for Dictionary"));
4515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004522 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4523 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 }
4528
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004529 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4530 {
4531 if (rettv->v_type != var2.v_type
4532 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4533 {
4534 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004535 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004536 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004537 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004538 clear_tv(rettv);
4539 clear_tv(&var2);
4540 return FAIL;
4541 }
4542 else
4543 {
4544 /* Compare two Funcrefs for being equal or unequal. */
4545 if (rettv->vval.v_string == NULL
4546 || var2.vval.v_string == NULL)
4547 n1 = FALSE;
4548 else
4549 n1 = STRCMP(rettv->vval.v_string,
4550 var2.vval.v_string) == 0;
4551 if (type == TYPE_NEQUAL)
4552 n1 = !n1;
4553 }
4554 }
4555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556#ifdef FEAT_FLOAT
4557 /*
4558 * If one of the two variables is a float, compare as a float.
4559 * When using "=~" or "!~", always compare as string.
4560 */
4561 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4562 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4563 {
4564 float_T f1, f2;
4565
4566 if (rettv->v_type == VAR_FLOAT)
4567 f1 = rettv->vval.v_float;
4568 else
4569 f1 = get_tv_number(rettv);
4570 if (var2.v_type == VAR_FLOAT)
4571 f2 = var2.vval.v_float;
4572 else
4573 f2 = get_tv_number(&var2);
4574 n1 = FALSE;
4575 switch (type)
4576 {
4577 case TYPE_EQUAL: n1 = (f1 == f2); break;
4578 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4579 case TYPE_GREATER: n1 = (f1 > f2); break;
4580 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4581 case TYPE_SMALLER: n1 = (f1 < f2); break;
4582 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4583 case TYPE_UNKNOWN:
4584 case TYPE_MATCH:
4585 case TYPE_NOMATCH: break; /* avoid gcc warning */
4586 }
4587 }
4588#endif
4589
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 /*
4591 * If one of the two variables is a number, compare as a number.
4592 * When using "=~" or "!~", always compare as string.
4593 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 n1 = get_tv_number(rettv);
4598 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 switch (type)
4600 {
4601 case TYPE_EQUAL: n1 = (n1 == n2); break;
4602 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4603 case TYPE_GREATER: n1 = (n1 > n2); break;
4604 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4605 case TYPE_SMALLER: n1 = (n1 < n2); break;
4606 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4607 case TYPE_UNKNOWN:
4608 case TYPE_MATCH:
4609 case TYPE_NOMATCH: break; /* avoid gcc warning */
4610 }
4611 }
4612 else
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 s1 = get_tv_string_buf(rettv, buf1);
4615 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4617 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4618 else
4619 i = 0;
4620 n1 = FALSE;
4621 switch (type)
4622 {
4623 case TYPE_EQUAL: n1 = (i == 0); break;
4624 case TYPE_NEQUAL: n1 = (i != 0); break;
4625 case TYPE_GREATER: n1 = (i > 0); break;
4626 case TYPE_GEQUAL: n1 = (i >= 0); break;
4627 case TYPE_SMALLER: n1 = (i < 0); break;
4628 case TYPE_SEQUAL: n1 = (i <= 0); break;
4629
4630 case TYPE_MATCH:
4631 case TYPE_NOMATCH:
4632 /* avoid 'l' flag in 'cpoptions' */
4633 save_cpo = p_cpo;
4634 p_cpo = (char_u *)"";
4635 regmatch.regprog = vim_regcomp(s2,
4636 RE_MAGIC + RE_STRING);
4637 regmatch.rm_ic = ic;
4638 if (regmatch.regprog != NULL)
4639 {
4640 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004641 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (type == TYPE_NOMATCH)
4643 n1 = !n1;
4644 }
4645 p_cpo = save_cpo;
4646 break;
4647
4648 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4649 }
4650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 clear_tv(rettv);
4652 clear_tv(&var2);
4653 rettv->v_type = VAR_NUMBER;
4654 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657
4658 return OK;
4659}
4660
4661/*
4662 * Handle fourth level expression:
4663 * + number addition
4664 * - number subtraction
4665 * . string concatenation
4666 *
4667 * "arg" must point to the first non-white of the expression.
4668 * "arg" is advanced to the next non-white after the recognized expression.
4669 *
4670 * Return OK or FAIL.
4671 */
4672 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 int evaluate;
4677{
Bram Moolenaar33570922005-01-25 22:26:29 +00004678 typval_T var2;
4679 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 int op;
4681 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682#ifdef FEAT_FLOAT
4683 float_T f1 = 0, f2 = 0;
4684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 char_u *s1, *s2;
4686 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4687 char_u *p;
4688
4689 /*
4690 * Get the first variable.
4691 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004692 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 return FAIL;
4694
4695 /*
4696 * Repeat computing, until no '+', '-' or '.' is following.
4697 */
4698 for (;;)
4699 {
4700 op = **arg;
4701 if (op != '+' && op != '-' && op != '.')
4702 break;
4703
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 if ((op != '+' || rettv->v_type != VAR_LIST)
4705#ifdef FEAT_FLOAT
4706 && (op == '.' || rettv->v_type != VAR_FLOAT)
4707#endif
4708 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
4710 /* For "list + ...", an illegal use of the first operand as
4711 * a number cannot be determined before evaluating the 2nd
4712 * operand: if this is also a list, all is ok.
4713 * For "something . ...", "something - ..." or "non-list + ...",
4714 * we know that the first operand needs to be a string or number
4715 * without evaluating the 2nd operand. So check before to avoid
4716 * side effects after an error. */
4717 if (evaluate && get_tv_string_chk(rettv) == NULL)
4718 {
4719 clear_tv(rettv);
4720 return FAIL;
4721 }
4722 }
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 /*
4725 * Get the second variable.
4726 */
4727 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004728 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 return FAIL;
4732 }
4733
4734 if (evaluate)
4735 {
4736 /*
4737 * Compute the result.
4738 */
4739 if (op == '.')
4740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4742 s2 = get_tv_string_buf_chk(&var2, buf2);
4743 if (s2 == NULL) /* type error ? */
4744 {
4745 clear_tv(rettv);
4746 clear_tv(&var2);
4747 return FAIL;
4748 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004749 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
4751 rettv->v_type = VAR_STRING;
4752 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004754 else if (op == '+' && rettv->v_type == VAR_LIST
4755 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004756 {
4757 /* concatenate Lists */
4758 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4759 &var3) == FAIL)
4760 {
4761 clear_tv(rettv);
4762 clear_tv(&var2);
4763 return FAIL;
4764 }
4765 clear_tv(rettv);
4766 *rettv = var3;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 else
4769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 int error = FALSE;
4771
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772#ifdef FEAT_FLOAT
4773 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004775 f1 = rettv->vval.v_float;
4776 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778 else
4779#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781 n1 = get_tv_number_chk(rettv, &error);
4782 if (error)
4783 {
4784 /* This can only happen for "list + non-list". For
4785 * "non-list + ..." or "something - ...", we returned
4786 * before evaluating the 2nd operand. */
4787 clear_tv(rettv);
4788 return FAIL;
4789 }
4790#ifdef FEAT_FLOAT
4791 if (var2.v_type == VAR_FLOAT)
4792 f1 = n1;
4793#endif
4794 }
4795#ifdef FEAT_FLOAT
4796 if (var2.v_type == VAR_FLOAT)
4797 {
4798 f2 = var2.vval.v_float;
4799 n2 = 0;
4800 }
4801 else
4802#endif
4803 {
4804 n2 = get_tv_number_chk(&var2, &error);
4805 if (error)
4806 {
4807 clear_tv(rettv);
4808 clear_tv(&var2);
4809 return FAIL;
4810 }
4811#ifdef FEAT_FLOAT
4812 if (rettv->v_type == VAR_FLOAT)
4813 f2 = n2;
4814#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817
4818#ifdef FEAT_FLOAT
4819 /* If there is a float on either side the result is a float. */
4820 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4821 {
4822 if (op == '+')
4823 f1 = f1 + f2;
4824 else
4825 f1 = f1 - f2;
4826 rettv->v_type = VAR_FLOAT;
4827 rettv->vval.v_float = f1;
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#endif
4831 {
4832 if (op == '+')
4833 n1 = n1 + n2;
4834 else
4835 n1 = n1 - n2;
4836 rettv->v_type = VAR_NUMBER;
4837 rettv->vval.v_number = n1;
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 }
4843 return OK;
4844}
4845
4846/*
4847 * Handle fifth level expression:
4848 * * number multiplication
4849 * / number division
4850 * % number modulo
4851 *
4852 * "arg" must point to the first non-white of the expression.
4853 * "arg" is advanced to the next non-white after the recognized expression.
4854 *
4855 * Return OK or FAIL.
4856 */
4857 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004858eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004862 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaar33570922005-01-25 22:26:29 +00004864 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 int op;
4866 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867#ifdef FEAT_FLOAT
4868 int use_float = FALSE;
4869 float_T f1 = 0, f2;
4870#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 /*
4874 * Get the first variable.
4875 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004876 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return FAIL;
4878
4879 /*
4880 * Repeat computing, until no '*', '/' or '%' is following.
4881 */
4882 for (;;)
4883 {
4884 op = **arg;
4885 if (op != '*' && op != '/' && op != '%')
4886 break;
4887
4888 if (evaluate)
4889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 if (rettv->v_type == VAR_FLOAT)
4892 {
4893 f1 = rettv->vval.v_float;
4894 use_float = TRUE;
4895 n1 = 0;
4896 }
4897 else
4898#endif
4899 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 if (error)
4902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else
4905 n1 = 0;
4906
4907 /*
4908 * Get the second variable.
4909 */
4910 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004911 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 return FAIL;
4913
4914 if (evaluate)
4915 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916#ifdef FEAT_FLOAT
4917 if (var2.v_type == VAR_FLOAT)
4918 {
4919 if (!use_float)
4920 {
4921 f1 = n1;
4922 use_float = TRUE;
4923 }
4924 f2 = var2.vval.v_float;
4925 n2 = 0;
4926 }
4927 else
4928#endif
4929 {
4930 n2 = get_tv_number_chk(&var2, &error);
4931 clear_tv(&var2);
4932 if (error)
4933 return FAIL;
4934#ifdef FEAT_FLOAT
4935 if (use_float)
4936 f2 = n2;
4937#endif
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 /*
4941 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004944#ifdef FEAT_FLOAT
4945 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 if (op == '*')
4948 f1 = f1 * f2;
4949 else if (op == '/')
4950 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004951# ifdef VMS
4952 /* VMS crashes on divide by zero, work around it */
4953 if (f2 == 0.0)
4954 {
4955 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004956 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004957 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004958 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004960 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004961 }
4962 else
4963 f1 = f1 / f2;
4964# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 /* We rely on the floating point library to handle divide
4966 * by zero to result in "inf" and not a crash. */
4967 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004972 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 return FAIL;
4974 }
4975 rettv->v_type = VAR_FLOAT;
4976 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 if (op == '*')
4982 n1 = n1 * n2;
4983 else if (op == '/')
4984 {
4985 if (n2 == 0) /* give an error message? */
4986 {
4987 if (n1 == 0)
4988 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4989 else if (n1 < 0)
4990 n1 = -0x7fffffffL;
4991 else
4992 n1 = 0x7fffffffL;
4993 }
4994 else
4995 n1 = n1 / n2;
4996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998 {
4999 if (n2 == 0) /* give an error message? */
5000 n1 = 0;
5001 else
5002 n1 = n1 % n2;
5003 }
5004 rettv->v_type = VAR_NUMBER;
5005 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 }
5009
5010 return OK;
5011}
5012
5013/*
5014 * Handle sixth level expression:
5015 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005016 * "string" string constant
5017 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * &option-name option value
5019 * @r register contents
5020 * identifier variable value
5021 * function() function call
5022 * $VAR environment variable
5023 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005024 * [expr, expr] List
5025 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 *
5027 * Also handle:
5028 * ! in front logical NOT
5029 * - in front unary minus
5030 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 * trailing [] subscript in String or List
5032 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *
5034 * "arg" must point to the first non-white of the expression.
5035 * "arg" is advanced to the next non-white after the recognized expression.
5036 *
5037 * Return OK or FAIL.
5038 */
5039 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005040eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005044 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 long n;
5047 int len;
5048 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 char_u *start_leader, *end_leader;
5050 int ret = OK;
5051 char_u *alias;
5052
5053 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 /*
5060 * Skip '!' and '-' characters. They are handled later.
5061 */
5062 start_leader = *arg;
5063 while (**arg == '!' || **arg == '-' || **arg == '+')
5064 *arg = skipwhite(*arg + 1);
5065 end_leader = *arg;
5066
5067 switch (**arg)
5068 {
5069 /*
5070 * Number constant.
5071 */
5072 case '0':
5073 case '1':
5074 case '2':
5075 case '3':
5076 case '4':
5077 case '5':
5078 case '6':
5079 case '7':
5080 case '8':
5081 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 {
5083#ifdef FEAT_FLOAT
5084 char_u *p = skipdigits(*arg + 1);
5085 int get_float = FALSE;
5086
5087 /* We accept a float when the format matches
5088 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005089 * strict to avoid backwards compatibility problems.
5090 * Don't look for a float after the "." operator, so that
5091 * ":let vers = 1.2.3" doesn't fail. */
5092 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 get_float = TRUE;
5095 p = skipdigits(p + 2);
5096 if (*p == 'e' || *p == 'E')
5097 {
5098 ++p;
5099 if (*p == '-' || *p == '+')
5100 ++p;
5101 if (!vim_isdigit(*p))
5102 get_float = FALSE;
5103 else
5104 p = skipdigits(p + 1);
5105 }
5106 if (ASCII_ISALPHA(*p) || *p == '.')
5107 get_float = FALSE;
5108 }
5109 if (get_float)
5110 {
5111 float_T f;
5112
5113 *arg += string2float(*arg, &f);
5114 if (evaluate)
5115 {
5116 rettv->v_type = VAR_FLOAT;
5117 rettv->vval.v_float = f;
5118 }
5119 }
5120 else
5121#endif
5122 {
5123 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5124 *arg += len;
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_NUMBER;
5128 rettv->vval.v_number = n;
5129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 /*
5135 * String constant: "string".
5136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005137 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139
5140 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 break;
5145
5146 /*
5147 * List: [expr, expr]
5148 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151
5152 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 * Dictionary: {key: val, key: val}
5154 */
5155 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5156 break;
5157
5158 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005161 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /*
5165 * Environment variable: $VAR.
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
5171 * Register contents: @r.
5172 */
5173 case '@': ++*arg;
5174 if (evaluate)
5175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005177 rettv->vval.v_string = get_reg_contents(**arg,
5178 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 if (**arg != NUL)
5181 ++*arg;
5182 break;
5183
5184 /*
5185 * nested expression: (expression).
5186 */
5187 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (**arg == ')')
5190 ++*arg;
5191 else if (ret == OK)
5192 {
5193 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 ret = FAIL;
5196 }
5197 break;
5198
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202
5203 if (ret == NOTDONE)
5204 {
5205 /*
5206 * Must be a variable or function name.
5207 * Can also be a curly-braces kind of name: {expr}.
5208 */
5209 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005210 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 if (alias != NULL)
5212 s = alias;
5213
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005214 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 ret = FAIL;
5216 else
5217 {
5218 if (**arg == '(') /* recursive! */
5219 {
5220 /* If "s" is the name of a variable of type VAR_FUNC
5221 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005222 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223
5224 /* Invoke the function. */
5225 ret = get_func_tv(s, len, rettv, arg,
5226 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005227 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005228
5229 /* If evaluate is FALSE rettv->v_type was not set in
5230 * get_func_tv, but it's needed in handle_subscript() to parse
5231 * what follows. So set it here. */
5232 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5233 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005234 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005235 rettv->v_type = VAR_FUNC;
5236 }
5237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 /* Stop the expression evaluation when immediately
5239 * aborting on error, or when an interrupt occurred or
5240 * an exception was thrown but not caught. */
5241 if (aborting())
5242 {
5243 if (ret == OK)
5244 clear_tv(rettv);
5245 ret = FAIL;
5246 }
5247 }
5248 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005249 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005250 else
5251 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005253 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 }
5255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 *arg = skipwhite(*arg);
5257
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5259 * expr(expr). */
5260 if (ret == OK)
5261 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263 /*
5264 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5265 */
5266 if (ret == OK && evaluate && end_leader > start_leader)
5267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 int val = 0;
5270#ifdef FEAT_FLOAT
5271 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005273 if (rettv->v_type == VAR_FLOAT)
5274 f = rettv->vval.v_float;
5275 else
5276#endif
5277 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 clear_tv(rettv);
5281 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 else
5284 {
5285 while (end_leader > start_leader)
5286 {
5287 --end_leader;
5288 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 {
5290#ifdef FEAT_FLOAT
5291 if (rettv->v_type == VAR_FLOAT)
5292 f = !f;
5293 else
5294#endif
5295 val = !val;
5296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 {
5299#ifdef FEAT_FLOAT
5300 if (rettv->v_type == VAR_FLOAT)
5301 f = -f;
5302 else
5303#endif
5304 val = -val;
5305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307#ifdef FEAT_FLOAT
5308 if (rettv->v_type == VAR_FLOAT)
5309 {
5310 clear_tv(rettv);
5311 rettv->vval.v_float = f;
5312 }
5313 else
5314#endif
5315 {
5316 clear_tv(rettv);
5317 rettv->v_type = VAR_NUMBER;
5318 rettv->vval.v_number = val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322
5323 return ret;
5324}
5325
5326/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005327 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5328 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5330 */
5331 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005332eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005336 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337{
5338 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 long len = -1;
5342 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005346 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005352#ifdef FEAT_FLOAT
5353 else if (rettv->v_type == VAR_FLOAT)
5354 {
5355 if (verbose)
5356 EMSG(_(e_float_as_string));
5357 return FAIL;
5358 }
5359#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 /*
5364 * dict.name
5365 */
5366 key = *arg + 1;
5367 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5368 ;
5369 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 }
5373 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375 /*
5376 * something[idx]
5377 *
5378 * Get the (first) variable from inside the [].
5379 */
5380 *arg = skipwhite(*arg + 1);
5381 if (**arg == ':')
5382 empty1 = TRUE;
5383 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5384 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5386 {
5387 /* not a number or string */
5388 clear_tv(&var1);
5389 return FAIL;
5390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391
5392 /*
5393 * Get the second variable from inside the [:].
5394 */
5395 if (**arg == ':')
5396 {
5397 range = TRUE;
5398 *arg = skipwhite(*arg + 1);
5399 if (**arg == ']')
5400 empty2 = TRUE;
5401 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005403 if (!empty1)
5404 clear_tv(&var1);
5405 return FAIL;
5406 }
5407 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5408 {
5409 /* not a number or string */
5410 if (!empty1)
5411 clear_tv(&var1);
5412 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413 return FAIL;
5414 }
5415 }
5416
5417 /* Check for the ']'. */
5418 if (**arg != ']')
5419 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005420 if (verbose)
5421 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 clear_tv(&var1);
5423 if (range)
5424 clear_tv(&var2);
5425 return FAIL;
5426 }
5427 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429
5430 if (evaluate)
5431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 n1 = 0;
5433 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 n1 = get_tv_number(&var1);
5436 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438 if (range)
5439 {
5440 if (empty2)
5441 n2 = -1;
5442 else
5443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 n2 = get_tv_number(&var2);
5445 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447 }
5448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 {
5451 case VAR_NUMBER:
5452 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 len = (long)STRLEN(s);
5455 if (range)
5456 {
5457 /* The resulting variable is a substring. If the indexes
5458 * are out of range the result is empty. */
5459 if (n1 < 0)
5460 {
5461 n1 = len + n1;
5462 if (n1 < 0)
5463 n1 = 0;
5464 }
5465 if (n2 < 0)
5466 n2 = len + n2;
5467 else if (n2 >= len)
5468 n2 = len;
5469 if (n1 >= len || n2 < 0 || n1 > n2)
5470 s = NULL;
5471 else
5472 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5473 }
5474 else
5475 {
5476 /* The resulting variable is a string of a single
5477 * character. If the index is too big or negative the
5478 * result is empty. */
5479 if (n1 >= len || n1 < 0)
5480 s = NULL;
5481 else
5482 s = vim_strnsave(s + n1, 1);
5483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 clear_tv(rettv);
5485 rettv->v_type = VAR_STRING;
5486 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 break;
5488
5489 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 if (n1 < 0)
5492 n1 = len + n1;
5493 if (!empty1 && (n1 < 0 || n1 >= len))
5494 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005495 /* For a range we allow invalid values and return an empty
5496 * list. A list index out of range is an error. */
5497 if (!range)
5498 {
5499 if (verbose)
5500 EMSGN(_(e_listidx), n1);
5501 return FAIL;
5502 }
5503 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 }
5505 if (range)
5506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 list_T *l;
5508 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509
5510 if (n2 < 0)
5511 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005512 else if (n2 >= len)
5513 n2 = len - 1;
5514 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005515 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 l = list_alloc();
5517 if (l == NULL)
5518 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 n1 <= n2; ++n1)
5521 {
5522 if (list_append_tv(l, &item->li_tv) == FAIL)
5523 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005524 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 return FAIL;
5526 }
5527 item = item->li_next;
5528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 clear_tv(rettv);
5530 rettv->v_type = VAR_LIST;
5531 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005532 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 }
5534 else
5535 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 clear_tv(rettv);
5538 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 }
5540 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541
5542 case VAR_DICT:
5543 if (range)
5544 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005545 if (verbose)
5546 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547 if (len == -1)
5548 clear_tv(&var1);
5549 return FAIL;
5550 }
5551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553
5554 if (len == -1)
5555 {
5556 key = get_tv_string(&var1);
5557 if (*key == NUL)
5558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005559 if (verbose)
5560 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 clear_tv(&var1);
5562 return FAIL;
5563 }
5564 }
5565
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005566 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005568 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005569 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 if (len == -1)
5571 clear_tv(&var1);
5572 if (item == NULL)
5573 return FAIL;
5574
5575 copy_tv(&item->di_tv, &var1);
5576 clear_tv(rettv);
5577 *rettv = var1;
5578 }
5579 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 }
5581 }
5582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 return OK;
5584}
5585
5586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 * Get an option value.
5588 * "arg" points to the '&' or '+' before the option name.
5589 * "arg" is advanced to character after the option name.
5590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *option_end;
5599 long numval;
5600 char_u *stringval;
5601 int opt_type;
5602 int c;
5603 int working = (**arg == '+'); /* has("+option") */
5604 int ret = OK;
5605 int opt_flags;
5606
5607 /*
5608 * Isolate the option name and find its value.
5609 */
5610 option_end = find_option_end(arg, &opt_flags);
5611 if (option_end == NULL)
5612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 EMSG2(_("E112: Option name missing: %s"), *arg);
5615 return FAIL;
5616 }
5617
5618 if (!evaluate)
5619 {
5620 *arg = option_end;
5621 return OK;
5622 }
5623
5624 c = *option_end;
5625 *option_end = NUL;
5626 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 if (opt_type == -3) /* invalid name */
5630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 EMSG2(_("E113: Unknown option: %s"), *arg);
5633 ret = FAIL;
5634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637 if (opt_type == -2) /* hidden string option */
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 else if (opt_type == -1) /* hidden number option */
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 rettv->v_type = VAR_NUMBER;
5645 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 else if (opt_type == 1) /* number option */
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv->v_type = VAR_NUMBER;
5650 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 else /* string option */
5653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 rettv->v_type = VAR_STRING;
5655 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 }
5658 else if (working && (opt_type == -2 || opt_type == -1))
5659 ret = FAIL;
5660
5661 *option_end = c; /* put back for error messages */
5662 *arg = option_end;
5663
5664 return ret;
5665}
5666
5667/*
5668 * Allocate a variable for a string constant.
5669 * Return OK or FAIL.
5670 */
5671 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 int evaluate;
5676{
5677 char_u *p;
5678 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 int extra = 0;
5680
5681 /*
5682 * Find the end of the string, skipping backslashed characters.
5683 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 if (*p == '\\' && p[1] != NUL)
5687 {
5688 ++p;
5689 /* A "\<x>" form occupies at least 4 characters, and produces up
5690 * to 6 characters: reserve space for 2 extra */
5691 if (*p == '<')
5692 extra += 2;
5693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695
5696 if (*p != '"')
5697 {
5698 EMSG2(_("E114: Missing quote: %s"), *arg);
5699 return FAIL;
5700 }
5701
5702 /* If only parsing, set *arg and return here */
5703 if (!evaluate)
5704 {
5705 *arg = p + 1;
5706 return OK;
5707 }
5708
5709 /*
5710 * Copy the string into allocated memory, handling backslashed
5711 * characters.
5712 */
5713 name = alloc((unsigned)(p - *arg + extra));
5714 if (name == NULL)
5715 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 rettv->v_type = VAR_STRING;
5717 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 if (*p == '\\')
5722 {
5723 switch (*++p)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 case 'b': *name++ = BS; ++p; break;
5726 case 'e': *name++ = ESC; ++p; break;
5727 case 'f': *name++ = FF; ++p; break;
5728 case 'n': *name++ = NL; ++p; break;
5729 case 'r': *name++ = CAR; ++p; break;
5730 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 case 'X': /* hex: "\x1", "\x12" */
5733 case 'x':
5734 case 'u': /* Unicode: "\u0023" */
5735 case 'U':
5736 if (vim_isxdigit(p[1]))
5737 {
5738 int n, nr;
5739 int c = toupper(*p);
5740
5741 if (c == 'X')
5742 n = 2;
5743 else
5744 n = 4;
5745 nr = 0;
5746 while (--n >= 0 && vim_isxdigit(p[1]))
5747 {
5748 ++p;
5749 nr = (nr << 4) + hex2nr(*p);
5750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752#ifdef FEAT_MBYTE
5753 /* For "\u" store the number according to
5754 * 'encoding'. */
5755 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 else
5758#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 break;
5762
5763 /* octal: "\1", "\12", "\123" */
5764 case '0':
5765 case '1':
5766 case '2':
5767 case '3':
5768 case '4':
5769 case '5':
5770 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 case '7': *name = *p++ - '0';
5772 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 *name = (*name << 3) + *p++ - '0';
5775 if (*p >= '0' && *p <= '7')
5776 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 break;
5780
5781 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (extra != 0)
5784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 break;
5787 }
5788 /* FALLTHROUGH */
5789
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 break;
5792 }
5793 }
5794 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 *arg = p + 1;
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 return OK;
5802}
5803
5804/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 * Return OK or FAIL.
5807 */
5808 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005809get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 int evaluate;
5813{
5814 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 int reduce = 0;
5817
5818 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 */
5821 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 break;
5827 ++reduce;
5828 ++p;
5829 }
5830 }
5831
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 return FAIL;
5836 }
5837
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 if (!evaluate)
5840 {
5841 *arg = p + 1;
5842 return OK;
5843 }
5844
5845 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 */
5848 str = alloc((unsigned)((p - *arg) - reduce));
5849 if (str == NULL)
5850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 rettv->v_type = VAR_STRING;
5852 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 break;
5860 ++p;
5861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 *arg = p + 1;
5866
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 return OK;
5868}
5869
5870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 * Allocate a variable for a List and fill it from "*arg".
5872 * Return OK or FAIL.
5873 */
5874 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005875get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 int evaluate;
5879{
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 list_T *l = NULL;
5881 typval_T tv;
5882 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883
5884 if (evaluate)
5885 {
5886 l = list_alloc();
5887 if (l == NULL)
5888 return FAIL;
5889 }
5890
5891 *arg = skipwhite(*arg + 1);
5892 while (**arg != ']' && **arg != NUL)
5893 {
5894 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5895 goto failret;
5896 if (evaluate)
5897 {
5898 item = listitem_alloc();
5899 if (item != NULL)
5900 {
5901 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005902 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 list_append(l, item);
5904 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005905 else
5906 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 }
5908
5909 if (**arg == ']')
5910 break;
5911 if (**arg != ',')
5912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 goto failret;
5915 }
5916 *arg = skipwhite(*arg + 1);
5917 }
5918
5919 if (**arg != ']')
5920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922failret:
5923 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005924 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 return FAIL;
5926 }
5927
5928 *arg = skipwhite(*arg + 1);
5929 if (evaluate)
5930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005931 rettv->v_type = VAR_LIST;
5932 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 ++l->lv_refcount;
5934 }
5935
5936 return OK;
5937}
5938
5939/*
5940 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005941 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005943 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944list_alloc()
5945{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 list_T *l;
5947
5948 l = (list_T *)alloc_clear(sizeof(list_T));
5949 if (l != NULL)
5950 {
5951 /* Prepend the list to the list of lists for garbage collection. */
5952 if (first_list != NULL)
5953 first_list->lv_used_prev = l;
5954 l->lv_used_prev = NULL;
5955 l->lv_used_next = first_list;
5956 first_list = l;
5957 }
5958 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959}
5960
5961/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005962 * Allocate an empty list for a return value.
5963 * Returns OK or FAIL.
5964 */
5965 static int
5966rettv_list_alloc(rettv)
5967 typval_T *rettv;
5968{
5969 list_T *l = list_alloc();
5970
5971 if (l == NULL)
5972 return FAIL;
5973
5974 rettv->vval.v_list = l;
5975 rettv->v_type = VAR_LIST;
5976 ++l->lv_refcount;
5977 return OK;
5978}
5979
5980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 * Unreference a list: decrement the reference count and free it when it
5982 * becomes zero.
5983 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005984 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005988 if (l != NULL && --l->lv_refcount <= 0)
5989 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990}
5991
5992/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005993 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 * Ignores the reference count.
5995 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005996 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005997list_free(l, recurse)
5998 list_T *l;
5999 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006003 /* Remove the list from the list of lists for garbage collection. */
6004 if (l->lv_used_prev == NULL)
6005 first_list = l->lv_used_next;
6006 else
6007 l->lv_used_prev->lv_used_next = l->lv_used_next;
6008 if (l->lv_used_next != NULL)
6009 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6010
Bram Moolenaard9fba312005-06-26 22:34:35 +00006011 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013 /* Remove the item before deleting it. */
6014 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (recurse || (item->li_tv.v_type != VAR_LIST
6016 && item->li_tv.v_type != VAR_DICT))
6017 clear_tv(&item->li_tv);
6018 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 }
6020 vim_free(l);
6021}
6022
6023/*
6024 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006025 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006027 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028listitem_alloc()
6029{
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031}
6032
6033/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006034 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006036 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 vim_free(item);
6042}
6043
6044/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006045 * Remove a list item from a List and free it. Also clears the value.
6046 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006047 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006048listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 list_T *l;
6050 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006051{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006052 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006053 listitem_free(item);
6054}
6055
6056/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 * Get the number of items in a list.
6058 */
6059 static long
6060list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 if (l == NULL)
6064 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006065 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066}
6067
6068/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 * Return TRUE when two lists have exactly the same values.
6070 */
6071 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 list_T *l1;
6074 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006077{
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006079
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006080 if (l1 == NULL || l2 == NULL)
6081 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006082 if (l1 == l2)
6083 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006084 if (list_len(l1) != list_len(l2))
6085 return FALSE;
6086
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 for (item1 = l1->lv_first, item2 = l2->lv_first;
6088 item1 != NULL && item2 != NULL;
6089 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091 return FALSE;
6092 return item1 == NULL && item2 == NULL;
6093}
6094
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006095#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6096 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006097/*
6098 * Return the dictitem that an entry in a hashtable points to.
6099 */
6100 dictitem_T *
6101dict_lookup(hi)
6102 hashitem_T *hi;
6103{
6104 return HI2DI(hi);
6105}
6106#endif
6107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006109 * Return TRUE when two dictionaries have exactly the same key/values.
6110 */
6111 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 dict_T *d1;
6114 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006115 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006117{
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 hashitem_T *hi;
6119 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006120 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006121
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006122 if (d1 == NULL || d2 == NULL)
6123 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006124 if (d1 == d2)
6125 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 if (dict_len(d1) != dict_len(d2))
6127 return FALSE;
6128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006129 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132 if (!HASHITEM_EMPTY(hi))
6133 {
6134 item2 = dict_find(d2, hi->hi_key, -1);
6135 if (item2 == NULL)
6136 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006137 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 return FALSE;
6139 --todo;
6140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 }
6142 return TRUE;
6143}
6144
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145static int tv_equal_recurse_limit;
6146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006150 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 */
6152 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 typval_T *tv1;
6155 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 int ic; /* ignore case */
6157 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006158{
6159 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006160 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006162 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006164 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006165 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006167 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168 * recursiveness to a limit. We guess they are equal then.
6169 * A fixed limit has the problem of still taking an awful long time.
6170 * Reduce the limit every time running into it. That should work fine for
6171 * deeply linked structures that are not recursively linked and catch
6172 * recursiveness quickly. */
6173 if (!recursive)
6174 tv_equal_recurse_limit = 1000;
6175 if (recursive_cnt >= tv_equal_recurse_limit)
6176 {
6177 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006178 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180
6181 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 ++recursive_cnt;
6185 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6186 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006187 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188
6189 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 ++recursive_cnt;
6191 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6192 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006193 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006194
6195 case VAR_FUNC:
6196 return (tv1->vval.v_string != NULL
6197 && tv2->vval.v_string != NULL
6198 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6199
6200 case VAR_NUMBER:
6201 return tv1->vval.v_number == tv2->vval.v_number;
6202
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006203#ifdef FEAT_FLOAT
6204 case VAR_FLOAT:
6205 return tv1->vval.v_float == tv2->vval.v_float;
6206#endif
6207
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208 case VAR_STRING:
6209 s1 = get_tv_string_buf(tv1, buf1);
6210 s2 = get_tv_string_buf(tv2, buf2);
6211 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213
6214 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215 return TRUE;
6216}
6217
6218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 * Locate item with index "n" in list "l" and return it.
6220 * A negative index is counted from the end; -1 is the last item.
6221 * Returns NULL when "n" is out of range.
6222 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006223 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226 long n;
6227{
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 long idx;
6230
6231 if (l == NULL)
6232 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006233
6234 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006235 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006236 n = l->lv_len + n;
6237
6238 /* Check for index out of range. */
6239 if (n < 0 || n >= l->lv_len)
6240 return NULL;
6241
6242 /* When there is a cached index may start search from there. */
6243 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006245 if (n < l->lv_idx / 2)
6246 {
6247 /* closest to the start of the list */
6248 item = l->lv_first;
6249 idx = 0;
6250 }
6251 else if (n > (l->lv_idx + l->lv_len) / 2)
6252 {
6253 /* closest to the end of the list */
6254 item = l->lv_last;
6255 idx = l->lv_len - 1;
6256 }
6257 else
6258 {
6259 /* closest to the cached index */
6260 item = l->lv_idx_item;
6261 idx = l->lv_idx;
6262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 }
6264 else
6265 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006266 if (n < l->lv_len / 2)
6267 {
6268 /* closest to the start of the list */
6269 item = l->lv_first;
6270 idx = 0;
6271 }
6272 else
6273 {
6274 /* closest to the end of the list */
6275 item = l->lv_last;
6276 idx = l->lv_len - 1;
6277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279
6280 while (n > idx)
6281 {
6282 /* search forward */
6283 item = item->li_next;
6284 ++idx;
6285 }
6286 while (n < idx)
6287 {
6288 /* search backward */
6289 item = item->li_prev;
6290 --idx;
6291 }
6292
6293 /* cache the used index */
6294 l->lv_idx = idx;
6295 l->lv_idx_item = item;
6296
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 return item;
6298}
6299
6300/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006301 * Get list item "l[idx]" as a number.
6302 */
6303 static long
6304list_find_nr(l, idx, errorp)
6305 list_T *l;
6306 long idx;
6307 int *errorp; /* set to TRUE when something wrong */
6308{
6309 listitem_T *li;
6310
6311 li = list_find(l, idx);
6312 if (li == NULL)
6313 {
6314 if (errorp != NULL)
6315 *errorp = TRUE;
6316 return -1L;
6317 }
6318 return get_tv_number_chk(&li->li_tv, errorp);
6319}
6320
6321/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006322 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6323 */
6324 char_u *
6325list_find_str(l, idx)
6326 list_T *l;
6327 long idx;
6328{
6329 listitem_T *li;
6330
6331 li = list_find(l, idx - 1);
6332 if (li == NULL)
6333 {
6334 EMSGN(_(e_listidx), idx);
6335 return NULL;
6336 }
6337 return get_tv_string(&li->li_tv);
6338}
6339
6340/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006341 * Locate "item" list "l" and return its index.
6342 * Returns -1 when "item" is not in the list.
6343 */
6344 static long
6345list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 list_T *l;
6347 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006348{
6349 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006351
6352 if (l == NULL)
6353 return -1;
6354 idx = 0;
6355 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6356 ++idx;
6357 if (li == NULL)
6358 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006359 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006360}
6361
6362/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 * Append item "item" to the end of list "l".
6364 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006365 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 list_T *l;
6368 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369{
6370 if (l->lv_last == NULL)
6371 {
6372 /* empty list */
6373 l->lv_first = item;
6374 l->lv_last = item;
6375 item->li_prev = NULL;
6376 }
6377 else
6378 {
6379 l->lv_last->li_next = item;
6380 item->li_prev = l->lv_last;
6381 l->lv_last = item;
6382 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006383 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 item->li_next = NULL;
6385}
6386
6387/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006389 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006391 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 list_T *l;
6394 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006396 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397
Bram Moolenaar05159a02005-02-26 23:04:13 +00006398 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006400 copy_tv(tv, &li->li_tv);
6401 list_append(l, li);
6402 return OK;
6403}
6404
6405/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006406 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006407 * Return FAIL when out of memory.
6408 */
6409 int
6410list_append_dict(list, dict)
6411 list_T *list;
6412 dict_T *dict;
6413{
6414 listitem_T *li = listitem_alloc();
6415
6416 if (li == NULL)
6417 return FAIL;
6418 li->li_tv.v_type = VAR_DICT;
6419 li->li_tv.v_lock = 0;
6420 li->li_tv.vval.v_dict = dict;
6421 list_append(list, li);
6422 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 return OK;
6424}
6425
6426/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006427 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006428 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006429 * Returns FAIL when out of memory.
6430 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006431 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006432list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006433 list_T *l;
6434 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006435 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006436{
6437 listitem_T *li = listitem_alloc();
6438
6439 if (li == NULL)
6440 return FAIL;
6441 list_append(l, li);
6442 li->li_tv.v_type = VAR_STRING;
6443 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006444 if (str == NULL)
6445 li->li_tv.vval.v_string = NULL;
6446 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006447 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 return FAIL;
6449 return OK;
6450}
6451
6452/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 * Append "n" to list "l".
6454 * Returns FAIL when out of memory.
6455 */
6456 static int
6457list_append_number(l, n)
6458 list_T *l;
6459 varnumber_T n;
6460{
6461 listitem_T *li;
6462
6463 li = listitem_alloc();
6464 if (li == NULL)
6465 return FAIL;
6466 li->li_tv.v_type = VAR_NUMBER;
6467 li->li_tv.v_lock = 0;
6468 li->li_tv.vval.v_number = n;
6469 list_append(l, li);
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475 * If "item" is NULL append at the end.
6476 * Return FAIL when out of memory.
6477 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006478 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 list_T *l;
6481 typval_T *tv;
6482 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483{
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485
6486 if (ni == NULL)
6487 return FAIL;
6488 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006489 list_insert(l, ni, item);
6490 return OK;
6491}
6492
6493 void
6494list_insert(l, ni, item)
6495 list_T *l;
6496 listitem_T *ni;
6497 listitem_T *item;
6498{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 if (item == NULL)
6500 /* Append new item at end of list. */
6501 list_append(l, ni);
6502 else
6503 {
6504 /* Insert new item before existing item. */
6505 ni->li_prev = item->li_prev;
6506 ni->li_next = item;
6507 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006508 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 ++l->lv_idx;
6511 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006515 l->lv_idx_item = NULL;
6516 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520}
6521
6522/*
6523 * Extend "l1" with "l2".
6524 * If "bef" is NULL append at the end, otherwise insert before this item.
6525 * Returns FAIL when out of memory.
6526 */
6527 static int
6528list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006529 list_T *l1;
6530 list_T *l2;
6531 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532{
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006534 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006536 /* We also quit the loop when we have inserted the original item count of
6537 * the list, avoid a hang when we extend a list with itself. */
6538 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6540 return FAIL;
6541 return OK;
6542}
6543
6544/*
6545 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6546 * Return FAIL when out of memory.
6547 */
6548 static int
6549list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006550 list_T *l1;
6551 list_T *l2;
6552 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006556 if (l1 == NULL || l2 == NULL)
6557 return FAIL;
6558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (l == NULL)
6562 return FAIL;
6563 tv->v_type = VAR_LIST;
6564 tv->vval.v_list = l;
6565
6566 /* append all items from the second list */
6567 return list_extend(l, l2, NULL);
6568}
6569
6570/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * Returns NULL when out of memory.
6575 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581{
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 list_T *copy;
6583 listitem_T *item;
6584 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 if (orig == NULL)
6587 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 copy = list_alloc();
6590 if (copy != NULL)
6591 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 if (copyID != 0)
6593 {
6594 /* Do this before adding the items, because one of the items may
6595 * refer back to this list. */
6596 orig->lv_copyID = copyID;
6597 orig->lv_copylist = copy;
6598 }
6599 for (item = orig->lv_first; item != NULL && !got_int;
6600 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 {
6602 ni = listitem_alloc();
6603 if (ni == NULL)
6604 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 {
6607 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6608 {
6609 vim_free(ni);
6610 break;
6611 }
6612 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006614 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 list_append(copy, ni);
6616 }
6617 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618 if (item != NULL)
6619 {
6620 list_unref(copy);
6621 copy = NULL;
6622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 }
6624
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 return copy;
6626}
6627
6628/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006630 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006631 * This used to be called list_remove, but that conflicts with a Sun header
6632 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006634 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006635vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 list_T *l;
6637 listitem_T *item;
6638 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639{
Bram Moolenaar33570922005-01-25 22:26:29 +00006640 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 /* notify watchers */
6643 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006645 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646 list_fix_watch(l, ip);
6647 if (ip == item2)
6648 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650
6651 if (item2->li_next == NULL)
6652 l->lv_last = item->li_prev;
6653 else
6654 item2->li_next->li_prev = item->li_prev;
6655 if (item->li_prev == NULL)
6656 l->lv_first = item2->li_next;
6657 else
6658 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006659 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660}
6661
6662/*
6663 * Return an allocated string with the string representation of a list.
6664 * May return NULL.
6665 */
6666 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006667list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
6671 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
6673 if (tv->vval.v_list == NULL)
6674 return NULL;
6675 ga_init2(&ga, (int)sizeof(char), 80);
6676 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 {
6679 vim_free(ga.ga_data);
6680 return NULL;
6681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 ga_append(&ga, ']');
6683 ga_append(&ga, NUL);
6684 return (char_u *)ga.ga_data;
6685}
6686
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687typedef struct join_S {
6688 char_u *s;
6689 char_u *tofree;
6690} join_T;
6691
6692 static int
6693list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6694 garray_T *gap; /* to store the result in */
6695 list_T *l;
6696 char_u *sep;
6697 int echo_style;
6698 int copyID;
6699 garray_T *join_gap; /* to keep each list item string */
6700{
6701 int i;
6702 join_T *p;
6703 int len;
6704 int sumlen = 0;
6705 int first = TRUE;
6706 char_u *tofree;
6707 char_u numbuf[NUMBUFLEN];
6708 listitem_T *item;
6709 char_u *s;
6710
6711 /* Stringify each item in the list. */
6712 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6713 {
6714 if (echo_style)
6715 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6716 else
6717 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6718 if (s == NULL)
6719 return FAIL;
6720
6721 len = (int)STRLEN(s);
6722 sumlen += len;
6723
6724 ga_grow(join_gap, 1);
6725 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6726 if (tofree != NULL || s != numbuf)
6727 {
6728 p->s = s;
6729 p->tofree = tofree;
6730 }
6731 else
6732 {
6733 p->s = vim_strnsave(s, len);
6734 p->tofree = p->s;
6735 }
6736
6737 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006738 if (did_echo_string_emsg) /* recursion error, bail out */
6739 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740 }
6741
6742 /* Allocate result buffer with its total size, avoid re-allocation and
6743 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6744 if (join_gap->ga_len >= 2)
6745 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6746 if (ga_grow(gap, sumlen + 2) == FAIL)
6747 return FAIL;
6748
6749 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6750 {
6751 if (first)
6752 first = FALSE;
6753 else
6754 ga_concat(gap, sep);
6755 p = ((join_T *)join_gap->ga_data) + i;
6756
6757 if (p->s != NULL)
6758 ga_concat(gap, p->s);
6759 line_breakcheck();
6760 }
6761
6762 return OK;
6763}
6764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006767 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006770 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006771list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006773 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006775 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006776 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778 garray_T join_ga;
6779 int retval;
6780 join_T *p;
6781 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6784 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6785
6786 /* Dispose each item in join_ga. */
6787 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789 p = (join_T *)join_ga.ga_data;
6790 for (i = 0; i < join_ga.ga_len; ++i)
6791 {
6792 vim_free(p->tofree);
6793 ++p;
6794 }
6795 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006797
6798 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799}
6800
6801/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006802 * Garbage collection for lists and dictionaries.
6803 *
6804 * We use reference counts to be able to free most items right away when they
6805 * are no longer used. But for composite items it's possible that it becomes
6806 * unused while the reference count is > 0: When there is a recursive
6807 * reference. Example:
6808 * :let l = [1, 2, 3]
6809 * :let d = {9: l}
6810 * :let l[1] = d
6811 *
6812 * Since this is quite unusual we handle this with garbage collection: every
6813 * once in a while find out which lists and dicts are not referenced from any
6814 * variable.
6815 *
6816 * Here is a good reference text about garbage collection (refers to Python
6817 * but it applies to all reference-counting mechanisms):
6818 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006819 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006820
6821/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 * Do garbage collection for lists and dicts.
6823 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006824 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825 int
6826garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006828 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006829 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 buf_T *buf;
6831 win_T *wp;
6832 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006833 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006834 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006836#ifdef FEAT_WINDOWS
6837 tabpage_T *tp;
6838#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006840 /* Only do this once. */
6841 want_garbage_collect = FALSE;
6842 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006843 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006844
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006845 /* We advance by two because we add one for items referenced through
6846 * previous_funccal. */
6847 current_copyID += COPYID_INC;
6848 copyID = current_copyID;
6849
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 /*
6851 * 1. Go through all accessible variables and mark all lists and dicts
6852 * with copyID.
6853 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854
6855 /* Don't free variables in the previous_funccal list unless they are only
6856 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006857 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6859 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006860 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6861 NULL);
6862 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6863 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 }
6865
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 /* script-local variables */
6867 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006868 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869
6870 /* buffer-local variables */
6871 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6873 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874
6875 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006876 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6878 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006879#ifdef FEAT_AUTOCMD
6880 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006883#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006885#ifdef FEAT_WINDOWS
6886 /* tabpage-local variables */
6887 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6889 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006890#endif
6891
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* function-local variables */
6896 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6897 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6899 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 }
6901
Bram Moolenaard812df62008-11-09 12:46:09 +00006902 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006904
Bram Moolenaar1dced572012-04-05 16:54:08 +02006905#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006907#endif
6908
Bram Moolenaardb913952012-06-29 12:54:53 +02006909#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006911#endif
6912
6913#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#endif
6916
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006918 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 /*
6920 * 2. Free lists and dictionaries that are not referenced.
6921 */
6922 did_free = free_unref_items(copyID);
6923
6924 /*
6925 * 3. Check if any funccal can be freed now.
6926 */
6927 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006928 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 if (can_free_funccal(*pfc, copyID))
6930 {
6931 fc = *pfc;
6932 *pfc = fc->caller;
6933 free_funccal(fc, TRUE);
6934 did_free = TRUE;
6935 did_free_funccal = TRUE;
6936 }
6937 else
6938 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006939 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 if (did_free_funccal)
6941 /* When a funccal was freed some more items might be garbage
6942 * collected, so run again. */
6943 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 else if (p_verbose > 0)
6946 {
6947 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6948 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949
6950 return did_free;
6951}
6952
6953/*
6954 * Free lists and dictionaries that are no longer referenced.
6955 */
6956 static int
6957free_unref_items(copyID)
6958 int copyID;
6959{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006960 dict_T *dd, *dd_next;
6961 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962 int did_free = FALSE;
6963
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 */
6967 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006968 {
6969 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006972 /* Free the Dictionary and ordinary items it contains, but don't
6973 * recurse into Lists and Dictionaries, they will be in the list
6974 * of dicts or list of lists. */
6975 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006978 dd = dd_next;
6979 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980
6981 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 * Go through the list of lists and free items without the copyID.
6983 * But don't free a list that has a watcher (used in a for loop), these
6984 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 */
6986 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 {
6988 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6990 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 /* Free the List and ordinary items it contains, but don't recurse
6993 * into Lists and Dictionaries, they will be in the list of dicts
6994 * or list of lists. */
6995 list_free(ll, 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 ll = ll_next;
6999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 return did_free;
7001}
7002
7003/*
7004 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 * "list_stack" is used to add lists to be marked. Can be NULL.
7006 *
7007 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 int
7010set_ref_in_ht(ht, copyID, list_stack)
7011 hashtab_T *ht;
7012 int copyID;
7013 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014{
7015 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 hashtab_T *cur_ht;
7019 ht_stack_T *ht_stack = NULL;
7020 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 cur_ht = ht;
7023 for (;;)
7024 {
7025 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 /* Mark each item in the hashtab. If the item contains a hashtab
7028 * it is added to ht_stack, if it contains a list it is added to
7029 * list_stack. */
7030 todo = (int)cur_ht->ht_used;
7031 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7032 if (!HASHITEM_EMPTY(hi))
7033 {
7034 --todo;
7035 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7036 &ht_stack, list_stack);
7037 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039
7040 if (ht_stack == NULL)
7041 break;
7042
7043 /* take an item from the stack */
7044 cur_ht = ht_stack->ht;
7045 tempitem = ht_stack;
7046 ht_stack = ht_stack->prev;
7047 free(tempitem);
7048 }
7049
7050 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051}
7052
7053/*
7054 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7056 *
7057 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 int
7060set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061 list_T *l;
7062 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 listitem_T *li;
7066 int abort = FALSE;
7067 list_T *cur_l;
7068 list_stack_T *list_stack = NULL;
7069 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 cur_l = l;
7072 for (;;)
7073 {
7074 if (!abort)
7075 /* Mark each item in the list. If the item contains a hashtab
7076 * it is added to ht_stack, if it contains a list it is added to
7077 * list_stack. */
7078 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7079 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7080 ht_stack, &list_stack);
7081 if (list_stack == NULL)
7082 break;
7083
7084 /* take an item from the stack */
7085 cur_l = list_stack->list;
7086 tempitem = list_stack;
7087 list_stack = list_stack->prev;
7088 free(tempitem);
7089 }
7090
7091 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092}
7093
7094/*
7095 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 * "list_stack" is used to add lists to be marked. Can be NULL.
7097 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7098 *
7099 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 int
7102set_ref_in_item(tv, copyID, ht_stack, list_stack)
7103 typval_T *tv;
7104 int copyID;
7105 ht_stack_T **ht_stack;
7106 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107{
7108 dict_T *dd;
7109 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007111
7112 switch (tv->v_type)
7113 {
7114 case VAR_DICT:
7115 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007116 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118 /* Didn't see this dict yet. */
7119 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 if (ht_stack == NULL)
7121 {
7122 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7123 }
7124 else
7125 {
7126 ht_stack_T *newitem = (ht_stack_T*)malloc(
7127 sizeof(ht_stack_T));
7128 if (newitem == NULL)
7129 abort = TRUE;
7130 else
7131 {
7132 newitem->ht = &dd->dv_hashtab;
7133 newitem->prev = *ht_stack;
7134 *ht_stack = newitem;
7135 }
7136 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007138 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007139
7140 case VAR_LIST:
7141 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007142 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007144 /* Didn't see this list yet. */
7145 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 if (list_stack == NULL)
7147 {
7148 abort = set_ref_in_list(ll, copyID, ht_stack);
7149 }
7150 else
7151 {
7152 list_stack_T *newitem = (list_stack_T*)malloc(
7153 sizeof(list_stack_T));
7154 if (newitem == NULL)
7155 abort = TRUE;
7156 else
7157 {
7158 newitem->list = ll;
7159 newitem->prev = *list_stack;
7160 *list_stack = newitem;
7161 }
7162 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007163 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167}
7168
7169/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 * Allocate an empty header for a dictionary.
7171 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007172 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173dict_alloc()
7174{
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007179 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007180 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007181 if (first_dict != NULL)
7182 first_dict->dv_used_prev = d;
7183 d->dv_used_next = first_dict;
7184 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007185 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007188 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007189 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007190 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194}
7195
7196/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007197 * Allocate an empty dict for a return value.
7198 * Returns OK or FAIL.
7199 */
7200 static int
7201rettv_dict_alloc(rettv)
7202 typval_T *rettv;
7203{
7204 dict_T *d = dict_alloc();
7205
7206 if (d == NULL)
7207 return FAIL;
7208
7209 rettv->vval.v_dict = d;
7210 rettv->v_type = VAR_DICT;
7211 ++d->dv_refcount;
7212 return OK;
7213}
7214
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Unreference a Dictionary: decrement the reference count and free it when it
7218 * becomes zero.
7219 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007220 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007224 if (d != NULL && --d->dv_refcount <= 0)
7225 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
7228/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007229 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 * Ignores the reference count.
7231 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007232 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007233dict_free(d, recurse)
7234 dict_T *d;
7235 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007239 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241 /* Remove the dict from the list of dicts for garbage collection. */
7242 if (d->dv_used_prev == NULL)
7243 first_dict = d->dv_used_next;
7244 else
7245 d->dv_used_prev->dv_used_next = d->dv_used_next;
7246 if (d->dv_used_next != NULL)
7247 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7248
7249 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007250 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007251 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007254 if (!HASHITEM_EMPTY(hi))
7255 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007256 /* Remove the item before deleting it, just in case there is
7257 * something recursive causing trouble. */
7258 di = HI2DI(hi);
7259 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007260 if (recurse || (di->di_tv.v_type != VAR_LIST
7261 && di->di_tv.v_type != VAR_DICT))
7262 clear_tv(&di->di_tv);
7263 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 --todo;
7265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 vim_free(d);
7269}
7270
7271/*
7272 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 * The "key" is copied to the new item.
7274 * Note that the value of the item "di_tv" still needs to be initialized!
7275 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007277 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278dictitem_alloc(key)
7279 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007287 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290}
7291
7292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 * Make a copy of a Dictionary item.
7294 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298{
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7302 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 if (di != NULL)
7304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007306 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307 copy_tv(&org->di_tv, &di->di_tv);
7308 }
7309 return di;
7310}
7311
7312/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 * Remove item "item" from Dictionary "dict" and free it.
7314 */
7315 static void
7316dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *dict;
7318 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319{
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (HASHITEM_EMPTY(hi))
7324 EMSG2(_(e_intern2), "dictitem_remove()");
7325 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 dictitem_free(item);
7328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Free a dict item. Also clears the value.
7332 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007333 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 if (item->di_flags & DI_FLAGS_ALLOC)
7339 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7344 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 * Returns NULL when out of memory.
7347 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007349dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007352 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353{
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 dict_T *copy;
7355 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358
7359 if (orig == NULL)
7360 return NULL;
7361
7362 copy = dict_alloc();
7363 if (copy != NULL)
7364 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007365 if (copyID != 0)
7366 {
7367 orig->dv_copyID = copyID;
7368 orig->dv_copydict = copy;
7369 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007370 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007371 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 --todo;
7376
7377 di = dictitem_alloc(hi->hi_key);
7378 if (di == NULL)
7379 break;
7380 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 {
7382 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7383 copyID) == FAIL)
7384 {
7385 vim_free(di);
7386 break;
7387 }
7388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 else
7390 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7391 if (dict_add(copy, di) == FAIL)
7392 {
7393 dictitem_free(di);
7394 break;
7395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007397 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 if (todo > 0)
7401 {
7402 dict_unref(copy);
7403 copy = NULL;
7404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 }
7406
7407 return copy;
7408}
7409
7410/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007412 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007414 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 dict_T *d;
7417 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418{
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420}
7421
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007423 * Add a number or string entry to dictionary "d".
7424 * When "str" is NULL use number "nr", otherwise use "str".
7425 * Returns FAIL when out of memory and when key already exists.
7426 */
7427 int
7428dict_add_nr_str(d, key, nr, str)
7429 dict_T *d;
7430 char *key;
7431 long nr;
7432 char_u *str;
7433{
7434 dictitem_T *item;
7435
7436 item = dictitem_alloc((char_u *)key);
7437 if (item == NULL)
7438 return FAIL;
7439 item->di_tv.v_lock = 0;
7440 if (str == NULL)
7441 {
7442 item->di_tv.v_type = VAR_NUMBER;
7443 item->di_tv.vval.v_number = nr;
7444 }
7445 else
7446 {
7447 item->di_tv.v_type = VAR_STRING;
7448 item->di_tv.vval.v_string = vim_strsave(str);
7449 }
7450 if (dict_add(d, item) == FAIL)
7451 {
7452 dictitem_free(item);
7453 return FAIL;
7454 }
7455 return OK;
7456}
7457
7458/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007459 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007460 * Returns FAIL when out of memory and when key already exists.
7461 */
7462 int
7463dict_add_list(d, key, list)
7464 dict_T *d;
7465 char *key;
7466 list_T *list;
7467{
7468 dictitem_T *item;
7469
7470 item = dictitem_alloc((char_u *)key);
7471 if (item == NULL)
7472 return FAIL;
7473 item->di_tv.v_lock = 0;
7474 item->di_tv.v_type = VAR_LIST;
7475 item->di_tv.vval.v_list = list;
7476 if (dict_add(d, item) == FAIL)
7477 {
7478 dictitem_free(item);
7479 return FAIL;
7480 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007481 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007482 return OK;
7483}
7484
7485/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486 * Get the number of items in a Dictionary.
7487 */
7488 static long
7489dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 if (d == NULL)
7493 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007494 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495}
7496
7497/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 * Find item "key[len]" in Dictionary "d".
7499 * If "len" is negative use strlen(key).
7500 * Returns NULL when not found.
7501 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007502 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 char_u *key;
7506 int len;
7507{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508#define AKEYLEN 200
7509 char_u buf[AKEYLEN];
7510 char_u *akey;
7511 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 if (len < 0)
7515 akey = key;
7516 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 tofree = akey = vim_strnsave(key, len);
7519 if (akey == NULL)
7520 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007521 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 else
7523 {
7524 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007525 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 akey = buf;
7527 }
7528
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 vim_free(tofree);
7531 if (HASHITEM_EMPTY(hi))
7532 return NULL;
7533 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534}
7535
7536/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007537 * Get a string item from a dictionary.
7538 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007539 * Returns NULL if the entry doesn't exist or out of memory.
7540 */
7541 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007542get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543 dict_T *d;
7544 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007545 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546{
7547 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007548 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007549
7550 di = dict_find(d, key, -1);
7551 if (di == NULL)
7552 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007553 s = get_tv_string(&di->di_tv);
7554 if (save && s != NULL)
7555 s = vim_strsave(s);
7556 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007557}
7558
7559/*
7560 * Get a number item from a dictionary.
7561 * Returns 0 if the entry doesn't exist or out of memory.
7562 */
7563 long
7564get_dict_number(d, key)
7565 dict_T *d;
7566 char_u *key;
7567{
7568 dictitem_T *di;
7569
7570 di = dict_find(d, key, -1);
7571 if (di == NULL)
7572 return 0;
7573 return get_tv_number(&di->di_tv);
7574}
7575
7576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 * Return an allocated string with the string representation of a Dictionary.
7578 * May return NULL.
7579 */
7580 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007581dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584{
7585 garray_T ga;
7586 int first = TRUE;
7587 char_u *tofree;
7588 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 return NULL;
7596 ga_init2(&ga, (int)sizeof(char), 80);
7597 ga_append(&ga, '{');
7598
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007599 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 --todo;
7605
7606 if (first)
7607 first = FALSE;
7608 else
7609 ga_concat(&ga, (char_u *)", ");
7610
7611 tofree = string_quote(hi->hi_key, FALSE);
7612 if (tofree != NULL)
7613 {
7614 ga_concat(&ga, tofree);
7615 vim_free(tofree);
7616 }
7617 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 if (s != NULL)
7620 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007622 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007623 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007624 line_breakcheck();
7625
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007628 if (todo > 0)
7629 {
7630 vim_free(ga.ga_data);
7631 return NULL;
7632 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633
7634 ga_append(&ga, '}');
7635 ga_append(&ga, NUL);
7636 return (char_u *)ga.ga_data;
7637}
7638
7639/*
7640 * Allocate a variable for a Dictionary and fill it from "*arg".
7641 * Return OK or FAIL. Returns NOTDONE for {expr}.
7642 */
7643 static int
7644get_dict_tv(arg, rettv, evaluate)
7645 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 int evaluate;
7648{
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 dict_T *d = NULL;
7650 typval_T tvkey;
7651 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007652 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656
7657 /*
7658 * First check if it's not a curly-braces thing: {expr}.
7659 * Must do this without evaluating, otherwise a function may be called
7660 * twice. Unfortunately this means we need to call eval1() twice for the
7661 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007662 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 if (*start != '}')
7665 {
7666 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7667 return FAIL;
7668 if (*start == '}')
7669 return NOTDONE;
7670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671
7672 if (evaluate)
7673 {
7674 d = dict_alloc();
7675 if (d == NULL)
7676 return FAIL;
7677 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007678 tvkey.v_type = VAR_UNKNOWN;
7679 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680
7681 *arg = skipwhite(*arg + 1);
7682 while (**arg != '}' && **arg != NUL)
7683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 goto failret;
7686 if (**arg != ':')
7687 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007688 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 goto failret;
7691 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007692 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007694 key = get_tv_string_buf_chk(&tvkey, buf);
7695 if (key == NULL || *key == NUL)
7696 {
7697 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7698 if (key != NULL)
7699 EMSG(_(e_emptykey));
7700 clear_tv(&tvkey);
7701 goto failret;
7702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704
7705 *arg = skipwhite(*arg + 1);
7706 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7707 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007708 if (evaluate)
7709 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 }
7712 if (evaluate)
7713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 if (item != NULL)
7716 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007717 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007718 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 clear_tv(&tv);
7720 goto failret;
7721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007722 item = dictitem_alloc(key);
7723 clear_tv(&tvkey);
7724 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007727 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007728 if (dict_add(d, item) == FAIL)
7729 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 }
7731 }
7732
7733 if (**arg == '}')
7734 break;
7735 if (**arg != ',')
7736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007737 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 goto failret;
7739 }
7740 *arg = skipwhite(*arg + 1);
7741 }
7742
7743 if (**arg != '}')
7744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007745 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746failret:
7747 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007748 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749 return FAIL;
7750 }
7751
7752 *arg = skipwhite(*arg + 1);
7753 if (evaluate)
7754 {
7755 rettv->v_type = VAR_DICT;
7756 rettv->vval.v_dict = d;
7757 ++d->dv_refcount;
7758 }
7759
7760 return OK;
7761}
7762
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007764 * Return a string with the string representation of a variable.
7765 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007766 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007768 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007769 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770 */
7771 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007772echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007775 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007776 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007778 static int recurse = 0;
7779 char_u *r = NULL;
7780
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007782 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007783 if (!did_echo_string_emsg)
7784 {
7785 /* Only give this message once for a recursive call to avoid
7786 * flooding the user with errors. And stop iterating over lists
7787 * and dicts. */
7788 did_echo_string_emsg = TRUE;
7789 EMSG(_("E724: variable nested too deep for displaying"));
7790 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007791 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007792 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 }
7794 ++recurse;
7795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 switch (tv->v_type)
7797 {
7798 case VAR_FUNC:
7799 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 r = tv->vval.v_string;
7801 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804 if (tv->vval.v_list == NULL)
7805 {
7806 *tofree = NULL;
7807 r = NULL;
7808 }
7809 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7810 {
7811 *tofree = NULL;
7812 r = (char_u *)"[...]";
7813 }
7814 else
7815 {
7816 tv->vval.v_list->lv_copyID = copyID;
7817 *tofree = list2string(tv, copyID);
7818 r = *tofree;
7819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007821
Bram Moolenaar8c711452005-01-14 21:53:12 +00007822 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007823 if (tv->vval.v_dict == NULL)
7824 {
7825 *tofree = NULL;
7826 r = NULL;
7827 }
7828 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7829 {
7830 *tofree = NULL;
7831 r = (char_u *)"{...}";
7832 }
7833 else
7834 {
7835 tv->vval.v_dict->dv_copyID = copyID;
7836 *tofree = dict2string(tv, copyID);
7837 r = *tofree;
7838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007839 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 case VAR_STRING:
7842 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 *tofree = NULL;
7844 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847#ifdef FEAT_FLOAT
7848 case VAR_FLOAT:
7849 *tofree = NULL;
7850 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7851 r = numbuf;
7852 break;
7853#endif
7854
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007856 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859
Bram Moolenaar8502c702014-06-17 12:51:16 +02007860 if (--recurse == 0)
7861 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007863}
7864
7865/*
7866 * Return a string with the string representation of a variable.
7867 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7868 * "numbuf" is used for a number.
7869 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007870 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007871 */
7872 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007873tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007875 char_u **tofree;
7876 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007877 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878{
7879 switch (tv->v_type)
7880 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 case VAR_FUNC:
7882 *tofree = string_quote(tv->vval.v_string, TRUE);
7883 return *tofree;
7884 case VAR_STRING:
7885 *tofree = string_quote(tv->vval.v_string, FALSE);
7886 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007887#ifdef FEAT_FLOAT
7888 case VAR_FLOAT:
7889 *tofree = NULL;
7890 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7891 return numbuf;
7892#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007898 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007899 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007900 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901}
7902
7903/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 * Return string "str" in ' quotes, doubling ' characters.
7905 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007906 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 */
7908 static char_u *
7909string_quote(str, function)
7910 char_u *str;
7911 int function;
7912{
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 char_u *p, *r, *s;
7915
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 len = (function ? 13 : 3);
7917 if (str != NULL)
7918 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007919 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 for (p = str; *p != NUL; mb_ptr_adv(p))
7921 if (*p == '\'')
7922 ++len;
7923 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 s = r = alloc(len);
7925 if (r != NULL)
7926 {
7927 if (function)
7928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 r += 10;
7931 }
7932 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007934 if (str != NULL)
7935 for (p = str; *p != NUL; )
7936 {
7937 if (*p == '\'')
7938 *r++ = '\'';
7939 MB_COPY_CHAR(p, r);
7940 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 if (function)
7943 *r++ = ')';
7944 *r++ = NUL;
7945 }
7946 return s;
7947}
7948
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007949#ifdef FEAT_FLOAT
7950/*
7951 * Convert the string "text" to a floating point number.
7952 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7953 * this always uses a decimal point.
7954 * Returns the length of the text that was consumed.
7955 */
7956 static int
7957string2float(text, value)
7958 char_u *text;
7959 float_T *value; /* result stored here */
7960{
7961 char *s = (char *)text;
7962 float_T f;
7963
7964 f = strtod(s, &s);
7965 *value = f;
7966 return (int)((char_u *)s - text);
7967}
7968#endif
7969
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 * Get the value of an environment variable.
7972 * "arg" is pointing to the '$'. It is advanced to after the name.
7973 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007974 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 */
7976 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 int evaluate;
7981{
7982 char_u *string = NULL;
7983 int len;
7984 int cc;
7985 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007986 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987
7988 ++*arg;
7989 name = *arg;
7990 len = get_env_len(arg);
7991 if (evaluate)
7992 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007993 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007994 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007995
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007996 cc = name[len];
7997 name[len] = NUL;
7998 /* first try vim_getenv(), fast for normal environment vars */
7999 string = vim_getenv(name, &mustfree);
8000 if (string != NULL && *string != NUL)
8001 {
8002 if (!mustfree)
8003 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008005 else
8006 {
8007 if (mustfree)
8008 vim_free(string);
8009
8010 /* next try expanding things like $VIM and ${HOME} */
8011 string = expand_env_save(name - 1);
8012 if (string != NULL && *string == '$')
8013 {
8014 vim_free(string);
8015 string = NULL;
8016 }
8017 }
8018 name[len] = cc;
8019
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->v_type = VAR_STRING;
8021 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023
8024 return OK;
8025}
8026
8027/*
8028 * Array with names and number of arguments of all internal functions
8029 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8030 */
8031static struct fst
8032{
8033 char *f_name; /* function name */
8034 char f_min_argc; /* minimal number of arguments */
8035 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008037 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038} functions[] =
8039{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008040#ifdef FEAT_FLOAT
8041 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008042 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008043#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008044 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008045 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {"append", 2, 2, f_append},
8047 {"argc", 0, 0, f_argc},
8048 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008049 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008050 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008051#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008052 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008057 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"bufexists", 1, 1, f_bufexists},
8059 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8060 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8061 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8062 {"buflisted", 1, 1, f_buflisted},
8063 {"bufloaded", 1, 1, f_bufloaded},
8064 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008065 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"bufwinnr", 1, 1, f_bufwinnr},
8067 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008068 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008069 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071#ifdef FEAT_FLOAT
8072 {"ceil", 1, 1, f_ceil},
8073#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008074 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008075 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008077 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008079#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008080 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008081 {"complete_add", 1, 1, f_complete_add},
8082 {"complete_check", 0, 0, f_complete_check},
8083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008085 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008088 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008089#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008090 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008092 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008093 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"delete", 1, 1, f_delete},
8095 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008096 {"diff_filler", 1, 1, f_diff_filler},
8097 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008098 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008100 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"eventhandler", 0, 0, f_eventhandler},
8102 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008103 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105#ifdef FEAT_FLOAT
8106 {"exp", 1, 1, f_exp},
8107#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008108 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008109 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008110 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8112 {"filereadable", 1, 1, f_filereadable},
8113 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008114 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008115 {"finddir", 1, 3, f_finddir},
8116 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008117#ifdef FEAT_FLOAT
8118 {"float2nr", 1, 1, f_float2nr},
8119 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008120 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008121#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008122 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"fnamemodify", 2, 2, f_fnamemodify},
8124 {"foldclosed", 1, 1, f_foldclosed},
8125 {"foldclosedend", 1, 1, f_foldclosedend},
8126 {"foldlevel", 1, 1, f_foldlevel},
8127 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008128 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008131 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008132 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008133 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008134 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"getchar", 0, 1, f_getchar},
8136 {"getcharmod", 0, 0, f_getcharmod},
8137 {"getcmdline", 0, 0, f_getcmdline},
8138 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008139 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008140 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008141 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008143 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008144 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"getfsize", 1, 1, f_getfsize},
8146 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008147 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008148 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008149 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008150 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008151 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008152 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008153 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008154 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008156 {"gettabvar", 2, 3, f_gettabvar},
8157 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"getwinposx", 0, 0, f_getwinposx},
8159 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008160 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008161 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008162 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008163 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008166 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008167 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8169 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8170 {"histadd", 2, 2, f_histadd},
8171 {"histdel", 1, 2, f_histdel},
8172 {"histget", 1, 2, f_histget},
8173 {"histnr", 1, 1, f_histnr},
8174 {"hlID", 1, 1, f_hlID},
8175 {"hlexists", 1, 1, f_hlexists},
8176 {"hostname", 0, 0, f_hostname},
8177 {"iconv", 3, 3, f_iconv},
8178 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008179 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008180 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008182 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"inputrestore", 0, 0, f_inputrestore},
8184 {"inputsave", 0, 0, f_inputsave},
8185 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008187 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008189 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008190 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008191 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008192 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008194 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"libcall", 3, 3, f_libcall},
8196 {"libcallnr", 3, 3, f_libcallnr},
8197 {"line", 1, 1, f_line},
8198 {"line2byte", 1, 1, f_line2byte},
8199 {"lispindent", 1, 1, f_lispindent},
8200 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008202 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203 {"log10", 1, 1, f_log10},
8204#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008205#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008206 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008207#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008208 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008209 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008210 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008211 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008212 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008213 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008214 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008215 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008216 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008217 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008218 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008219 {"max", 1, 1, f_max},
8220 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008221#ifdef vim_mkdir
8222 {"mkdir", 1, 3, f_mkdir},
8223#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008224 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008225#ifdef FEAT_MZSCHEME
8226 {"mzeval", 1, 1, f_mzeval},
8227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008229 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008230 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008231 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232#ifdef FEAT_FLOAT
8233 {"pow", 2, 2, f_pow},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008236 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008237 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008238#ifdef FEAT_PYTHON3
8239 {"py3eval", 1, 1, f_py3eval},
8240#endif
8241#ifdef FEAT_PYTHON
8242 {"pyeval", 1, 1, f_pyeval},
8243#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008244 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008245 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008246 {"reltime", 0, 2, f_reltime},
8247 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {"remote_expr", 2, 3, f_remote_expr},
8249 {"remote_foreground", 1, 1, f_remote_foreground},
8250 {"remote_peek", 1, 2, f_remote_peek},
8251 {"remote_read", 1, 1, f_remote_read},
8252 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008253 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008255 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008257 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258#ifdef FEAT_FLOAT
8259 {"round", 1, 1, f_round},
8260#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008261 {"screenattr", 2, 2, f_screenattr},
8262 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008263 {"screencol", 0, 0, f_screencol},
8264 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008265 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008266 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008267 {"searchpair", 3, 7, f_searchpair},
8268 {"searchpairpos", 3, 7, f_searchpairpos},
8269 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"server2client", 2, 2, f_server2client},
8271 {"serverlist", 0, 0, f_serverlist},
8272 {"setbufvar", 3, 3, f_setbufvar},
8273 {"setcmdpos", 1, 1, f_setcmdpos},
8274 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008275 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008276 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008277 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008278 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008280 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008281 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008283#ifdef FEAT_CRYPT
8284 {"sha256", 1, 1, f_sha256},
8285#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008286 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008287 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008289#ifdef FEAT_FLOAT
8290 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008291 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008292#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008293 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008294 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008295 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008296 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008297 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298#ifdef FEAT_FLOAT
8299 {"sqrt", 1, 1, f_sqrt},
8300 {"str2float", 1, 1, f_str2float},
8301#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008302 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008303 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008304 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305#ifdef HAVE_STRFTIME
8306 {"strftime", 1, 2, f_strftime},
8307#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"strlen", 1, 1, f_strlen},
8311 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008312 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008314 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008315 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"substitute", 4, 4, f_substitute},
8317 {"synID", 3, 3, f_synID},
8318 {"synIDattr", 2, 3, f_synIDattr},
8319 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008320 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008321 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008322 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008323 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008324 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008325 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008326 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008327 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008328 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008329#ifdef FEAT_FLOAT
8330 {"tan", 1, 1, f_tan},
8331 {"tanh", 1, 1, f_tanh},
8332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008334 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"tolower", 1, 1, f_tolower},
8336 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008337 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008338#ifdef FEAT_FLOAT
8339 {"trunc", 1, 1, f_trunc},
8340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008342 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008343 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008344 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008345 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"virtcol", 1, 1, f_virtcol},
8347 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008348 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"winbufnr", 1, 1, f_winbufnr},
8350 {"wincol", 0, 0, f_wincol},
8351 {"winheight", 1, 1, f_winheight},
8352 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008353 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008355 {"winrestview", 1, 1, f_winrestview},
8356 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008358 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008359 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360};
8361
8362#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8363
8364/*
8365 * Function given to ExpandGeneric() to obtain the list of internal
8366 * or user defined function names.
8367 */
8368 char_u *
8369get_function_name(xp, idx)
8370 expand_T *xp;
8371 int idx;
8372{
8373 static int intidx = -1;
8374 char_u *name;
8375
8376 if (idx == 0)
8377 intidx = -1;
8378 if (intidx < 0)
8379 {
8380 name = get_user_func_name(xp, idx);
8381 if (name != NULL)
8382 return name;
8383 }
8384 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8385 {
8386 STRCPY(IObuff, functions[intidx].f_name);
8387 STRCAT(IObuff, "(");
8388 if (functions[intidx].f_max_argc == 0)
8389 STRCAT(IObuff, ")");
8390 return IObuff;
8391 }
8392
8393 return NULL;
8394}
8395
8396/*
8397 * Function given to ExpandGeneric() to obtain the list of internal or
8398 * user defined variable or function names.
8399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 char_u *
8401get_expr_name(xp, idx)
8402 expand_T *xp;
8403 int idx;
8404{
8405 static int intidx = -1;
8406 char_u *name;
8407
8408 if (idx == 0)
8409 intidx = -1;
8410 if (intidx < 0)
8411 {
8412 name = get_function_name(xp, idx);
8413 if (name != NULL)
8414 return name;
8415 }
8416 return get_user_var_name(xp, ++intidx);
8417}
8418
8419#endif /* FEAT_CMDL_COMPL */
8420
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008421#if defined(EBCDIC) || defined(PROTO)
8422/*
8423 * Compare struct fst by function name.
8424 */
8425 static int
8426compare_func_name(s1, s2)
8427 const void *s1;
8428 const void *s2;
8429{
8430 struct fst *p1 = (struct fst *)s1;
8431 struct fst *p2 = (struct fst *)s2;
8432
8433 return STRCMP(p1->f_name, p2->f_name);
8434}
8435
8436/*
8437 * Sort the function table by function name.
8438 * The sorting of the table above is ASCII dependant.
8439 * On machines using EBCDIC we have to sort it.
8440 */
8441 static void
8442sortFunctions()
8443{
8444 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8445
8446 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8447}
8448#endif
8449
8450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451/*
8452 * Find internal function in table above.
8453 * Return index, or -1 if not found
8454 */
8455 static int
8456find_internal_func(name)
8457 char_u *name; /* name of the function */
8458{
8459 int first = 0;
8460 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8461 int cmp;
8462 int x;
8463
8464 /*
8465 * Find the function name in the table. Binary search.
8466 */
8467 while (first <= last)
8468 {
8469 x = first + ((unsigned)(last - first) >> 1);
8470 cmp = STRCMP(name, functions[x].f_name);
8471 if (cmp < 0)
8472 last = x - 1;
8473 else if (cmp > 0)
8474 first = x + 1;
8475 else
8476 return x;
8477 }
8478 return -1;
8479}
8480
8481/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8483 * name it contains, otherwise return "name".
8484 */
8485 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008486deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008487 char_u *name;
8488 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008489 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008490{
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008492 int cc;
8493
8494 cc = name[*lenp];
8495 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008496 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {
8502 *lenp = 0;
8503 return (char_u *)""; /* just in case */
8504 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008505 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 }
8508
8509 return name;
8510}
8511
8512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 * Allocate a variable for the result of a function.
8514 * Return OK or FAIL.
8515 */
8516 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008517get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8518 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 char_u *name; /* name of the function */
8520 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 char_u **arg; /* argument, pointing to the '(' */
8523 linenr_T firstline; /* first line of range */
8524 linenr_T lastline; /* last line of range */
8525 int *doesrange; /* return: function handled range */
8526 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008527 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 char_u *argp;
8530 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008531 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 int argcount = 0; /* number of arguments found */
8533
8534 /*
8535 * Get the arguments.
8536 */
8537 argp = *arg;
8538 while (argcount < MAX_FUNC_ARGS)
8539 {
8540 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8541 if (*argp == ')' || *argp == ',' || *argp == NUL)
8542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8544 {
8545 ret = FAIL;
8546 break;
8547 }
8548 ++argcount;
8549 if (*argp != ',')
8550 break;
8551 }
8552 if (*argp == ')')
8553 ++argp;
8554 else
8555 ret = FAIL;
8556
8557 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008558 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 {
8562 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008563 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008564 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008565 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
8568 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
8571 *arg = skipwhite(argp);
8572 return ret;
8573}
8574
8575
8576/*
8577 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008578 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008579 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 */
8581 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008582call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008584 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008588 typval_T *argvars; /* vars for arguments, must have "argcount"
8589 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 linenr_T firstline; /* first line of range */
8591 linenr_T lastline; /* last line of range */
8592 int *doesrange; /* return: function handled range */
8593 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008594 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
8596 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#define ERROR_UNKNOWN 0
8598#define ERROR_TOOMANY 1
8599#define ERROR_TOOFEW 2
8600#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008601#define ERROR_DICT 4
8602#define ERROR_NONE 5
8603#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 int error = ERROR_NONE;
8605 int i;
8606 int llen;
8607 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608#define FLEN_FIXED 40
8609 char_u fname_buf[FLEN_FIXED + 1];
8610 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008611 char_u *name;
8612
8613 /* Make a copy of the name, if it comes from a funcref variable it could
8614 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008615 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008616 if (name == NULL)
8617 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 /*
8620 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8621 * Change <SNR>123_name() to K_SNR 123_name().
8622 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 llen = eval_fname_script(name);
8625 if (llen > 0)
8626 {
8627 fname_buf[0] = K_SPECIAL;
8628 fname_buf[1] = KS_EXTRA;
8629 fname_buf[2] = (int)KE_SNR;
8630 i = 3;
8631 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8632 {
8633 if (current_SID <= 0)
8634 error = ERROR_SCRIPT;
8635 else
8636 {
8637 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8638 i = (int)STRLEN(fname_buf);
8639 }
8640 }
8641 if (i + STRLEN(name + llen) < FLEN_FIXED)
8642 {
8643 STRCPY(fname_buf + i, name + llen);
8644 fname = fname_buf;
8645 }
8646 else
8647 {
8648 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8649 if (fname == NULL)
8650 error = ERROR_OTHER;
8651 else
8652 {
8653 mch_memmove(fname, fname_buf, (size_t)i);
8654 STRCPY(fname + i, name + llen);
8655 }
8656 }
8657 }
8658 else
8659 fname = name;
8660
8661 *doesrange = FALSE;
8662
8663
8664 /* execute the function if no errors detected and executing */
8665 if (evaluate && error == ERROR_NONE)
8666 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008667 char_u *rfname = fname;
8668
8669 /* Ignore "g:" before a function name. */
8670 if (fname[0] == 'g' && fname[1] == ':')
8671 rfname = fname + 2;
8672
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008673 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8674 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 error = ERROR_UNKNOWN;
8676
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008677 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
8679 /*
8680 * User defined function.
8681 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008682 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685 /* Trigger FuncUndefined event, may load the function. */
8686 if (fp == NULL
8687 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008688 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008689 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008691 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008692 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 }
8694#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008695 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008696 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008697 {
8698 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700 }
8701
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 if (fp != NULL)
8703 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008704 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008706 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008708 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008710 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008711 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 else
8713 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008714 int did_save_redo = FALSE;
8715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 /*
8717 * Call the user function.
8718 * Save and restore search patterns, script variables and
8719 * redo buffer.
8720 */
8721 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008722 if (!ins_compl_active())
8723 {
8724 saveRedobuff();
8725 did_save_redo = TRUE;
8726 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008729 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008730 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8731 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8732 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008733 /* Function was unreferenced while being used, free it
8734 * now. */
8735 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008736 if (did_save_redo)
8737 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 restore_search_patterns();
8739 error = ERROR_NONE;
8740 }
8741 }
8742 }
8743 else
8744 {
8745 /*
8746 * Find the function name in the table, call its implementation.
8747 */
8748 i = find_internal_func(fname);
8749 if (i >= 0)
8750 {
8751 if (argcount < functions[i].f_min_argc)
8752 error = ERROR_TOOFEW;
8753 else if (argcount > functions[i].f_max_argc)
8754 error = ERROR_TOOMANY;
8755 else
8756 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 error = ERROR_NONE;
8760 }
8761 }
8762 }
8763 /*
8764 * The function call (or "FuncUndefined" autocommand sequence) might
8765 * have been aborted by an error, an interrupt, or an explicitly thrown
8766 * exception that has not been caught so far. This situation can be
8767 * tested for by calling aborting(). For an error in an internal
8768 * function or for the "E132" error in call_user_func(), however, the
8769 * throw point at which the "force_abort" flag (temporarily reset by
8770 * emsg()) is normally updated has not been reached yet. We need to
8771 * update that flag first to make aborting() reliable.
8772 */
8773 update_force_abort();
8774 }
8775 if (error == ERROR_NONE)
8776 ret = OK;
8777
8778 /*
8779 * Report an error unless the argument evaluation or function call has been
8780 * cancelled due to an aborting error, an interrupt, or an exception.
8781 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008782 if (!aborting())
8783 {
8784 switch (error)
8785 {
8786 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008787 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008788 break;
8789 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008790 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008791 break;
8792 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008793 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008794 name);
8795 break;
8796 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008797 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008798 name);
8799 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008800 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008801 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 name);
8803 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008804 }
8805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (fname != name && fname != fname_buf)
8808 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008809 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
8811 return ret;
8812}
8813
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008814/*
8815 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008816 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008817 */
8818 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008819emsg_funcname(ermsg, name)
8820 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008821 char_u *name;
8822{
8823 char_u *p;
8824
8825 if (*name == K_SPECIAL)
8826 p = concat_str((char_u *)"<SNR>", name + 3);
8827 else
8828 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008829 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008830 if (p != name)
8831 vim_free(p);
8832}
8833
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008834/*
8835 * Return TRUE for a non-zero Number and a non-empty String.
8836 */
8837 static int
8838non_zero_arg(argvars)
8839 typval_T *argvars;
8840{
8841 return ((argvars[0].v_type == VAR_NUMBER
8842 && argvars[0].vval.v_number != 0)
8843 || (argvars[0].v_type == VAR_STRING
8844 && argvars[0].vval.v_string != NULL
8845 && *argvars[0].vval.v_string != NUL));
8846}
8847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848/*********************************************
8849 * Implementation of the built-in functions
8850 */
8851
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008852#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008853static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8854
8855/*
8856 * Get the float value of "argvars[0]" into "f".
8857 * Returns FAIL when the argument is not a Number or Float.
8858 */
8859 static int
8860get_float_arg(argvars, f)
8861 typval_T *argvars;
8862 float_T *f;
8863{
8864 if (argvars[0].v_type == VAR_FLOAT)
8865 {
8866 *f = argvars[0].vval.v_float;
8867 return OK;
8868 }
8869 if (argvars[0].v_type == VAR_NUMBER)
8870 {
8871 *f = (float_T)argvars[0].vval.v_number;
8872 return OK;
8873 }
8874 EMSG(_("E808: Number or Float required"));
8875 return FAIL;
8876}
8877
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878/*
8879 * "abs(expr)" function
8880 */
8881 static void
8882f_abs(argvars, rettv)
8883 typval_T *argvars;
8884 typval_T *rettv;
8885{
8886 if (argvars[0].v_type == VAR_FLOAT)
8887 {
8888 rettv->v_type = VAR_FLOAT;
8889 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8890 }
8891 else
8892 {
8893 varnumber_T n;
8894 int error = FALSE;
8895
8896 n = get_tv_number_chk(&argvars[0], &error);
8897 if (error)
8898 rettv->vval.v_number = -1;
8899 else if (n > 0)
8900 rettv->vval.v_number = n;
8901 else
8902 rettv->vval.v_number = -n;
8903 }
8904}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008905
8906/*
8907 * "acos()" function
8908 */
8909 static void
8910f_acos(argvars, rettv)
8911 typval_T *argvars;
8912 typval_T *rettv;
8913{
8914 float_T f;
8915
8916 rettv->v_type = VAR_FLOAT;
8917 if (get_float_arg(argvars, &f) == OK)
8918 rettv->vval.v_float = acos(f);
8919 else
8920 rettv->vval.v_float = 0.0;
8921}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008922#endif
8923
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008925 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 */
8927 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008928f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008929 typval_T *argvars;
8930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008935 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008937 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008938 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008939 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008941 }
8942 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943 EMSG(_(e_listreq));
8944}
8945
8946/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008947 * "and(expr, expr)" function
8948 */
8949 static void
8950f_and(argvars, rettv)
8951 typval_T *argvars;
8952 typval_T *rettv;
8953{
8954 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8955 & get_tv_number_chk(&argvars[1], NULL);
8956}
8957
8958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 * "append(lnum, string/list)" function
8960 */
8961 static void
8962f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008965{
8966 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 list_T *l = NULL;
8969 listitem_T *li = NULL;
8970 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008971 long added = 0;
8972
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008973 /* When coming here from Insert mode, sync undo, so that this can be
8974 * undone separately from what was previously inserted. */
8975 if (u_sync_once == 2)
8976 {
8977 u_sync_once = 1; /* notify that u_sync() was called */
8978 u_sync(TRUE);
8979 }
8980
Bram Moolenaar0d660222005-01-07 21:51:51 +00008981 lnum = get_tv_lnum(argvars);
8982 if (lnum >= 0
8983 && lnum <= curbuf->b_ml.ml_line_count
8984 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008985 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008986 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008988 l = argvars[1].vval.v_list;
8989 if (l == NULL)
8990 return;
8991 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993 for (;;)
8994 {
8995 if (l == NULL)
8996 tv = &argvars[1]; /* append a string */
8997 else if (li == NULL)
8998 break; /* end of list */
8999 else
9000 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 line = get_tv_string_chk(tv);
9002 if (line == NULL) /* type error */
9003 {
9004 rettv->vval.v_number = 1; /* Failed */
9005 break;
9006 }
9007 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 ++added;
9009 if (l == NULL)
9010 break;
9011 li = li->li_next;
9012 }
9013
9014 appended_lines_mark(lnum, added);
9015 if (curwin->w_cursor.lnum > lnum)
9016 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018 else
9019 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020}
9021
9022/*
9023 * "argc()" function
9024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009027 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031}
9032
9033/*
9034 * "argidx()" function
9035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042}
9043
9044/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009045 * "arglistid()" function
9046 */
9047 static void
9048f_arglistid(argvars, rettv)
9049 typval_T *argvars UNUSED;
9050 typval_T *rettv;
9051{
9052 win_T *wp;
9053 tabpage_T *tp = NULL;
9054 long n;
9055
9056 rettv->vval.v_number = -1;
9057 if (argvars[0].v_type != VAR_UNKNOWN)
9058 {
9059 if (argvars[1].v_type != VAR_UNKNOWN)
9060 {
9061 n = get_tv_number(&argvars[1]);
9062 if (n >= 0)
9063 tp = find_tabpage(n);
9064 }
9065 else
9066 tp = curtab;
9067
9068 if (tp != NULL)
9069 {
9070 wp = find_win_by_nr(&argvars[0], tp);
9071 if (wp != NULL)
9072 rettv->vval.v_number = wp->w_alist->id;
9073 }
9074 }
9075 else
9076 rettv->vval.v_number = curwin->w_alist->id;
9077}
9078
9079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 * "argv(nr)" function
9081 */
9082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087 int idx;
9088
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009089 if (argvars[0].v_type != VAR_UNKNOWN)
9090 {
9091 idx = get_tv_number_chk(&argvars[0], NULL);
9092 if (idx >= 0 && idx < ARGCOUNT)
9093 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9094 else
9095 rettv->vval.v_string = NULL;
9096 rettv->v_type = VAR_STRING;
9097 }
9098 else if (rettv_list_alloc(rettv) == OK)
9099 for (idx = 0; idx < ARGCOUNT; ++idx)
9100 list_append_string(rettv->vval.v_list,
9101 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009104#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009105/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009106 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009107 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009108 static void
9109f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009110 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009111 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009112{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009113 float_T f;
9114
9115 rettv->v_type = VAR_FLOAT;
9116 if (get_float_arg(argvars, &f) == OK)
9117 rettv->vval.v_float = asin(f);
9118 else
9119 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009120}
9121
9122/*
9123 * "atan()" function
9124 */
9125 static void
9126f_atan(argvars, rettv)
9127 typval_T *argvars;
9128 typval_T *rettv;
9129{
9130 float_T f;
9131
9132 rettv->v_type = VAR_FLOAT;
9133 if (get_float_arg(argvars, &f) == OK)
9134 rettv->vval.v_float = atan(f);
9135 else
9136 rettv->vval.v_float = 0.0;
9137}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009138
9139/*
9140 * "atan2()" function
9141 */
9142 static void
9143f_atan2(argvars, rettv)
9144 typval_T *argvars;
9145 typval_T *rettv;
9146{
9147 float_T fx, fy;
9148
9149 rettv->v_type = VAR_FLOAT;
9150 if (get_float_arg(argvars, &fx) == OK
9151 && get_float_arg(&argvars[1], &fy) == OK)
9152 rettv->vval.v_float = atan2(fx, fy);
9153 else
9154 rettv->vval.v_float = 0.0;
9155}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009156#endif
9157
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158/*
9159 * "browse(save, title, initdir, default)" function
9160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166#ifdef FEAT_BROWSE
9167 int save;
9168 char_u *title;
9169 char_u *initdir;
9170 char_u *defname;
9171 char_u buf[NUMBUFLEN];
9172 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009173 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 save = get_tv_number_chk(&argvars[0], &error);
9176 title = get_tv_string_chk(&argvars[1]);
9177 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9178 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 if (error || title == NULL || initdir == NULL || defname == NULL)
9181 rettv->vval.v_string = NULL;
9182 else
9183 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009184 do_browse(save ? BROWSE_SAVE : 0,
9185 title, defname, NULL, initdir, NULL, curbuf);
9186#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009190}
9191
9192/*
9193 * "browsedir(title, initdir)" function
9194 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009197 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009199{
9200#ifdef FEAT_BROWSE
9201 char_u *title;
9202 char_u *initdir;
9203 char_u buf[NUMBUFLEN];
9204
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009205 title = get_tv_string_chk(&argvars[0]);
9206 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 if (title == NULL || initdir == NULL)
9209 rettv->vval.v_string = NULL;
9210 else
9211 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009212 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217}
9218
Bram Moolenaar33570922005-01-25 22:26:29 +00009219static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009220
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221/*
9222 * Find a buffer by number or exact name.
9223 */
9224 static buf_T *
9225find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227{
9228 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009230 if (avar->v_type == VAR_NUMBER)
9231 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009232 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009234 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009235 if (buf == NULL)
9236 {
9237 /* No full path name match, try a match with a URL or a "nofile"
9238 * buffer, these don't use the full path. */
9239 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9240 if (buf->b_fname != NULL
9241 && (path_with_url(buf->b_fname)
9242#ifdef FEAT_QUICKFIX
9243 || bt_nofile(buf)
9244#endif
9245 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009246 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009247 break;
9248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250 return buf;
9251}
9252
9253/*
9254 * "bufexists(expr)" function
9255 */
9256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 typval_T *argvars;
9259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009261 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262}
9263
9264/*
9265 * "buflisted(expr)" function
9266 */
9267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009268f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009269 typval_T *argvars;
9270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
9272 buf_T *buf;
9273
9274 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276}
9277
9278/*
9279 * "bufloaded(expr)" function
9280 */
9281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *argvars;
9284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 buf_T *buf;
9287
9288 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290}
9291
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009292static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009293
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294/*
9295 * Get buffer by number or pattern.
9296 */
9297 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009298get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009299 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009300 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 int save_magic;
9304 char_u *save_cpo;
9305 buf_T *buf;
9306
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307 if (tv->v_type == VAR_NUMBER)
9308 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009309 if (tv->v_type != VAR_STRING)
9310 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 if (name == NULL || *name == NUL)
9312 return curbuf;
9313 if (name[0] == '$' && name[1] == NUL)
9314 return lastbuf;
9315
9316 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9317 save_magic = p_magic;
9318 p_magic = TRUE;
9319 save_cpo = p_cpo;
9320 p_cpo = (char_u *)"";
9321
9322 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009323 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
9325 p_magic = save_magic;
9326 p_cpo = save_cpo;
9327
9328 /* If not found, try expanding the name, like done for bufexists(). */
9329 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331
9332 return buf;
9333}
9334
9335/*
9336 * "bufname(expr)" function
9337 */
9338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009340 typval_T *argvars;
9341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 buf_T *buf;
9344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009347 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 --emsg_off;
9354}
9355
9356/*
9357 * "bufnr(expr)" function
9358 */
9359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009361 typval_T *argvars;
9362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009365 int error = FALSE;
9366 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
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 Moolenaar0e34f622006-03-03 23:00:03 +00009371 --emsg_off;
9372
9373 /* If the buffer isn't found and the second argument is not zero create a
9374 * new buffer. */
9375 if (buf == NULL
9376 && argvars[1].v_type != VAR_UNKNOWN
9377 && get_tv_number_chk(&argvars[1], &error) != 0
9378 && !error
9379 && (name = get_tv_string_chk(&argvars[0])) != NULL
9380 && !error)
9381 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9382
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009384 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387}
9388
9389/*
9390 * "bufwinnr(nr)" function
9391 */
9392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 typval_T *argvars;
9395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397#ifdef FEAT_WINDOWS
9398 win_T *wp;
9399 int winnr = 0;
9400#endif
9401 buf_T *buf;
9402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009403 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009405 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406#ifdef FEAT_WINDOWS
9407 for (wp = firstwin; wp; wp = wp->w_next)
9408 {
9409 ++winnr;
9410 if (wp->w_buffer == buf)
9411 break;
9412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#endif
9417 --emsg_off;
9418}
9419
9420/*
9421 * "byte2line(byte)" function
9422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009425 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427{
9428#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#else
9431 long boff = 0;
9432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 (linenr_T)0, &boff);
9439#endif
9440}
9441
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009442 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009443byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009444 typval_T *argvars;
9445 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009446 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009447{
9448#ifdef FEAT_MBYTE
9449 char_u *t;
9450#endif
9451 char_u *str;
9452 long idx;
9453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009454 str = get_tv_string_chk(&argvars[0]);
9455 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009457 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009458 return;
9459
9460#ifdef FEAT_MBYTE
9461 t = str;
9462 for ( ; idx > 0; idx--)
9463 {
9464 if (*t == NUL) /* EOL reached */
9465 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009466 if (enc_utf8 && comp)
9467 t += utf_ptr2len(t);
9468 else
9469 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009470 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009471 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009472#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009475#endif
9476}
9477
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009478/*
9479 * "byteidx()" function
9480 */
9481 static void
9482f_byteidx(argvars, rettv)
9483 typval_T *argvars;
9484 typval_T *rettv;
9485{
9486 byteidx(argvars, rettv, FALSE);
9487}
9488
9489/*
9490 * "byteidxcomp()" function
9491 */
9492 static void
9493f_byteidxcomp(argvars, rettv)
9494 typval_T *argvars;
9495 typval_T *rettv;
9496{
9497 byteidx(argvars, rettv, TRUE);
9498}
9499
Bram Moolenaardb913952012-06-29 12:54:53 +02009500 int
9501func_call(name, args, selfdict, rettv)
9502 char_u *name;
9503 typval_T *args;
9504 dict_T *selfdict;
9505 typval_T *rettv;
9506{
9507 listitem_T *item;
9508 typval_T argv[MAX_FUNC_ARGS + 1];
9509 int argc = 0;
9510 int dummy;
9511 int r = 0;
9512
9513 for (item = args->vval.v_list->lv_first; item != NULL;
9514 item = item->li_next)
9515 {
9516 if (argc == MAX_FUNC_ARGS)
9517 {
9518 EMSG(_("E699: Too many arguments"));
9519 break;
9520 }
9521 /* Make a copy of each argument. This is needed to be able to set
9522 * v_lock to VAR_FIXED in the copy without changing the original list.
9523 */
9524 copy_tv(&item->li_tv, &argv[argc++]);
9525 }
9526
9527 if (item == NULL)
9528 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9529 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9530 &dummy, TRUE, selfdict);
9531
9532 /* Free the arguments. */
9533 while (argc > 0)
9534 clear_tv(&argv[--argc]);
9535
9536 return r;
9537}
9538
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009540 * "call(func, arglist)" function
9541 */
9542 static void
9543f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 typval_T *argvars;
9545 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009546{
9547 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009549
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009550 if (argvars[1].v_type != VAR_LIST)
9551 {
9552 EMSG(_(e_listreq));
9553 return;
9554 }
9555 if (argvars[1].vval.v_list == NULL)
9556 return;
9557
9558 if (argvars[0].v_type == VAR_FUNC)
9559 func = argvars[0].vval.v_string;
9560 else
9561 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 if (*func == NUL)
9563 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009564
Bram Moolenaare9a41262005-01-15 22:18:47 +00009565 if (argvars[2].v_type != VAR_UNKNOWN)
9566 {
9567 if (argvars[2].v_type != VAR_DICT)
9568 {
9569 EMSG(_(e_dictreq));
9570 return;
9571 }
9572 selfdict = argvars[2].vval.v_dict;
9573 }
9574
Bram Moolenaardb913952012-06-29 12:54:53 +02009575 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009576}
9577
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009578#ifdef FEAT_FLOAT
9579/*
9580 * "ceil({float})" function
9581 */
9582 static void
9583f_ceil(argvars, rettv)
9584 typval_T *argvars;
9585 typval_T *rettv;
9586{
9587 float_T f;
9588
9589 rettv->v_type = VAR_FLOAT;
9590 if (get_float_arg(argvars, &f) == OK)
9591 rettv->vval.v_float = ceil(f);
9592 else
9593 rettv->vval.v_float = 0.0;
9594}
9595#endif
9596
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009597/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009598 * "changenr()" function
9599 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009600 static void
9601f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009602 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009603 typval_T *rettv;
9604{
9605 rettv->vval.v_number = curbuf->b_u_seq_cur;
9606}
9607
9608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 * "char2nr(string)" function
9610 */
9611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616#ifdef FEAT_MBYTE
9617 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009618 {
9619 int utf8 = 0;
9620
9621 if (argvars[1].v_type != VAR_UNKNOWN)
9622 utf8 = get_tv_number_chk(&argvars[1], NULL);
9623
9624 if (utf8)
9625 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9626 else
9627 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 else
9630#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634/*
9635 * "cindent(lnum)" function
9636 */
9637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642#ifdef FEAT_CINDENT
9643 pos_T pos;
9644 linenr_T lnum;
9645
9646 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9649 {
9650 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 curwin->w_cursor = pos;
9653 }
9654 else
9655#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
9659/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009660 * "clearmatches()" function
9661 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009662 static void
9663f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009664 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009665 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009666{
9667#ifdef FEAT_SEARCH_EXTRA
9668 clear_matches(curwin);
9669#endif
9670}
9671
9672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 * "col(string)" function
9674 */
9675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
9680 colnr_T col = 0;
9681 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009682 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009684 fp = var2fpos(&argvars[0], FALSE, &fnum);
9685 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 {
9687 if (fp->col == MAXCOL)
9688 {
9689 /* '> can be MAXCOL, get the length of the line then */
9690 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009691 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 else
9693 col = MAXCOL;
9694 }
9695 else
9696 {
9697 col = fp->col + 1;
9698#ifdef FEAT_VIRTUALEDIT
9699 /* col(".") when the cursor is on the NUL at the end of the line
9700 * because of "coladd" can be seen as an extra column. */
9701 if (virtual_active() && fp == &curwin->w_cursor)
9702 {
9703 char_u *p = ml_get_cursor();
9704
9705 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9706 curwin->w_virtcol - curwin->w_cursor.coladd))
9707 {
9708# ifdef FEAT_MBYTE
9709 int l;
9710
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009711 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 col += l;
9713# else
9714 if (*p != NUL && p[1] == NUL)
9715 ++col;
9716# endif
9717 }
9718 }
9719#endif
9720 }
9721 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
Bram Moolenaar572cb562005-08-05 21:35:02 +00009725#if defined(FEAT_INS_EXPAND)
9726/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009727 * "complete()" function
9728 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009729 static void
9730f_complete(argvars, rettv)
9731 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009732 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009733{
9734 int startcol;
9735
9736 if ((State & INSERT) == 0)
9737 {
9738 EMSG(_("E785: complete() can only be used in Insert mode"));
9739 return;
9740 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009741
9742 /* Check for undo allowed here, because if something was already inserted
9743 * the line was already saved for undo and this check isn't done. */
9744 if (!undo_allowed())
9745 return;
9746
Bram Moolenaarade00832006-03-10 21:46:58 +00009747 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9748 {
9749 EMSG(_(e_invarg));
9750 return;
9751 }
9752
9753 startcol = get_tv_number_chk(&argvars[0], NULL);
9754 if (startcol <= 0)
9755 return;
9756
9757 set_completion(startcol - 1, argvars[1].vval.v_list);
9758}
9759
9760/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009761 * "complete_add()" function
9762 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009763 static void
9764f_complete_add(argvars, rettv)
9765 typval_T *argvars;
9766 typval_T *rettv;
9767{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009768 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009769}
9770
9771/*
9772 * "complete_check()" function
9773 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009774 static void
9775f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009776 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009777 typval_T *rettv;
9778{
9779 int saved = RedrawingDisabled;
9780
9781 RedrawingDisabled = 0;
9782 ins_compl_check_keys(0);
9783 rettv->vval.v_number = compl_interrupted;
9784 RedrawingDisabled = saved;
9785}
9786#endif
9787
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788/*
9789 * "confirm(message, buttons[, default [, type]])" function
9790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009793 typval_T *argvars UNUSED;
9794 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9797 char_u *message;
9798 char_u *buttons = NULL;
9799 char_u buf[NUMBUFLEN];
9800 char_u buf2[NUMBUFLEN];
9801 int def = 1;
9802 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 char_u *typestr;
9804 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009806 message = get_tv_string_chk(&argvars[0]);
9807 if (message == NULL)
9808 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009809 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9812 if (buttons == NULL)
9813 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009814 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009817 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9820 if (typestr == NULL)
9821 error = TRUE;
9822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 switch (TOUPPER_ASC(*typestr))
9825 {
9826 case 'E': type = VIM_ERROR; break;
9827 case 'Q': type = VIM_QUESTION; break;
9828 case 'I': type = VIM_INFO; break;
9829 case 'W': type = VIM_WARNING; break;
9830 case 'G': type = VIM_GENERIC; break;
9831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 }
9833 }
9834 }
9835 }
9836
9837 if (buttons == NULL || *buttons == NUL)
9838 buttons = (char_u *)_("&Ok");
9839
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009840 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009841 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009842 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843#endif
9844}
9845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009846/*
9847 * "copy()" function
9848 */
9849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009851 typval_T *argvars;
9852 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009853{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009854 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009857#ifdef FEAT_FLOAT
9858/*
9859 * "cos()" function
9860 */
9861 static void
9862f_cos(argvars, rettv)
9863 typval_T *argvars;
9864 typval_T *rettv;
9865{
9866 float_T f;
9867
9868 rettv->v_type = VAR_FLOAT;
9869 if (get_float_arg(argvars, &f) == OK)
9870 rettv->vval.v_float = cos(f);
9871 else
9872 rettv->vval.v_float = 0.0;
9873}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009874
9875/*
9876 * "cosh()" function
9877 */
9878 static void
9879f_cosh(argvars, rettv)
9880 typval_T *argvars;
9881 typval_T *rettv;
9882{
9883 float_T f;
9884
9885 rettv->v_type = VAR_FLOAT;
9886 if (get_float_arg(argvars, &f) == OK)
9887 rettv->vval.v_float = cosh(f);
9888 else
9889 rettv->vval.v_float = 0.0;
9890}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009891#endif
9892
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894 * "count()" function
9895 */
9896 static void
9897f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901 long n = 0;
9902 int ic = FALSE;
9903
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009905 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009906 listitem_T *li;
9907 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009909
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 if ((l = argvars[0].vval.v_list) != NULL)
9911 {
9912 li = l->lv_first;
9913 if (argvars[2].v_type != VAR_UNKNOWN)
9914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 int error = FALSE;
9916
9917 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 if (argvars[3].v_type != VAR_UNKNOWN)
9919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 idx = get_tv_number_chk(&argvars[3], &error);
9921 if (!error)
9922 {
9923 li = list_find(l, idx);
9924 if (li == NULL)
9925 EMSGN(_(e_listidx), idx);
9926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 if (error)
9929 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 }
9931
9932 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009933 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 ++n;
9935 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009937 else if (argvars[0].v_type == VAR_DICT)
9938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009939 int todo;
9940 dict_T *d;
9941 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009942
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009943 if ((d = argvars[0].vval.v_dict) != NULL)
9944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 int error = FALSE;
9946
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 if (argvars[2].v_type != VAR_UNKNOWN)
9948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 if (argvars[3].v_type != VAR_UNKNOWN)
9951 EMSG(_(e_invarg));
9952 }
9953
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009954 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009956 {
9957 if (!HASHITEM_EMPTY(hi))
9958 {
9959 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009960 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009961 ++n;
9962 }
9963 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009964 }
9965 }
9966 else
9967 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009968 rettv->vval.v_number = n;
9969}
9970
9971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9973 *
9974 * Checks the existence of a cscope connection.
9975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009978 typval_T *argvars UNUSED;
9979 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980{
9981#ifdef FEAT_CSCOPE
9982 int num = 0;
9983 char_u *dbpath = NULL;
9984 char_u *prepend = NULL;
9985 char_u buf[NUMBUFLEN];
9986
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009987 if (argvars[0].v_type != VAR_UNKNOWN
9988 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 num = (int)get_tv_number(&argvars[0]);
9991 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009992 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 }
9995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997#endif
9998}
9999
10000/*
10001 * "cursor(lnum, col)" function
10002 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010003 * Moves the cursor to the specified line and column.
10004 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010008 typval_T *argvars;
10009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010{
10011 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010012#ifdef FEAT_VIRTUALEDIT
10013 long coladd = 0;
10014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010016 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010017 if (argvars[1].v_type == VAR_UNKNOWN)
10018 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010019 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010020 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010021
Bram Moolenaar493c1782014-05-28 14:34:46 +020010022 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010023 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010024 line = pos.lnum;
10025 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010026#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010027 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010028#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010029 if (curswant >= 0)
10030 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010031 }
10032 else
10033 {
10034 line = get_tv_lnum(argvars);
10035 col = get_tv_number_chk(&argvars[1], NULL);
10036#ifdef FEAT_VIRTUALEDIT
10037 if (argvars[2].v_type != VAR_UNKNOWN)
10038 coladd = get_tv_number_chk(&argvars[2], NULL);
10039#endif
10040 }
10041 if (line < 0 || col < 0
10042#ifdef FEAT_VIRTUALEDIT
10043 || coladd < 0
10044#endif
10045 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010046 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 if (line > 0)
10048 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (col > 0)
10050 curwin->w_cursor.col = col - 1;
10051#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010052 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#endif
10054
10055 /* Make sure the cursor is in a valid position. */
10056 check_cursor();
10057#ifdef FEAT_MBYTE
10058 /* Correct cursor for multi-byte character. */
10059 if (has_mbyte)
10060 mb_adjust_cursor();
10061#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010062
10063 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010064 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065}
10066
10067/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010068 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 */
10070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 typval_T *argvars;
10073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010075 int noref = 0;
10076
10077 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010079 if (noref < 0 || noref > 1)
10080 EMSG(_(e_invarg));
10081 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010082 {
10083 current_copyID += COPYID_INC;
10084 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087
10088/*
10089 * "delete()" function
10090 */
10091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010092f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010093 typval_T *argvars;
10094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
10096 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100}
10101
10102/*
10103 * "did_filetype()" function
10104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010107 typval_T *argvars UNUSED;
10108 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
10110#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112#endif
10113}
10114
10115/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010116 * "diff_filler()" function
10117 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010120 typval_T *argvars UNUSED;
10121 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010122{
10123#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010125#endif
10126}
10127
10128/*
10129 * "diff_hlID()" function
10130 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010133 typval_T *argvars UNUSED;
10134 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010135{
10136#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138 static linenr_T prev_lnum = 0;
10139 static int changedtick = 0;
10140 static int fnum = 0;
10141 static int change_start = 0;
10142 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010143 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010144 int filler_lines;
10145 int col;
10146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 if (lnum < 0) /* ignore type error in {lnum} arg */
10148 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010149 if (lnum != prev_lnum
10150 || changedtick != curbuf->b_changedtick
10151 || fnum != curbuf->b_fnum)
10152 {
10153 /* New line, buffer, change: need to get the values. */
10154 filler_lines = diff_check(curwin, lnum);
10155 if (filler_lines < 0)
10156 {
10157 if (filler_lines == -1)
10158 {
10159 change_start = MAXCOL;
10160 change_end = -1;
10161 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10162 hlID = HLF_ADD; /* added line */
10163 else
10164 hlID = HLF_CHD; /* changed line */
10165 }
10166 else
10167 hlID = HLF_ADD; /* added line */
10168 }
10169 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010170 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010171 prev_lnum = lnum;
10172 changedtick = curbuf->b_changedtick;
10173 fnum = curbuf->b_fnum;
10174 }
10175
10176 if (hlID == HLF_CHD || hlID == HLF_TXD)
10177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010179 if (col >= change_start && col <= change_end)
10180 hlID = HLF_TXD; /* changed text */
10181 else
10182 hlID = HLF_CHD; /* changed line */
10183 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010184 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010185#endif
10186}
10187
10188/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010189 * "empty({expr})" function
10190 */
10191 static void
10192f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 typval_T *argvars;
10194 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010195{
10196 int n;
10197
10198 switch (argvars[0].v_type)
10199 {
10200 case VAR_STRING:
10201 case VAR_FUNC:
10202 n = argvars[0].vval.v_string == NULL
10203 || *argvars[0].vval.v_string == NUL;
10204 break;
10205 case VAR_NUMBER:
10206 n = argvars[0].vval.v_number == 0;
10207 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010208#ifdef FEAT_FLOAT
10209 case VAR_FLOAT:
10210 n = argvars[0].vval.v_float == 0.0;
10211 break;
10212#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010213 case VAR_LIST:
10214 n = argvars[0].vval.v_list == NULL
10215 || argvars[0].vval.v_list->lv_first == NULL;
10216 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 case VAR_DICT:
10218 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010221 default:
10222 EMSG2(_(e_intern2), "f_empty()");
10223 n = 0;
10224 }
10225
10226 rettv->vval.v_number = n;
10227}
10228
10229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 * "escape({string}, {chars})" function
10231 */
10232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 typval_T *argvars;
10235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236{
10237 char_u buf[NUMBUFLEN];
10238
Bram Moolenaar758711c2005-02-02 23:11:38 +000010239 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10240 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242}
10243
10244/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010245 * "eval()" function
10246 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010247 static void
10248f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 typval_T *argvars;
10250 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010251{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010252 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010254 s = get_tv_string_chk(&argvars[0]);
10255 if (s != NULL)
10256 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010257
Bram Moolenaar615b9972015-01-14 17:15:05 +010010258 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10260 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010261 if (p != NULL && !aborting())
10262 EMSG2(_(e_invexpr2), p);
10263 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267 else if (*s != NUL)
10268 EMSG(_(e_trailing));
10269}
10270
10271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 * "eventhandler()" function
10273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010276 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280}
10281
10282/*
10283 * "executable()" function
10284 */
10285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T *argvars;
10288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010290 char_u *name = get_tv_string(&argvars[0]);
10291
10292 /* Check in $PATH and also check directly if there is a directory name. */
10293 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10294 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010295}
10296
10297/*
10298 * "exepath()" function
10299 */
10300 static void
10301f_exepath(argvars, rettv)
10302 typval_T *argvars;
10303 typval_T *rettv;
10304{
10305 char_u *p = NULL;
10306
Bram Moolenaarb5971142015-03-21 17:32:19 +010010307 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010308 rettv->v_type = VAR_STRING;
10309 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310}
10311
10312/*
10313 * "exists()" function
10314 */
10315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010317 typval_T *argvars;
10318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319{
10320 char_u *p;
10321 char_u *name;
10322 int n = FALSE;
10323 int len = 0;
10324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (*p == '$') /* environment variable */
10327 {
10328 /* first try "normal" environment variables (fast) */
10329 if (mch_getenv(p + 1) != NULL)
10330 n = TRUE;
10331 else
10332 {
10333 /* try expanding things like $VIM and ${HOME} */
10334 p = expand_env_save(p);
10335 if (p != NULL && *p != '$')
10336 n = TRUE;
10337 vim_free(p);
10338 }
10339 }
10340 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010343 if (*skipwhite(p) != NUL)
10344 n = FALSE; /* trailing garbage */
10345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 else if (*p == '*') /* internal or user defined function */
10347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010348 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 }
10350 else if (*p == ':')
10351 {
10352 n = cmd_exists(p + 1);
10353 }
10354 else if (*p == '#')
10355 {
10356#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010357 if (p[1] == '#')
10358 n = autocmd_supported(p + 2);
10359 else
10360 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361#endif
10362 }
10363 else /* internal variable */
10364 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010365 char_u *tofree;
10366 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010368 /* get_name_len() takes care of expanding curly braces */
10369 name = p;
10370 len = get_name_len(&p, &tofree, TRUE, FALSE);
10371 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010373 if (tofree != NULL)
10374 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010375 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010376 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010378 /* handle d.key, l[idx], f(expr) */
10379 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10380 if (n)
10381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 }
10383 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010384 if (*p != NUL)
10385 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010387 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 }
10389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391}
10392
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010393#ifdef FEAT_FLOAT
10394/*
10395 * "exp()" function
10396 */
10397 static void
10398f_exp(argvars, rettv)
10399 typval_T *argvars;
10400 typval_T *rettv;
10401{
10402 float_T f;
10403
10404 rettv->v_type = VAR_FLOAT;
10405 if (get_float_arg(argvars, &f) == OK)
10406 rettv->vval.v_float = exp(f);
10407 else
10408 rettv->vval.v_float = 0.0;
10409}
10410#endif
10411
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412/*
10413 * "expand()" function
10414 */
10415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010417 typval_T *argvars;
10418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419{
10420 char_u *s;
10421 int len;
10422 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010423 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010426 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010429 if (argvars[1].v_type != VAR_UNKNOWN
10430 && argvars[2].v_type != VAR_UNKNOWN
10431 && get_tv_number_chk(&argvars[2], &error)
10432 && !error)
10433 {
10434 rettv->v_type = VAR_LIST;
10435 rettv->vval.v_list = NULL;
10436 }
10437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 if (*s == '%' || *s == '#' || *s == '<')
10440 {
10441 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010442 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010444 if (rettv->v_type == VAR_LIST)
10445 {
10446 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10447 list_append_string(rettv->vval.v_list, result, -1);
10448 else
10449 vim_free(result);
10450 }
10451 else
10452 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 }
10454 else
10455 {
10456 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010457 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 if (argvars[1].v_type != VAR_UNKNOWN
10459 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010460 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 if (!error)
10462 {
10463 ExpandInit(&xpc);
10464 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010465 if (p_wic)
10466 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010467 if (rettv->v_type == VAR_STRING)
10468 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10469 options, WILD_ALL);
10470 else if (rettv_list_alloc(rettv) != FAIL)
10471 {
10472 int i;
10473
10474 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10475 for (i = 0; i < xpc.xp_numfiles; i++)
10476 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10477 ExpandCleanup(&xpc);
10478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 }
10480 else
10481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 }
10483}
10484
10485/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010486 * Go over all entries in "d2" and add them to "d1".
10487 * When "action" is "error" then a duplicate key is an error.
10488 * When "action" is "force" then a duplicate key is overwritten.
10489 * Otherwise duplicate keys are ignored ("action" is "keep").
10490 */
10491 void
10492dict_extend(d1, d2, action)
10493 dict_T *d1;
10494 dict_T *d2;
10495 char_u *action;
10496{
10497 dictitem_T *di1;
10498 hashitem_T *hi2;
10499 int todo;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010500 char *arg_errmsg = N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010501
10502 todo = (int)d2->dv_hashtab.ht_used;
10503 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10504 {
10505 if (!HASHITEM_EMPTY(hi2))
10506 {
10507 --todo;
10508 di1 = dict_find(d1, hi2->hi_key, -1);
10509 if (d1->dv_scope != 0)
10510 {
10511 /* Disallow replacing a builtin function in l: and g:.
10512 * Check the key to be valid when adding to any
10513 * scope. */
10514 if (d1->dv_scope == VAR_DEF_SCOPE
10515 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10516 && var_check_func_name(hi2->hi_key,
10517 di1 == NULL))
10518 break;
10519 if (!valid_varname(hi2->hi_key))
10520 break;
10521 }
10522 if (di1 == NULL)
10523 {
10524 di1 = dictitem_copy(HI2DI(hi2));
10525 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10526 dictitem_free(di1);
10527 }
10528 else if (*action == 'e')
10529 {
10530 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10531 break;
10532 }
10533 else if (*action == 'f' && HI2DI(hi2) != di1)
10534 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010535 if (tv_check_lock(di1->di_tv.v_lock, (char_u *)_(arg_errmsg))
10536 || var_check_ro(di1->di_flags, (char_u *)_(arg_errmsg)))
10537 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010538 clear_tv(&di1->di_tv);
10539 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10540 }
10541 }
10542 }
10543}
10544
10545/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010546 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010548 */
10549 static void
10550f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010551 typval_T *argvars;
10552 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010553{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010554 char *arg_errmsg = N_("extend() argument");
10555
Bram Moolenaare9a41262005-01-15 22:18:47 +000010556 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010557 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010558 list_T *l1, *l2;
10559 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010560 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010562
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 l1 = argvars[0].vval.v_list;
10564 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010565 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010566 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 {
10568 if (argvars[2].v_type != VAR_UNKNOWN)
10569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 before = get_tv_number_chk(&argvars[2], &error);
10571 if (error)
10572 return; /* type error; errmsg already given */
10573
Bram Moolenaar758711c2005-02-02 23:11:38 +000010574 if (before == l1->lv_len)
10575 item = NULL;
10576 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010578 item = list_find(l1, before);
10579 if (item == NULL)
10580 {
10581 EMSGN(_(e_listidx), before);
10582 return;
10583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 }
10585 }
10586 else
10587 item = NULL;
10588 list_extend(l1, l2, item);
10589
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 copy_tv(&argvars[0], rettv);
10591 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010592 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10594 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010595 dict_T *d1, *d2;
10596 char_u *action;
10597 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598
10599 d1 = argvars[0].vval.v_dict;
10600 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010601 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010602 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 {
10604 /* Check the third argument. */
10605 if (argvars[2].v_type != VAR_UNKNOWN)
10606 {
10607 static char *(av[]) = {"keep", "force", "error"};
10608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 action = get_tv_string_chk(&argvars[2]);
10610 if (action == NULL)
10611 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010612 for (i = 0; i < 3; ++i)
10613 if (STRCMP(action, av[i]) == 0)
10614 break;
10615 if (i == 3)
10616 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010617 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 return;
10619 }
10620 }
10621 else
10622 action = (char_u *)"force";
10623
Bram Moolenaara9922d62013-05-30 13:01:18 +020010624 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010625
Bram Moolenaare9a41262005-01-15 22:18:47 +000010626 copy_tv(&argvars[0], rettv);
10627 }
10628 }
10629 else
10630 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010631}
10632
10633/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010634 * "feedkeys()" function
10635 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010636 static void
10637f_feedkeys(argvars, rettv)
10638 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010639 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010640{
10641 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010642 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010643 char_u *keys, *flags;
10644 char_u nbuf[NUMBUFLEN];
10645 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010646 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010647
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010648 /* This is not allowed in the sandbox. If the commands would still be
10649 * executed in the sandbox it would be OK, but it probably happens later,
10650 * when "sandbox" is no longer set. */
10651 if (check_secure())
10652 return;
10653
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010654 keys = get_tv_string(&argvars[0]);
10655 if (*keys != NUL)
10656 {
10657 if (argvars[1].v_type != VAR_UNKNOWN)
10658 {
10659 flags = get_tv_string_buf(&argvars[1], nbuf);
10660 for ( ; *flags != NUL; ++flags)
10661 {
10662 switch (*flags)
10663 {
10664 case 'n': remap = FALSE; break;
10665 case 'm': remap = TRUE; break;
10666 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010667 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010668 }
10669 }
10670 }
10671
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010672 /* Need to escape K_SPECIAL and CSI before putting the string in the
10673 * typeahead buffer. */
10674 keys_esc = vim_strsave_escape_csi(keys);
10675 if (keys_esc != NULL)
10676 {
10677 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010678 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010679 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010680 if (vgetc_busy)
10681 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010682 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010683 }
10684}
10685
10686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 * "filereadable()" function
10688 */
10689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010694 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 char_u *p;
10696 int n;
10697
Bram Moolenaarc236c162008-07-13 17:41:49 +000010698#ifndef O_NONBLOCK
10699# define O_NONBLOCK 0
10700#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010702 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10703 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
10705 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010706 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 }
10708 else
10709 n = FALSE;
10710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712}
10713
10714/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010715 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 * rights to write into.
10717 */
10718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *argvars;
10721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010723 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010726static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010727
10728 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010729findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010732 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010733{
10734#ifdef FEAT_SEARCHPATH
10735 char_u *fname;
10736 char_u *fresult = NULL;
10737 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10738 char_u *p;
10739 char_u pathbuf[NUMBUFLEN];
10740 int count = 1;
10741 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010742 int error = FALSE;
10743#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010744
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010745 rettv->vval.v_string = NULL;
10746 rettv->v_type = VAR_STRING;
10747
10748#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010750
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010751 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10754 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010755 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 else
10757 {
10758 if (*p != NUL)
10759 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010762 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010764 }
10765
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010766 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10767 error = TRUE;
10768
10769 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 do
10772 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010773 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010774 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 fresult = find_file_in_path_option(first ? fname : NULL,
10776 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010777 0, first, path,
10778 find_what,
10779 curbuf->b_ffname,
10780 find_what == FINDFILE_DIR
10781 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010782 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010783
10784 if (fresult != NULL && rettv->v_type == VAR_LIST)
10785 list_append_string(rettv->vval.v_list, fresult, -1);
10786
10787 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010789
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010790 if (rettv->v_type == VAR_STRING)
10791 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010792#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010793}
10794
Bram Moolenaar33570922005-01-25 22:26:29 +000010795static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10796static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010797
10798/*
10799 * Implementation of map() and filter().
10800 */
10801 static void
10802filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010803 typval_T *argvars;
10804 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010805 int map;
10806{
10807 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 listitem_T *li, *nli;
10810 list_T *l = NULL;
10811 dictitem_T *di;
10812 hashtab_T *ht;
10813 hashitem_T *hi;
10814 dict_T *d = NULL;
10815 typval_T save_val;
10816 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010817 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010818 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010819 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10820 char *arg_errmsg = (map ? N_("map() argument")
10821 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010822 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010823 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010824
Bram Moolenaare9a41262005-01-15 22:18:47 +000010825 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010826 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010827 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010828 || (!map && tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg))))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010829 return;
10830 }
10831 else if (argvars[0].v_type == VAR_DICT)
10832 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010833 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010834 || (!map && tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg))))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 return;
10836 }
10837 else
10838 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010839 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 return;
10841 }
10842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 expr = get_tv_string_buf_chk(&argvars[1], buf);
10844 /* On type errors, the preceding call has already displayed an error
10845 * message. Avoid a misleading error message for an empty string that
10846 * was not passed as argument. */
10847 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 prepare_vimvar(VV_VAL, &save_val);
10850 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010851
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010852 /* We reset "did_emsg" to be able to detect whether an error
10853 * occurred during evaluation of the expression. */
10854 save_did_emsg = did_emsg;
10855 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010856
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010857 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010860 vimvars[VV_KEY].vv_type = VAR_STRING;
10861
10862 ht = &d->dv_hashtab;
10863 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010864 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010867 if (!HASHITEM_EMPTY(hi))
10868 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010869 int r;
10870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 --todo;
10872 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010873 if (map &&
10874 (tv_check_lock(di->di_tv.v_lock,
10875 (char_u *)_(arg_errmsg))
10876 || var_check_ro(di->di_flags,
10877 (char_u *)_(arg_errmsg))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 break;
10879 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010880 r = filter_map_one(&di->di_tv, expr, map, &rem);
10881 clear_tv(&vimvars[VV_KEY].vv_tv);
10882 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 break;
10884 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010885 {
10886 if (var_check_fixed(di->di_flags,
10887 (char_u *)_(arg_errmsg))
10888 || var_check_ro(di->di_flags,
10889 (char_u *)_(arg_errmsg)))
10890 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010892 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 }
10894 }
10895 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 }
10897 else
10898 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010899 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 for (li = l->lv_first; li != NULL; li = nli)
10902 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010903 if (map && tv_check_lock(li->li_tv.v_lock,
10904 (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010905 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010907 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010908 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010909 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010910 break;
10911 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010913 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010914 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010915 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010916
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010917 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010919
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010920 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010922
10923 copy_tv(&argvars[0], rettv);
10924}
10925
10926 static int
10927filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010928 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 char_u *expr;
10930 int map;
10931 int *remp;
10932{
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010935 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010936
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010938 s = expr;
10939 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010940 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 if (*s != NUL) /* check for trailing chars after expr */
10942 {
10943 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010944 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010945 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 }
10947 if (map)
10948 {
10949 /* map(): replace the list item value */
10950 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010951 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 *tv = rettv;
10953 }
10954 else
10955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010956 int error = FALSE;
10957
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 /* On type error, nothing has been removed; return FAIL to stop the
10962 * loop. The error message was given by get_tv_number_chk(). */
10963 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010964 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010965 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010966 retval = OK;
10967theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010969 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010970}
10971
10972/*
10973 * "filter()" function
10974 */
10975 static void
10976f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010977 typval_T *argvars;
10978 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010979{
10980 filter_map(argvars, rettv, FALSE);
10981}
10982
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010984 * "finddir({fname}[, {path}[, {count}]])" function
10985 */
10986 static void
10987f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010991 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992}
10993
10994/*
10995 * "findfile({fname}[, {path}[, {count}]])" function
10996 */
10997 static void
10998f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 typval_T *argvars;
11000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011002 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003}
11004
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011005#ifdef FEAT_FLOAT
11006/*
11007 * "float2nr({float})" function
11008 */
11009 static void
11010f_float2nr(argvars, rettv)
11011 typval_T *argvars;
11012 typval_T *rettv;
11013{
11014 float_T f;
11015
11016 if (get_float_arg(argvars, &f) == OK)
11017 {
11018 if (f < -0x7fffffff)
11019 rettv->vval.v_number = -0x7fffffff;
11020 else if (f > 0x7fffffff)
11021 rettv->vval.v_number = 0x7fffffff;
11022 else
11023 rettv->vval.v_number = (varnumber_T)f;
11024 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011025}
11026
11027/*
11028 * "floor({float})" function
11029 */
11030 static void
11031f_floor(argvars, rettv)
11032 typval_T *argvars;
11033 typval_T *rettv;
11034{
11035 float_T f;
11036
11037 rettv->v_type = VAR_FLOAT;
11038 if (get_float_arg(argvars, &f) == OK)
11039 rettv->vval.v_float = floor(f);
11040 else
11041 rettv->vval.v_float = 0.0;
11042}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011043
11044/*
11045 * "fmod()" function
11046 */
11047 static void
11048f_fmod(argvars, rettv)
11049 typval_T *argvars;
11050 typval_T *rettv;
11051{
11052 float_T fx, fy;
11053
11054 rettv->v_type = VAR_FLOAT;
11055 if (get_float_arg(argvars, &fx) == OK
11056 && get_float_arg(&argvars[1], &fy) == OK)
11057 rettv->vval.v_float = fmod(fx, fy);
11058 else
11059 rettv->vval.v_float = 0.0;
11060}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011061#endif
11062
Bram Moolenaar0d660222005-01-07 21:51:51 +000011063/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011064 * "fnameescape({string})" function
11065 */
11066 static void
11067f_fnameescape(argvars, rettv)
11068 typval_T *argvars;
11069 typval_T *rettv;
11070{
11071 rettv->vval.v_string = vim_strsave_fnameescape(
11072 get_tv_string(&argvars[0]), FALSE);
11073 rettv->v_type = VAR_STRING;
11074}
11075
11076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 * "fnamemodify({fname}, {mods})" function
11078 */
11079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011081 typval_T *argvars;
11082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083{
11084 char_u *fname;
11085 char_u *mods;
11086 int usedlen = 0;
11087 int len;
11088 char_u *fbuf = NULL;
11089 char_u buf[NUMBUFLEN];
11090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 fname = get_tv_string_chk(&argvars[0]);
11092 mods = get_tv_string_buf_chk(&argvars[1], buf);
11093 if (fname == NULL || mods == NULL)
11094 fname = NULL;
11095 else
11096 {
11097 len = (int)STRLEN(fname);
11098 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 vim_free(fbuf);
11107}
11108
Bram Moolenaar33570922005-01-25 22:26:29 +000011109static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
11111/*
11112 * "foldclosed()" function
11113 */
11114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011116 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011118 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119{
11120#ifdef FEAT_FOLDING
11121 linenr_T lnum;
11122 linenr_T first, last;
11123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011124 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11126 {
11127 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11128 {
11129 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 return;
11134 }
11135 }
11136#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138}
11139
11140/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141 * "foldclosed()" function
11142 */
11143 static void
11144f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 typval_T *argvars;
11146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147{
11148 foldclosed_both(argvars, rettv, FALSE);
11149}
11150
11151/*
11152 * "foldclosedend()" function
11153 */
11154 static void
11155f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158{
11159 foldclosed_both(argvars, rettv, TRUE);
11160}
11161
11162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 * "foldlevel()" function
11164 */
11165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011167 typval_T *argvars UNUSED;
11168 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
11170#ifdef FEAT_FOLDING
11171 linenr_T lnum;
11172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
11180 * "foldtext()" function
11181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011184 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186{
11187#ifdef FEAT_FOLDING
11188 linenr_T lnum;
11189 char_u *s;
11190 char_u *r;
11191 int len;
11192 char *txt;
11193#endif
11194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->v_type = VAR_STRING;
11196 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011198 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11199 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11200 <= curbuf->b_ml.ml_line_count
11201 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 {
11203 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11205 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 {
11207 if (!linewhite(lnum))
11208 break;
11209 ++lnum;
11210 }
11211
11212 /* Find interesting text in this line. */
11213 s = skipwhite(ml_get(lnum));
11214 /* skip C comment-start */
11215 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011218 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011220 {
11221 s = skipwhite(ml_get(lnum + 1));
11222 if (*s == '*')
11223 s = skipwhite(s + 1);
11224 }
11225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 txt = _("+-%s%3ld lines: ");
11227 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 + 20 /* for %3ld */
11230 + STRLEN(s))); /* concatenated */
11231 if (r != NULL)
11232 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11234 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11235 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 len = (int)STRLEN(r);
11237 STRCAT(r, s);
11238 /* remove 'foldmarker' and 'commentstring' */
11239 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242 }
11243#endif
11244}
11245
11246/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011247 * "foldtextresult(lnum)" function
11248 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011251 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011252 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011253{
11254#ifdef FEAT_FOLDING
11255 linenr_T lnum;
11256 char_u *text;
11257 char_u buf[51];
11258 foldinfo_T foldinfo;
11259 int fold_count;
11260#endif
11261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 rettv->v_type = VAR_STRING;
11263 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011264#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 /* treat illegal types and illegal string values for {lnum} the same */
11267 if (lnum < 0)
11268 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011269 fold_count = foldedCount(curwin, lnum, &foldinfo);
11270 if (fold_count > 0)
11271 {
11272 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11273 &foldinfo, buf);
11274 if (text == buf)
11275 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011277 }
11278#endif
11279}
11280
11281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 * "foreground()" function
11283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011286 typval_T *argvars UNUSED;
11287 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289#ifdef FEAT_GUI
11290 if (gui.in_use)
11291 gui_mch_set_foreground();
11292#else
11293# ifdef WIN32
11294 win32_set_foreground();
11295# endif
11296#endif
11297}
11298
11299/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 * "function()" function
11301 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011304 typval_T *argvars;
11305 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011306{
11307 char_u *s;
11308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011310 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011311 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011312 /* Don't check an autoload name for existence here. */
11313 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011314 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011315 else
11316 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011317 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011318 {
11319 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011320 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011321
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011322 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11323 * also be called from another script. Using trans_function_name()
11324 * would also work, but some plugins depend on the name being
11325 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011326 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011327 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011328 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011329 if (rettv->vval.v_string != NULL)
11330 {
11331 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011332 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011333 }
11334 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011335 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011336 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011338 }
11339}
11340
11341/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011342 * "garbagecollect()" function
11343 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011344 static void
11345f_garbagecollect(argvars, rettv)
11346 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011347 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011348{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011349 /* This is postponed until we are back at the toplevel, because we may be
11350 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11351 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011352
11353 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11354 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011355}
11356
11357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358 * "get()" function
11359 */
11360 static void
11361f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011362 typval_T *argvars;
11363 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011364{
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 listitem_T *li;
11366 list_T *l;
11367 dictitem_T *di;
11368 dict_T *d;
11369 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011373 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 int error = FALSE;
11376
11377 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11378 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011379 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011380 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011381 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011382 else if (argvars[0].v_type == VAR_DICT)
11383 {
11384 if ((d = argvars[0].vval.v_dict) != NULL)
11385 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011386 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011387 if (di != NULL)
11388 tv = &di->di_tv;
11389 }
11390 }
11391 else
11392 EMSG2(_(e_listdictarg), "get()");
11393
11394 if (tv == NULL)
11395 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011397 copy_tv(&argvars[2], rettv);
11398 }
11399 else
11400 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011401}
11402
Bram Moolenaar342337a2005-07-21 21:11:17 +000011403static 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 +000011404
11405/*
11406 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011407 * Return a range (from start to end) of lines in rettv from the specified
11408 * buffer.
11409 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011410 */
11411 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011412get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011413 buf_T *buf;
11414 linenr_T start;
11415 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011416 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011417 typval_T *rettv;
11418{
11419 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011420
Bram Moolenaar959a1432013-12-14 12:17:38 +010011421 rettv->v_type = VAR_STRING;
11422 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011423 if (retlist && rettv_list_alloc(rettv) == FAIL)
11424 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011425
11426 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11427 return;
11428
11429 if (!retlist)
11430 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011431 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11432 p = ml_get_buf(buf, start, FALSE);
11433 else
11434 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011435 rettv->vval.v_string = vim_strsave(p);
11436 }
11437 else
11438 {
11439 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011440 return;
11441
11442 if (start < 1)
11443 start = 1;
11444 if (end > buf->b_ml.ml_line_count)
11445 end = buf->b_ml.ml_line_count;
11446 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011447 if (list_append_string(rettv->vval.v_list,
11448 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011449 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011450 }
11451}
11452
11453/*
11454 * "getbufline()" function
11455 */
11456 static void
11457f_getbufline(argvars, rettv)
11458 typval_T *argvars;
11459 typval_T *rettv;
11460{
11461 linenr_T lnum;
11462 linenr_T end;
11463 buf_T *buf;
11464
11465 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11466 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011467 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011468 --emsg_off;
11469
Bram Moolenaar661b1822005-07-28 22:36:45 +000011470 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011471 if (argvars[2].v_type == VAR_UNKNOWN)
11472 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011473 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011474 end = get_tv_lnum_buf(&argvars[2], buf);
11475
Bram Moolenaar342337a2005-07-21 21:11:17 +000011476 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011477}
11478
Bram Moolenaar0d660222005-01-07 21:51:51 +000011479/*
11480 * "getbufvar()" function
11481 */
11482 static void
11483f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 typval_T *argvars;
11485 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011486{
11487 buf_T *buf;
11488 buf_T *save_curbuf;
11489 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011490 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011491 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11494 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011496 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011498 rettv->v_type = VAR_STRING;
11499 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500
11501 if (buf != NULL && varname != NULL)
11502 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011503 /* set curbuf to be our buf, temporarily */
11504 save_curbuf = curbuf;
11505 curbuf = buf;
11506
Bram Moolenaar0d660222005-01-07 21:51:51 +000011507 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011508 {
11509 if (get_option_tv(&varname, rettv, TRUE) == OK)
11510 done = TRUE;
11511 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011512 else if (STRCMP(varname, "changedtick") == 0)
11513 {
11514 rettv->v_type = VAR_NUMBER;
11515 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011516 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011517 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011518 else
11519 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011520 /* Look up the variable. */
11521 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11522 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11523 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011524 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011525 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011527 done = TRUE;
11528 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011530
11531 /* restore previous notion of curbuf */
11532 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533 }
11534
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011535 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11536 /* use the default value */
11537 copy_tv(&argvars[2], rettv);
11538
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 --emsg_off;
11540}
11541
11542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 * "getchar()" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
11550 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011551 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011553 /* Position the cursor. Needed after a message that ends in a space. */
11554 windgoto(msg_row, msg_col);
11555
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 ++no_mapping;
11557 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011558 for (;;)
11559 {
11560 if (argvars[0].v_type == VAR_UNKNOWN)
11561 /* getchar(): blocking wait. */
11562 n = safe_vgetc();
11563 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11564 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011565 n = vpeekc_any();
11566 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011567 /* illegal argument or getchar(0) and no char avail: return zero */
11568 n = 0;
11569 else
11570 /* getchar(0) and char avail: return char */
11571 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011572
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011573 if (n == K_IGNORE)
11574 continue;
11575 break;
11576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 --no_mapping;
11578 --allow_keys;
11579
Bram Moolenaar219b8702006-11-01 14:32:36 +000011580 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11581 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11582 vimvars[VV_MOUSE_COL].vv_nr = 0;
11583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 if (IS_SPECIAL(n) || mod_mask != 0)
11586 {
11587 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11588 int i = 0;
11589
11590 /* Turn a special key into three bytes, plus modifier. */
11591 if (mod_mask != 0)
11592 {
11593 temp[i++] = K_SPECIAL;
11594 temp[i++] = KS_MODIFIER;
11595 temp[i++] = mod_mask;
11596 }
11597 if (IS_SPECIAL(n))
11598 {
11599 temp[i++] = K_SPECIAL;
11600 temp[i++] = K_SECOND(n);
11601 temp[i++] = K_THIRD(n);
11602 }
11603#ifdef FEAT_MBYTE
11604 else if (has_mbyte)
11605 i += (*mb_char2bytes)(n, temp + i);
11606#endif
11607 else
11608 temp[i++] = n;
11609 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->v_type = VAR_STRING;
11611 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011612
11613#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011614 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011615 {
11616 int row = mouse_row;
11617 int col = mouse_col;
11618 win_T *win;
11619 linenr_T lnum;
11620# ifdef FEAT_WINDOWS
11621 win_T *wp;
11622# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011623 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011624
11625 if (row >= 0 && col >= 0)
11626 {
11627 /* Find the window at the mouse coordinates and compute the
11628 * text position. */
11629 win = mouse_find_win(&row, &col);
11630 (void)mouse_comp_pos(win, &row, &col, &lnum);
11631# ifdef FEAT_WINDOWS
11632 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011633 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011634# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011635 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011636 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11637 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11638 }
11639 }
11640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 }
11642}
11643
11644/*
11645 * "getcharmod()" function
11646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011648f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011649 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
11656 * "getcmdline()" function
11657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663 rettv->v_type = VAR_STRING;
11664 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665}
11666
11667/*
11668 * "getcmdpos()" function
11669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011672 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676}
11677
11678/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011679 * "getcmdtype()" function
11680 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011681 static void
11682f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011683 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011684 typval_T *rettv;
11685{
11686 rettv->v_type = VAR_STRING;
11687 rettv->vval.v_string = alloc(2);
11688 if (rettv->vval.v_string != NULL)
11689 {
11690 rettv->vval.v_string[0] = get_cmdline_type();
11691 rettv->vval.v_string[1] = NUL;
11692 }
11693}
11694
11695/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011696 * "getcmdwintype()" function
11697 */
11698 static void
11699f_getcmdwintype(argvars, rettv)
11700 typval_T *argvars UNUSED;
11701 typval_T *rettv;
11702{
11703 rettv->v_type = VAR_STRING;
11704 rettv->vval.v_string = NULL;
11705#ifdef FEAT_CMDWIN
11706 rettv->vval.v_string = alloc(2);
11707 if (rettv->vval.v_string != NULL)
11708 {
11709 rettv->vval.v_string[0] = cmdwin_type;
11710 rettv->vval.v_string[1] = NUL;
11711 }
11712#endif
11713}
11714
11715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 * "getcwd()" function
11717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011720 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011723 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011726 rettv->vval.v_string = NULL;
11727 cwd = alloc(MAXPATHL);
11728 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011730 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11731 {
11732 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011734 if (rettv->vval.v_string != NULL)
11735 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011737 }
11738 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 }
11740}
11741
11742/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011743 * "getfontname()" function
11744 */
11745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011746f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011748 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->v_type = VAR_STRING;
11751 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011752#ifdef FEAT_GUI
11753 if (gui.in_use)
11754 {
11755 GuiFont font;
11756 char_u *name = NULL;
11757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011758 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011759 {
11760 /* Get the "Normal" font. Either the name saved by
11761 * hl_set_font_name() or from the font ID. */
11762 font = gui.norm_font;
11763 name = hl_get_font_name();
11764 }
11765 else
11766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011768 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11769 return;
11770 font = gui_mch_get_font(name, FALSE);
11771 if (font == NOFONT)
11772 return; /* Invalid font name, return empty string. */
11773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011775 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011776 gui_mch_free_font(font);
11777 }
11778#endif
11779}
11780
11781/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011782 * "getfperm({fname})" function
11783 */
11784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011786 typval_T *argvars;
11787 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011788{
11789 char_u *fname;
11790 struct stat st;
11791 char_u *perm = NULL;
11792 char_u flags[] = "rwx";
11793 int i;
11794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011798 if (mch_stat((char *)fname, &st) >= 0)
11799 {
11800 perm = vim_strsave((char_u *)"---------");
11801 if (perm != NULL)
11802 {
11803 for (i = 0; i < 9; i++)
11804 {
11805 if (st.st_mode & (1 << (8 - i)))
11806 perm[i] = flags[i % 3];
11807 }
11808 }
11809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011811}
11812
11813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 * "getfsize({fname})" function
11815 */
11816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 typval_T *argvars;
11819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
11821 char_u *fname;
11822 struct stat st;
11823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827
11828 if (mch_stat((char *)fname, &st) >= 0)
11829 {
11830 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011835
11836 /* non-perfect check for overflow */
11837 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11838 rettv->vval.v_number = -2;
11839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 }
11841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843}
11844
11845/*
11846 * "getftime({fname})" function
11847 */
11848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011850 typval_T *argvars;
11851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
11853 char_u *fname;
11854 struct stat st;
11855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857
11858 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862}
11863
11864/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011865 * "getftype({fname})" function
11866 */
11867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011869 typval_T *argvars;
11870 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011871{
11872 char_u *fname;
11873 struct stat st;
11874 char_u *type = NULL;
11875 char *t;
11876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011880 if (mch_lstat((char *)fname, &st) >= 0)
11881 {
11882#ifdef S_ISREG
11883 if (S_ISREG(st.st_mode))
11884 t = "file";
11885 else if (S_ISDIR(st.st_mode))
11886 t = "dir";
11887# ifdef S_ISLNK
11888 else if (S_ISLNK(st.st_mode))
11889 t = "link";
11890# endif
11891# ifdef S_ISBLK
11892 else if (S_ISBLK(st.st_mode))
11893 t = "bdev";
11894# endif
11895# ifdef S_ISCHR
11896 else if (S_ISCHR(st.st_mode))
11897 t = "cdev";
11898# endif
11899# ifdef S_ISFIFO
11900 else if (S_ISFIFO(st.st_mode))
11901 t = "fifo";
11902# endif
11903# ifdef S_ISSOCK
11904 else if (S_ISSOCK(st.st_mode))
11905 t = "fifo";
11906# endif
11907 else
11908 t = "other";
11909#else
11910# ifdef S_IFMT
11911 switch (st.st_mode & S_IFMT)
11912 {
11913 case S_IFREG: t = "file"; break;
11914 case S_IFDIR: t = "dir"; break;
11915# ifdef S_IFLNK
11916 case S_IFLNK: t = "link"; break;
11917# endif
11918# ifdef S_IFBLK
11919 case S_IFBLK: t = "bdev"; break;
11920# endif
11921# ifdef S_IFCHR
11922 case S_IFCHR: t = "cdev"; break;
11923# endif
11924# ifdef S_IFIFO
11925 case S_IFIFO: t = "fifo"; break;
11926# endif
11927# ifdef S_IFSOCK
11928 case S_IFSOCK: t = "socket"; break;
11929# endif
11930 default: t = "other";
11931 }
11932# else
11933 if (mch_isdir(fname))
11934 t = "dir";
11935 else
11936 t = "file";
11937# endif
11938#endif
11939 type = vim_strsave((char_u *)t);
11940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011942}
11943
11944/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011945 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011946 */
11947 static void
11948f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011949 typval_T *argvars;
11950 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951{
11952 linenr_T lnum;
11953 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011954 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011955
11956 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011957 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011958 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011959 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011960 retlist = FALSE;
11961 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011962 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011963 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011964 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011965 retlist = TRUE;
11966 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011967
Bram Moolenaar342337a2005-07-21 21:11:17 +000011968 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011969}
11970
11971/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011972 * "getmatches()" function
11973 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011974 static void
11975f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011976 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011977 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011978{
11979#ifdef FEAT_SEARCH_EXTRA
11980 dict_T *dict;
11981 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011982 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011983
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011984 if (rettv_list_alloc(rettv) == OK)
11985 {
11986 while (cur != NULL)
11987 {
11988 dict = dict_alloc();
11989 if (dict == NULL)
11990 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011991 if (cur->match.regprog == NULL)
11992 {
11993 /* match added with matchaddpos() */
11994 for (i = 0; i < MAXPOSMATCH; ++i)
11995 {
11996 llpos_T *llpos;
11997 char buf[6];
11998 list_T *l;
11999
12000 llpos = &cur->pos.pos[i];
12001 if (llpos->lnum == 0)
12002 break;
12003 l = list_alloc();
12004 if (l == NULL)
12005 break;
12006 list_append_number(l, (varnumber_T)llpos->lnum);
12007 if (llpos->col > 0)
12008 {
12009 list_append_number(l, (varnumber_T)llpos->col);
12010 list_append_number(l, (varnumber_T)llpos->len);
12011 }
12012 sprintf(buf, "pos%d", i + 1);
12013 dict_add_list(dict, buf, l);
12014 }
12015 }
12016 else
12017 {
12018 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12019 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012020 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012021 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12022 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12023 list_append_dict(rettv->vval.v_list, dict);
12024 cur = cur->next;
12025 }
12026 }
12027#endif
12028}
12029
12030/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012031 * "getpid()" function
12032 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012033 static void
12034f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012035 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012036 typval_T *rettv;
12037{
12038 rettv->vval.v_number = mch_get_pid();
12039}
12040
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012041static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12042
12043/*
12044 * "getcurpos()" function
12045 */
12046 static void
12047f_getcurpos(argvars, rettv)
12048 typval_T *argvars;
12049 typval_T *rettv;
12050{
12051 getpos_both(argvars, rettv, TRUE);
12052}
12053
Bram Moolenaar18081e32008-02-20 19:11:07 +000012054/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012055 * "getpos(string)" function
12056 */
12057 static void
12058f_getpos(argvars, rettv)
12059 typval_T *argvars;
12060 typval_T *rettv;
12061{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012062 getpos_both(argvars, rettv, FALSE);
12063}
12064
12065 static void
12066getpos_both(argvars, rettv, getcurpos)
12067 typval_T *argvars;
12068 typval_T *rettv;
12069 int getcurpos;
12070{
Bram Moolenaara5525202006-03-02 22:52:09 +000012071 pos_T *fp;
12072 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012073 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012074
12075 if (rettv_list_alloc(rettv) == OK)
12076 {
12077 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012078 if (getcurpos)
12079 fp = &curwin->w_cursor;
12080 else
12081 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012082 if (fnum != -1)
12083 list_append_number(l, (varnumber_T)fnum);
12084 else
12085 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012086 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12087 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012088 list_append_number(l, (fp != NULL)
12089 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012090 : (varnumber_T)0);
12091 list_append_number(l,
12092#ifdef FEAT_VIRTUALEDIT
12093 (fp != NULL) ? (varnumber_T)fp->coladd :
12094#endif
12095 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012096 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012097 list_append_number(l, curwin->w_curswant == MAXCOL ?
12098 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012099 }
12100 else
12101 rettv->vval.v_number = FALSE;
12102}
12103
12104/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012105 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012106 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012107 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012108f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012109 typval_T *argvars UNUSED;
12110 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012111{
12112#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012113 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012114#endif
12115
Bram Moolenaar2641f772005-03-25 21:58:17 +000012116#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012117 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012118 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012119 wp = NULL;
12120 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12121 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012122 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012123 if (wp == NULL)
12124 return;
12125 }
12126
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012127 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012128 }
12129#endif
12130}
12131
12132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 * "getreg()" function
12134 */
12135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012137 typval_T *argvars;
12138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139{
12140 char_u *strregname;
12141 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012142 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012143 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012148 strregname = get_tv_string_chk(&argvars[0]);
12149 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012150 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012153 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12154 return_list = get_tv_number_chk(&argvars[2], &error);
12155 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012158 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012159
12160 if (error)
12161 return;
12162
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 regname = (strregname == NULL ? '"' : *strregname);
12164 if (regname == 0)
12165 regname = '"';
12166
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012167 if (return_list)
12168 {
12169 rettv->v_type = VAR_LIST;
12170 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12171 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012172 if (rettv->vval.v_list != NULL)
12173 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012174 }
12175 else
12176 {
12177 rettv->v_type = VAR_STRING;
12178 rettv->vval.v_string = get_reg_contents(regname,
12179 arg2 ? GREG_EXPR_SRC : 0);
12180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182
12183/*
12184 * "getregtype()" function
12185 */
12186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012188 typval_T *argvars;
12189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
12191 char_u *strregname;
12192 int regname;
12193 char_u buf[NUMBUFLEN + 2];
12194 long reglen = 0;
12195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012196 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 {
12198 strregname = get_tv_string_chk(&argvars[0]);
12199 if (strregname == NULL) /* type error; errmsg already given */
12200 {
12201 rettv->v_type = VAR_STRING;
12202 rettv->vval.v_string = NULL;
12203 return;
12204 }
12205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 else
12207 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012208 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
12210 regname = (strregname == NULL ? '"' : *strregname);
12211 if (regname == 0)
12212 regname = '"';
12213
12214 buf[0] = NUL;
12215 buf[1] = NUL;
12216 switch (get_reg_type(regname, &reglen))
12217 {
12218 case MLINE: buf[0] = 'V'; break;
12219 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 case MBLOCK:
12221 buf[0] = Ctrl_V;
12222 sprintf((char *)buf + 1, "%ld", reglen + 1);
12223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 rettv->v_type = VAR_STRING;
12226 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228
12229/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012230 * "gettabvar()" function
12231 */
12232 static void
12233f_gettabvar(argvars, rettv)
12234 typval_T *argvars;
12235 typval_T *rettv;
12236{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012237 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012238 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012239 dictitem_T *v;
12240 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012241 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012242
12243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = NULL;
12245
12246 varname = get_tv_string_chk(&argvars[1]);
12247 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12248 if (tp != NULL && varname != NULL)
12249 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012250 /* Set tp to be our tabpage, temporarily. Also set the window to the
12251 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012252 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12253 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012254 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012255 /* look up the variable */
12256 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12257 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12258 if (v != NULL)
12259 {
12260 copy_tv(&v->di_tv, rettv);
12261 done = TRUE;
12262 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012263 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012264
12265 /* restore previous notion of curwin */
12266 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012267 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012268
12269 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012270 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012271}
12272
12273/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012274 * "gettabwinvar()" function
12275 */
12276 static void
12277f_gettabwinvar(argvars, rettv)
12278 typval_T *argvars;
12279 typval_T *rettv;
12280{
12281 getwinvar(argvars, rettv, 1);
12282}
12283
12284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 * "getwinposx()" function
12286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293#ifdef FEAT_GUI
12294 if (gui.in_use)
12295 {
12296 int x, y;
12297
12298 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 }
12301#endif
12302}
12303
12304/*
12305 * "getwinposy()" function
12306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313#ifdef FEAT_GUI
12314 if (gui.in_use)
12315 {
12316 int x, y;
12317
12318 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 }
12321#endif
12322}
12323
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012324/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012325 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012326 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012327 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012328find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012329 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012330 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012331{
12332#ifdef FEAT_WINDOWS
12333 win_T *wp;
12334#endif
12335 int nr;
12336
12337 nr = get_tv_number_chk(vp, NULL);
12338
12339#ifdef FEAT_WINDOWS
12340 if (nr < 0)
12341 return NULL;
12342 if (nr == 0)
12343 return curwin;
12344
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012345 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12346 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012347 if (--nr <= 0)
12348 break;
12349 return wp;
12350#else
12351 if (nr == 0 || nr == 1)
12352 return curwin;
12353 return NULL;
12354#endif
12355}
12356
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357/*
12358 * "getwinvar()" function
12359 */
12360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012362 typval_T *argvars;
12363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012365 getwinvar(argvars, rettv, 0);
12366}
12367
12368/*
12369 * getwinvar() and gettabwinvar()
12370 */
12371 static void
12372getwinvar(argvars, rettv, off)
12373 typval_T *argvars;
12374 typval_T *rettv;
12375 int off; /* 1 for gettabwinvar() */
12376{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 win_T *win, *oldcurwin;
12378 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012380 tabpage_T *tp = NULL;
12381 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012382 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012384#ifdef FEAT_WINDOWS
12385 if (off == 1)
12386 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12387 else
12388 tp = curtab;
12389#endif
12390 win = find_win_by_nr(&argvars[off], tp);
12391 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012392 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012394 rettv->v_type = VAR_STRING;
12395 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396
12397 if (win != NULL && varname != NULL)
12398 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012399 /* Set curwin to be our win, temporarily. Also set the tabpage,
12400 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012401 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012402 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012403 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012404 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012405 if (get_option_tv(&varname, rettv, 1) == OK)
12406 done = TRUE;
12407 }
12408 else
12409 {
12410 /* Look up the variable. */
12411 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12412 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12413 varname, FALSE);
12414 if (v != NULL)
12415 {
12416 copy_tv(&v->di_tv, rettv);
12417 done = TRUE;
12418 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012421
12422 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012423 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 }
12425
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012426 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12427 /* use the default return value */
12428 copy_tv(&argvars[off + 2], rettv);
12429
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 --emsg_off;
12431}
12432
12433/*
12434 * "glob()" function
12435 */
12436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012438 typval_T *argvars;
12439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012441 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012443 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012445 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012446 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012448 if (argvars[1].v_type != VAR_UNKNOWN)
12449 {
12450 if (get_tv_number_chk(&argvars[1], &error))
12451 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012453 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012454 if (get_tv_number_chk(&argvars[2], &error))
12455 {
12456 rettv->v_type = VAR_LIST;
12457 rettv->vval.v_list = NULL;
12458 }
12459 if (argvars[3].v_type != VAR_UNKNOWN
12460 && get_tv_number_chk(&argvars[3], &error))
12461 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012462 }
12463 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012464 if (!error)
12465 {
12466 ExpandInit(&xpc);
12467 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012468 if (p_wic)
12469 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012470 if (rettv->v_type == VAR_STRING)
12471 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012472 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012473 else if (rettv_list_alloc(rettv) != FAIL)
12474 {
12475 int i;
12476
12477 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12478 NULL, options, WILD_ALL_KEEP);
12479 for (i = 0; i < xpc.xp_numfiles; i++)
12480 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12481
12482 ExpandCleanup(&xpc);
12483 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012484 }
12485 else
12486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487}
12488
12489/*
12490 * "globpath()" function
12491 */
12492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012494 typval_T *argvars;
12495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012497 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012500 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012501 garray_T ga;
12502 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012504 /* When the optional second argument is non-zero, don't remove matches
12505 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012507 if (argvars[2].v_type != VAR_UNKNOWN)
12508 {
12509 if (get_tv_number_chk(&argvars[2], &error))
12510 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012511 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012512 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012513 if (get_tv_number_chk(&argvars[3], &error))
12514 {
12515 rettv->v_type = VAR_LIST;
12516 rettv->vval.v_list = NULL;
12517 }
12518 if (argvars[4].v_type != VAR_UNKNOWN
12519 && get_tv_number_chk(&argvars[4], &error))
12520 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012521 }
12522 }
12523 if (file != NULL && !error)
12524 {
12525 ga_init2(&ga, (int)sizeof(char_u *), 10);
12526 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12527 if (rettv->v_type == VAR_STRING)
12528 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12529 else if (rettv_list_alloc(rettv) != FAIL)
12530 for (i = 0; i < ga.ga_len; ++i)
12531 list_append_string(rettv->vval.v_list,
12532 ((char_u **)(ga.ga_data))[i], -1);
12533 ga_clear_strings(&ga);
12534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012535 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537}
12538
12539/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012540 * "glob2regpat()" function
12541 */
12542 static void
12543f_glob2regpat(argvars, rettv)
12544 typval_T *argvars;
12545 typval_T *rettv;
12546{
12547 char_u *pat = get_tv_string_chk(&argvars[0]);
12548
12549 rettv->v_type = VAR_STRING;
12550 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12551}
12552
12553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 * "has()" function
12555 */
12556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012557f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012558 typval_T *argvars;
12559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560{
12561 int i;
12562 char_u *name;
12563 int n = FALSE;
12564 static char *(has_list[]) =
12565 {
12566#ifdef AMIGA
12567 "amiga",
12568# ifdef FEAT_ARP
12569 "arp",
12570# endif
12571#endif
12572#ifdef __BEOS__
12573 "beos",
12574#endif
12575#ifdef MSDOS
12576# ifdef DJGPP
12577 "dos32",
12578# else
12579 "dos16",
12580# endif
12581#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012582#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 "mac",
12584#endif
12585#if defined(MACOS_X_UNIX)
12586 "macunix",
12587#endif
12588#ifdef OS2
12589 "os2",
12590#endif
12591#ifdef __QNX__
12592 "qnx",
12593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594#ifdef UNIX
12595 "unix",
12596#endif
12597#ifdef VMS
12598 "vms",
12599#endif
12600#ifdef WIN16
12601 "win16",
12602#endif
12603#ifdef WIN32
12604 "win32",
12605#endif
12606#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12607 "win32unix",
12608#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012609#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 "win64",
12611#endif
12612#ifdef EBCDIC
12613 "ebcdic",
12614#endif
12615#ifndef CASE_INSENSITIVE_FILENAME
12616 "fname_case",
12617#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012618#ifdef HAVE_ACL
12619 "acl",
12620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621#ifdef FEAT_ARABIC
12622 "arabic",
12623#endif
12624#ifdef FEAT_AUTOCMD
12625 "autocmd",
12626#endif
12627#ifdef FEAT_BEVAL
12628 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012629# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12630 "balloon_multiline",
12631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632#endif
12633#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12634 "builtin_terms",
12635# ifdef ALL_BUILTIN_TCAPS
12636 "all_builtin_terms",
12637# endif
12638#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012639#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12640 || defined(FEAT_GUI_W32) \
12641 || defined(FEAT_GUI_MOTIF))
12642 "browsefilter",
12643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644#ifdef FEAT_BYTEOFF
12645 "byte_offset",
12646#endif
12647#ifdef FEAT_CINDENT
12648 "cindent",
12649#endif
12650#ifdef FEAT_CLIENTSERVER
12651 "clientserver",
12652#endif
12653#ifdef FEAT_CLIPBOARD
12654 "clipboard",
12655#endif
12656#ifdef FEAT_CMDL_COMPL
12657 "cmdline_compl",
12658#endif
12659#ifdef FEAT_CMDHIST
12660 "cmdline_hist",
12661#endif
12662#ifdef FEAT_COMMENTS
12663 "comments",
12664#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012665#ifdef FEAT_CONCEAL
12666 "conceal",
12667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668#ifdef FEAT_CRYPT
12669 "cryptv",
12670#endif
12671#ifdef FEAT_CSCOPE
12672 "cscope",
12673#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012674#ifdef FEAT_CURSORBIND
12675 "cursorbind",
12676#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012677#ifdef CURSOR_SHAPE
12678 "cursorshape",
12679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680#ifdef DEBUG
12681 "debug",
12682#endif
12683#ifdef FEAT_CON_DIALOG
12684 "dialog_con",
12685#endif
12686#ifdef FEAT_GUI_DIALOG
12687 "dialog_gui",
12688#endif
12689#ifdef FEAT_DIFF
12690 "diff",
12691#endif
12692#ifdef FEAT_DIGRAPHS
12693 "digraphs",
12694#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012695#ifdef FEAT_DIRECTX
12696 "directx",
12697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698#ifdef FEAT_DND
12699 "dnd",
12700#endif
12701#ifdef FEAT_EMACS_TAGS
12702 "emacs_tags",
12703#endif
12704 "eval", /* always present, of course! */
12705#ifdef FEAT_EX_EXTRA
12706 "ex_extra",
12707#endif
12708#ifdef FEAT_SEARCH_EXTRA
12709 "extra_search",
12710#endif
12711#ifdef FEAT_FKMAP
12712 "farsi",
12713#endif
12714#ifdef FEAT_SEARCHPATH
12715 "file_in_path",
12716#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012717#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012718 "filterpipe",
12719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720#ifdef FEAT_FIND_ID
12721 "find_in_path",
12722#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012723#ifdef FEAT_FLOAT
12724 "float",
12725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726#ifdef FEAT_FOLDING
12727 "folding",
12728#endif
12729#ifdef FEAT_FOOTER
12730 "footer",
12731#endif
12732#if !defined(USE_SYSTEM) && defined(UNIX)
12733 "fork",
12734#endif
12735#ifdef FEAT_GETTEXT
12736 "gettext",
12737#endif
12738#ifdef FEAT_GUI
12739 "gui",
12740#endif
12741#ifdef FEAT_GUI_ATHENA
12742# ifdef FEAT_GUI_NEXTAW
12743 "gui_neXtaw",
12744# else
12745 "gui_athena",
12746# endif
12747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748#ifdef FEAT_GUI_GTK
12749 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012752#ifdef FEAT_GUI_GNOME
12753 "gui_gnome",
12754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755#ifdef FEAT_GUI_MAC
12756 "gui_mac",
12757#endif
12758#ifdef FEAT_GUI_MOTIF
12759 "gui_motif",
12760#endif
12761#ifdef FEAT_GUI_PHOTON
12762 "gui_photon",
12763#endif
12764#ifdef FEAT_GUI_W16
12765 "gui_win16",
12766#endif
12767#ifdef FEAT_GUI_W32
12768 "gui_win32",
12769#endif
12770#ifdef FEAT_HANGULIN
12771 "hangul_input",
12772#endif
12773#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12774 "iconv",
12775#endif
12776#ifdef FEAT_INS_EXPAND
12777 "insert_expand",
12778#endif
12779#ifdef FEAT_JUMPLIST
12780 "jumplist",
12781#endif
12782#ifdef FEAT_KEYMAP
12783 "keymap",
12784#endif
12785#ifdef FEAT_LANGMAP
12786 "langmap",
12787#endif
12788#ifdef FEAT_LIBCALL
12789 "libcall",
12790#endif
12791#ifdef FEAT_LINEBREAK
12792 "linebreak",
12793#endif
12794#ifdef FEAT_LISP
12795 "lispindent",
12796#endif
12797#ifdef FEAT_LISTCMDS
12798 "listcmds",
12799#endif
12800#ifdef FEAT_LOCALMAP
12801 "localmap",
12802#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012803#ifdef FEAT_LUA
12804# ifndef DYNAMIC_LUA
12805 "lua",
12806# endif
12807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808#ifdef FEAT_MENU
12809 "menu",
12810#endif
12811#ifdef FEAT_SESSION
12812 "mksession",
12813#endif
12814#ifdef FEAT_MODIFY_FNAME
12815 "modify_fname",
12816#endif
12817#ifdef FEAT_MOUSE
12818 "mouse",
12819#endif
12820#ifdef FEAT_MOUSESHAPE
12821 "mouseshape",
12822#endif
12823#if defined(UNIX) || defined(VMS)
12824# ifdef FEAT_MOUSE_DEC
12825 "mouse_dec",
12826# endif
12827# ifdef FEAT_MOUSE_GPM
12828 "mouse_gpm",
12829# endif
12830# ifdef FEAT_MOUSE_JSB
12831 "mouse_jsbterm",
12832# endif
12833# ifdef FEAT_MOUSE_NET
12834 "mouse_netterm",
12835# endif
12836# ifdef FEAT_MOUSE_PTERM
12837 "mouse_pterm",
12838# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012839# ifdef FEAT_MOUSE_SGR
12840 "mouse_sgr",
12841# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012842# ifdef FEAT_SYSMOUSE
12843 "mouse_sysmouse",
12844# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012845# ifdef FEAT_MOUSE_URXVT
12846 "mouse_urxvt",
12847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848# ifdef FEAT_MOUSE_XTERM
12849 "mouse_xterm",
12850# endif
12851#endif
12852#ifdef FEAT_MBYTE
12853 "multi_byte",
12854#endif
12855#ifdef FEAT_MBYTE_IME
12856 "multi_byte_ime",
12857#endif
12858#ifdef FEAT_MULTI_LANG
12859 "multi_lang",
12860#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012861#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012862#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012863 "mzscheme",
12864#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866#ifdef FEAT_OLE
12867 "ole",
12868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869#ifdef FEAT_PATH_EXTRA
12870 "path_extra",
12871#endif
12872#ifdef FEAT_PERL
12873#ifndef DYNAMIC_PERL
12874 "perl",
12875#endif
12876#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012877#ifdef FEAT_PERSISTENT_UNDO
12878 "persistent_undo",
12879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880#ifdef FEAT_PYTHON
12881#ifndef DYNAMIC_PYTHON
12882 "python",
12883#endif
12884#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012885#ifdef FEAT_PYTHON3
12886#ifndef DYNAMIC_PYTHON3
12887 "python3",
12888#endif
12889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890#ifdef FEAT_POSTSCRIPT
12891 "postscript",
12892#endif
12893#ifdef FEAT_PRINTER
12894 "printer",
12895#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012896#ifdef FEAT_PROFILE
12897 "profile",
12898#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012899#ifdef FEAT_RELTIME
12900 "reltime",
12901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902#ifdef FEAT_QUICKFIX
12903 "quickfix",
12904#endif
12905#ifdef FEAT_RIGHTLEFT
12906 "rightleft",
12907#endif
12908#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12909 "ruby",
12910#endif
12911#ifdef FEAT_SCROLLBIND
12912 "scrollbind",
12913#endif
12914#ifdef FEAT_CMDL_INFO
12915 "showcmd",
12916 "cmdline_info",
12917#endif
12918#ifdef FEAT_SIGNS
12919 "signs",
12920#endif
12921#ifdef FEAT_SMARTINDENT
12922 "smartindent",
12923#endif
12924#ifdef FEAT_SNIFF
12925 "sniff",
12926#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012927#ifdef STARTUPTIME
12928 "startuptime",
12929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930#ifdef FEAT_STL_OPT
12931 "statusline",
12932#endif
12933#ifdef FEAT_SUN_WORKSHOP
12934 "sun_workshop",
12935#endif
12936#ifdef FEAT_NETBEANS_INTG
12937 "netbeans_intg",
12938#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012939#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012940 "spell",
12941#endif
12942#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 "syntax",
12944#endif
12945#if defined(USE_SYSTEM) || !defined(UNIX)
12946 "system",
12947#endif
12948#ifdef FEAT_TAG_BINS
12949 "tag_binary",
12950#endif
12951#ifdef FEAT_TAG_OLDSTATIC
12952 "tag_old_static",
12953#endif
12954#ifdef FEAT_TAG_ANYWHITE
12955 "tag_any_white",
12956#endif
12957#ifdef FEAT_TCL
12958# ifndef DYNAMIC_TCL
12959 "tcl",
12960# endif
12961#endif
12962#ifdef TERMINFO
12963 "terminfo",
12964#endif
12965#ifdef FEAT_TERMRESPONSE
12966 "termresponse",
12967#endif
12968#ifdef FEAT_TEXTOBJ
12969 "textobjects",
12970#endif
12971#ifdef HAVE_TGETENT
12972 "tgetent",
12973#endif
12974#ifdef FEAT_TITLE
12975 "title",
12976#endif
12977#ifdef FEAT_TOOLBAR
12978 "toolbar",
12979#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012980#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12981 "unnamedplus",
12982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#ifdef FEAT_USR_CMDS
12984 "user-commands", /* was accidentally included in 5.4 */
12985 "user_commands",
12986#endif
12987#ifdef FEAT_VIMINFO
12988 "viminfo",
12989#endif
12990#ifdef FEAT_VERTSPLIT
12991 "vertsplit",
12992#endif
12993#ifdef FEAT_VIRTUALEDIT
12994 "virtualedit",
12995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997#ifdef FEAT_VISUALEXTRA
12998 "visualextra",
12999#endif
13000#ifdef FEAT_VREPLACE
13001 "vreplace",
13002#endif
13003#ifdef FEAT_WILDIGN
13004 "wildignore",
13005#endif
13006#ifdef FEAT_WILDMENU
13007 "wildmenu",
13008#endif
13009#ifdef FEAT_WINDOWS
13010 "windows",
13011#endif
13012#ifdef FEAT_WAK
13013 "winaltkeys",
13014#endif
13015#ifdef FEAT_WRITEBACKUP
13016 "writebackup",
13017#endif
13018#ifdef FEAT_XIM
13019 "xim",
13020#endif
13021#ifdef FEAT_XFONTSET
13022 "xfontset",
13023#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013024#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013025 "xpm",
13026 "xpm_w32", /* for backward compatibility */
13027#else
13028# if defined(HAVE_XPM)
13029 "xpm",
13030# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032#ifdef USE_XSMP
13033 "xsmp",
13034#endif
13035#ifdef USE_XSMP_INTERACT
13036 "xsmp_interact",
13037#endif
13038#ifdef FEAT_XCLIPBOARD
13039 "xterm_clipboard",
13040#endif
13041#ifdef FEAT_XTERM_SAVE
13042 "xterm_save",
13043#endif
13044#if defined(UNIX) && defined(FEAT_X11)
13045 "X11",
13046#endif
13047 NULL
13048 };
13049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 for (i = 0; has_list[i] != NULL; ++i)
13052 if (STRICMP(name, has_list[i]) == 0)
13053 {
13054 n = TRUE;
13055 break;
13056 }
13057
13058 if (n == FALSE)
13059 {
13060 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013061 {
13062 if (name[5] == '-'
13063 && STRLEN(name) > 11
13064 && vim_isdigit(name[6])
13065 && vim_isdigit(name[8])
13066 && vim_isdigit(name[10]))
13067 {
13068 int major = atoi((char *)name + 6);
13069 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013070
13071 /* Expect "patch-9.9.01234". */
13072 n = (major < VIM_VERSION_MAJOR
13073 || (major == VIM_VERSION_MAJOR
13074 && (minor < VIM_VERSION_MINOR
13075 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013076 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013077 }
13078 else
13079 n = has_patch(atoi((char *)name + 5));
13080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 else if (STRICMP(name, "vim_starting") == 0)
13082 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013083#ifdef FEAT_MBYTE
13084 else if (STRICMP(name, "multi_byte_encoding") == 0)
13085 n = has_mbyte;
13086#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013087#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13088 else if (STRICMP(name, "balloon_multiline") == 0)
13089 n = multiline_balloon_available();
13090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091#ifdef DYNAMIC_TCL
13092 else if (STRICMP(name, "tcl") == 0)
13093 n = tcl_enabled(FALSE);
13094#endif
13095#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13096 else if (STRICMP(name, "iconv") == 0)
13097 n = iconv_enabled(FALSE);
13098#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013099#ifdef DYNAMIC_LUA
13100 else if (STRICMP(name, "lua") == 0)
13101 n = lua_enabled(FALSE);
13102#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013103#ifdef DYNAMIC_MZSCHEME
13104 else if (STRICMP(name, "mzscheme") == 0)
13105 n = mzscheme_enabled(FALSE);
13106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#ifdef DYNAMIC_RUBY
13108 else if (STRICMP(name, "ruby") == 0)
13109 n = ruby_enabled(FALSE);
13110#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013111#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112#ifdef DYNAMIC_PYTHON
13113 else if (STRICMP(name, "python") == 0)
13114 n = python_enabled(FALSE);
13115#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013116#endif
13117#ifdef FEAT_PYTHON3
13118#ifdef DYNAMIC_PYTHON3
13119 else if (STRICMP(name, "python3") == 0)
13120 n = python3_enabled(FALSE);
13121#endif
13122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123#ifdef DYNAMIC_PERL
13124 else if (STRICMP(name, "perl") == 0)
13125 n = perl_enabled(FALSE);
13126#endif
13127#ifdef FEAT_GUI
13128 else if (STRICMP(name, "gui_running") == 0)
13129 n = (gui.in_use || gui.starting);
13130# ifdef FEAT_GUI_W32
13131 else if (STRICMP(name, "gui_win32s") == 0)
13132 n = gui_is_win32s();
13133# endif
13134# ifdef FEAT_BROWSE
13135 else if (STRICMP(name, "browse") == 0)
13136 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13137# endif
13138#endif
13139#ifdef FEAT_SYN_HL
13140 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013141 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142#endif
13143#if defined(WIN3264)
13144 else if (STRICMP(name, "win95") == 0)
13145 n = mch_windows95();
13146#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013147#ifdef FEAT_NETBEANS_INTG
13148 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013149 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 }
13152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154}
13155
13156/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013157 * "has_key()" function
13158 */
13159 static void
13160f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *argvars;
13162 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013163{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013164 if (argvars[0].v_type != VAR_DICT)
13165 {
13166 EMSG(_(e_dictreq));
13167 return;
13168 }
13169 if (argvars[0].vval.v_dict == NULL)
13170 return;
13171
13172 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013173 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013174}
13175
13176/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013177 * "haslocaldir()" function
13178 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013179 static void
13180f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013181 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013182 typval_T *rettv;
13183{
13184 rettv->vval.v_number = (curwin->w_localdir != NULL);
13185}
13186
13187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 * "hasmapto()" function
13189 */
13190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013192 typval_T *argvars;
13193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194{
13195 char_u *name;
13196 char_u *mode;
13197 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013198 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013201 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 mode = (char_u *)"nvo";
13203 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013206 if (argvars[2].v_type != VAR_UNKNOWN)
13207 abbr = get_tv_number(&argvars[2]);
13208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209
Bram Moolenaar2c932302006-03-18 21:42:09 +000013210 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214}
13215
13216/*
13217 * "histadd()" function
13218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223{
13224#ifdef FEAT_CMDHIST
13225 int histype;
13226 char_u *str;
13227 char_u buf[NUMBUFLEN];
13228#endif
13229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 if (check_restricted() || check_secure())
13232 return;
13233#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13235 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 if (histype >= 0)
13237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 if (*str != NUL)
13240 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013241 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 return;
13245 }
13246 }
13247#endif
13248}
13249
13250/*
13251 * "histdel()" function
13252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013255 typval_T *argvars UNUSED;
13256 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257{
13258#ifdef FEAT_CMDHIST
13259 int n;
13260 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13264 if (str == NULL)
13265 n = 0;
13266 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013269 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 else
13274 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 get_tv_string_buf(&argvars[1], buf));
13277 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278#endif
13279}
13280
13281/*
13282 * "histget()" function
13283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013286 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288{
13289#ifdef FEAT_CMDHIST
13290 int type;
13291 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13295 if (str == NULL)
13296 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013298 {
13299 type = get_histtype(str);
13300 if (argvars[1].v_type == VAR_UNKNOWN)
13301 idx = get_history_idx(type);
13302 else
13303 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13304 /* -1 on type error */
13305 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311}
13312
13313/*
13314 * "histnr()" function
13315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013318 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320{
13321 int i;
13322
13323#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 char_u *history = get_tv_string_chk(&argvars[0]);
13325
13326 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 if (i >= HIST_CMD && i < HIST_COUNT)
13328 i = get_history_idx(i);
13329 else
13330#endif
13331 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333}
13334
13335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 * "highlightID(name)" function
13337 */
13338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 typval_T *argvars;
13341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344}
13345
13346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347 * "highlight_exists()" function
13348 */
13349 static void
13350f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013351 typval_T *argvars;
13352 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013353{
13354 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13355}
13356
13357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 * "hostname()" function
13359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
13365 char_u hostname[256];
13366
13367 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->v_type = VAR_STRING;
13369 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370}
13371
13372/*
13373 * iconv() function
13374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379{
13380#ifdef FEAT_MBYTE
13381 char_u buf1[NUMBUFLEN];
13382 char_u buf2[NUMBUFLEN];
13383 char_u *from, *to, *str;
13384 vimconv_T vimconv;
13385#endif
13386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387 rettv->v_type = VAR_STRING;
13388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389
13390#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 str = get_tv_string(&argvars[0]);
13392 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13393 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 vimconv.vc_type = CONV_NONE;
13395 convert_setup(&vimconv, from, to);
13396
13397 /* If the encodings are equal, no conversion needed. */
13398 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402
13403 convert_setup(&vimconv, NULL, NULL);
13404 vim_free(from);
13405 vim_free(to);
13406#endif
13407}
13408
13409/*
13410 * "indent()" function
13411 */
13412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013413f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013414 typval_T *argvars;
13415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416{
13417 linenr_T lnum;
13418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424}
13425
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013426/*
13427 * "index()" function
13428 */
13429 static void
13430f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 typval_T *argvars;
13432 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013433{
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 list_T *l;
13435 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013436 long idx = 0;
13437 int ic = FALSE;
13438
13439 rettv->vval.v_number = -1;
13440 if (argvars[0].v_type != VAR_LIST)
13441 {
13442 EMSG(_(e_listreq));
13443 return;
13444 }
13445 l = argvars[0].vval.v_list;
13446 if (l != NULL)
13447 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013448 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 int error = FALSE;
13452
Bram Moolenaar758711c2005-02-02 23:11:38 +000013453 /* Start at specified item. Use the cached index that list_find()
13454 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013456 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013457 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458 ic = get_tv_number_chk(&argvars[3], &error);
13459 if (error)
13460 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013462
Bram Moolenaar758711c2005-02-02 23:11:38 +000013463 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013464 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013465 {
13466 rettv->vval.v_number = idx;
13467 break;
13468 }
13469 }
13470}
13471
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472static int inputsecret_flag = 0;
13473
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013474static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13475
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013477 * This function is used by f_input() and f_inputdialog() functions. The third
13478 * argument to f_input() specifies the type of completion to use at the
13479 * prompt. The third argument to f_inputdialog() specifies the value to return
13480 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 */
13482 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013483get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013484 typval_T *argvars;
13485 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013486 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013488 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 char_u *p = NULL;
13490 int c;
13491 char_u buf[NUMBUFLEN];
13492 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013494 int xp_type = EXPAND_NOTHING;
13495 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499
13500#ifdef NO_CONSOLE_INPUT
13501 /* While starting up, there is no place to enter text. */
13502 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504#endif
13505
13506 cmd_silent = FALSE; /* Want to see the prompt. */
13507 if (prompt != NULL)
13508 {
13509 /* Only the part of the message after the last NL is considered as
13510 * prompt for the command line */
13511 p = vim_strrchr(prompt, '\n');
13512 if (p == NULL)
13513 p = prompt;
13514 else
13515 {
13516 ++p;
13517 c = *p;
13518 *p = NUL;
13519 msg_start();
13520 msg_clr_eos();
13521 msg_puts_attr(prompt, echo_attr);
13522 msg_didout = FALSE;
13523 msg_starthere();
13524 *p = c;
13525 }
13526 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 if (argvars[1].v_type != VAR_UNKNOWN)
13529 {
13530 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13531 if (defstr != NULL)
13532 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013534 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013535 {
13536 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013537 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013538 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013539
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013540 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013541 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013542
Bram Moolenaar4463f292005-09-25 22:20:24 +000013543 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13544 if (xp_name == NULL)
13545 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013547 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013548
Bram Moolenaar4463f292005-09-25 22:20:24 +000013549 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13550 &xp_arg) == FAIL)
13551 return;
13552 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013553 }
13554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013556 {
13557# ifdef FEAT_EX_EXTRA
13558 int save_ex_normal_busy = ex_normal_busy;
13559 ex_normal_busy = 0;
13560# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013562 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13563 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013564# ifdef FEAT_EX_EXTRA
13565 ex_normal_busy = save_ex_normal_busy;
13566# endif
13567 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013568 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013569 && argvars[1].v_type != VAR_UNKNOWN
13570 && argvars[2].v_type != VAR_UNKNOWN)
13571 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13572 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013573
13574 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013576 /* since the user typed this, no need to wait for return */
13577 need_wait_return = FALSE;
13578 msg_didout = FALSE;
13579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 cmd_silent = cmd_silent_save;
13581}
13582
13583/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013584 * "input()" function
13585 * Also handles inputsecret() when inputsecret is set.
13586 */
13587 static void
13588f_input(argvars, rettv)
13589 typval_T *argvars;
13590 typval_T *rettv;
13591{
13592 get_user_input(argvars, rettv, FALSE);
13593}
13594
13595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 * "inputdialog()" function
13597 */
13598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013600 typval_T *argvars;
13601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602{
13603#if defined(FEAT_GUI_TEXTDIALOG)
13604 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13605 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13606 {
13607 char_u *message;
13608 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 message = get_tv_string_chk(&argvars[0]);
13612 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013613 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013614 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 else
13616 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 if (message != NULL && defstr != NULL
13618 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013619 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 else
13622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 if (message != NULL && defstr != NULL
13624 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 rettv->vval.v_string = vim_strsave(
13627 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 }
13633 else
13634#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013635 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636}
13637
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013638/*
13639 * "inputlist()" function
13640 */
13641 static void
13642f_inputlist(argvars, rettv)
13643 typval_T *argvars;
13644 typval_T *rettv;
13645{
13646 listitem_T *li;
13647 int selected;
13648 int mouse_used;
13649
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013650#ifdef NO_CONSOLE_INPUT
13651 /* While starting up, there is no place to enter text. */
13652 if (no_console_input())
13653 return;
13654#endif
13655 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13656 {
13657 EMSG2(_(e_listarg), "inputlist()");
13658 return;
13659 }
13660
13661 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013662 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013663 lines_left = Rows; /* avoid more prompt */
13664 msg_scroll = TRUE;
13665 msg_clr_eos();
13666
13667 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13668 {
13669 msg_puts(get_tv_string(&li->li_tv));
13670 msg_putchar('\n');
13671 }
13672
13673 /* Ask for choice. */
13674 selected = prompt_for_number(&mouse_used);
13675 if (mouse_used)
13676 selected -= lines_left;
13677
13678 rettv->vval.v_number = selected;
13679}
13680
13681
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13683
13684/*
13685 * "inputrestore()" function
13686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691{
13692 if (ga_userinput.ga_len > 0)
13693 {
13694 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13696 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013697 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 }
13699 else if (p_verbose > 1)
13700 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013701 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 }
13704}
13705
13706/*
13707 * "inputsave()" function
13708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013711 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013714 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 if (ga_grow(&ga_userinput, 1) == OK)
13716 {
13717 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13718 + ga_userinput.ga_len);
13719 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013720 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 }
13722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013723 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724}
13725
13726/*
13727 * "inputsecret()" function
13728 */
13729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *argvars;
13732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733{
13734 ++cmdline_star;
13735 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013736 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 --cmdline_star;
13738 --inputsecret_flag;
13739}
13740
13741/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013742 * "insert()" function
13743 */
13744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013746 typval_T *argvars;
13747 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013748{
13749 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 listitem_T *item;
13751 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013753
13754 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013756 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013757 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013758 {
13759 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 before = get_tv_number_chk(&argvars[2], &error);
13761 if (error)
13762 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763
Bram Moolenaar758711c2005-02-02 23:11:38 +000013764 if (before == l->lv_len)
13765 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013766 else
13767 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013768 item = list_find(l, before);
13769 if (item == NULL)
13770 {
13771 EMSGN(_(e_listidx), before);
13772 l = NULL;
13773 }
13774 }
13775 if (l != NULL)
13776 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013777 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013778 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013779 }
13780 }
13781}
13782
13783/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013784 * "invert(expr)" function
13785 */
13786 static void
13787f_invert(argvars, rettv)
13788 typval_T *argvars;
13789 typval_T *rettv;
13790{
13791 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13792}
13793
13794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 * "isdirectory()" function
13796 */
13797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 typval_T *argvars;
13800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013802 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803}
13804
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013805/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013806 * "islocked()" function
13807 */
13808 static void
13809f_islocked(argvars, rettv)
13810 typval_T *argvars;
13811 typval_T *rettv;
13812{
13813 lval_T lv;
13814 char_u *end;
13815 dictitem_T *di;
13816
13817 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013818 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13819 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013820 if (end != NULL && lv.ll_name != NULL)
13821 {
13822 if (*end != NUL)
13823 EMSG(_(e_trailing));
13824 else
13825 {
13826 if (lv.ll_tv == NULL)
13827 {
13828 if (check_changedtick(lv.ll_name))
13829 rettv->vval.v_number = 1; /* always locked */
13830 else
13831 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013832 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013833 if (di != NULL)
13834 {
13835 /* Consider a variable locked when:
13836 * 1. the variable itself is locked
13837 * 2. the value of the variable is locked.
13838 * 3. the List or Dict value is locked.
13839 */
13840 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13841 || tv_islocked(&di->di_tv));
13842 }
13843 }
13844 }
13845 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013846 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013847 else if (lv.ll_newkey != NULL)
13848 EMSG2(_(e_dictkey), lv.ll_newkey);
13849 else if (lv.ll_list != NULL)
13850 /* List item. */
13851 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13852 else
13853 /* Dictionary item. */
13854 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13855 }
13856 }
13857
13858 clear_lval(&lv);
13859}
13860
Bram Moolenaar33570922005-01-25 22:26:29 +000013861static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013862
13863/*
13864 * Turn a dict into a list:
13865 * "what" == 0: list of keys
13866 * "what" == 1: list of values
13867 * "what" == 2: list of items
13868 */
13869 static void
13870dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013873 int what;
13874{
Bram Moolenaar33570922005-01-25 22:26:29 +000013875 list_T *l2;
13876 dictitem_T *di;
13877 hashitem_T *hi;
13878 listitem_T *li;
13879 listitem_T *li2;
13880 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013881 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013882
Bram Moolenaar8c711452005-01-14 21:53:12 +000013883 if (argvars[0].v_type != VAR_DICT)
13884 {
13885 EMSG(_(e_dictreq));
13886 return;
13887 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013888 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013889 return;
13890
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013891 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013892 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013894 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013897 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013898 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013899 --todo;
13900 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013901
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013902 li = listitem_alloc();
13903 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013905 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013906
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013907 if (what == 0)
13908 {
13909 /* keys() */
13910 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013911 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013912 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13913 }
13914 else if (what == 1)
13915 {
13916 /* values() */
13917 copy_tv(&di->di_tv, &li->li_tv);
13918 }
13919 else
13920 {
13921 /* items() */
13922 l2 = list_alloc();
13923 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013924 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013925 li->li_tv.vval.v_list = l2;
13926 if (l2 == NULL)
13927 break;
13928 ++l2->lv_refcount;
13929
13930 li2 = listitem_alloc();
13931 if (li2 == NULL)
13932 break;
13933 list_append(l2, li2);
13934 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013935 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013936 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13937
13938 li2 = listitem_alloc();
13939 if (li2 == NULL)
13940 break;
13941 list_append(l2, li2);
13942 copy_tv(&di->di_tv, &li2->li_tv);
13943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013944 }
13945 }
13946}
13947
13948/*
13949 * "items(dict)" function
13950 */
13951 static void
13952f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013953 typval_T *argvars;
13954 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013955{
13956 dict_list(argvars, rettv, 2);
13957}
13958
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013960 * "join()" function
13961 */
13962 static void
13963f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *argvars;
13965 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966{
13967 garray_T ga;
13968 char_u *sep;
13969
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 if (argvars[0].v_type != VAR_LIST)
13971 {
13972 EMSG(_(e_listreq));
13973 return;
13974 }
13975 if (argvars[0].vval.v_list == NULL)
13976 return;
13977 if (argvars[1].v_type == VAR_UNKNOWN)
13978 sep = (char_u *)" ";
13979 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013981
13982 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013983
13984 if (sep != NULL)
13985 {
13986 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013987 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 ga_append(&ga, NUL);
13989 rettv->vval.v_string = (char_u *)ga.ga_data;
13990 }
13991 else
13992 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013993}
13994
13995/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996 * "keys()" function
13997 */
13998 static void
13999f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014000 typval_T *argvars;
14001 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014002{
14003 dict_list(argvars, rettv, 0);
14004}
14005
14006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 * "last_buffer_nr()" function.
14008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014010f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013{
14014 int n = 0;
14015 buf_T *buf;
14016
14017 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14018 if (n < buf->b_fnum)
14019 n = buf->b_fnum;
14020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014022}
14023
14024/*
14025 * "len()" function
14026 */
14027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014029 typval_T *argvars;
14030 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014031{
14032 switch (argvars[0].v_type)
14033 {
14034 case VAR_STRING:
14035 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 rettv->vval.v_number = (varnumber_T)STRLEN(
14037 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014038 break;
14039 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014041 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014042 case VAR_DICT:
14043 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14044 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014045 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014046 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 break;
14048 }
14049}
14050
Bram Moolenaar33570922005-01-25 22:26:29 +000014051static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014052
14053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 typval_T *argvars;
14056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014057 int type;
14058{
14059#ifdef FEAT_LIBCALL
14060 char_u *string_in;
14061 char_u **string_result;
14062 int nr_result;
14063#endif
14064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014066 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014068
14069 if (check_restricted() || check_secure())
14070 return;
14071
14072#ifdef FEAT_LIBCALL
14073 /* The first two args must be strings, otherwise its meaningless */
14074 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14075 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014076 string_in = NULL;
14077 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014078 string_in = argvars[2].vval.v_string;
14079 if (type == VAR_NUMBER)
14080 string_result = NULL;
14081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083 if (mch_libcall(argvars[0].vval.v_string,
14084 argvars[1].vval.v_string,
14085 string_in,
14086 argvars[2].vval.v_number,
14087 string_result,
14088 &nr_result) == OK
14089 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014091 }
14092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093}
14094
14095/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096 * "libcall()" function
14097 */
14098 static void
14099f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014100 typval_T *argvars;
14101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014102{
14103 libcall_common(argvars, rettv, VAR_STRING);
14104}
14105
14106/*
14107 * "libcallnr()" function
14108 */
14109 static void
14110f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 typval_T *argvars;
14112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113{
14114 libcall_common(argvars, rettv, VAR_NUMBER);
14115}
14116
14117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 * "line(string)" function
14119 */
14120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014121f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 typval_T *argvars;
14123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124{
14125 linenr_T lnum = 0;
14126 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014127 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014129 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 if (fp != NULL)
14131 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133}
14134
14135/*
14136 * "line2byte(lnum)" function
14137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014140 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142{
14143#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145#else
14146 linenr_T lnum;
14147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14153 if (rettv->vval.v_number >= 0)
14154 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155#endif
14156}
14157
14158/*
14159 * "lispindent(lnum)" function
14160 */
14161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165{
14166#ifdef FEAT_LISP
14167 pos_T pos;
14168 linenr_T lnum;
14169
14170 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14173 {
14174 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 curwin->w_cursor = pos;
14177 }
14178 else
14179#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
14183/*
14184 * "localtime()" function
14185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014188 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192}
14193
Bram Moolenaar33570922005-01-25 22:26:29 +000014194static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195
14196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014197get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 typval_T *argvars;
14199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 int exact;
14201{
14202 char_u *keys;
14203 char_u *which;
14204 char_u buf[NUMBUFLEN];
14205 char_u *keys_buf = NULL;
14206 char_u *rhs;
14207 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014208 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014209 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014210 mapblock_T *mp;
14211 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212
14213 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214 rettv->v_type = VAR_STRING;
14215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 if (*keys == NUL)
14219 return;
14220
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014221 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014224 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014225 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014226 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014227 if (argvars[3].v_type != VAR_UNKNOWN)
14228 get_dict = get_tv_number(&argvars[3]);
14229 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 else
14232 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 if (which == NULL)
14234 return;
14235
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 mode = get_map_mode(&which, 0);
14237
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014238 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014239 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014241
14242 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014244 /* Return a string. */
14245 if (rhs != NULL)
14246 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247
Bram Moolenaarbd743252010-10-20 21:23:33 +020014248 }
14249 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14250 {
14251 /* Return a dictionary. */
14252 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14253 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14254 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255
Bram Moolenaarbd743252010-10-20 21:23:33 +020014256 dict_add_nr_str(dict, "lhs", 0L, lhs);
14257 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14258 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14259 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14260 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14261 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14262 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014263 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014264 dict_add_nr_str(dict, "mode", 0L, mapmode);
14265
14266 vim_free(lhs);
14267 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 }
14269}
14270
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014271#ifdef FEAT_FLOAT
14272/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014273 * "log()" function
14274 */
14275 static void
14276f_log(argvars, rettv)
14277 typval_T *argvars;
14278 typval_T *rettv;
14279{
14280 float_T f;
14281
14282 rettv->v_type = VAR_FLOAT;
14283 if (get_float_arg(argvars, &f) == OK)
14284 rettv->vval.v_float = log(f);
14285 else
14286 rettv->vval.v_float = 0.0;
14287}
14288
14289/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014290 * "log10()" function
14291 */
14292 static void
14293f_log10(argvars, rettv)
14294 typval_T *argvars;
14295 typval_T *rettv;
14296{
14297 float_T f;
14298
14299 rettv->v_type = VAR_FLOAT;
14300 if (get_float_arg(argvars, &f) == OK)
14301 rettv->vval.v_float = log10(f);
14302 else
14303 rettv->vval.v_float = 0.0;
14304}
14305#endif
14306
Bram Moolenaar1dced572012-04-05 16:54:08 +020014307#ifdef FEAT_LUA
14308/*
14309 * "luaeval()" function
14310 */
14311 static void
14312f_luaeval(argvars, rettv)
14313 typval_T *argvars;
14314 typval_T *rettv;
14315{
14316 char_u *str;
14317 char_u buf[NUMBUFLEN];
14318
14319 str = get_tv_string_buf(&argvars[0], buf);
14320 do_luaeval(str, argvars + 1, rettv);
14321}
14322#endif
14323
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014325 * "map()" function
14326 */
14327 static void
14328f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014331{
14332 filter_map(argvars, rettv, TRUE);
14333}
14334
14335/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 */
14338 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014339f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 typval_T *argvars;
14341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344}
14345
14346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 */
14349 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014351 typval_T *argvars;
14352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014354 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355}
14356
Bram Moolenaar33570922005-01-25 22:26:29 +000014357static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358
14359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 int type;
14364{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014365 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014366 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 char_u *pat;
14369 regmatch_T regmatch;
14370 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014371 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 char_u *save_cpo;
14373 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014374 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014375 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014376 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014377 list_T *l = NULL;
14378 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014379 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014380 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381
14382 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14383 save_cpo = p_cpo;
14384 p_cpo = (char_u *)"";
14385
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014386 rettv->vval.v_number = -1;
14387 if (type == 3)
14388 {
14389 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014390 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014392 }
14393 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->v_type = VAR_STRING;
14396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014399 if (argvars[0].v_type == VAR_LIST)
14400 {
14401 if ((l = argvars[0].vval.v_list) == NULL)
14402 goto theend;
14403 li = l->lv_first;
14404 }
14405 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014406 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014407 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014408 len = (long)STRLEN(str);
14409 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14412 if (pat == NULL)
14413 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014415 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 int error = FALSE;
14418
14419 start = get_tv_number_chk(&argvars[2], &error);
14420 if (error)
14421 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014422 if (l != NULL)
14423 {
14424 li = list_find(l, start);
14425 if (li == NULL)
14426 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014427 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014428 }
14429 else
14430 {
14431 if (start < 0)
14432 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014433 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014434 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014435 /* When "count" argument is there ignore matches before "start",
14436 * otherwise skip part of the string. Differs when pattern is "^"
14437 * or "\<". */
14438 if (argvars[3].v_type != VAR_UNKNOWN)
14439 startcol = start;
14440 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014441 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014442 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014443 len -= start;
14444 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014445 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014446
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014447 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 nth = get_tv_number_chk(&argvars[3], &error);
14449 if (error)
14450 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 }
14452
14453 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14454 if (regmatch.regprog != NULL)
14455 {
14456 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014457
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014458 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014459 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014460 if (l != NULL)
14461 {
14462 if (li == NULL)
14463 {
14464 match = FALSE;
14465 break;
14466 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014467 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014468 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014469 if (str == NULL)
14470 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014471 }
14472
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014473 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014474
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014475 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014476 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014477 if (l == NULL && !match)
14478 break;
14479
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014480 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014481 if (l != NULL)
14482 {
14483 li = li->li_next;
14484 ++idx;
14485 }
14486 else
14487 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014488#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014489 startcol = (colnr_T)(regmatch.startp[0]
14490 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014491#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014492 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014493#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014494 if (startcol > (colnr_T)len
14495 || str + startcol <= regmatch.startp[0])
14496 {
14497 match = FALSE;
14498 break;
14499 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014500 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014501 }
14502
14503 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014505 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014506 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014507 int i;
14508
14509 /* return list with matched string and submatches */
14510 for (i = 0; i < NSUBEXP; ++i)
14511 {
14512 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014513 {
14514 if (list_append_string(rettv->vval.v_list,
14515 (char_u *)"", 0) == FAIL)
14516 break;
14517 }
14518 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014519 regmatch.startp[i],
14520 (int)(regmatch.endp[i] - regmatch.startp[i]))
14521 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014522 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014523 }
14524 }
14525 else if (type == 2)
14526 {
14527 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014528 if (l != NULL)
14529 copy_tv(&li->li_tv, rettv);
14530 else
14531 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014533 }
14534 else if (l != NULL)
14535 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 else
14537 {
14538 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 (varnumber_T)(regmatch.startp[0] - str);
14541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014544 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 }
14546 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014547 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 }
14549
14550theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014551 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 p_cpo = save_cpo;
14553}
14554
14555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556 * "match()" function
14557 */
14558 static void
14559f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *argvars;
14561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014562{
14563 find_some_match(argvars, rettv, 1);
14564}
14565
14566/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014567 * "matchadd()" function
14568 */
14569 static void
14570f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014571 typval_T *argvars UNUSED;
14572 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014573{
14574#ifdef FEAT_SEARCH_EXTRA
14575 char_u buf[NUMBUFLEN];
14576 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14577 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14578 int prio = 10; /* default priority */
14579 int id = -1;
14580 int error = FALSE;
14581
14582 rettv->vval.v_number = -1;
14583
14584 if (grp == NULL || pat == NULL)
14585 return;
14586 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014587 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014588 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014589 if (argvars[3].v_type != VAR_UNKNOWN)
14590 id = get_tv_number_chk(&argvars[3], &error);
14591 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014592 if (error == TRUE)
14593 return;
14594 if (id >= 1 && id <= 3)
14595 {
14596 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14597 return;
14598 }
14599
Bram Moolenaarb3414592014-06-17 17:48:32 +020014600 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14601#endif
14602}
14603
14604/*
14605 * "matchaddpos()" function
14606 */
14607 static void
14608f_matchaddpos(argvars, rettv)
14609 typval_T *argvars UNUSED;
14610 typval_T *rettv UNUSED;
14611{
14612#ifdef FEAT_SEARCH_EXTRA
14613 char_u buf[NUMBUFLEN];
14614 char_u *group;
14615 int prio = 10;
14616 int id = -1;
14617 int error = FALSE;
14618 list_T *l;
14619
14620 rettv->vval.v_number = -1;
14621
14622 group = get_tv_string_buf_chk(&argvars[0], buf);
14623 if (group == NULL)
14624 return;
14625
14626 if (argvars[1].v_type != VAR_LIST)
14627 {
14628 EMSG2(_(e_listarg), "matchaddpos()");
14629 return;
14630 }
14631 l = argvars[1].vval.v_list;
14632 if (l == NULL)
14633 return;
14634
14635 if (argvars[2].v_type != VAR_UNKNOWN)
14636 {
14637 prio = get_tv_number_chk(&argvars[2], &error);
14638 if (argvars[3].v_type != VAR_UNKNOWN)
14639 id = get_tv_number_chk(&argvars[3], &error);
14640 }
14641 if (error == TRUE)
14642 return;
14643
14644 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14645 if (id == 1 || id == 2)
14646 {
14647 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14648 return;
14649 }
14650
14651 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014652#endif
14653}
14654
14655/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014656 * "matcharg()" function
14657 */
14658 static void
14659f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014660 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014661 typval_T *rettv;
14662{
14663 if (rettv_list_alloc(rettv) == OK)
14664 {
14665#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014666 int id = get_tv_number(&argvars[0]);
14667 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014668
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014669 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014670 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014671 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14672 {
14673 list_append_string(rettv->vval.v_list,
14674 syn_id2name(m->hlg_id), -1);
14675 list_append_string(rettv->vval.v_list, m->pattern, -1);
14676 }
14677 else
14678 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014679 list_append_string(rettv->vval.v_list, NULL, -1);
14680 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014681 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014682 }
14683#endif
14684 }
14685}
14686
14687/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014688 * "matchdelete()" function
14689 */
14690 static void
14691f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014692 typval_T *argvars UNUSED;
14693 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014694{
14695#ifdef FEAT_SEARCH_EXTRA
14696 rettv->vval.v_number = match_delete(curwin,
14697 (int)get_tv_number(&argvars[0]), TRUE);
14698#endif
14699}
14700
14701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 * "matchend()" function
14703 */
14704 static void
14705f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014706 typval_T *argvars;
14707 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708{
14709 find_some_match(argvars, rettv, 0);
14710}
14711
14712/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014713 * "matchlist()" function
14714 */
14715 static void
14716f_matchlist(argvars, rettv)
14717 typval_T *argvars;
14718 typval_T *rettv;
14719{
14720 find_some_match(argvars, rettv, 3);
14721}
14722
14723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724 * "matchstr()" function
14725 */
14726 static void
14727f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014728 typval_T *argvars;
14729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730{
14731 find_some_match(argvars, rettv, 2);
14732}
14733
Bram Moolenaar33570922005-01-25 22:26:29 +000014734static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014735
14736 static void
14737max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 typval_T *argvars;
14739 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014740 int domax;
14741{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014742 long n = 0;
14743 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014745
14746 if (argvars[0].v_type == VAR_LIST)
14747 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 list_T *l;
14749 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014750
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014751 l = argvars[0].vval.v_list;
14752 if (l != NULL)
14753 {
14754 li = l->lv_first;
14755 if (li != NULL)
14756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014757 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014758 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014759 {
14760 li = li->li_next;
14761 if (li == NULL)
14762 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014763 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014764 if (domax ? i > n : i < n)
14765 n = i;
14766 }
14767 }
14768 }
14769 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014770 else if (argvars[0].v_type == VAR_DICT)
14771 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014772 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014773 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014775 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014776
14777 d = argvars[0].vval.v_dict;
14778 if (d != NULL)
14779 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014780 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014781 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014782 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014783 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014784 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014785 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014787 if (first)
14788 {
14789 n = i;
14790 first = FALSE;
14791 }
14792 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014793 n = i;
14794 }
14795 }
14796 }
14797 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014798 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014799 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014801}
14802
14803/*
14804 * "max()" function
14805 */
14806 static void
14807f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014808 typval_T *argvars;
14809 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014810{
14811 max_min(argvars, rettv, TRUE);
14812}
14813
14814/*
14815 * "min()" function
14816 */
14817 static void
14818f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014819 typval_T *argvars;
14820 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014821{
14822 max_min(argvars, rettv, FALSE);
14823}
14824
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014825static int mkdir_recurse __ARGS((char_u *dir, int prot));
14826
14827/*
14828 * Create the directory in which "dir" is located, and higher levels when
14829 * needed.
14830 */
14831 static int
14832mkdir_recurse(dir, prot)
14833 char_u *dir;
14834 int prot;
14835{
14836 char_u *p;
14837 char_u *updir;
14838 int r = FAIL;
14839
14840 /* Get end of directory name in "dir".
14841 * We're done when it's "/" or "c:/". */
14842 p = gettail_sep(dir);
14843 if (p <= get_past_head(dir))
14844 return OK;
14845
14846 /* If the directory exists we're done. Otherwise: create it.*/
14847 updir = vim_strnsave(dir, (int)(p - dir));
14848 if (updir == NULL)
14849 return FAIL;
14850 if (mch_isdir(updir))
14851 r = OK;
14852 else if (mkdir_recurse(updir, prot) == OK)
14853 r = vim_mkdir_emsg(updir, prot);
14854 vim_free(updir);
14855 return r;
14856}
14857
14858#ifdef vim_mkdir
14859/*
14860 * "mkdir()" function
14861 */
14862 static void
14863f_mkdir(argvars, rettv)
14864 typval_T *argvars;
14865 typval_T *rettv;
14866{
14867 char_u *dir;
14868 char_u buf[NUMBUFLEN];
14869 int prot = 0755;
14870
14871 rettv->vval.v_number = FAIL;
14872 if (check_restricted() || check_secure())
14873 return;
14874
14875 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014876 if (*dir == NUL)
14877 rettv->vval.v_number = FAIL;
14878 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014879 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014880 if (*gettail(dir) == NUL)
14881 /* remove trailing slashes */
14882 *gettail_sep(dir) = NUL;
14883
14884 if (argvars[1].v_type != VAR_UNKNOWN)
14885 {
14886 if (argvars[2].v_type != VAR_UNKNOWN)
14887 prot = get_tv_number_chk(&argvars[2], NULL);
14888 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14889 mkdir_recurse(dir, prot);
14890 }
14891 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014892 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014893}
14894#endif
14895
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 * "mode()" function
14898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014901 typval_T *argvars;
14902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014904 char_u buf[3];
14905
14906 buf[1] = NUL;
14907 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 if (VIsual_active)
14910 {
14911 if (VIsual_select)
14912 buf[0] = VIsual_mode + 's' - 'v';
14913 else
14914 buf[0] = VIsual_mode;
14915 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014916 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014917 || State == CONFIRM)
14918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014920 if (State == ASKMORE)
14921 buf[1] = 'm';
14922 else if (State == CONFIRM)
14923 buf[1] = '?';
14924 }
14925 else if (State == EXTERNCMD)
14926 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 else if (State & INSERT)
14928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014929#ifdef FEAT_VREPLACE
14930 if (State & VREPLACE_FLAG)
14931 {
14932 buf[0] = 'R';
14933 buf[1] = 'v';
14934 }
14935 else
14936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 if (State & REPLACE_FLAG)
14938 buf[0] = 'R';
14939 else
14940 buf[0] = 'i';
14941 }
14942 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014945 if (exmode_active)
14946 buf[1] = 'v';
14947 }
14948 else if (exmode_active)
14949 {
14950 buf[0] = 'c';
14951 buf[1] = 'e';
14952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014956 if (finish_op)
14957 buf[1] = 'o';
14958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014960 /* Clear out the minor mode when the argument is not a non-zero number or
14961 * non-empty string. */
14962 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014963 buf[1] = NUL;
14964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014965 rettv->vval.v_string = vim_strsave(buf);
14966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967}
14968
Bram Moolenaar429fa852013-04-15 12:27:36 +020014969#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014970/*
14971 * "mzeval()" function
14972 */
14973 static void
14974f_mzeval(argvars, rettv)
14975 typval_T *argvars;
14976 typval_T *rettv;
14977{
14978 char_u *str;
14979 char_u buf[NUMBUFLEN];
14980
14981 str = get_tv_string_buf(&argvars[0], buf);
14982 do_mzeval(str, rettv);
14983}
Bram Moolenaar75676462013-01-30 14:55:42 +010014984
14985 void
14986mzscheme_call_vim(name, args, rettv)
14987 char_u *name;
14988 typval_T *args;
14989 typval_T *rettv;
14990{
14991 typval_T argvars[3];
14992
14993 argvars[0].v_type = VAR_STRING;
14994 argvars[0].vval.v_string = name;
14995 copy_tv(args, &argvars[1]);
14996 argvars[2].v_type = VAR_UNKNOWN;
14997 f_call(argvars, rettv);
14998 clear_tv(&argvars[1]);
14999}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015000#endif
15001
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003 * "nextnonblank()" function
15004 */
15005 static void
15006f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015007 typval_T *argvars;
15008 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015009{
15010 linenr_T lnum;
15011
15012 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015014 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015 {
15016 lnum = 0;
15017 break;
15018 }
15019 if (*skipwhite(ml_get(lnum)) != NUL)
15020 break;
15021 }
15022 rettv->vval.v_number = lnum;
15023}
15024
15025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026 * "nr2char()" function
15027 */
15028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015029f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 typval_T *argvars;
15031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032{
15033 char_u buf[NUMBUFLEN];
15034
15035#ifdef FEAT_MBYTE
15036 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015037 {
15038 int utf8 = 0;
15039
15040 if (argvars[1].v_type != VAR_UNKNOWN)
15041 utf8 = get_tv_number_chk(&argvars[1], NULL);
15042 if (utf8)
15043 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15044 else
15045 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 else
15048#endif
15049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 buf[1] = NUL;
15052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->v_type = VAR_STRING;
15054 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015055}
15056
15057/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015058 * "or(expr, expr)" function
15059 */
15060 static void
15061f_or(argvars, rettv)
15062 typval_T *argvars;
15063 typval_T *rettv;
15064{
15065 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15066 | get_tv_number_chk(&argvars[1], NULL);
15067}
15068
15069/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015070 * "pathshorten()" function
15071 */
15072 static void
15073f_pathshorten(argvars, rettv)
15074 typval_T *argvars;
15075 typval_T *rettv;
15076{
15077 char_u *p;
15078
15079 rettv->v_type = VAR_STRING;
15080 p = get_tv_string_chk(&argvars[0]);
15081 if (p == NULL)
15082 rettv->vval.v_string = NULL;
15083 else
15084 {
15085 p = vim_strsave(p);
15086 rettv->vval.v_string = p;
15087 if (p != NULL)
15088 shorten_dir(p);
15089 }
15090}
15091
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015092#ifdef FEAT_FLOAT
15093/*
15094 * "pow()" function
15095 */
15096 static void
15097f_pow(argvars, rettv)
15098 typval_T *argvars;
15099 typval_T *rettv;
15100{
15101 float_T fx, fy;
15102
15103 rettv->v_type = VAR_FLOAT;
15104 if (get_float_arg(argvars, &fx) == OK
15105 && get_float_arg(&argvars[1], &fy) == OK)
15106 rettv->vval.v_float = pow(fx, fy);
15107 else
15108 rettv->vval.v_float = 0.0;
15109}
15110#endif
15111
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015112/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 * "prevnonblank()" function
15114 */
15115 static void
15116f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015117 typval_T *argvars;
15118 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119{
15120 linenr_T lnum;
15121
15122 lnum = get_tv_lnum(argvars);
15123 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15124 lnum = 0;
15125 else
15126 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15127 --lnum;
15128 rettv->vval.v_number = lnum;
15129}
15130
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015131#ifdef HAVE_STDARG_H
15132/* This dummy va_list is here because:
15133 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15134 * - locally in the function results in a "used before set" warning
15135 * - using va_start() to initialize it gives "function with fixed args" error */
15136static va_list ap;
15137#endif
15138
Bram Moolenaar8c711452005-01-14 21:53:12 +000015139/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015140 * "printf()" function
15141 */
15142 static void
15143f_printf(argvars, rettv)
15144 typval_T *argvars;
15145 typval_T *rettv;
15146{
15147 rettv->v_type = VAR_STRING;
15148 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015149#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015150 {
15151 char_u buf[NUMBUFLEN];
15152 int len;
15153 char_u *s;
15154 int saved_did_emsg = did_emsg;
15155 char *fmt;
15156
15157 /* Get the required length, allocate the buffer and do it for real. */
15158 did_emsg = FALSE;
15159 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015160 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015161 if (!did_emsg)
15162 {
15163 s = alloc(len + 1);
15164 if (s != NULL)
15165 {
15166 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015167 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015168 }
15169 }
15170 did_emsg |= saved_did_emsg;
15171 }
15172#endif
15173}
15174
15175/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015176 * "pumvisible()" function
15177 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015178 static void
15179f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015180 typval_T *argvars UNUSED;
15181 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015182{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183#ifdef FEAT_INS_EXPAND
15184 if (pum_visible())
15185 rettv->vval.v_number = 1;
15186#endif
15187}
15188
Bram Moolenaardb913952012-06-29 12:54:53 +020015189#ifdef FEAT_PYTHON3
15190/*
15191 * "py3eval()" function
15192 */
15193 static void
15194f_py3eval(argvars, rettv)
15195 typval_T *argvars;
15196 typval_T *rettv;
15197{
15198 char_u *str;
15199 char_u buf[NUMBUFLEN];
15200
15201 str = get_tv_string_buf(&argvars[0], buf);
15202 do_py3eval(str, rettv);
15203}
15204#endif
15205
15206#ifdef FEAT_PYTHON
15207/*
15208 * "pyeval()" function
15209 */
15210 static void
15211f_pyeval(argvars, rettv)
15212 typval_T *argvars;
15213 typval_T *rettv;
15214{
15215 char_u *str;
15216 char_u buf[NUMBUFLEN];
15217
15218 str = get_tv_string_buf(&argvars[0], buf);
15219 do_pyeval(str, rettv);
15220}
15221#endif
15222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015223/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015224 * "range()" function
15225 */
15226 static void
15227f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015228 typval_T *argvars;
15229 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015230{
15231 long start;
15232 long end;
15233 long stride = 1;
15234 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015235 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015238 if (argvars[1].v_type == VAR_UNKNOWN)
15239 {
15240 end = start - 1;
15241 start = 0;
15242 }
15243 else
15244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015245 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015248 }
15249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 if (error)
15251 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015252 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015253 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015254 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015255 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015256 else
15257 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015258 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015259 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015260 if (list_append_number(rettv->vval.v_list,
15261 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015262 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015263 }
15264}
15265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015266/*
15267 * "readfile()" function
15268 */
15269 static void
15270f_readfile(argvars, rettv)
15271 typval_T *argvars;
15272 typval_T *rettv;
15273{
15274 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015275 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015276 char_u *fname;
15277 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015278 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15279 int io_size = sizeof(buf);
15280 int readlen; /* size of last fread() */
15281 char_u *prev = NULL; /* previously read bytes, if any */
15282 long prevlen = 0; /* length of data in prev */
15283 long prevsize = 0; /* size of prev buffer */
15284 long maxline = MAXLNUM;
15285 long cnt = 0;
15286 char_u *p; /* position in buf */
15287 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015288
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015289 if (argvars[1].v_type != VAR_UNKNOWN)
15290 {
15291 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15292 binary = TRUE;
15293 if (argvars[2].v_type != VAR_UNKNOWN)
15294 maxline = get_tv_number(&argvars[2]);
15295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015297 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015298 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015299
15300 /* Always open the file in binary mode, library functions have a mind of
15301 * their own about CR-LF conversion. */
15302 fname = get_tv_string(&argvars[0]);
15303 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15304 {
15305 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15306 return;
15307 }
15308
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015309 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015310 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015311 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015312
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015313 /* This for loop processes what was read, but is also entered at end
15314 * of file so that either:
15315 * - an incomplete line gets written
15316 * - a "binary" file gets an empty line at the end if it ends in a
15317 * newline. */
15318 for (p = buf, start = buf;
15319 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15320 ++p)
15321 {
15322 if (*p == '\n' || readlen <= 0)
15323 {
15324 listitem_T *li;
15325 char_u *s = NULL;
15326 long_u len = p - start;
15327
15328 /* Finished a line. Remove CRs before NL. */
15329 if (readlen > 0 && !binary)
15330 {
15331 while (len > 0 && start[len - 1] == '\r')
15332 --len;
15333 /* removal may cross back to the "prev" string */
15334 if (len == 0)
15335 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15336 --prevlen;
15337 }
15338 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015339 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015340 else
15341 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015342 /* Change "prev" buffer to be the right size. This way
15343 * the bytes are only copied once, and very long lines are
15344 * allocated only once. */
15345 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015346 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015347 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015349 prev = NULL; /* the list will own the string */
15350 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 }
15352 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015353 if (s == NULL)
15354 {
15355 do_outofmem_msg((long_u) prevlen + len + 1);
15356 failed = TRUE;
15357 break;
15358 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015359
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015360 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015361 {
15362 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015363 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 break;
15365 }
15366 li->li_tv.v_type = VAR_STRING;
15367 li->li_tv.v_lock = 0;
15368 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015369 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015371 start = p + 1; /* step over newline */
15372 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373 break;
15374 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015375 else if (*p == NUL)
15376 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015377#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015378 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15379 * when finding the BF and check the previous two bytes. */
15380 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015381 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015382 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15383 * + 1, these may be in the "prev" string. */
15384 char_u back1 = p >= buf + 1 ? p[-1]
15385 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15386 char_u back2 = p >= buf + 2 ? p[-2]
15387 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15388 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015389
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015390 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015391 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015392 char_u *dest = p - 2;
15393
15394 /* Usually a BOM is at the beginning of a file, and so at
15395 * the beginning of a line; then we can just step over it.
15396 */
15397 if (start == dest)
15398 start = p + 1;
15399 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015400 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015401 /* have to shuffle buf to close gap */
15402 int adjust_prevlen = 0;
15403
15404 if (dest < buf)
15405 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015406 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015407 dest = buf;
15408 }
15409 if (readlen > p - buf + 1)
15410 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15411 readlen -= 3 - adjust_prevlen;
15412 prevlen -= adjust_prevlen;
15413 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015414 }
15415 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015416 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015417#endif
15418 } /* for */
15419
15420 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15421 break;
15422 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015423 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015424 /* There's part of a line in buf, store it in "prev". */
15425 if (p - start + prevlen >= prevsize)
15426 {
15427 /* need bigger "prev" buffer */
15428 char_u *newprev;
15429
15430 /* A common use case is ordinary text files and "prev" gets a
15431 * fragment of a line, so the first allocation is made
15432 * small, to avoid repeatedly 'allocing' large and
15433 * 'reallocing' small. */
15434 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015435 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015436 else
15437 {
15438 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015439 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015440 prevsize = grow50pc > growmin ? grow50pc : growmin;
15441 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015442 newprev = prev == NULL ? alloc(prevsize)
15443 : vim_realloc(prev, prevsize);
15444 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015445 {
15446 do_outofmem_msg((long_u)prevsize);
15447 failed = TRUE;
15448 break;
15449 }
15450 prev = newprev;
15451 }
15452 /* Add the line part to end of "prev". */
15453 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015454 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015455 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015458 /*
15459 * For a negative line count use only the lines at the end of the file,
15460 * free the rest.
15461 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015462 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015463 while (cnt > -maxline)
15464 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015465 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015466 --cnt;
15467 }
15468
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015469 if (failed)
15470 {
15471 list_free(rettv->vval.v_list, TRUE);
15472 /* readfile doc says an empty list is returned on error */
15473 rettv->vval.v_list = list_alloc();
15474 }
15475
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015476 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015477 fclose(fd);
15478}
15479
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015480#if defined(FEAT_RELTIME)
15481static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15482
15483/*
15484 * Convert a List to proftime_T.
15485 * Return FAIL when there is something wrong.
15486 */
15487 static int
15488list2proftime(arg, tm)
15489 typval_T *arg;
15490 proftime_T *tm;
15491{
15492 long n1, n2;
15493 int error = FALSE;
15494
15495 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15496 || arg->vval.v_list->lv_len != 2)
15497 return FAIL;
15498 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15499 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15500# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015501 tm->HighPart = n1;
15502 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015503# else
15504 tm->tv_sec = n1;
15505 tm->tv_usec = n2;
15506# endif
15507 return error ? FAIL : OK;
15508}
15509#endif /* FEAT_RELTIME */
15510
15511/*
15512 * "reltime()" function
15513 */
15514 static void
15515f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015516 typval_T *argvars UNUSED;
15517 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015518{
15519#ifdef FEAT_RELTIME
15520 proftime_T res;
15521 proftime_T start;
15522
15523 if (argvars[0].v_type == VAR_UNKNOWN)
15524 {
15525 /* No arguments: get current time. */
15526 profile_start(&res);
15527 }
15528 else if (argvars[1].v_type == VAR_UNKNOWN)
15529 {
15530 if (list2proftime(&argvars[0], &res) == FAIL)
15531 return;
15532 profile_end(&res);
15533 }
15534 else
15535 {
15536 /* Two arguments: compute the difference. */
15537 if (list2proftime(&argvars[0], &start) == FAIL
15538 || list2proftime(&argvars[1], &res) == FAIL)
15539 return;
15540 profile_sub(&res, &start);
15541 }
15542
15543 if (rettv_list_alloc(rettv) == OK)
15544 {
15545 long n1, n2;
15546
15547# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015548 n1 = res.HighPart;
15549 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015550# else
15551 n1 = res.tv_sec;
15552 n2 = res.tv_usec;
15553# endif
15554 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15555 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15556 }
15557#endif
15558}
15559
15560/*
15561 * "reltimestr()" function
15562 */
15563 static void
15564f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015565 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015566 typval_T *rettv;
15567{
15568#ifdef FEAT_RELTIME
15569 proftime_T tm;
15570#endif
15571
15572 rettv->v_type = VAR_STRING;
15573 rettv->vval.v_string = NULL;
15574#ifdef FEAT_RELTIME
15575 if (list2proftime(&argvars[0], &tm) == OK)
15576 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15577#endif
15578}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015579
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15581static void make_connection __ARGS((void));
15582static int check_connection __ARGS((void));
15583
15584 static void
15585make_connection()
15586{
15587 if (X_DISPLAY == NULL
15588# ifdef FEAT_GUI
15589 && !gui.in_use
15590# endif
15591 )
15592 {
15593 x_force_connect = TRUE;
15594 setup_term_clip();
15595 x_force_connect = FALSE;
15596 }
15597}
15598
15599 static int
15600check_connection()
15601{
15602 make_connection();
15603 if (X_DISPLAY == NULL)
15604 {
15605 EMSG(_("E240: No connection to Vim server"));
15606 return FAIL;
15607 }
15608 return OK;
15609}
15610#endif
15611
15612#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015613static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614
15615 static void
15616remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015617 typval_T *argvars;
15618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619 int expr;
15620{
15621 char_u *server_name;
15622 char_u *keys;
15623 char_u *r = NULL;
15624 char_u buf[NUMBUFLEN];
15625# ifdef WIN32
15626 HWND w;
15627# else
15628 Window w;
15629# endif
15630
15631 if (check_restricted() || check_secure())
15632 return;
15633
15634# ifdef FEAT_X11
15635 if (check_connection() == FAIL)
15636 return;
15637# endif
15638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 server_name = get_tv_string_chk(&argvars[0]);
15640 if (server_name == NULL)
15641 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642 keys = get_tv_string_buf(&argvars[1], buf);
15643# ifdef WIN32
15644 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15645# else
15646 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15647 < 0)
15648# endif
15649 {
15650 if (r != NULL)
15651 EMSG(r); /* sending worked but evaluation failed */
15652 else
15653 EMSG2(_("E241: Unable to send to %s"), server_name);
15654 return;
15655 }
15656
15657 rettv->vval.v_string = r;
15658
15659 if (argvars[2].v_type != VAR_UNKNOWN)
15660 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015662 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015663 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015665 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015666 v.di_tv.v_type = VAR_STRING;
15667 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015668 idvar = get_tv_string_chk(&argvars[2]);
15669 if (idvar != NULL)
15670 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015672 }
15673}
15674#endif
15675
15676/*
15677 * "remote_expr()" function
15678 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015679 static void
15680f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683{
15684 rettv->v_type = VAR_STRING;
15685 rettv->vval.v_string = NULL;
15686#ifdef FEAT_CLIENTSERVER
15687 remote_common(argvars, rettv, TRUE);
15688#endif
15689}
15690
15691/*
15692 * "remote_foreground()" function
15693 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694 static void
15695f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015696 typval_T *argvars UNUSED;
15697 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015698{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699#ifdef FEAT_CLIENTSERVER
15700# ifdef WIN32
15701 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015702 {
15703 char_u *server_name = get_tv_string_chk(&argvars[0]);
15704
15705 if (server_name != NULL)
15706 serverForeground(server_name);
15707 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015708# else
15709 /* Send a foreground() expression to the server. */
15710 argvars[1].v_type = VAR_STRING;
15711 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15712 argvars[2].v_type = VAR_UNKNOWN;
15713 remote_common(argvars, rettv, TRUE);
15714 vim_free(argvars[1].vval.v_string);
15715# endif
15716#endif
15717}
15718
Bram Moolenaar0d660222005-01-07 21:51:51 +000015719 static void
15720f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015721 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723{
15724#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726 char_u *s = NULL;
15727# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015728 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015730 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731
15732 if (check_restricted() || check_secure())
15733 {
15734 rettv->vval.v_number = -1;
15735 return;
15736 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015737 serverid = get_tv_string_chk(&argvars[0]);
15738 if (serverid == NULL)
15739 {
15740 rettv->vval.v_number = -1;
15741 return; /* type error; errmsg already given */
15742 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015744 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 if (n == 0)
15746 rettv->vval.v_number = -1;
15747 else
15748 {
15749 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15750 rettv->vval.v_number = (s != NULL);
15751 }
15752# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 if (check_connection() == FAIL)
15754 return;
15755
15756 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015757 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758# endif
15759
15760 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762 char_u *retvar;
15763
Bram Moolenaar33570922005-01-25 22:26:29 +000015764 v.di_tv.v_type = VAR_STRING;
15765 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015766 retvar = get_tv_string_chk(&argvars[1]);
15767 if (retvar != NULL)
15768 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015769 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015770 }
15771#else
15772 rettv->vval.v_number = -1;
15773#endif
15774}
15775
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776 static void
15777f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015778 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780{
15781 char_u *r = NULL;
15782
15783#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015784 char_u *serverid = get_tv_string_chk(&argvars[0]);
15785
15786 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015787 {
15788# ifdef WIN32
15789 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015790 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015792 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793 if (n != 0)
15794 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15795 if (r == NULL)
15796# else
15797 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799# endif
15800 EMSG(_("E277: Unable to read a server reply"));
15801 }
15802#endif
15803 rettv->v_type = VAR_STRING;
15804 rettv->vval.v_string = r;
15805}
15806
15807/*
15808 * "remote_send()" function
15809 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810 static void
15811f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015812 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814{
15815 rettv->v_type = VAR_STRING;
15816 rettv->vval.v_string = NULL;
15817#ifdef FEAT_CLIENTSERVER
15818 remote_common(argvars, rettv, FALSE);
15819#endif
15820}
15821
15822/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015823 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015824 */
15825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015826f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015827 typval_T *argvars;
15828 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015829{
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 list_T *l;
15831 listitem_T *item, *item2;
15832 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015833 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015834 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015835 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015836 dict_T *d;
15837 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015838 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015839
Bram Moolenaar8c711452005-01-14 21:53:12 +000015840 if (argvars[0].v_type == VAR_DICT)
15841 {
15842 if (argvars[2].v_type != VAR_UNKNOWN)
15843 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015844 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015845 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 key = get_tv_string_chk(&argvars[1]);
15848 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 di = dict_find(d, key, -1);
15851 if (di == NULL)
15852 EMSG2(_(e_dictkey), key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020015853 else if (!var_check_fixed(di->di_flags, (char_u *)_(arg_errmsg))
15854 && !var_check_ro(di->di_flags,
15855 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015856 {
15857 *rettv = di->di_tv;
15858 init_tv(&di->di_tv);
15859 dictitem_remove(d, di);
15860 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015862 }
15863 }
15864 else if (argvars[0].v_type != VAR_LIST)
15865 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015866 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015867 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015869 int error = FALSE;
15870
15871 idx = get_tv_number_chk(&argvars[1], &error);
15872 if (error)
15873 ; /* type error: do nothing, errmsg already given */
15874 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015875 EMSGN(_(e_listidx), idx);
15876 else
15877 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015878 if (argvars[2].v_type == VAR_UNKNOWN)
15879 {
15880 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015881 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015882 *rettv = item->li_tv;
15883 vim_free(item);
15884 }
15885 else
15886 {
15887 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015888 end = get_tv_number_chk(&argvars[2], &error);
15889 if (error)
15890 ; /* type error: do nothing */
15891 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015892 EMSGN(_(e_listidx), end);
15893 else
15894 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015895 int cnt = 0;
15896
15897 for (li = item; li != NULL; li = li->li_next)
15898 {
15899 ++cnt;
15900 if (li == item2)
15901 break;
15902 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015903 if (li == NULL) /* didn't find "item2" after "item" */
15904 EMSG(_(e_invrange));
15905 else
15906 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015907 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015908 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015909 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015910 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015911 l->lv_first = item;
15912 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015913 item->li_prev = NULL;
15914 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015915 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015916 }
15917 }
15918 }
15919 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015920 }
15921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922}
15923
15924/*
15925 * "rename({from}, {to})" function
15926 */
15927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015928f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 typval_T *argvars;
15930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931{
15932 char_u buf[NUMBUFLEN];
15933
15934 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15938 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939}
15940
15941/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015942 * "repeat()" function
15943 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015944 static void
15945f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015946 typval_T *argvars;
15947 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015948{
15949 char_u *p;
15950 int n;
15951 int slen;
15952 int len;
15953 char_u *r;
15954 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955
15956 n = get_tv_number(&argvars[1]);
15957 if (argvars[0].v_type == VAR_LIST)
15958 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015959 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015960 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 if (list_extend(rettv->vval.v_list,
15962 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015963 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015964 }
15965 else
15966 {
15967 p = get_tv_string(&argvars[0]);
15968 rettv->v_type = VAR_STRING;
15969 rettv->vval.v_string = NULL;
15970
15971 slen = (int)STRLEN(p);
15972 len = slen * n;
15973 if (len <= 0)
15974 return;
15975
15976 r = alloc(len + 1);
15977 if (r != NULL)
15978 {
15979 for (i = 0; i < n; i++)
15980 mch_memmove(r + i * slen, p, (size_t)slen);
15981 r[len] = NUL;
15982 }
15983
15984 rettv->vval.v_string = r;
15985 }
15986}
15987
15988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989 * "resolve()" function
15990 */
15991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015992f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015993 typval_T *argvars;
15994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995{
15996 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015997#ifdef HAVE_READLINK
15998 char_u *buf = NULL;
15999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002#ifdef FEAT_SHORTCUT
16003 {
16004 char_u *v = NULL;
16005
16006 v = mch_resolve_shortcut(p);
16007 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 }
16012#else
16013# ifdef HAVE_READLINK
16014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 char_u *cpy;
16016 int len;
16017 char_u *remain = NULL;
16018 char_u *q;
16019 int is_relative_to_current = FALSE;
16020 int has_trailing_pathsep = FALSE;
16021 int limit = 100;
16022
16023 p = vim_strsave(p);
16024
16025 if (p[0] == '.' && (vim_ispathsep(p[1])
16026 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16027 is_relative_to_current = TRUE;
16028
16029 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016030 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016033 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035
16036 q = getnextcomp(p);
16037 if (*q != NUL)
16038 {
16039 /* Separate the first path component in "p", and keep the
16040 * remainder (beginning with the path separator). */
16041 remain = vim_strsave(q - 1);
16042 q[-1] = NUL;
16043 }
16044
Bram Moolenaard9462e32011-04-11 21:35:11 +020016045 buf = alloc(MAXPATHL + 1);
16046 if (buf == NULL)
16047 goto fail;
16048
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 for (;;)
16050 {
16051 for (;;)
16052 {
16053 len = readlink((char *)p, (char *)buf, MAXPATHL);
16054 if (len <= 0)
16055 break;
16056 buf[len] = NUL;
16057
16058 if (limit-- == 0)
16059 {
16060 vim_free(p);
16061 vim_free(remain);
16062 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 goto fail;
16065 }
16066
16067 /* Ensure that the result will have a trailing path separator
16068 * if the argument has one. */
16069 if (remain == NULL && has_trailing_pathsep)
16070 add_pathsep(buf);
16071
16072 /* Separate the first path component in the link value and
16073 * concatenate the remainders. */
16074 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16075 if (*q != NUL)
16076 {
16077 if (remain == NULL)
16078 remain = vim_strsave(q - 1);
16079 else
16080 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016081 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 if (cpy != NULL)
16083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 vim_free(remain);
16085 remain = cpy;
16086 }
16087 }
16088 q[-1] = NUL;
16089 }
16090
16091 q = gettail(p);
16092 if (q > p && *q == NUL)
16093 {
16094 /* Ignore trailing path separator. */
16095 q[-1] = NUL;
16096 q = gettail(p);
16097 }
16098 if (q > p && !mch_isFullName(buf))
16099 {
16100 /* symlink is relative to directory of argument */
16101 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16102 if (cpy != NULL)
16103 {
16104 STRCPY(cpy, p);
16105 STRCPY(gettail(cpy), buf);
16106 vim_free(p);
16107 p = cpy;
16108 }
16109 }
16110 else
16111 {
16112 vim_free(p);
16113 p = vim_strsave(buf);
16114 }
16115 }
16116
16117 if (remain == NULL)
16118 break;
16119
16120 /* Append the first path component of "remain" to "p". */
16121 q = getnextcomp(remain + 1);
16122 len = q - remain - (*q != NUL);
16123 cpy = vim_strnsave(p, STRLEN(p) + len);
16124 if (cpy != NULL)
16125 {
16126 STRNCAT(cpy, remain, len);
16127 vim_free(p);
16128 p = cpy;
16129 }
16130 /* Shorten "remain". */
16131 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016132 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 else
16134 {
16135 vim_free(remain);
16136 remain = NULL;
16137 }
16138 }
16139
16140 /* If the result is a relative path name, make it explicitly relative to
16141 * the current directory if and only if the argument had this form. */
16142 if (!vim_ispathsep(*p))
16143 {
16144 if (is_relative_to_current
16145 && *p != NUL
16146 && !(p[0] == '.'
16147 && (p[1] == NUL
16148 || vim_ispathsep(p[1])
16149 || (p[1] == '.'
16150 && (p[2] == NUL
16151 || vim_ispathsep(p[2]))))))
16152 {
16153 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016154 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 if (cpy != NULL)
16156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 vim_free(p);
16158 p = cpy;
16159 }
16160 }
16161 else if (!is_relative_to_current)
16162 {
16163 /* Strip leading "./". */
16164 q = p;
16165 while (q[0] == '.' && vim_ispathsep(q[1]))
16166 q += 2;
16167 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016168 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 }
16170 }
16171
16172 /* Ensure that the result will have no trailing path separator
16173 * if the argument had none. But keep "/" or "//". */
16174 if (!has_trailing_pathsep)
16175 {
16176 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016177 if (after_pathsep(p, q))
16178 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 }
16180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016181 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 }
16183# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185# endif
16186#endif
16187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189
16190#ifdef HAVE_READLINK
16191fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016192 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195}
16196
16197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 */
16200 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016202 typval_T *argvars;
16203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204{
Bram Moolenaar33570922005-01-25 22:26:29 +000016205 list_T *l;
16206 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208 if (argvars[0].v_type != VAR_LIST)
16209 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016210 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016211 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 {
16213 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016214 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016215 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016216 while (li != NULL)
16217 {
16218 ni = li->li_prev;
16219 list_append(l, li);
16220 li = ni;
16221 }
16222 rettv->vval.v_list = l;
16223 rettv->v_type = VAR_LIST;
16224 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016225 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227}
16228
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016229#define SP_NOMOVE 0x01 /* don't move cursor */
16230#define SP_REPEAT 0x02 /* repeat to find outer pair */
16231#define SP_RETCOUNT 0x04 /* return matchcount */
16232#define SP_SETPCMARK 0x08 /* set previous context mark */
16233#define SP_START 0x10 /* accept match at start position */
16234#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16235#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016236
Bram Moolenaar33570922005-01-25 22:26:29 +000016237static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016238
16239/*
16240 * Get flags for a search function.
16241 * Possibly sets "p_ws".
16242 * Returns BACKWARD, FORWARD or zero (for an error).
16243 */
16244 static int
16245get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016246 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 int *flagsp;
16248{
16249 int dir = FORWARD;
16250 char_u *flags;
16251 char_u nbuf[NUMBUFLEN];
16252 int mask;
16253
16254 if (varp->v_type != VAR_UNKNOWN)
16255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 flags = get_tv_string_buf_chk(varp, nbuf);
16257 if (flags == NULL)
16258 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016259 while (*flags != NUL)
16260 {
16261 switch (*flags)
16262 {
16263 case 'b': dir = BACKWARD; break;
16264 case 'w': p_ws = TRUE; break;
16265 case 'W': p_ws = FALSE; break;
16266 default: mask = 0;
16267 if (flagsp != NULL)
16268 switch (*flags)
16269 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016270 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016271 case 'e': mask = SP_END; break;
16272 case 'm': mask = SP_RETCOUNT; break;
16273 case 'n': mask = SP_NOMOVE; break;
16274 case 'p': mask = SP_SUBPAT; break;
16275 case 'r': mask = SP_REPEAT; break;
16276 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277 }
16278 if (mask == 0)
16279 {
16280 EMSG2(_(e_invarg2), flags);
16281 dir = 0;
16282 }
16283 else
16284 *flagsp |= mask;
16285 }
16286 if (dir == 0)
16287 break;
16288 ++flags;
16289 }
16290 }
16291 return dir;
16292}
16293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016295 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016297 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016298search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016300 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016301 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016303 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 char_u *pat;
16305 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016306 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 int save_p_ws = p_ws;
16308 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016309 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016310 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016311 proftime_T tm;
16312#ifdef FEAT_RELTIME
16313 long time_limit = 0;
16314#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016315 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016316 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016319 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016320 if (dir == 0)
16321 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016322 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016323 if (flags & SP_START)
16324 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016325 if (flags & SP_END)
16326 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016327
Bram Moolenaar76929292008-01-06 19:07:36 +000016328 /* Optional arguments: line number to stop searching and timeout. */
16329 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016330 {
16331 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16332 if (lnum_stop < 0)
16333 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016334#ifdef FEAT_RELTIME
16335 if (argvars[3].v_type != VAR_UNKNOWN)
16336 {
16337 time_limit = get_tv_number_chk(&argvars[3], NULL);
16338 if (time_limit < 0)
16339 goto theend;
16340 }
16341#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016342 }
16343
Bram Moolenaar76929292008-01-06 19:07:36 +000016344#ifdef FEAT_RELTIME
16345 /* Set the time limit, if there is one. */
16346 profile_setlimit(time_limit, &tm);
16347#endif
16348
Bram Moolenaar231334e2005-07-25 20:46:57 +000016349 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016350 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016351 * Check to make sure only those flags are set.
16352 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16353 * flags cannot be set. Check for that condition also.
16354 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016355 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016356 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016359 goto theend;
16360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016362 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016363 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016364 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016365 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016367 if (flags & SP_SUBPAT)
16368 retval = subpatnum;
16369 else
16370 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016371 if (flags & SP_SETPCMARK)
16372 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016374 if (match_pos != NULL)
16375 {
16376 /* Store the match cursor position */
16377 match_pos->lnum = pos.lnum;
16378 match_pos->col = pos.col + 1;
16379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 /* "/$" will put the cursor after the end of the line, may need to
16381 * correct that here */
16382 check_cursor();
16383 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016384
16385 /* If 'n' flag is used: restore cursor position. */
16386 if (flags & SP_NOMOVE)
16387 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016388 else
16389 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016390theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016392
16393 return retval;
16394}
16395
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016396#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016397
16398/*
16399 * round() is not in C90, use ceil() or floor() instead.
16400 */
16401 float_T
16402vim_round(f)
16403 float_T f;
16404{
16405 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16406}
16407
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016408/*
16409 * "round({float})" function
16410 */
16411 static void
16412f_round(argvars, rettv)
16413 typval_T *argvars;
16414 typval_T *rettv;
16415{
16416 float_T f;
16417
16418 rettv->v_type = VAR_FLOAT;
16419 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016420 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016421 else
16422 rettv->vval.v_float = 0.0;
16423}
16424#endif
16425
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016426/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016427 * "screenattr()" function
16428 */
16429 static void
16430f_screenattr(argvars, rettv)
16431 typval_T *argvars UNUSED;
16432 typval_T *rettv;
16433{
16434 int row;
16435 int col;
16436 int c;
16437
16438 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16439 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16440 if (row < 0 || row >= screen_Rows
16441 || col < 0 || col >= screen_Columns)
16442 c = -1;
16443 else
16444 c = ScreenAttrs[LineOffset[row] + col];
16445 rettv->vval.v_number = c;
16446}
16447
16448/*
16449 * "screenchar()" function
16450 */
16451 static void
16452f_screenchar(argvars, rettv)
16453 typval_T *argvars UNUSED;
16454 typval_T *rettv;
16455{
16456 int row;
16457 int col;
16458 int off;
16459 int c;
16460
16461 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16462 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16463 if (row < 0 || row >= screen_Rows
16464 || col < 0 || col >= screen_Columns)
16465 c = -1;
16466 else
16467 {
16468 off = LineOffset[row] + col;
16469#ifdef FEAT_MBYTE
16470 if (enc_utf8 && ScreenLinesUC[off] != 0)
16471 c = ScreenLinesUC[off];
16472 else
16473#endif
16474 c = ScreenLines[off];
16475 }
16476 rettv->vval.v_number = c;
16477}
16478
16479/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016480 * "screencol()" function
16481 *
16482 * First column is 1 to be consistent with virtcol().
16483 */
16484 static void
16485f_screencol(argvars, rettv)
16486 typval_T *argvars UNUSED;
16487 typval_T *rettv;
16488{
16489 rettv->vval.v_number = screen_screencol() + 1;
16490}
16491
16492/*
16493 * "screenrow()" function
16494 */
16495 static void
16496f_screenrow(argvars, rettv)
16497 typval_T *argvars UNUSED;
16498 typval_T *rettv;
16499{
16500 rettv->vval.v_number = screen_screenrow() + 1;
16501}
16502
16503/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016504 * "search()" function
16505 */
16506 static void
16507f_search(argvars, rettv)
16508 typval_T *argvars;
16509 typval_T *rettv;
16510{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016511 int flags = 0;
16512
16513 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514}
16515
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016517 * "searchdecl()" function
16518 */
16519 static void
16520f_searchdecl(argvars, rettv)
16521 typval_T *argvars;
16522 typval_T *rettv;
16523{
16524 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016525 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016526 int error = FALSE;
16527 char_u *name;
16528
16529 rettv->vval.v_number = 1; /* default: FAIL */
16530
16531 name = get_tv_string_chk(&argvars[0]);
16532 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016533 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016534 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016535 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16536 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16537 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016538 if (!error && name != NULL)
16539 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016540 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016541}
16542
16543/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016544 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016546 static int
16547searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016548 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016549 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550{
16551 char_u *spat, *mpat, *epat;
16552 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 int dir;
16555 int flags = 0;
16556 char_u nbuf1[NUMBUFLEN];
16557 char_u nbuf2[NUMBUFLEN];
16558 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016559 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016560 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016561 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016564 spat = get_tv_string_chk(&argvars[0]);
16565 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16566 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16567 if (spat == NULL || mpat == NULL || epat == NULL)
16568 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 /* Handle the optional fourth argument: flags */
16571 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016572 if (dir == 0)
16573 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016574
16575 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016576 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16577 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016578 if ((flags & (SP_END | SP_SUBPAT)) != 0
16579 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016580 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016581 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016582 goto theend;
16583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016585 /* Using 'r' implies 'W', otherwise it doesn't work. */
16586 if (flags & SP_REPEAT)
16587 p_ws = FALSE;
16588
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016589 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016590 if (argvars[3].v_type == VAR_UNKNOWN
16591 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 skip = (char_u *)"";
16593 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016595 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016596 if (argvars[5].v_type != VAR_UNKNOWN)
16597 {
16598 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16599 if (lnum_stop < 0)
16600 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016601#ifdef FEAT_RELTIME
16602 if (argvars[6].v_type != VAR_UNKNOWN)
16603 {
16604 time_limit = get_tv_number_chk(&argvars[6], NULL);
16605 if (time_limit < 0)
16606 goto theend;
16607 }
16608#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016609 }
16610 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016611 if (skip == NULL)
16612 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016614 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016615 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016616
16617theend:
16618 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016619
16620 return retval;
16621}
16622
16623/*
16624 * "searchpair()" function
16625 */
16626 static void
16627f_searchpair(argvars, rettv)
16628 typval_T *argvars;
16629 typval_T *rettv;
16630{
16631 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16632}
16633
16634/*
16635 * "searchpairpos()" function
16636 */
16637 static void
16638f_searchpairpos(argvars, rettv)
16639 typval_T *argvars;
16640 typval_T *rettv;
16641{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016642 pos_T match_pos;
16643 int lnum = 0;
16644 int col = 0;
16645
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016646 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016647 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016648
16649 if (searchpair_cmn(argvars, &match_pos) > 0)
16650 {
16651 lnum = match_pos.lnum;
16652 col = match_pos.col;
16653 }
16654
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016655 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16656 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016657}
16658
16659/*
16660 * Search for a start/middle/end thing.
16661 * Used by searchpair(), see its documentation for the details.
16662 * Returns 0 or -1 for no match,
16663 */
16664 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016665do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16666 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016667 char_u *spat; /* start pattern */
16668 char_u *mpat; /* middle pattern */
16669 char_u *epat; /* end pattern */
16670 int dir; /* BACKWARD or FORWARD */
16671 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016672 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016673 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016674 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016675 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016676{
16677 char_u *save_cpo;
16678 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16679 long retval = 0;
16680 pos_T pos;
16681 pos_T firstpos;
16682 pos_T foundpos;
16683 pos_T save_cursor;
16684 pos_T save_pos;
16685 int n;
16686 int r;
16687 int nest = 1;
16688 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016689 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016690 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016691
16692 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16693 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016694 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016695
Bram Moolenaar76929292008-01-06 19:07:36 +000016696#ifdef FEAT_RELTIME
16697 /* Set the time limit, if there is one. */
16698 profile_setlimit(time_limit, &tm);
16699#endif
16700
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016701 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16702 * start/middle/end (pat3, for the top pair). */
16703 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16704 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16705 if (pat2 == NULL || pat3 == NULL)
16706 goto theend;
16707 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16708 if (*mpat == NUL)
16709 STRCPY(pat3, pat2);
16710 else
16711 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16712 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016713 if (flags & SP_START)
16714 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016715
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 save_cursor = curwin->w_cursor;
16717 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016718 clearpos(&firstpos);
16719 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 pat = pat3;
16721 for (;;)
16722 {
16723 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016724 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16726 /* didn't find it or found the first match again: FAIL */
16727 break;
16728
16729 if (firstpos.lnum == 0)
16730 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016731 if (equalpos(pos, foundpos))
16732 {
16733 /* Found the same position again. Can happen with a pattern that
16734 * has "\zs" at the end and searching backwards. Advance one
16735 * character and try again. */
16736 if (dir == BACKWARD)
16737 decl(&pos);
16738 else
16739 incl(&pos);
16740 }
16741 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016743 /* clear the start flag to avoid getting stuck here */
16744 options &= ~SEARCH_START;
16745
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 /* If the skip pattern matches, ignore this match. */
16747 if (*skip != NUL)
16748 {
16749 save_pos = curwin->w_cursor;
16750 curwin->w_cursor = pos;
16751 r = eval_to_bool(skip, &err, NULL, FALSE);
16752 curwin->w_cursor = save_pos;
16753 if (err)
16754 {
16755 /* Evaluating {skip} caused an error, break here. */
16756 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016757 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 break;
16759 }
16760 if (r)
16761 continue;
16762 }
16763
16764 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16765 {
16766 /* Found end when searching backwards or start when searching
16767 * forward: nested pair. */
16768 ++nest;
16769 pat = pat2; /* nested, don't search for middle */
16770 }
16771 else
16772 {
16773 /* Found end when searching forward or start when searching
16774 * backward: end of (nested) pair; or found middle in outer pair. */
16775 if (--nest == 1)
16776 pat = pat3; /* outer level, search for middle */
16777 }
16778
16779 if (nest == 0)
16780 {
16781 /* Found the match: return matchcount or line number. */
16782 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016783 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016785 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016786 if (flags & SP_SETPCMARK)
16787 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 curwin->w_cursor = pos;
16789 if (!(flags & SP_REPEAT))
16790 break;
16791 nest = 1; /* search for next unmatched */
16792 }
16793 }
16794
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016795 if (match_pos != NULL)
16796 {
16797 /* Store the match cursor position */
16798 match_pos->lnum = curwin->w_cursor.lnum;
16799 match_pos->col = curwin->w_cursor.col + 1;
16800 }
16801
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016803 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 curwin->w_cursor = save_cursor;
16805
16806theend:
16807 vim_free(pat2);
16808 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016809 if (p_cpo == empty_option)
16810 p_cpo = save_cpo;
16811 else
16812 /* Darn, evaluating the {skip} expression changed the value. */
16813 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016814
16815 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816}
16817
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016818/*
16819 * "searchpos()" function
16820 */
16821 static void
16822f_searchpos(argvars, rettv)
16823 typval_T *argvars;
16824 typval_T *rettv;
16825{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016826 pos_T match_pos;
16827 int lnum = 0;
16828 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016829 int n;
16830 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016831
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016832 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016833 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016834
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016835 n = search_cmn(argvars, &match_pos, &flags);
16836 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016837 {
16838 lnum = match_pos.lnum;
16839 col = match_pos.col;
16840 }
16841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016842 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16843 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016844 if (flags & SP_SUBPAT)
16845 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016846}
16847
16848
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849 static void
16850f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016854#ifdef FEAT_CLIENTSERVER
16855 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016856 char_u *server = get_tv_string_chk(&argvars[0]);
16857 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016860 if (server == NULL || reply == NULL)
16861 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 if (check_restricted() || check_secure())
16863 return;
16864# ifdef FEAT_X11
16865 if (check_connection() == FAIL)
16866 return;
16867# endif
16868
16869 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871 EMSG(_("E258: Unable to send to client"));
16872 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 rettv->vval.v_number = 0;
16875#else
16876 rettv->vval.v_number = -1;
16877#endif
16878}
16879
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880 static void
16881f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016882 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016884{
16885 char_u *r = NULL;
16886
16887#ifdef FEAT_CLIENTSERVER
16888# ifdef WIN32
16889 r = serverGetVimNames();
16890# else
16891 make_connection();
16892 if (X_DISPLAY != NULL)
16893 r = serverGetVimNames(X_DISPLAY);
16894# endif
16895#endif
16896 rettv->v_type = VAR_STRING;
16897 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898}
16899
16900/*
16901 * "setbufvar()" function
16902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016904f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016905 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016906 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907{
16908 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 char_u nbuf[NUMBUFLEN];
16913
16914 if (check_restricted() || check_secure())
16915 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016916 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16917 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016918 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 varp = &argvars[2];
16920
16921 if (buf != NULL && varname != NULL && varp != NULL)
16922 {
16923 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
16926 if (*varname == '&')
16927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016928 long numval;
16929 char_u *strval;
16930 int error = FALSE;
16931
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 numval = get_tv_number_chk(varp, &error);
16934 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935 if (!error && strval != NULL)
16936 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 }
16938 else
16939 {
16940 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16941 if (bufvarname != NULL)
16942 {
16943 STRCPY(bufvarname, "b:");
16944 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016945 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 vim_free(bufvarname);
16947 }
16948 }
16949
16950 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953}
16954
16955/*
16956 * "setcmdpos()" function
16957 */
16958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016959f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 typval_T *argvars;
16961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016963 int pos = (int)get_tv_number(&argvars[0]) - 1;
16964
16965 if (pos >= 0)
16966 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967}
16968
16969/*
16970 * "setline()" function
16971 */
16972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016974 typval_T *argvars;
16975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976{
16977 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016978 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016979 list_T *l = NULL;
16980 listitem_T *li = NULL;
16981 long added = 0;
16982 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016984 lnum = get_tv_lnum(&argvars[0]);
16985 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016987 l = argvars[1].vval.v_list;
16988 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016990 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016991 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016992
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016993 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016994 for (;;)
16995 {
16996 if (l != NULL)
16997 {
16998 /* list argument, get next string */
16999 if (li == NULL)
17000 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017002 li = li->li_next;
17003 }
17004
17005 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017007 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017008
17009 /* When coming here from Insert mode, sync undo, so that this can be
17010 * undone separately from what was previously inserted. */
17011 if (u_sync_once == 2)
17012 {
17013 u_sync_once = 1; /* notify that u_sync() was called */
17014 u_sync(TRUE);
17015 }
17016
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017017 if (lnum <= curbuf->b_ml.ml_line_count)
17018 {
17019 /* existing line, replace it */
17020 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17021 {
17022 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017023 if (lnum == curwin->w_cursor.lnum)
17024 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017025 rettv->vval.v_number = 0; /* OK */
17026 }
17027 }
17028 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17029 {
17030 /* lnum is one past the last line, append the line */
17031 ++added;
17032 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17033 rettv->vval.v_number = 0; /* OK */
17034 }
17035
17036 if (l == NULL) /* only one string argument */
17037 break;
17038 ++lnum;
17039 }
17040
17041 if (added > 0)
17042 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043}
17044
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017045static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17046
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017048 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017049 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017050 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017051set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017052 win_T *wp UNUSED;
17053 typval_T *list_arg UNUSED;
17054 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017055 typval_T *rettv;
17056{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017057#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017058 char_u *act;
17059 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017060#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017061
Bram Moolenaar2641f772005-03-25 21:58:17 +000017062 rettv->vval.v_number = -1;
17063
17064#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017065 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017066 EMSG(_(e_listreq));
17067 else
17068 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017069 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017070
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017071 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017072 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017073 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017074 if (act == NULL)
17075 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017076 if (*act == 'a' || *act == 'r')
17077 action = *act;
17078 }
17079
Bram Moolenaar81484f42012-12-05 15:16:47 +010017080 if (l != NULL && set_errorlist(wp, l, action,
17081 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017082 rettv->vval.v_number = 0;
17083 }
17084#endif
17085}
17086
17087/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017088 * "setloclist()" function
17089 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017090 static void
17091f_setloclist(argvars, rettv)
17092 typval_T *argvars;
17093 typval_T *rettv;
17094{
17095 win_T *win;
17096
17097 rettv->vval.v_number = -1;
17098
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017099 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017100 if (win != NULL)
17101 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17102}
17103
17104/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017105 * "setmatches()" function
17106 */
17107 static void
17108f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017109 typval_T *argvars UNUSED;
17110 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017111{
17112#ifdef FEAT_SEARCH_EXTRA
17113 list_T *l;
17114 listitem_T *li;
17115 dict_T *d;
17116
17117 rettv->vval.v_number = -1;
17118 if (argvars[0].v_type != VAR_LIST)
17119 {
17120 EMSG(_(e_listreq));
17121 return;
17122 }
17123 if ((l = argvars[0].vval.v_list) != NULL)
17124 {
17125
17126 /* To some extent make sure that we are dealing with a list from
17127 * "getmatches()". */
17128 li = l->lv_first;
17129 while (li != NULL)
17130 {
17131 if (li->li_tv.v_type != VAR_DICT
17132 || (d = li->li_tv.vval.v_dict) == NULL)
17133 {
17134 EMSG(_(e_invarg));
17135 return;
17136 }
17137 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17138 && dict_find(d, (char_u *)"pattern", -1) != NULL
17139 && dict_find(d, (char_u *)"priority", -1) != NULL
17140 && dict_find(d, (char_u *)"id", -1) != NULL))
17141 {
17142 EMSG(_(e_invarg));
17143 return;
17144 }
17145 li = li->li_next;
17146 }
17147
17148 clear_matches(curwin);
17149 li = l->lv_first;
17150 while (li != NULL)
17151 {
17152 d = li->li_tv.vval.v_dict;
17153 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17154 get_dict_string(d, (char_u *)"pattern", FALSE),
17155 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017156 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017157 li = li->li_next;
17158 }
17159 rettv->vval.v_number = 0;
17160 }
17161#endif
17162}
17163
17164/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017165 * "setpos()" function
17166 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017167 static void
17168f_setpos(argvars, rettv)
17169 typval_T *argvars;
17170 typval_T *rettv;
17171{
17172 pos_T pos;
17173 int fnum;
17174 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017175 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017176
Bram Moolenaar08250432008-02-13 11:42:46 +000017177 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017178 name = get_tv_string_chk(argvars);
17179 if (name != NULL)
17180 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017181 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017182 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017183 if (--pos.col < 0)
17184 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017185 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017186 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017187 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017188 if (fnum == curbuf->b_fnum)
17189 {
17190 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017191 if (curswant >= 0)
17192 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017193 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017194 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017195 }
17196 else
17197 EMSG(_(e_invarg));
17198 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017199 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17200 {
17201 /* set mark */
17202 if (setmark_pos(name[1], &pos, fnum) == OK)
17203 rettv->vval.v_number = 0;
17204 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017205 else
17206 EMSG(_(e_invarg));
17207 }
17208 }
17209}
17210
17211/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017212 * "setqflist()" function
17213 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017214 static void
17215f_setqflist(argvars, rettv)
17216 typval_T *argvars;
17217 typval_T *rettv;
17218{
17219 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17220}
17221
17222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 * "setreg()" function
17224 */
17225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 typval_T *argvars;
17228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229{
17230 int regname;
17231 char_u *strregname;
17232 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 int append;
17235 char_u yank_type;
17236 long block_len;
17237
17238 block_len = -1;
17239 yank_type = MAUTO;
17240 append = FALSE;
17241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017242 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 if (strregname == NULL)
17246 return; /* type error; errmsg already given */
17247 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 if (regname == 0 || regname == '@')
17249 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253 stropt = get_tv_string_chk(&argvars[2]);
17254 if (stropt == NULL)
17255 return; /* type error */
17256 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 switch (*stropt)
17258 {
17259 case 'a': case 'A': /* append */
17260 append = TRUE;
17261 break;
17262 case 'v': case 'c': /* character-wise selection */
17263 yank_type = MCHAR;
17264 break;
17265 case 'V': case 'l': /* line-wise selection */
17266 yank_type = MLINE;
17267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 case 'b': case Ctrl_V: /* block-wise selection */
17269 yank_type = MBLOCK;
17270 if (VIM_ISDIGIT(stropt[1]))
17271 {
17272 ++stropt;
17273 block_len = getdigits(&stropt) - 1;
17274 --stropt;
17275 }
17276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 }
17278 }
17279
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017280 if (argvars[1].v_type == VAR_LIST)
17281 {
17282 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017283 char_u **allocval;
17284 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017285 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017286 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017287 int len = argvars[1].vval.v_list->lv_len;
17288 listitem_T *li;
17289
Bram Moolenaar7d647822014-04-05 21:28:56 +020017290 /* First half: use for pointers to result lines; second half: use for
17291 * pointers to allocated copies. */
17292 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017293 if (lstval == NULL)
17294 return;
17295 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017296 allocval = lstval + len + 2;
17297 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017298
17299 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17300 li = li->li_next)
17301 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017302 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017303 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017304 goto free_lstval;
17305 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017306 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017307 /* Need to make a copy, next get_tv_string_buf_chk() will
17308 * overwrite the string. */
17309 strval = vim_strsave(buf);
17310 if (strval == NULL)
17311 goto free_lstval;
17312 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017313 }
17314 *curval++ = strval;
17315 }
17316 *curval++ = NULL;
17317
17318 write_reg_contents_lst(regname, lstval, -1,
17319 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017320free_lstval:
17321 while (curallocval > allocval)
17322 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017323 vim_free(lstval);
17324 }
17325 else
17326 {
17327 strval = get_tv_string_chk(&argvars[1]);
17328 if (strval == NULL)
17329 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017330 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017336/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017337 * "settabvar()" function
17338 */
17339 static void
17340f_settabvar(argvars, rettv)
17341 typval_T *argvars;
17342 typval_T *rettv;
17343{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017344#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017345 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017346 tabpage_T *tp;
17347#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017348 char_u *varname, *tabvarname;
17349 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017350
17351 rettv->vval.v_number = 0;
17352
17353 if (check_restricted() || check_secure())
17354 return;
17355
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017356#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017357 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017358#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017359 varname = get_tv_string_chk(&argvars[1]);
17360 varp = &argvars[2];
17361
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017362 if (varname != NULL && varp != NULL
17363#ifdef FEAT_WINDOWS
17364 && tp != NULL
17365#endif
17366 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017367 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017368#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017369 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017370 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017371#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017372
17373 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17374 if (tabvarname != NULL)
17375 {
17376 STRCPY(tabvarname, "t:");
17377 STRCPY(tabvarname + 2, varname);
17378 set_var(tabvarname, varp, TRUE);
17379 vim_free(tabvarname);
17380 }
17381
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017382#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017383 /* Restore current tabpage */
17384 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017385 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017386#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017387 }
17388}
17389
17390/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017391 * "settabwinvar()" function
17392 */
17393 static void
17394f_settabwinvar(argvars, rettv)
17395 typval_T *argvars;
17396 typval_T *rettv;
17397{
17398 setwinvar(argvars, rettv, 1);
17399}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400
17401/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017402 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017405f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017406 typval_T *argvars;
17407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017409 setwinvar(argvars, rettv, 0);
17410}
17411
17412/*
17413 * "setwinvar()" and "settabwinvar()" functions
17414 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017415
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017416 static void
17417setwinvar(argvars, rettv, off)
17418 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017419 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017420 int off;
17421{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 win_T *win;
17423#ifdef FEAT_WINDOWS
17424 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017425 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426#endif
17427 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017428 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017430 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431
17432 if (check_restricted() || check_secure())
17433 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017434
17435#ifdef FEAT_WINDOWS
17436 if (off == 1)
17437 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17438 else
17439 tp = curtab;
17440#endif
17441 win = find_win_by_nr(&argvars[off], tp);
17442 varname = get_tv_string_chk(&argvars[off + 1]);
17443 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444
17445 if (win != NULL && varname != NULL && varp != NULL)
17446 {
17447#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017448 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017451 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017453 long numval;
17454 char_u *strval;
17455 int error = FALSE;
17456
17457 ++varname;
17458 numval = get_tv_number_chk(varp, &error);
17459 strval = get_tv_string_buf_chk(varp, nbuf);
17460 if (!error && strval != NULL)
17461 set_option_value(varname, numval, strval, OPT_LOCAL);
17462 }
17463 else
17464 {
17465 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17466 if (winvarname != NULL)
17467 {
17468 STRCPY(winvarname, "w:");
17469 STRCPY(winvarname + 2, varname);
17470 set_var(winvarname, varp, TRUE);
17471 vim_free(winvarname);
17472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 }
17474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017476 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477#endif
17478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479}
17480
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017481#ifdef FEAT_CRYPT
17482/*
17483 * "sha256({string})" function
17484 */
17485 static void
17486f_sha256(argvars, rettv)
17487 typval_T *argvars;
17488 typval_T *rettv;
17489{
17490 char_u *p;
17491
17492 p = get_tv_string(&argvars[0]);
17493 rettv->vval.v_string = vim_strsave(
17494 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17495 rettv->v_type = VAR_STRING;
17496}
17497#endif /* FEAT_CRYPT */
17498
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017500 * "shellescape({string})" function
17501 */
17502 static void
17503f_shellescape(argvars, rettv)
17504 typval_T *argvars;
17505 typval_T *rettv;
17506{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017507 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017508 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017509 rettv->v_type = VAR_STRING;
17510}
17511
17512/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017513 * shiftwidth() function
17514 */
17515 static void
17516f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017517 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017518 typval_T *rettv;
17519{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017520 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017521}
17522
17523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 */
17526 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017527f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 typval_T *argvars;
17529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017531 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533 p = get_tv_string(&argvars[0]);
17534 rettv->vval.v_string = vim_strsave(p);
17535 simplify_filename(rettv->vval.v_string); /* simplify in place */
17536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537}
17538
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017539#ifdef FEAT_FLOAT
17540/*
17541 * "sin()" function
17542 */
17543 static void
17544f_sin(argvars, rettv)
17545 typval_T *argvars;
17546 typval_T *rettv;
17547{
17548 float_T f;
17549
17550 rettv->v_type = VAR_FLOAT;
17551 if (get_float_arg(argvars, &f) == OK)
17552 rettv->vval.v_float = sin(f);
17553 else
17554 rettv->vval.v_float = 0.0;
17555}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017556
17557/*
17558 * "sinh()" function
17559 */
17560 static void
17561f_sinh(argvars, rettv)
17562 typval_T *argvars;
17563 typval_T *rettv;
17564{
17565 float_T f;
17566
17567 rettv->v_type = VAR_FLOAT;
17568 if (get_float_arg(argvars, &f) == OK)
17569 rettv->vval.v_float = sinh(f);
17570 else
17571 rettv->vval.v_float = 0.0;
17572}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017573#endif
17574
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575static int
17576#ifdef __BORLANDC__
17577 _RTLENTRYF
17578#endif
17579 item_compare __ARGS((const void *s1, const void *s2));
17580static int
17581#ifdef __BORLANDC__
17582 _RTLENTRYF
17583#endif
17584 item_compare2 __ARGS((const void *s1, const void *s2));
17585
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017586/* struct used in the array that's given to qsort() */
17587typedef struct
17588{
17589 listitem_T *item;
17590 int idx;
17591} sortItem_T;
17592
Bram Moolenaar0d660222005-01-07 21:51:51 +000017593static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017594static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017596static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017597static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017598static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017599static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600#define ITEM_COMPARE_FAIL 999
17601
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017603 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017605 static int
17606#ifdef __BORLANDC__
17607_RTLENTRYF
17608#endif
17609item_compare(s1, s2)
17610 const void *s1;
17611 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017613 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017614 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017616 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 int res;
17618 char_u numbuf1[NUMBUFLEN];
17619 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017621 si1 = (sortItem_T *)s1;
17622 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017623 tv1 = &si1->item->li_tv;
17624 tv2 = &si2->item->li_tv;
17625 /* tv2string() puts quotes around a string and allocates memory. Don't do
17626 * that for string variables. Use a single quote when comparing with a
17627 * non-string to do what the docs promise. */
17628 if (tv1->v_type == VAR_STRING)
17629 {
17630 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17631 p1 = (char_u *)"'";
17632 else
17633 p1 = tv1->vval.v_string;
17634 }
17635 else
17636 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17637 if (tv2->v_type == VAR_STRING)
17638 {
17639 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17640 p2 = (char_u *)"'";
17641 else
17642 p2 = tv2->vval.v_string;
17643 }
17644 else
17645 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017646 if (p1 == NULL)
17647 p1 = (char_u *)"";
17648 if (p2 == NULL)
17649 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017650 if (!item_compare_numeric)
17651 {
17652 if (item_compare_ic)
17653 res = STRICMP(p1, p2);
17654 else
17655 res = STRCMP(p1, p2);
17656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017658 {
17659 double n1, n2;
17660 n1 = strtod((char *)p1, (char **)&p1);
17661 n2 = strtod((char *)p2, (char **)&p2);
17662 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17663 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017664
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017665 /* When the result would be zero, compare the item indexes. Makes the
17666 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017667 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017668 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017669
Bram Moolenaar0d660222005-01-07 21:51:51 +000017670 vim_free(tofree1);
17671 vim_free(tofree2);
17672 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673}
17674
17675 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017676#ifdef __BORLANDC__
17677_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017679item_compare2(s1, s2)
17680 const void *s1;
17681 const void *s2;
17682{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017683 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017684 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017685 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017686 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017689 /* shortcut after failure in previous call; compare all items equal */
17690 if (item_compare_func_err)
17691 return 0;
17692
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017693 si1 = (sortItem_T *)s1;
17694 si2 = (sortItem_T *)s2;
17695
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017696 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017697 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017698 copy_tv(&si1->item->li_tv, &argv[0]);
17699 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017700
17701 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017702 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017703 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17704 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017705 clear_tv(&argv[0]);
17706 clear_tv(&argv[1]);
17707
17708 if (res == FAIL)
17709 res = ITEM_COMPARE_FAIL;
17710 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017711 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17712 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017713 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017714 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017715
17716 /* When the result would be zero, compare the pointers themselves. Makes
17717 * the sort stable. */
17718 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017719 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017720
Bram Moolenaar0d660222005-01-07 21:51:51 +000017721 return res;
17722}
17723
17724/*
17725 * "sort({list})" function
17726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017728do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 typval_T *argvars;
17730 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017731 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732{
Bram Moolenaar33570922005-01-25 22:26:29 +000017733 list_T *l;
17734 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017735 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017736 long len;
17737 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017740 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 else
17742 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017743 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017744 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017745 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017746 return;
17747 rettv->vval.v_list = l;
17748 rettv->v_type = VAR_LIST;
17749 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750
Bram Moolenaar0d660222005-01-07 21:51:51 +000017751 len = list_len(l);
17752 if (len <= 1)
17753 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754
Bram Moolenaar0d660222005-01-07 21:51:51 +000017755 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017756 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017758 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017759 if (argvars[1].v_type != VAR_UNKNOWN)
17760 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017761 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017762 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017763 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017764 else
17765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 int error = FALSE;
17767
17768 i = get_tv_number_chk(&argvars[1], &error);
17769 if (error)
17770 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771 if (i == 1)
17772 item_compare_ic = TRUE;
17773 else
17774 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017775 if (item_compare_func != NULL)
17776 {
17777 if (STRCMP(item_compare_func, "n") == 0)
17778 {
17779 item_compare_func = NULL;
17780 item_compare_numeric = TRUE;
17781 }
17782 else if (STRCMP(item_compare_func, "i") == 0)
17783 {
17784 item_compare_func = NULL;
17785 item_compare_ic = TRUE;
17786 }
17787 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017789
17790 if (argvars[2].v_type != VAR_UNKNOWN)
17791 {
17792 /* optional third argument: {dict} */
17793 if (argvars[2].v_type != VAR_DICT)
17794 {
17795 EMSG(_(e_dictreq));
17796 return;
17797 }
17798 item_compare_selfdict = argvars[2].vval.v_dict;
17799 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801
Bram Moolenaar0d660222005-01-07 21:51:51 +000017802 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017803 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017804 if (ptrs == NULL)
17805 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806
Bram Moolenaar327aa022014-03-25 18:24:23 +010017807 i = 0;
17808 if (sort)
17809 {
17810 /* sort(): ptrs will be the list to sort */
17811 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017812 {
17813 ptrs[i].item = li;
17814 ptrs[i].idx = i;
17815 ++i;
17816 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017817
17818 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017819 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017820 /* test the compare function */
17821 if (item_compare_func != NULL
17822 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017823 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017824 EMSG(_("E702: Sort compare function failed"));
17825 else
17826 {
17827 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017828 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017829 item_compare_func == NULL ? item_compare : item_compare2);
17830
17831 if (!item_compare_func_err)
17832 {
17833 /* Clear the List and append the items in sorted order. */
17834 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17835 l->lv_len = 0;
17836 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017837 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017838 }
17839 }
17840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017842 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017843 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17844
17845 /* f_uniq(): ptrs will be a stack of items to remove */
17846 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017847 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017848 item_compare_func_ptr = item_compare_func
17849 ? item_compare2 : item_compare;
17850
17851 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17852 li = li->li_next)
17853 {
17854 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17855 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017856 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017857 if (item_compare_func_err)
17858 {
17859 EMSG(_("E882: Uniq compare function failed"));
17860 break;
17861 }
17862 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017864 if (!item_compare_func_err)
17865 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017866 while (--i >= 0)
17867 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017868 li = ptrs[i].item->li_next;
17869 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017870 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017871 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017872 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017873 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017874 list_fix_watch(l, li);
17875 listitem_free(li);
17876 l->lv_len--;
17877 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017878 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 }
17880
17881 vim_free(ptrs);
17882 }
17883}
17884
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017885/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017886 * "sort({list})" function
17887 */
17888 static void
17889f_sort(argvars, rettv)
17890 typval_T *argvars;
17891 typval_T *rettv;
17892{
17893 do_sort_uniq(argvars, rettv, TRUE);
17894}
17895
17896/*
17897 * "uniq({list})" function
17898 */
17899 static void
17900f_uniq(argvars, rettv)
17901 typval_T *argvars;
17902 typval_T *rettv;
17903{
17904 do_sort_uniq(argvars, rettv, FALSE);
17905}
17906
17907/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017908 * "soundfold({word})" function
17909 */
17910 static void
17911f_soundfold(argvars, rettv)
17912 typval_T *argvars;
17913 typval_T *rettv;
17914{
17915 char_u *s;
17916
17917 rettv->v_type = VAR_STRING;
17918 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017919#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017920 rettv->vval.v_string = eval_soundfold(s);
17921#else
17922 rettv->vval.v_string = vim_strsave(s);
17923#endif
17924}
17925
17926/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017927 * "spellbadword()" function
17928 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017929 static void
17930f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017931 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017932 typval_T *rettv;
17933{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017934 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017935 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017936 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017938 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017939 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017940
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017941#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017942 if (argvars[0].v_type == VAR_UNKNOWN)
17943 {
17944 /* Find the start and length of the badly spelled word. */
17945 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17946 if (len != 0)
17947 word = ml_get_cursor();
17948 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017949 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017950 {
17951 char_u *str = get_tv_string_chk(&argvars[0]);
17952 int capcol = -1;
17953
17954 if (str != NULL)
17955 {
17956 /* Check the argument for spelling. */
17957 while (*str != NUL)
17958 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017959 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017960 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017961 {
17962 word = str;
17963 break;
17964 }
17965 str += len;
17966 }
17967 }
17968 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017969#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017970
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017971 list_append_string(rettv->vval.v_list, word, len);
17972 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017973 attr == HLF_SPB ? "bad" :
17974 attr == HLF_SPR ? "rare" :
17975 attr == HLF_SPL ? "local" :
17976 attr == HLF_SPC ? "caps" :
17977 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017978}
17979
17980/*
17981 * "spellsuggest()" function
17982 */
17983 static void
17984f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017985 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017986 typval_T *rettv;
17987{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017988#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017989 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017990 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017991 int maxcount;
17992 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017993 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017994 listitem_T *li;
17995 int need_capital = FALSE;
17996#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017997
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017998 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017999 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018000
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018001#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018002 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018003 {
18004 str = get_tv_string(&argvars[0]);
18005 if (argvars[1].v_type != VAR_UNKNOWN)
18006 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018007 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018008 if (maxcount <= 0)
18009 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018010 if (argvars[2].v_type != VAR_UNKNOWN)
18011 {
18012 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18013 if (typeerr)
18014 return;
18015 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018016 }
18017 else
18018 maxcount = 25;
18019
Bram Moolenaar4770d092006-01-12 23:22:24 +000018020 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018021
18022 for (i = 0; i < ga.ga_len; ++i)
18023 {
18024 str = ((char_u **)ga.ga_data)[i];
18025
18026 li = listitem_alloc();
18027 if (li == NULL)
18028 vim_free(str);
18029 else
18030 {
18031 li->li_tv.v_type = VAR_STRING;
18032 li->li_tv.v_lock = 0;
18033 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018034 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018035 }
18036 }
18037 ga_clear(&ga);
18038 }
18039#endif
18040}
18041
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018043f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018044 typval_T *argvars;
18045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046{
18047 char_u *str;
18048 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018049 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018050 regmatch_T regmatch;
18051 char_u patbuf[NUMBUFLEN];
18052 char_u *save_cpo;
18053 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018055 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018056 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018057
18058 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18059 save_cpo = p_cpo;
18060 p_cpo = (char_u *)"";
18061
18062 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018063 if (argvars[1].v_type != VAR_UNKNOWN)
18064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018065 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18066 if (pat == NULL)
18067 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018069 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018070 }
18071 if (pat == NULL || *pat == NUL)
18072 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018074 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018076 if (typeerr)
18077 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18080 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018082 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018083 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018085 if (*str == NUL)
18086 match = FALSE; /* empty item at the end */
18087 else
18088 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018089 if (match)
18090 end = regmatch.startp[0];
18091 else
18092 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018093 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18094 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018095 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018096 if (list_append_string(rettv->vval.v_list, str,
18097 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 }
18100 if (!match)
18101 break;
18102 /* Advance to just after the match. */
18103 if (regmatch.endp[0] > str)
18104 col = 0;
18105 else
18106 {
18107 /* Don't get stuck at the same match. */
18108#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018109 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018110#else
18111 col = 1;
18112#endif
18113 }
18114 str = regmatch.endp[0];
18115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116
Bram Moolenaar473de612013-06-08 18:19:48 +020018117 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119
Bram Moolenaar0d660222005-01-07 21:51:51 +000018120 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121}
18122
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018123#ifdef FEAT_FLOAT
18124/*
18125 * "sqrt()" function
18126 */
18127 static void
18128f_sqrt(argvars, rettv)
18129 typval_T *argvars;
18130 typval_T *rettv;
18131{
18132 float_T f;
18133
18134 rettv->v_type = VAR_FLOAT;
18135 if (get_float_arg(argvars, &f) == OK)
18136 rettv->vval.v_float = sqrt(f);
18137 else
18138 rettv->vval.v_float = 0.0;
18139}
18140
18141/*
18142 * "str2float()" function
18143 */
18144 static void
18145f_str2float(argvars, rettv)
18146 typval_T *argvars;
18147 typval_T *rettv;
18148{
18149 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18150
18151 if (*p == '+')
18152 p = skipwhite(p + 1);
18153 (void)string2float(p, &rettv->vval.v_float);
18154 rettv->v_type = VAR_FLOAT;
18155}
18156#endif
18157
Bram Moolenaar2c932302006-03-18 21:42:09 +000018158/*
18159 * "str2nr()" function
18160 */
18161 static void
18162f_str2nr(argvars, rettv)
18163 typval_T *argvars;
18164 typval_T *rettv;
18165{
18166 int base = 10;
18167 char_u *p;
18168 long n;
18169
18170 if (argvars[1].v_type != VAR_UNKNOWN)
18171 {
18172 base = get_tv_number(&argvars[1]);
18173 if (base != 8 && base != 10 && base != 16)
18174 {
18175 EMSG(_(e_invarg));
18176 return;
18177 }
18178 }
18179
18180 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018181 if (*p == '+')
18182 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018183 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18184 rettv->vval.v_number = n;
18185}
18186
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187#ifdef HAVE_STRFTIME
18188/*
18189 * "strftime({format}[, {time}])" function
18190 */
18191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018192f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018193 typval_T *argvars;
18194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195{
18196 char_u result_buf[256];
18197 struct tm *curtime;
18198 time_t seconds;
18199 char_u *p;
18200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018201 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018203 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018204 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 seconds = time(NULL);
18206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018207 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 curtime = localtime(&seconds);
18209 /* MSVC returns NULL for an invalid value of seconds. */
18210 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 else
18213 {
18214# ifdef FEAT_MBYTE
18215 vimconv_T conv;
18216 char_u *enc;
18217
18218 conv.vc_type = CONV_NONE;
18219 enc = enc_locale();
18220 convert_setup(&conv, p_enc, enc);
18221 if (conv.vc_type != CONV_NONE)
18222 p = string_convert(&conv, p, NULL);
18223# endif
18224 if (p != NULL)
18225 (void)strftime((char *)result_buf, sizeof(result_buf),
18226 (char *)p, curtime);
18227 else
18228 result_buf[0] = NUL;
18229
18230# ifdef FEAT_MBYTE
18231 if (conv.vc_type != CONV_NONE)
18232 vim_free(p);
18233 convert_setup(&conv, enc, p_enc);
18234 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018235 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 else
18237# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239
18240# ifdef FEAT_MBYTE
18241 /* Release conversion descriptors */
18242 convert_setup(&conv, NULL, NULL);
18243 vim_free(enc);
18244# endif
18245 }
18246}
18247#endif
18248
18249/*
18250 * "stridx()" function
18251 */
18252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018254 typval_T *argvars;
18255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256{
18257 char_u buf[NUMBUFLEN];
18258 char_u *needle;
18259 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018260 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018262 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018264 needle = get_tv_string_chk(&argvars[1]);
18265 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018267 if (needle == NULL || haystack == NULL)
18268 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269
Bram Moolenaar33570922005-01-25 22:26:29 +000018270 if (argvars[2].v_type != VAR_UNKNOWN)
18271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 int error = FALSE;
18273
18274 start_idx = get_tv_number_chk(&argvars[2], &error);
18275 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018276 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018277 if (start_idx >= 0)
18278 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018279 }
18280
18281 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18282 if (pos != NULL)
18283 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284}
18285
18286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018287 * "string()" function
18288 */
18289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018290f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018291 typval_T *argvars;
18292 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018293{
18294 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018295 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018298 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018299 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018300 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302}
18303
18304/*
18305 * "strlen()" function
18306 */
18307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018308f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 typval_T *argvars;
18310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018312 rettv->vval.v_number = (varnumber_T)(STRLEN(
18313 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314}
18315
18316/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018317 * "strchars()" function
18318 */
18319 static void
18320f_strchars(argvars, rettv)
18321 typval_T *argvars;
18322 typval_T *rettv;
18323{
18324 char_u *s = get_tv_string(&argvars[0]);
18325#ifdef FEAT_MBYTE
18326 varnumber_T len = 0;
18327
18328 while (*s != NUL)
18329 {
18330 mb_cptr2char_adv(&s);
18331 ++len;
18332 }
18333 rettv->vval.v_number = len;
18334#else
18335 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18336#endif
18337}
18338
18339/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018340 * "strdisplaywidth()" function
18341 */
18342 static void
18343f_strdisplaywidth(argvars, rettv)
18344 typval_T *argvars;
18345 typval_T *rettv;
18346{
18347 char_u *s = get_tv_string(&argvars[0]);
18348 int col = 0;
18349
18350 if (argvars[1].v_type != VAR_UNKNOWN)
18351 col = get_tv_number(&argvars[1]);
18352
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018353 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018354}
18355
18356/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018357 * "strwidth()" function
18358 */
18359 static void
18360f_strwidth(argvars, rettv)
18361 typval_T *argvars;
18362 typval_T *rettv;
18363{
18364 char_u *s = get_tv_string(&argvars[0]);
18365
18366 rettv->vval.v_number = (varnumber_T)(
18367#ifdef FEAT_MBYTE
18368 mb_string2cells(s, -1)
18369#else
18370 STRLEN(s)
18371#endif
18372 );
18373}
18374
18375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 * "strpart()" function
18377 */
18378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018379f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 typval_T *argvars;
18381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382{
18383 char_u *p;
18384 int n;
18385 int len;
18386 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018387 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018389 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 slen = (int)STRLEN(p);
18391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018392 n = get_tv_number_chk(&argvars[1], &error);
18393 if (error)
18394 len = 0;
18395 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018396 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 else
18398 len = slen - n; /* default len: all bytes that are available. */
18399
18400 /*
18401 * Only return the overlap between the specified part and the actual
18402 * string.
18403 */
18404 if (n < 0)
18405 {
18406 len += n;
18407 n = 0;
18408 }
18409 else if (n > slen)
18410 n = slen;
18411 if (len < 0)
18412 len = 0;
18413 else if (n + len > slen)
18414 len = slen - n;
18415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 rettv->v_type = VAR_STRING;
18417 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418}
18419
18420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421 * "strridx()" function
18422 */
18423 static void
18424f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018425 typval_T *argvars;
18426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427{
18428 char_u buf[NUMBUFLEN];
18429 char_u *needle;
18430 char_u *haystack;
18431 char_u *rest;
18432 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018433 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018435 needle = get_tv_string_chk(&argvars[1]);
18436 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018437
18438 rettv->vval.v_number = -1;
18439 if (needle == NULL || haystack == NULL)
18440 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018441
18442 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018443 if (argvars[2].v_type != VAR_UNKNOWN)
18444 {
18445 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018446 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018447 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018448 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018449 }
18450 else
18451 end_idx = haystack_len;
18452
Bram Moolenaar0d660222005-01-07 21:51:51 +000018453 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018454 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018455 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018456 lastmatch = haystack + end_idx;
18457 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018459 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460 for (rest = haystack; *rest != '\0'; ++rest)
18461 {
18462 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018463 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 break;
18465 lastmatch = rest;
18466 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468
18469 if (lastmatch == NULL)
18470 rettv->vval.v_number = -1;
18471 else
18472 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18473}
18474
18475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 * "strtrans()" function
18477 */
18478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T *argvars;
18481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 rettv->v_type = VAR_STRING;
18484 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485}
18486
18487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018488 * "submatch()" function
18489 */
18490 static void
18491f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *argvars;
18493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018494{
Bram Moolenaar41571762014-04-02 19:00:58 +020018495 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018496 int no;
18497 int retList = 0;
18498
18499 no = (int)get_tv_number_chk(&argvars[0], &error);
18500 if (error)
18501 return;
18502 error = FALSE;
18503 if (argvars[1].v_type != VAR_UNKNOWN)
18504 retList = get_tv_number_chk(&argvars[1], &error);
18505 if (error)
18506 return;
18507
18508 if (retList == 0)
18509 {
18510 rettv->v_type = VAR_STRING;
18511 rettv->vval.v_string = reg_submatch(no);
18512 }
18513 else
18514 {
18515 rettv->v_type = VAR_LIST;
18516 rettv->vval.v_list = reg_submatch_list(no);
18517 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018518}
18519
18520/*
18521 * "substitute()" function
18522 */
18523 static void
18524f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018525 typval_T *argvars;
18526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018527{
18528 char_u patbuf[NUMBUFLEN];
18529 char_u subbuf[NUMBUFLEN];
18530 char_u flagsbuf[NUMBUFLEN];
18531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018532 char_u *str = get_tv_string_chk(&argvars[0]);
18533 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18534 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18535 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18536
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018538 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18539 rettv->vval.v_string = NULL;
18540 else
18541 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018542}
18543
18544/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018545 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551{
18552 int id = 0;
18553#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018554 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 long col;
18556 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018557 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018559 lnum = get_tv_lnum(argvars); /* -1 on type error */
18560 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18561 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018563 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018564 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018565 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566#endif
18567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569}
18570
18571/*
18572 * "synIDattr(id, what [, mode])" function
18573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018576 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578{
18579 char_u *p = NULL;
18580#ifdef FEAT_SYN_HL
18581 int id;
18582 char_u *what;
18583 char_u *mode;
18584 char_u modebuf[NUMBUFLEN];
18585 int modec;
18586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587 id = get_tv_number(&argvars[0]);
18588 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018593 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 modec = 0; /* replace invalid with current */
18595 }
18596 else
18597 {
18598#ifdef FEAT_GUI
18599 if (gui.in_use)
18600 modec = 'g';
18601 else
18602#endif
18603 if (t_colors > 1)
18604 modec = 'c';
18605 else
18606 modec = 't';
18607 }
18608
18609
18610 switch (TOLOWER_ASC(what[0]))
18611 {
18612 case 'b':
18613 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18614 p = highlight_color(id, what, modec);
18615 else /* bold */
18616 p = highlight_has_attr(id, HL_BOLD, modec);
18617 break;
18618
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018619 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 p = highlight_color(id, what, modec);
18621 break;
18622
18623 case 'i':
18624 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18625 p = highlight_has_attr(id, HL_INVERSE, modec);
18626 else /* italic */
18627 p = highlight_has_attr(id, HL_ITALIC, modec);
18628 break;
18629
18630 case 'n': /* name */
18631 p = get_highlight_name(NULL, id - 1);
18632 break;
18633
18634 case 'r': /* reverse */
18635 p = highlight_has_attr(id, HL_INVERSE, modec);
18636 break;
18637
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018638 case 's':
18639 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18640 p = highlight_color(id, what, modec);
18641 else /* standout */
18642 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 break;
18644
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018645 case 'u':
18646 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18647 /* underline */
18648 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18649 else
18650 /* undercurl */
18651 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 break;
18653 }
18654
18655 if (p != NULL)
18656 p = vim_strsave(p);
18657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 rettv->v_type = VAR_STRING;
18659 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660}
18661
18662/*
18663 * "synIDtrans(id)" function
18664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669{
18670 int id;
18671
18672#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018673 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674
18675 if (id > 0)
18676 id = syn_get_final_id(id);
18677 else
18678#endif
18679 id = 0;
18680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682}
18683
18684/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018685 * "synconcealed(lnum, col)" function
18686 */
18687 static void
18688f_synconcealed(argvars, rettv)
18689 typval_T *argvars UNUSED;
18690 typval_T *rettv;
18691{
18692#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18693 long lnum;
18694 long col;
18695 int syntax_flags = 0;
18696 int cchar;
18697 int matchid = 0;
18698 char_u str[NUMBUFLEN];
18699#endif
18700
18701 rettv->v_type = VAR_LIST;
18702 rettv->vval.v_list = NULL;
18703
18704#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18705 lnum = get_tv_lnum(argvars); /* -1 on type error */
18706 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18707
18708 vim_memset(str, NUL, sizeof(str));
18709
18710 if (rettv_list_alloc(rettv) != FAIL)
18711 {
18712 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18713 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18714 && curwin->w_p_cole > 0)
18715 {
18716 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18717 syntax_flags = get_syntax_info(&matchid);
18718
18719 /* get the conceal character */
18720 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18721 {
18722 cchar = syn_get_sub_char();
18723 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18724 cchar = lcs_conceal;
18725 if (cchar != NUL)
18726 {
18727# ifdef FEAT_MBYTE
18728 if (has_mbyte)
18729 (*mb_char2bytes)(cchar, str);
18730 else
18731# endif
18732 str[0] = cchar;
18733 }
18734 }
18735 }
18736
18737 list_append_number(rettv->vval.v_list,
18738 (syntax_flags & HL_CONCEAL) != 0);
18739 /* -1 to auto-determine strlen */
18740 list_append_string(rettv->vval.v_list, str, -1);
18741 list_append_number(rettv->vval.v_list, matchid);
18742 }
18743#endif
18744}
18745
18746/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018747 * "synstack(lnum, col)" function
18748 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018749 static void
18750f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018751 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018752 typval_T *rettv;
18753{
18754#ifdef FEAT_SYN_HL
18755 long lnum;
18756 long col;
18757 int i;
18758 int id;
18759#endif
18760
18761 rettv->v_type = VAR_LIST;
18762 rettv->vval.v_list = NULL;
18763
18764#ifdef FEAT_SYN_HL
18765 lnum = get_tv_lnum(argvars); /* -1 on type error */
18766 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18767
18768 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018769 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018770 && rettv_list_alloc(rettv) != FAIL)
18771 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018772 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018773 for (i = 0; ; ++i)
18774 {
18775 id = syn_get_stack_item(i);
18776 if (id < 0)
18777 break;
18778 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18779 break;
18780 }
18781 }
18782#endif
18783}
18784
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018786get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 typval_T *argvars;
18788 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018789 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018791 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018793 char_u *infile = NULL;
18794 char_u buf[NUMBUFLEN];
18795 int err = FALSE;
18796 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018797 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018798 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018800 rettv->v_type = VAR_STRING;
18801 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018802 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018803 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018804
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018805 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018806 {
18807 /*
18808 * Write the string to a temp file, to be used for input of the shell
18809 * command.
18810 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018811 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018812 {
18813 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018814 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018815 }
18816
18817 fd = mch_fopen((char *)infile, WRITEBIN);
18818 if (fd == NULL)
18819 {
18820 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018821 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018822 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018823 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018824 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018825 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18826 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018827 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018828 else
18829 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018830 size_t len;
18831
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018832 p = get_tv_string_buf_chk(&argvars[1], buf);
18833 if (p == NULL)
18834 {
18835 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018836 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018837 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018838 len = STRLEN(p);
18839 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018840 err = TRUE;
18841 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018842 if (fclose(fd) != 0)
18843 err = TRUE;
18844 if (err)
18845 {
18846 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018847 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018848 }
18849 }
18850
Bram Moolenaar52a72462014-08-29 15:53:52 +020018851 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18852 * echoes typeahead, that messes up the display. */
18853 if (!msg_silent)
18854 flags += SHELL_COOKED;
18855
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018856 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018858 int len;
18859 listitem_T *li;
18860 char_u *s = NULL;
18861 char_u *start;
18862 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018863 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864
Bram Moolenaar52a72462014-08-29 15:53:52 +020018865 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018866 if (res == NULL)
18867 goto errret;
18868
18869 list = list_alloc();
18870 if (list == NULL)
18871 goto errret;
18872
18873 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018875 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018876 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018877 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018878 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018879
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018880 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018881 if (s == NULL)
18882 goto errret;
18883
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018884 for (p = s; start < end; ++p, ++start)
18885 *p = *start == NUL ? NL : *start;
18886 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018887
18888 li = listitem_alloc();
18889 if (li == NULL)
18890 {
18891 vim_free(s);
18892 goto errret;
18893 }
18894 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018895 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018896 li->li_tv.vval.v_string = s;
18897 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018899
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018900 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018901 rettv->v_type = VAR_LIST;
18902 rettv->vval.v_list = list;
18903 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018905 else
18906 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018907 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018908#ifdef USE_CR
18909 /* translate <CR> into <NL> */
18910 if (res != NULL)
18911 {
18912 char_u *s;
18913
18914 for (s = res; *s; ++s)
18915 {
18916 if (*s == CAR)
18917 *s = NL;
18918 }
18919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920#else
18921# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018922 /* translate <CR><NL> into <NL> */
18923 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018925 char_u *s, *d;
18926
18927 d = res;
18928 for (s = res; *s; ++s)
18929 {
18930 if (s[0] == CAR && s[1] == NL)
18931 ++s;
18932 *d++ = *s;
18933 }
18934 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936# endif
18937#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018938 rettv->vval.v_string = res;
18939 res = NULL;
18940 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018941
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018942errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018943 if (infile != NULL)
18944 {
18945 mch_remove(infile);
18946 vim_free(infile);
18947 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018948 if (res != NULL)
18949 vim_free(res);
18950 if (list != NULL)
18951 list_free(list, TRUE);
18952}
18953
18954/*
18955 * "system()" function
18956 */
18957 static void
18958f_system(argvars, rettv)
18959 typval_T *argvars;
18960 typval_T *rettv;
18961{
18962 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18963}
18964
18965/*
18966 * "systemlist()" function
18967 */
18968 static void
18969f_systemlist(argvars, rettv)
18970 typval_T *argvars;
18971 typval_T *rettv;
18972{
18973 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974}
18975
18976/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018977 * "tabpagebuflist()" function
18978 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018979 static void
18980f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018981 typval_T *argvars UNUSED;
18982 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018983{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018984#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018985 tabpage_T *tp;
18986 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018987
18988 if (argvars[0].v_type == VAR_UNKNOWN)
18989 wp = firstwin;
18990 else
18991 {
18992 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18993 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018994 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018995 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018996 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018997 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018998 for (; wp != NULL; wp = wp->w_next)
18999 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019000 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019001 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019002 }
19003#endif
19004}
19005
19006
19007/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019008 * "tabpagenr()" function
19009 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019010 static void
19011f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019012 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019013 typval_T *rettv;
19014{
19015 int nr = 1;
19016#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019017 char_u *arg;
19018
19019 if (argvars[0].v_type != VAR_UNKNOWN)
19020 {
19021 arg = get_tv_string_chk(&argvars[0]);
19022 nr = 0;
19023 if (arg != NULL)
19024 {
19025 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019026 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019027 else
19028 EMSG2(_(e_invexpr2), arg);
19029 }
19030 }
19031 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019032 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019033#endif
19034 rettv->vval.v_number = nr;
19035}
19036
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019037
19038#ifdef FEAT_WINDOWS
19039static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19040
19041/*
19042 * Common code for tabpagewinnr() and winnr().
19043 */
19044 static int
19045get_winnr(tp, argvar)
19046 tabpage_T *tp;
19047 typval_T *argvar;
19048{
19049 win_T *twin;
19050 int nr = 1;
19051 win_T *wp;
19052 char_u *arg;
19053
19054 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19055 if (argvar->v_type != VAR_UNKNOWN)
19056 {
19057 arg = get_tv_string_chk(argvar);
19058 if (arg == NULL)
19059 nr = 0; /* type error; errmsg already given */
19060 else if (STRCMP(arg, "$") == 0)
19061 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19062 else if (STRCMP(arg, "#") == 0)
19063 {
19064 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19065 if (twin == NULL)
19066 nr = 0;
19067 }
19068 else
19069 {
19070 EMSG2(_(e_invexpr2), arg);
19071 nr = 0;
19072 }
19073 }
19074
19075 if (nr > 0)
19076 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19077 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019078 {
19079 if (wp == NULL)
19080 {
19081 /* didn't find it in this tabpage */
19082 nr = 0;
19083 break;
19084 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019085 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019086 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019087 return nr;
19088}
19089#endif
19090
19091/*
19092 * "tabpagewinnr()" function
19093 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019094 static void
19095f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019096 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019097 typval_T *rettv;
19098{
19099 int nr = 1;
19100#ifdef FEAT_WINDOWS
19101 tabpage_T *tp;
19102
19103 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19104 if (tp == NULL)
19105 nr = 0;
19106 else
19107 nr = get_winnr(tp, &argvars[1]);
19108#endif
19109 rettv->vval.v_number = nr;
19110}
19111
19112
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019113/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019114 * "tagfiles()" function
19115 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019116 static void
19117f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019118 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019119 typval_T *rettv;
19120{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019121 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019122 tagname_T tn;
19123 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019125 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019126 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019127 fname = alloc(MAXPATHL);
19128 if (fname == NULL)
19129 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019130
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019131 for (first = TRUE; ; first = FALSE)
19132 if (get_tagfname(&tn, first, fname) == FAIL
19133 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019134 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019135 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019136 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019137}
19138
19139/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019140 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019141 */
19142 static void
19143f_taglist(argvars, rettv)
19144 typval_T *argvars;
19145 typval_T *rettv;
19146{
19147 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019148
19149 tag_pattern = get_tv_string(&argvars[0]);
19150
19151 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019152 if (*tag_pattern == NUL)
19153 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019155 if (rettv_list_alloc(rettv) == OK)
19156 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019157}
19158
19159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 * "tempname()" function
19161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019163f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166{
19167 static int x = 'A';
19168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019170 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
19172 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19173 * names. Skip 'I' and 'O', they are used for shell redirection. */
19174 do
19175 {
19176 if (x == 'Z')
19177 x = '0';
19178 else if (x == '9')
19179 x = 'A';
19180 else
19181 {
19182#ifdef EBCDIC
19183 if (x == 'I')
19184 x = 'J';
19185 else if (x == 'R')
19186 x = 'S';
19187 else
19188#endif
19189 ++x;
19190 }
19191 } while (x == 'I' || x == 'O');
19192}
19193
19194/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019195 * "test(list)" function: Just checking the walls...
19196 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019197 static void
19198f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019199 typval_T *argvars UNUSED;
19200 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019201{
19202 /* Used for unit testing. Change the code below to your liking. */
19203#if 0
19204 listitem_T *li;
19205 list_T *l;
19206 char_u *bad, *good;
19207
19208 if (argvars[0].v_type != VAR_LIST)
19209 return;
19210 l = argvars[0].vval.v_list;
19211 if (l == NULL)
19212 return;
19213 li = l->lv_first;
19214 if (li == NULL)
19215 return;
19216 bad = get_tv_string(&li->li_tv);
19217 li = li->li_next;
19218 if (li == NULL)
19219 return;
19220 good = get_tv_string(&li->li_tv);
19221 rettv->vval.v_number = test_edit_score(bad, good);
19222#endif
19223}
19224
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019225#ifdef FEAT_FLOAT
19226/*
19227 * "tan()" function
19228 */
19229 static void
19230f_tan(argvars, rettv)
19231 typval_T *argvars;
19232 typval_T *rettv;
19233{
19234 float_T f;
19235
19236 rettv->v_type = VAR_FLOAT;
19237 if (get_float_arg(argvars, &f) == OK)
19238 rettv->vval.v_float = tan(f);
19239 else
19240 rettv->vval.v_float = 0.0;
19241}
19242
19243/*
19244 * "tanh()" function
19245 */
19246 static void
19247f_tanh(argvars, rettv)
19248 typval_T *argvars;
19249 typval_T *rettv;
19250{
19251 float_T f;
19252
19253 rettv->v_type = VAR_FLOAT;
19254 if (get_float_arg(argvars, &f) == OK)
19255 rettv->vval.v_float = tanh(f);
19256 else
19257 rettv->vval.v_float = 0.0;
19258}
19259#endif
19260
Bram Moolenaard52d9742005-08-21 22:20:28 +000019261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 * "tolower(string)" function
19263 */
19264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019265f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019266 typval_T *argvars;
19267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268{
19269 char_u *p;
19270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 p = vim_strsave(get_tv_string(&argvars[0]));
19272 rettv->v_type = VAR_STRING;
19273 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274
19275 if (p != NULL)
19276 while (*p != NUL)
19277 {
19278#ifdef FEAT_MBYTE
19279 int l;
19280
19281 if (enc_utf8)
19282 {
19283 int c, lc;
19284
19285 c = utf_ptr2char(p);
19286 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019287 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 /* TODO: reallocate string when byte count changes. */
19289 if (utf_char2len(lc) == l)
19290 utf_char2bytes(lc, p);
19291 p += l;
19292 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019293 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 p += l; /* skip multi-byte character */
19295 else
19296#endif
19297 {
19298 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19299 ++p;
19300 }
19301 }
19302}
19303
19304/*
19305 * "toupper(string)" function
19306 */
19307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019309 typval_T *argvars;
19310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019312 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019313 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314}
19315
19316/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019317 * "tr(string, fromstr, tostr)" function
19318 */
19319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019320f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019321 typval_T *argvars;
19322 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019323{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019324 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019325 char_u *fromstr;
19326 char_u *tostr;
19327 char_u *p;
19328#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019329 int inlen;
19330 int fromlen;
19331 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019332 int idx;
19333 char_u *cpstr;
19334 int cplen;
19335 int first = TRUE;
19336#endif
19337 char_u buf[NUMBUFLEN];
19338 char_u buf2[NUMBUFLEN];
19339 garray_T ga;
19340
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019341 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019342 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19343 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019344
19345 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019346 rettv->v_type = VAR_STRING;
19347 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019348 if (fromstr == NULL || tostr == NULL)
19349 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019350 ga_init2(&ga, (int)sizeof(char), 80);
19351
19352#ifdef FEAT_MBYTE
19353 if (!has_mbyte)
19354#endif
19355 /* not multi-byte: fromstr and tostr must be the same length */
19356 if (STRLEN(fromstr) != STRLEN(tostr))
19357 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019358#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019359error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019360#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019361 EMSG2(_(e_invarg2), fromstr);
19362 ga_clear(&ga);
19363 return;
19364 }
19365
19366 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019367 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019368 {
19369#ifdef FEAT_MBYTE
19370 if (has_mbyte)
19371 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019372 inlen = (*mb_ptr2len)(in_str);
19373 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019374 cplen = inlen;
19375 idx = 0;
19376 for (p = fromstr; *p != NUL; p += fromlen)
19377 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019378 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019379 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019380 {
19381 for (p = tostr; *p != NUL; p += tolen)
19382 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019383 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019384 if (idx-- == 0)
19385 {
19386 cplen = tolen;
19387 cpstr = p;
19388 break;
19389 }
19390 }
19391 if (*p == NUL) /* tostr is shorter than fromstr */
19392 goto error;
19393 break;
19394 }
19395 ++idx;
19396 }
19397
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019398 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019399 {
19400 /* Check that fromstr and tostr have the same number of
19401 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019402 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019403 first = FALSE;
19404 for (p = tostr; *p != NUL; p += tolen)
19405 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019406 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019407 --idx;
19408 }
19409 if (idx != 0)
19410 goto error;
19411 }
19412
19413 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019414 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019415 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019416
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019417 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019418 }
19419 else
19420#endif
19421 {
19422 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019423 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019424 if (p != NULL)
19425 ga_append(&ga, tostr[p - fromstr]);
19426 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019427 ga_append(&ga, *in_str);
19428 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019429 }
19430 }
19431
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019432 /* add a terminating NUL */
19433 ga_grow(&ga, 1);
19434 ga_append(&ga, NUL);
19435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019436 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019437}
19438
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019439#ifdef FEAT_FLOAT
19440/*
19441 * "trunc({float})" function
19442 */
19443 static void
19444f_trunc(argvars, rettv)
19445 typval_T *argvars;
19446 typval_T *rettv;
19447{
19448 float_T f;
19449
19450 rettv->v_type = VAR_FLOAT;
19451 if (get_float_arg(argvars, &f) == OK)
19452 /* trunc() is not in C90, use floor() or ceil() instead. */
19453 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19454 else
19455 rettv->vval.v_float = 0.0;
19456}
19457#endif
19458
Bram Moolenaar8299df92004-07-10 09:47:34 +000019459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 * "type(expr)" function
19461 */
19462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019463f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019464 typval_T *argvars;
19465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019467 int n;
19468
19469 switch (argvars[0].v_type)
19470 {
19471 case VAR_NUMBER: n = 0; break;
19472 case VAR_STRING: n = 1; break;
19473 case VAR_FUNC: n = 2; break;
19474 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019475 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019476#ifdef FEAT_FLOAT
19477 case VAR_FLOAT: n = 5; break;
19478#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019479 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19480 }
19481 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482}
19483
19484/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019485 * "undofile(name)" function
19486 */
19487 static void
19488f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019489 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019490 typval_T *rettv;
19491{
19492 rettv->v_type = VAR_STRING;
19493#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019494 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019495 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019496
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019497 if (*fname == NUL)
19498 {
19499 /* If there is no file name there will be no undo file. */
19500 rettv->vval.v_string = NULL;
19501 }
19502 else
19503 {
19504 char_u *ffname = FullName_save(fname, FALSE);
19505
19506 if (ffname != NULL)
19507 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19508 vim_free(ffname);
19509 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019510 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019511#else
19512 rettv->vval.v_string = NULL;
19513#endif
19514}
19515
19516/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019517 * "undotree()" function
19518 */
19519 static void
19520f_undotree(argvars, rettv)
19521 typval_T *argvars UNUSED;
19522 typval_T *rettv;
19523{
19524 if (rettv_dict_alloc(rettv) == OK)
19525 {
19526 dict_T *dict = rettv->vval.v_dict;
19527 list_T *list;
19528
Bram Moolenaar730cde92010-06-27 05:18:54 +020019529 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019530 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019531 dict_add_nr_str(dict, "save_last",
19532 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019533 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19534 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019535 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019536
19537 list = list_alloc();
19538 if (list != NULL)
19539 {
19540 u_eval_tree(curbuf->b_u_oldhead, list);
19541 dict_add_list(dict, "entries", list);
19542 }
19543 }
19544}
19545
19546/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019547 * "values(dict)" function
19548 */
19549 static void
19550f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019551 typval_T *argvars;
19552 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019553{
19554 dict_list(argvars, rettv, 1);
19555}
19556
19557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 * "virtcol(string)" function
19559 */
19560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 typval_T *argvars;
19563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564{
19565 colnr_T vcol = 0;
19566 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019567 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019569 fp = var2fpos(&argvars[0], FALSE, &fnum);
19570 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19571 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 {
19573 getvvcol(curwin, fp, NULL, NULL, &vcol);
19574 ++vcol;
19575 }
19576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578}
19579
19580/*
19581 * "visualmode()" function
19582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019585 typval_T *argvars UNUSED;
19586 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 char_u str[2];
19589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 str[0] = curbuf->b_visual_mode_eval;
19592 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594
19595 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019596 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598}
19599
19600/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019601 * "wildmenumode()" function
19602 */
19603 static void
19604f_wildmenumode(argvars, rettv)
19605 typval_T *argvars UNUSED;
19606 typval_T *rettv UNUSED;
19607{
19608#ifdef FEAT_WILDMENU
19609 if (wild_menu_showing)
19610 rettv->vval.v_number = 1;
19611#endif
19612}
19613
19614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 * "winbufnr(nr)" function
19616 */
19617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019618f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 typval_T *argvars;
19620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621{
19622 win_T *wp;
19623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019624 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629}
19630
19631/*
19632 * "wincol()" function
19633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638{
19639 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019640 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641}
19642
19643/*
19644 * "winheight(nr)" function
19645 */
19646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 typval_T *argvars;
19649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650{
19651 win_T *wp;
19652
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019653 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019657 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658}
19659
19660/*
19661 * "winline()" function
19662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667{
19668 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019669 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670}
19671
19672/*
19673 * "winnr()" function
19674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019676f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679{
19680 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019681
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019683 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019685 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686}
19687
19688/*
19689 * "winrestcmd()" function
19690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019693 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695{
19696#ifdef FEAT_WINDOWS
19697 win_T *wp;
19698 int winnr = 1;
19699 garray_T ga;
19700 char_u buf[50];
19701
19702 ga_init2(&ga, (int)sizeof(char), 70);
19703 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19704 {
19705 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19706 ga_concat(&ga, buf);
19707# ifdef FEAT_VERTSPLIT
19708 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19709 ga_concat(&ga, buf);
19710# endif
19711 ++winnr;
19712 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019713 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720}
19721
19722/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019723 * "winrestview()" function
19724 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019725 static void
19726f_winrestview(argvars, rettv)
19727 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019728 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019729{
19730 dict_T *dict;
19731
19732 if (argvars[0].v_type != VAR_DICT
19733 || (dict = argvars[0].vval.v_dict) == NULL)
19734 EMSG(_(e_invarg));
19735 else
19736 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019737 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19738 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19739 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19740 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019741#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019742 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19743 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019744#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019745 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19746 {
19747 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19748 curwin->w_set_curswant = FALSE;
19749 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019750
Bram Moolenaar82c25852014-05-28 16:47:16 +020019751 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19752 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019753#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019754 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19755 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019756#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019757 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19758 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19759 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19760 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019761
19762 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019763 win_new_height(curwin, curwin->w_height);
19764# ifdef FEAT_VERTSPLIT
19765 win_new_width(curwin, W_WIDTH(curwin));
19766# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019767 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019768
Bram Moolenaarb851a962014-10-31 15:45:52 +010019769 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019770 curwin->w_topline = 1;
19771 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19772 curwin->w_topline = curbuf->b_ml.ml_line_count;
19773#ifdef FEAT_DIFF
19774 check_topfill(curwin, TRUE);
19775#endif
19776 }
19777}
19778
19779/*
19780 * "winsaveview()" function
19781 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019782 static void
19783f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019784 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019785 typval_T *rettv;
19786{
19787 dict_T *dict;
19788
Bram Moolenaara800b422010-06-27 01:15:55 +020019789 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019790 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019791 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019792
19793 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19794 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19795#ifdef FEAT_VIRTUALEDIT
19796 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19797#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019798 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019799 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19800
19801 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19802#ifdef FEAT_DIFF
19803 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19804#endif
19805 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19806 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19807}
19808
19809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 * "winwidth(nr)" function
19811 */
19812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 typval_T *argvars;
19815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
19817 win_T *wp;
19818
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019819 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 else
19823#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827#endif
19828}
19829
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019831 * Write list of strings to file
19832 */
19833 static int
19834write_list(fd, list, binary)
19835 FILE *fd;
19836 list_T *list;
19837 int binary;
19838{
19839 listitem_T *li;
19840 int c;
19841 int ret = OK;
19842 char_u *s;
19843
19844 for (li = list->lv_first; li != NULL; li = li->li_next)
19845 {
19846 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19847 {
19848 if (*s == '\n')
19849 c = putc(NUL, fd);
19850 else
19851 c = putc(*s, fd);
19852 if (c == EOF)
19853 {
19854 ret = FAIL;
19855 break;
19856 }
19857 }
19858 if (!binary || li->li_next != NULL)
19859 if (putc('\n', fd) == EOF)
19860 {
19861 ret = FAIL;
19862 break;
19863 }
19864 if (ret == FAIL)
19865 {
19866 EMSG(_(e_write));
19867 break;
19868 }
19869 }
19870 return ret;
19871}
19872
19873/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019874 * "writefile()" function
19875 */
19876 static void
19877f_writefile(argvars, rettv)
19878 typval_T *argvars;
19879 typval_T *rettv;
19880{
19881 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019882 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019883 char_u *fname;
19884 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019885 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019886
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019887 if (check_restricted() || check_secure())
19888 return;
19889
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019890 if (argvars[0].v_type != VAR_LIST)
19891 {
19892 EMSG2(_(e_listarg), "writefile()");
19893 return;
19894 }
19895 if (argvars[0].vval.v_list == NULL)
19896 return;
19897
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019898 if (argvars[2].v_type != VAR_UNKNOWN)
19899 {
19900 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19901 binary = TRUE;
19902 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19903 append = TRUE;
19904 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019905
19906 /* Always open the file in binary mode, library functions have a mind of
19907 * their own about CR-LF conversion. */
19908 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019909 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19910 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019911 {
19912 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19913 ret = -1;
19914 }
19915 else
19916 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019917 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19918 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019919 fclose(fd);
19920 }
19921
19922 rettv->vval.v_number = ret;
19923}
19924
19925/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019926 * "xor(expr, expr)" function
19927 */
19928 static void
19929f_xor(argvars, rettv)
19930 typval_T *argvars;
19931 typval_T *rettv;
19932{
19933 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19934 ^ get_tv_number_chk(&argvars[1], NULL);
19935}
19936
19937
19938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019940 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 */
19942 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019943var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019944 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019945 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019946 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019948 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019950 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951
Bram Moolenaara5525202006-03-02 22:52:09 +000019952 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019953 if (varp->v_type == VAR_LIST)
19954 {
19955 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019956 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019957 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019958 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019959
19960 l = varp->vval.v_list;
19961 if (l == NULL)
19962 return NULL;
19963
19964 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019965 pos.lnum = list_find_nr(l, 0L, &error);
19966 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019967 return NULL; /* invalid line number */
19968
19969 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019970 pos.col = list_find_nr(l, 1L, &error);
19971 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019972 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019973 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019974
19975 /* We accept "$" for the column number: last column. */
19976 li = list_find(l, 1L);
19977 if (li != NULL && li->li_tv.v_type == VAR_STRING
19978 && li->li_tv.vval.v_string != NULL
19979 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19980 pos.col = len + 1;
19981
Bram Moolenaara5525202006-03-02 22:52:09 +000019982 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019983 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019984 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019985 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019986
Bram Moolenaara5525202006-03-02 22:52:09 +000019987#ifdef FEAT_VIRTUALEDIT
19988 /* Get the virtual offset. Defaults to zero. */
19989 pos.coladd = list_find_nr(l, 2L, &error);
19990 if (error)
19991 pos.coladd = 0;
19992#endif
19993
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019994 return &pos;
19995 }
19996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019997 name = get_tv_string_chk(varp);
19998 if (name == NULL)
19999 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020000 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020002 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20003 {
20004 if (VIsual_active)
20005 return &VIsual;
20006 return &curwin->w_cursor;
20007 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020008 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020010 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20012 return NULL;
20013 return pp;
20014 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020015
20016#ifdef FEAT_VIRTUALEDIT
20017 pos.coladd = 0;
20018#endif
20019
Bram Moolenaar477933c2007-07-17 14:32:23 +000020020 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020021 {
20022 pos.col = 0;
20023 if (name[1] == '0') /* "w0": first visible line */
20024 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020025 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020026 pos.lnum = curwin->w_topline;
20027 return &pos;
20028 }
20029 else if (name[1] == '$') /* "w$": last visible line */
20030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020031 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020032 pos.lnum = curwin->w_botline - 1;
20033 return &pos;
20034 }
20035 }
20036 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020038 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 {
20040 pos.lnum = curbuf->b_ml.ml_line_count;
20041 pos.col = 0;
20042 }
20043 else
20044 {
20045 pos.lnum = curwin->w_cursor.lnum;
20046 pos.col = (colnr_T)STRLEN(ml_get_curline());
20047 }
20048 return &pos;
20049 }
20050 return NULL;
20051}
20052
20053/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020054 * Convert list in "arg" into a position and optional file number.
20055 * When "fnump" is NULL there is no file number, only 3 items.
20056 * Note that the column is passed on as-is, the caller may want to decrement
20057 * it to use 1 for the first column.
20058 * Return FAIL when conversion is not possible, doesn't check the position for
20059 * validity.
20060 */
20061 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020062list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020063 typval_T *arg;
20064 pos_T *posp;
20065 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020066 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020067{
20068 list_T *l = arg->vval.v_list;
20069 long i = 0;
20070 long n;
20071
Bram Moolenaar493c1782014-05-28 14:34:46 +020020072 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20073 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020074 if (arg->v_type != VAR_LIST
20075 || l == NULL
20076 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020077 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020078 return FAIL;
20079
20080 if (fnump != NULL)
20081 {
20082 n = list_find_nr(l, i++, NULL); /* fnum */
20083 if (n < 0)
20084 return FAIL;
20085 if (n == 0)
20086 n = curbuf->b_fnum; /* current buffer */
20087 *fnump = n;
20088 }
20089
20090 n = list_find_nr(l, i++, NULL); /* lnum */
20091 if (n < 0)
20092 return FAIL;
20093 posp->lnum = n;
20094
20095 n = list_find_nr(l, i++, NULL); /* col */
20096 if (n < 0)
20097 return FAIL;
20098 posp->col = n;
20099
20100#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020101 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020102 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020103 posp->coladd = 0;
20104 else
20105 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020106#endif
20107
Bram Moolenaar493c1782014-05-28 14:34:46 +020020108 if (curswantp != NULL)
20109 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20110
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020111 return OK;
20112}
20113
20114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 * Get the length of an environment variable name.
20116 * Advance "arg" to the first character after the name.
20117 * Return 0 for error.
20118 */
20119 static int
20120get_env_len(arg)
20121 char_u **arg;
20122{
20123 char_u *p;
20124 int len;
20125
20126 for (p = *arg; vim_isIDc(*p); ++p)
20127 ;
20128 if (p == *arg) /* no name found */
20129 return 0;
20130
20131 len = (int)(p - *arg);
20132 *arg = p;
20133 return len;
20134}
20135
20136/*
20137 * Get the length of the name of a function or internal variable.
20138 * "arg" is advanced to the first non-white character after the name.
20139 * Return 0 if something is wrong.
20140 */
20141 static int
20142get_id_len(arg)
20143 char_u **arg;
20144{
20145 char_u *p;
20146 int len;
20147
20148 /* Find the end of the name. */
20149 for (p = *arg; eval_isnamec(*p); ++p)
20150 ;
20151 if (p == *arg) /* no name found */
20152 return 0;
20153
20154 len = (int)(p - *arg);
20155 *arg = skipwhite(p);
20156
20157 return len;
20158}
20159
20160/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020161 * Get the length of the name of a variable or function.
20162 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020164 * Return -1 if curly braces expansion failed.
20165 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 * If the name contains 'magic' {}'s, expand them and return the
20167 * expanded name in an allocated string via 'alias' - caller must free.
20168 */
20169 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020170get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 char_u **arg;
20172 char_u **alias;
20173 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020174 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175{
20176 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 char_u *p;
20178 char_u *expr_start;
20179 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180
20181 *alias = NULL; /* default to no alias */
20182
20183 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20184 && (*arg)[2] == (int)KE_SNR)
20185 {
20186 /* hard coded <SNR>, already translated */
20187 *arg += 3;
20188 return get_id_len(arg) + 3;
20189 }
20190 len = eval_fname_script(*arg);
20191 if (len > 0)
20192 {
20193 /* literal "<SID>", "s:" or "<SNR>" */
20194 *arg += len;
20195 }
20196
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020200 p = find_name_end(*arg, &expr_start, &expr_end,
20201 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 if (expr_start != NULL)
20203 {
20204 char_u *temp_string;
20205
20206 if (!evaluate)
20207 {
20208 len += (int)(p - *arg);
20209 *arg = skipwhite(p);
20210 return len;
20211 }
20212
20213 /*
20214 * Include any <SID> etc in the expanded string:
20215 * Thus the -len here.
20216 */
20217 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20218 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020219 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 *alias = temp_string;
20221 *arg = skipwhite(p);
20222 return (int)STRLEN(temp_string);
20223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224
20225 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020226 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 EMSG2(_(e_invexpr2), *arg);
20228
20229 return len;
20230}
20231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020232/*
20233 * Find the end of a variable or function name, taking care of magic braces.
20234 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20235 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020236 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020237 * Return a pointer to just after the name. Equal to "arg" if there is no
20238 * valid name.
20239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020241find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 char_u *arg;
20243 char_u **expr_start;
20244 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020245 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 int mb_nest = 0;
20248 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 char_u *p;
20250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020251 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 *expr_start = NULL;
20254 *expr_end = NULL;
20255 }
20256
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020257 /* Quick check for valid starting character. */
20258 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20259 return arg;
20260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020261 for (p = arg; *p != NUL
20262 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020263 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020264 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020266 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020267 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020268 if (*p == '\'')
20269 {
20270 /* skip over 'string' to avoid counting [ and ] inside it. */
20271 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20272 ;
20273 if (*p == NUL)
20274 break;
20275 }
20276 else if (*p == '"')
20277 {
20278 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20279 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20280 if (*p == '\\' && p[1] != NUL)
20281 ++p;
20282 if (*p == NUL)
20283 break;
20284 }
20285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020288 if (*p == '[')
20289 ++br_nest;
20290 else if (*p == ']')
20291 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020294 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020296 if (*p == '{')
20297 {
20298 mb_nest++;
20299 if (expr_start != NULL && *expr_start == NULL)
20300 *expr_start = p;
20301 }
20302 else if (*p == '}')
20303 {
20304 mb_nest--;
20305 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20306 *expr_end = p;
20307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 }
20310
20311 return p;
20312}
20313
20314/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020315 * Expands out the 'magic' {}'s in a variable/function name.
20316 * Note that this can call itself recursively, to deal with
20317 * constructs like foo{bar}{baz}{bam}
20318 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20319 * "in_start" ^
20320 * "expr_start" ^
20321 * "expr_end" ^
20322 * "in_end" ^
20323 *
20324 * Returns a new allocated string, which the caller must free.
20325 * Returns NULL for failure.
20326 */
20327 static char_u *
20328make_expanded_name(in_start, expr_start, expr_end, in_end)
20329 char_u *in_start;
20330 char_u *expr_start;
20331 char_u *expr_end;
20332 char_u *in_end;
20333{
20334 char_u c1;
20335 char_u *retval = NULL;
20336 char_u *temp_result;
20337 char_u *nextcmd = NULL;
20338
20339 if (expr_end == NULL || in_end == NULL)
20340 return NULL;
20341 *expr_start = NUL;
20342 *expr_end = NUL;
20343 c1 = *in_end;
20344 *in_end = NUL;
20345
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020346 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020347 if (temp_result != NULL && nextcmd == NULL)
20348 {
20349 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20350 + (in_end - expr_end) + 1));
20351 if (retval != NULL)
20352 {
20353 STRCPY(retval, in_start);
20354 STRCAT(retval, temp_result);
20355 STRCAT(retval, expr_end + 1);
20356 }
20357 }
20358 vim_free(temp_result);
20359
20360 *in_end = c1; /* put char back for error messages */
20361 *expr_start = '{';
20362 *expr_end = '}';
20363
20364 if (retval != NULL)
20365 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020366 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020367 if (expr_start != NULL)
20368 {
20369 /* Further expansion! */
20370 temp_result = make_expanded_name(retval, expr_start,
20371 expr_end, temp_result);
20372 vim_free(retval);
20373 retval = temp_result;
20374 }
20375 }
20376
20377 return retval;
20378}
20379
20380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020382 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 */
20384 static int
20385eval_isnamec(c)
20386 int c;
20387{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020388 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20389}
20390
20391/*
20392 * Return TRUE if character "c" can be used as the first character in a
20393 * variable or function name (excluding '{' and '}').
20394 */
20395 static int
20396eval_isnamec1(c)
20397 int c;
20398{
20399 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400}
20401
20402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 * Set number v: variable to "val".
20404 */
20405 void
20406set_vim_var_nr(idx, val)
20407 int idx;
20408 long val;
20409{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020410 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411}
20412
20413/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020414 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 */
20416 long
20417get_vim_var_nr(idx)
20418 int idx;
20419{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020420 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421}
20422
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020423/*
20424 * Get string v: variable value. Uses a static buffer, can only be used once.
20425 */
20426 char_u *
20427get_vim_var_str(idx)
20428 int idx;
20429{
20430 return get_tv_string(&vimvars[idx].vv_tv);
20431}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020432
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020434 * Get List v: variable value. Caller must take care of reference count when
20435 * needed.
20436 */
20437 list_T *
20438get_vim_var_list(idx)
20439 int idx;
20440{
20441 return vimvars[idx].vv_list;
20442}
20443
20444/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020445 * Set v:char to character "c".
20446 */
20447 void
20448set_vim_var_char(c)
20449 int c;
20450{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020451 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020452
20453#ifdef FEAT_MBYTE
20454 if (has_mbyte)
20455 buf[(*mb_char2bytes)(c, buf)] = NUL;
20456 else
20457#endif
20458 {
20459 buf[0] = c;
20460 buf[1] = NUL;
20461 }
20462 set_vim_var_string(VV_CHAR, buf, -1);
20463}
20464
20465/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020466 * Set v:count to "count" and v:count1 to "count1".
20467 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 */
20469 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020470set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 long count;
20472 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020473 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020475 if (set_prevcount)
20476 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020477 vimvars[VV_COUNT].vv_nr = count;
20478 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479}
20480
20481/*
20482 * Set string v: variable to a copy of "val".
20483 */
20484 void
20485set_vim_var_string(idx, val, len)
20486 int idx;
20487 char_u *val;
20488 int len; /* length of "val" to use or -1 (whole string) */
20489{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020490 /* Need to do this (at least) once, since we can't initialize a union.
20491 * Will always be invoked when "v:progname" is set. */
20492 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20493
Bram Moolenaare9a41262005-01-15 22:18:47 +000020494 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020496 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020498 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020500 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501}
20502
20503/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020504 * Set List v: variable to "val".
20505 */
20506 void
20507set_vim_var_list(idx, val)
20508 int idx;
20509 list_T *val;
20510{
20511 list_unref(vimvars[idx].vv_list);
20512 vimvars[idx].vv_list = val;
20513 if (val != NULL)
20514 ++val->lv_refcount;
20515}
20516
20517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 * Set v:register if needed.
20519 */
20520 void
20521set_reg_var(c)
20522 int c;
20523{
20524 char_u regname;
20525
20526 if (c == 0 || c == ' ')
20527 regname = '"';
20528 else
20529 regname = c;
20530 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020531 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 set_vim_var_string(VV_REG, &regname, 1);
20533}
20534
20535/*
20536 * Get or set v:exception. If "oldval" == NULL, return the current value.
20537 * Otherwise, restore the value to "oldval" and return NULL.
20538 * Must always be called in pairs to save and restore v:exception! Does not
20539 * take care of memory allocations.
20540 */
20541 char_u *
20542v_exception(oldval)
20543 char_u *oldval;
20544{
20545 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020546 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547
Bram Moolenaare9a41262005-01-15 22:18:47 +000020548 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 return NULL;
20550}
20551
20552/*
20553 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20554 * Otherwise, restore the value to "oldval" and return NULL.
20555 * Must always be called in pairs to save and restore v:throwpoint! Does not
20556 * take care of memory allocations.
20557 */
20558 char_u *
20559v_throwpoint(oldval)
20560 char_u *oldval;
20561{
20562 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020563 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564
Bram Moolenaare9a41262005-01-15 22:18:47 +000020565 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 return NULL;
20567}
20568
20569#if defined(FEAT_AUTOCMD) || defined(PROTO)
20570/*
20571 * Set v:cmdarg.
20572 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20573 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20574 * Must always be called in pairs!
20575 */
20576 char_u *
20577set_cmdarg(eap, oldarg)
20578 exarg_T *eap;
20579 char_u *oldarg;
20580{
20581 char_u *oldval;
20582 char_u *newval;
20583 unsigned len;
20584
Bram Moolenaare9a41262005-01-15 22:18:47 +000020585 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020586 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020588 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020589 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020590 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 }
20592
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020593 if (eap->force_bin == FORCE_BIN)
20594 len = 6;
20595 else if (eap->force_bin == FORCE_NOBIN)
20596 len = 8;
20597 else
20598 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020599
20600 if (eap->read_edit)
20601 len += 7;
20602
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020603 if (eap->force_ff != 0)
20604 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20605# ifdef FEAT_MBYTE
20606 if (eap->force_enc != 0)
20607 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020608 if (eap->bad_char != 0)
20609 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020610# endif
20611
20612 newval = alloc(len + 1);
20613 if (newval == NULL)
20614 return NULL;
20615
20616 if (eap->force_bin == FORCE_BIN)
20617 sprintf((char *)newval, " ++bin");
20618 else if (eap->force_bin == FORCE_NOBIN)
20619 sprintf((char *)newval, " ++nobin");
20620 else
20621 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020622
20623 if (eap->read_edit)
20624 STRCAT(newval, " ++edit");
20625
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020626 if (eap->force_ff != 0)
20627 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20628 eap->cmd + eap->force_ff);
20629# ifdef FEAT_MBYTE
20630 if (eap->force_enc != 0)
20631 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20632 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020633 if (eap->bad_char == BAD_KEEP)
20634 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20635 else if (eap->bad_char == BAD_DROP)
20636 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20637 else if (eap->bad_char != 0)
20638 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020639# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020640 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020641 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642}
20643#endif
20644
20645/*
20646 * Get the value of internal variable "name".
20647 * Return OK or FAIL.
20648 */
20649 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020650get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 char_u *name;
20652 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020653 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020654 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020655 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656{
20657 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020658 typval_T *tv = NULL;
20659 typval_T atv;
20660 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662
20663 /* truncate the name, so that we can use strcmp() */
20664 cc = name[len];
20665 name[len] = NUL;
20666
20667 /*
20668 * Check for "b:changedtick".
20669 */
20670 if (STRCMP(name, "b:changedtick") == 0)
20671 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 atv.v_type = VAR_NUMBER;
20673 atv.vval.v_number = curbuf->b_changedtick;
20674 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 }
20676
20677 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 * Check for user-defined variables.
20679 */
20680 else
20681 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020682 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020684 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 }
20686
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020689 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020690 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 ret = FAIL;
20692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020693 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020694 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695
20696 name[len] = cc;
20697
20698 return ret;
20699}
20700
20701/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020702 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20703 * Also handle function call with Funcref variable: func(expr)
20704 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20705 */
20706 static int
20707handle_subscript(arg, rettv, evaluate, verbose)
20708 char_u **arg;
20709 typval_T *rettv;
20710 int evaluate; /* do more than finding the end */
20711 int verbose; /* give error messages */
20712{
20713 int ret = OK;
20714 dict_T *selfdict = NULL;
20715 char_u *s;
20716 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020717 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020718
20719 while (ret == OK
20720 && (**arg == '['
20721 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020722 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020723 && !vim_iswhite(*(*arg - 1)))
20724 {
20725 if (**arg == '(')
20726 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020727 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020728 if (evaluate)
20729 {
20730 functv = *rettv;
20731 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020732
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020733 /* Invoke the function. Recursive! */
20734 s = functv.vval.v_string;
20735 }
20736 else
20737 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020738 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020739 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20740 &len, evaluate, selfdict);
20741
20742 /* Clear the funcref afterwards, so that deleting it while
20743 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020744 if (evaluate)
20745 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020746
20747 /* Stop the expression evaluation when immediately aborting on
20748 * error, or when an interrupt occurred or an exception was thrown
20749 * but not caught. */
20750 if (aborting())
20751 {
20752 if (ret == OK)
20753 clear_tv(rettv);
20754 ret = FAIL;
20755 }
20756 dict_unref(selfdict);
20757 selfdict = NULL;
20758 }
20759 else /* **arg == '[' || **arg == '.' */
20760 {
20761 dict_unref(selfdict);
20762 if (rettv->v_type == VAR_DICT)
20763 {
20764 selfdict = rettv->vval.v_dict;
20765 if (selfdict != NULL)
20766 ++selfdict->dv_refcount;
20767 }
20768 else
20769 selfdict = NULL;
20770 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20771 {
20772 clear_tv(rettv);
20773 ret = FAIL;
20774 }
20775 }
20776 }
20777 dict_unref(selfdict);
20778 return ret;
20779}
20780
20781/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020782 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020783 * value).
20784 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020785 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020786alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020787{
Bram Moolenaar33570922005-01-25 22:26:29 +000020788 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020789}
20790
20791/*
20792 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 * The string "s" must have been allocated, it is consumed.
20794 * Return NULL for out of memory, the variable otherwise.
20795 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020796 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020797alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 char_u *s;
20799{
Bram Moolenaar33570922005-01-25 22:26:29 +000020800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020802 rettv = alloc_tv();
20803 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 rettv->v_type = VAR_STRING;
20806 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 }
20808 else
20809 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020810 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811}
20812
20813/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020814 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020816 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020817free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020818 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819{
20820 if (varp != NULL)
20821 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020822 switch (varp->v_type)
20823 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020825 func_unref(varp->vval.v_string);
20826 /*FALLTHROUGH*/
20827 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020828 vim_free(varp->vval.v_string);
20829 break;
20830 case VAR_LIST:
20831 list_unref(varp->vval.v_list);
20832 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020833 case VAR_DICT:
20834 dict_unref(varp->vval.v_dict);
20835 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020836 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020837#ifdef FEAT_FLOAT
20838 case VAR_FLOAT:
20839#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020840 case VAR_UNKNOWN:
20841 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020842 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020843 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020844 break;
20845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 vim_free(varp);
20847 }
20848}
20849
20850/*
20851 * Free the memory for a variable value and set the value to NULL or 0.
20852 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020853 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020854clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020855 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856{
20857 if (varp != NULL)
20858 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020859 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020861 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020862 func_unref(varp->vval.v_string);
20863 /*FALLTHROUGH*/
20864 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020865 vim_free(varp->vval.v_string);
20866 varp->vval.v_string = NULL;
20867 break;
20868 case VAR_LIST:
20869 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020870 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020871 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020872 case VAR_DICT:
20873 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020874 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020875 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020876 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020877 varp->vval.v_number = 0;
20878 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020879#ifdef FEAT_FLOAT
20880 case VAR_FLOAT:
20881 varp->vval.v_float = 0.0;
20882 break;
20883#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020884 case VAR_UNKNOWN:
20885 break;
20886 default:
20887 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020889 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 }
20891}
20892
20893/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 * Set the value of a variable to NULL without freeing items.
20895 */
20896 static void
20897init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020898 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020899{
20900 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020901 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902}
20903
20904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 * Get the number value of a variable.
20906 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020907 * For incompatible types, return 0.
20908 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20909 * caller of incompatible types: it sets *denote to TRUE if "denote"
20910 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 */
20912 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020914 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020916 int error = FALSE;
20917
20918 return get_tv_number_chk(varp, &error); /* return 0L on error */
20919}
20920
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020921 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020922get_tv_number_chk(varp, denote)
20923 typval_T *varp;
20924 int *denote;
20925{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020926 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020928 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020930 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020931 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020932#ifdef FEAT_FLOAT
20933 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020934 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020935 break;
20936#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020937 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020938 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020939 break;
20940 case VAR_STRING:
20941 if (varp->vval.v_string != NULL)
20942 vim_str2nr(varp->vval.v_string, NULL, NULL,
20943 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020944 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020945 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020946 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020947 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020948 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020949 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020951 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020955 if (denote == NULL) /* useful for values that must be unsigned */
20956 n = -1;
20957 else
20958 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020959 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960}
20961
20962/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020963 * Get the lnum from the first argument.
20964 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020965 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 */
20967 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020969 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970{
Bram Moolenaar33570922005-01-25 22:26:29 +000020971 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 linenr_T lnum;
20973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020974 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 if (lnum == 0) /* no valid number, try using line() */
20976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020977 rettv.v_type = VAR_NUMBER;
20978 f_line(argvars, &rettv);
20979 lnum = rettv.vval.v_number;
20980 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 }
20982 return lnum;
20983}
20984
20985/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020986 * Get the lnum from the first argument.
20987 * Also accepts "$", then "buf" is used.
20988 * Returns 0 on error.
20989 */
20990 static linenr_T
20991get_tv_lnum_buf(argvars, buf)
20992 typval_T *argvars;
20993 buf_T *buf;
20994{
20995 if (argvars[0].v_type == VAR_STRING
20996 && argvars[0].vval.v_string != NULL
20997 && argvars[0].vval.v_string[0] == '$'
20998 && buf != NULL)
20999 return buf->b_ml.ml_line_count;
21000 return get_tv_number_chk(&argvars[0], NULL);
21001}
21002
21003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 * Get the string value of a variable.
21005 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021006 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21007 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 * If the String variable has never been set, return an empty string.
21009 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021010 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21011 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 */
21013 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021014get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021015 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016{
21017 static char_u mybuf[NUMBUFLEN];
21018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021019 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020}
21021
21022 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021024 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021025 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021027 char_u *res = get_tv_string_buf_chk(varp, buf);
21028
21029 return res != NULL ? res : (char_u *)"";
21030}
21031
Bram Moolenaar7d647822014-04-05 21:28:56 +020021032/*
21033 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21034 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021035 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021036get_tv_string_chk(varp)
21037 typval_T *varp;
21038{
21039 static char_u mybuf[NUMBUFLEN];
21040
21041 return get_tv_string_buf_chk(varp, mybuf);
21042}
21043
21044 static char_u *
21045get_tv_string_buf_chk(varp, buf)
21046 typval_T *varp;
21047 char_u *buf;
21048{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021049 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021051 case VAR_NUMBER:
21052 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21053 return buf;
21054 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021055 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021056 break;
21057 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021058 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021059 break;
21060 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021061 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021062 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021063#ifdef FEAT_FLOAT
21064 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021065 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021066 break;
21067#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021068 case VAR_STRING:
21069 if (varp->vval.v_string != NULL)
21070 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021071 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021072 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021073 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021076 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077}
21078
21079/*
21080 * Find variable "name" in the list of variables.
21081 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021082 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021083 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021084 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021086 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021087find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021089 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021090 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021093 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094
Bram Moolenaara7043832005-01-21 11:56:39 +000021095 ht = find_var_ht(name, &varname);
21096 if (htp != NULL)
21097 *htp = ht;
21098 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021100 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101}
21102
21103/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021104 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021105 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021107 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021108find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021109 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021110 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021111 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021112 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021113{
Bram Moolenaar33570922005-01-25 22:26:29 +000021114 hashitem_T *hi;
21115
21116 if (*varname == NUL)
21117 {
21118 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021119 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021120 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021121 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 case 'g': return &globvars_var;
21123 case 'v': return &vimvars_var;
21124 case 'b': return &curbuf->b_bufvar;
21125 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021126#ifdef FEAT_WINDOWS
21127 case 't': return &curtab->tp_winvar;
21128#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021129 case 'l': return current_funccal == NULL
21130 ? NULL : &current_funccal->l_vars_var;
21131 case 'a': return current_funccal == NULL
21132 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021133 }
21134 return NULL;
21135 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021136
21137 hi = hash_find(ht, varname);
21138 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021139 {
21140 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021141 * worked find the variable again. Don't auto-load a script if it was
21142 * loaded already, otherwise it would be loaded every time when
21143 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021144 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021145 {
21146 /* Note: script_autoload() may make "hi" invalid. It must either
21147 * be obtained again or not used. */
21148 if (!script_autoload(varname, FALSE) || aborting())
21149 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021150 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021152 if (HASHITEM_EMPTY(hi))
21153 return NULL;
21154 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021155 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021156}
21157
21158/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021159 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021160 * Set "varname" to the start of name without ':'.
21161 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021162 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021163find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 char_u *name;
21165 char_u **varname;
21166{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021167 hashitem_T *hi;
21168
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 if (name[1] != ':')
21170 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021171 /* The name must not start with a colon or #. */
21172 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 return NULL;
21174 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021175
21176 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021177 hi = hash_find(&compat_hashtab, name);
21178 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021179 return &compat_hashtab;
21180
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021182 return &globvarht; /* global variable */
21183 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 }
21185 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021186 if (*name == 'g') /* global variable */
21187 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021188 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21189 */
21190 if (vim_strchr(name + 2, ':') != NULL
21191 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021192 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021194 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021196 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021197#ifdef FEAT_WINDOWS
21198 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021199 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021200#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 if (*name == 'v') /* v: variable */
21202 return &vimvarht;
21203 if (*name == 'a' && current_funccal != NULL) /* function argument */
21204 return &current_funccal->l_avars.dv_hashtab;
21205 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21206 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 if (*name == 's' /* script variable */
21208 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21209 return &SCRIPT_VARS(current_SID);
21210 return NULL;
21211}
21212
21213/*
21214 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021215 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 * Returns NULL when it doesn't exist.
21217 */
21218 char_u *
21219get_var_value(name)
21220 char_u *name;
21221{
Bram Moolenaar33570922005-01-25 22:26:29 +000021222 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021224 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 if (v == NULL)
21226 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021227 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228}
21229
21230/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021231 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 * sourcing this script and when executing functions defined in the script.
21233 */
21234 void
21235new_script_vars(id)
21236 scid_T id;
21237{
Bram Moolenaara7043832005-01-21 11:56:39 +000021238 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021239 hashtab_T *ht;
21240 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021241
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21243 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021244 /* Re-allocating ga_data means that an ht_array pointing to
21245 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021246 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021247 for (i = 1; i <= ga_scripts.ga_len; ++i)
21248 {
21249 ht = &SCRIPT_VARS(i);
21250 if (ht->ht_mask == HT_INIT_SIZE - 1)
21251 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021252 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021253 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021254 }
21255
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 while (ga_scripts.ga_len < id)
21257 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021258 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021259 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021260 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 }
21263 }
21264}
21265
21266/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021267 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21268 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 */
21270 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021271init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021272 dict_T *dict;
21273 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021274 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275{
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021277 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021278 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021279 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021280 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 dict_var->di_tv.vval.v_dict = dict;
21282 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021283 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21285 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286}
21287
21288/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021289 * Unreference a dictionary initialized by init_var_dict().
21290 */
21291 void
21292unref_var_dict(dict)
21293 dict_T *dict;
21294{
21295 /* Now the dict needs to be freed if no one else is using it, go back to
21296 * normal reference counting. */
21297 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21298 dict_unref(dict);
21299}
21300
21301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 * Frees all allocated variables and the value they contain.
21304 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 */
21306 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021307vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 hashtab_T *ht;
21309{
21310 vars_clear_ext(ht, TRUE);
21311}
21312
21313/*
21314 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21315 */
21316 static void
21317vars_clear_ext(ht, free_val)
21318 hashtab_T *ht;
21319 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320{
Bram Moolenaara7043832005-01-21 11:56:39 +000021321 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021322 hashitem_T *hi;
21323 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324
Bram Moolenaar33570922005-01-25 22:26:29 +000021325 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021326 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021327 for (hi = ht->ht_array; todo > 0; ++hi)
21328 {
21329 if (!HASHITEM_EMPTY(hi))
21330 {
21331 --todo;
21332
Bram Moolenaar33570922005-01-25 22:26:29 +000021333 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021334 * ht_array might change then. hash_clear() takes care of it
21335 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021336 v = HI2DI(hi);
21337 if (free_val)
21338 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021339 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021341 }
21342 }
21343 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021344 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345}
21346
Bram Moolenaara7043832005-01-21 11:56:39 +000021347/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021348 * Delete a variable from hashtab "ht" at item "hi".
21349 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021352delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021353 hashtab_T *ht;
21354 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355{
Bram Moolenaar33570922005-01-25 22:26:29 +000021356 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021357
21358 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021359 clear_tv(&di->di_tv);
21360 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361}
21362
21363/*
21364 * List the value of one internal variable.
21365 */
21366 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021367list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021368 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021370 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372 char_u *tofree;
21373 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021374 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021375
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021376 current_copyID += COPYID_INC;
21377 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021378 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021379 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021380 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381}
21382
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021384list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 char_u *prefix;
21386 char_u *name;
21387 int type;
21388 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021389 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390{
Bram Moolenaar31859182007-08-14 20:41:13 +000021391 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21392 msg_start();
21393 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 if (name != NULL) /* "a:" vars don't have a name stored */
21395 msg_puts(name);
21396 msg_putchar(' ');
21397 msg_advance(22);
21398 if (type == VAR_NUMBER)
21399 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021400 else if (type == VAR_FUNC)
21401 msg_putchar('*');
21402 else if (type == VAR_LIST)
21403 {
21404 msg_putchar('[');
21405 if (*string == '[')
21406 ++string;
21407 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021408 else if (type == VAR_DICT)
21409 {
21410 msg_putchar('{');
21411 if (*string == '{')
21412 ++string;
21413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 else
21415 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021416
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021418
21419 if (type == VAR_FUNC)
21420 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021421 if (*first)
21422 {
21423 msg_clr_eos();
21424 *first = FALSE;
21425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426}
21427
21428/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 * If the variable already exists, the value is updated.
21431 * Otherwise the variable is created.
21432 */
21433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021436 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021437 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438{
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021443 ht = find_var_ht(name, &varname);
21444 if (ht == NULL || *varname == NUL)
21445 {
21446 EMSG2(_(e_illvar), name);
21447 return;
21448 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021449 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021450
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021451 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21452 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021453
Bram Moolenaar33570922005-01-25 22:26:29 +000021454 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021457 if (var_check_ro(v->di_flags, name)
21458 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 return;
21460 if (v->di_tv.v_type != tv->v_type
21461 && !((v->di_tv.v_type == VAR_STRING
21462 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021463 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021464 || tv->v_type == VAR_NUMBER))
21465#ifdef FEAT_FLOAT
21466 && !((v->di_tv.v_type == VAR_NUMBER
21467 || v->di_tv.v_type == VAR_FLOAT)
21468 && (tv->v_type == VAR_NUMBER
21469 || tv->v_type == VAR_FLOAT))
21470#endif
21471 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021472 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021473 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021474 return;
21475 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021476
21477 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021478 * Handle setting internal v: variables separately: we don't change
21479 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 */
21481 if (ht == &vimvarht)
21482 {
21483 if (v->di_tv.v_type == VAR_STRING)
21484 {
21485 vim_free(v->di_tv.vval.v_string);
21486 if (copy || tv->v_type != VAR_STRING)
21487 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21488 else
21489 {
21490 /* Take over the string to avoid an extra alloc/free. */
21491 v->di_tv.vval.v_string = tv->vval.v_string;
21492 tv->vval.v_string = NULL;
21493 }
21494 }
21495 else if (v->di_tv.v_type != VAR_NUMBER)
21496 EMSG2(_(e_intern2), "set_var()");
21497 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021499 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021500 if (STRCMP(varname, "searchforward") == 0)
21501 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021502#ifdef FEAT_SEARCH_EXTRA
21503 else if (STRCMP(varname, "hlsearch") == 0)
21504 {
21505 no_hlsearch = !v->di_tv.vval.v_number;
21506 redraw_all_later(SOME_VALID);
21507 }
21508#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021509 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021510 return;
21511 }
21512
21513 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 }
21515 else /* add a new variable */
21516 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021517 /* Can't add "v:" variable. */
21518 if (ht == &vimvarht)
21519 {
21520 EMSG2(_(e_illvar), name);
21521 return;
21522 }
21523
Bram Moolenaar92124a32005-06-17 22:03:40 +000021524 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021525 if (!valid_varname(varname))
21526 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021527
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021528 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21529 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021530 if (v == NULL)
21531 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021535 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 return;
21537 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021538 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021540
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021541 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021543 else
21544 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021545 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021546 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021547 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549}
21550
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021551/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021552 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 * Also give an error message.
21554 */
21555 static int
21556var_check_ro(flags, name)
21557 int flags;
21558 char_u *name;
21559{
21560 if (flags & DI_FLAGS_RO)
21561 {
21562 EMSG2(_(e_readonlyvar), name);
21563 return TRUE;
21564 }
21565 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21566 {
21567 EMSG2(_(e_readonlysbx), name);
21568 return TRUE;
21569 }
21570 return FALSE;
21571}
21572
21573/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021574 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21575 * Also give an error message.
21576 */
21577 static int
21578var_check_fixed(flags, name)
21579 int flags;
21580 char_u *name;
21581{
21582 if (flags & DI_FLAGS_FIX)
21583 {
21584 EMSG2(_("E795: Cannot delete variable %s"), name);
21585 return TRUE;
21586 }
21587 return FALSE;
21588}
21589
21590/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021591 * Check if a funcref is assigned to a valid variable name.
21592 * Return TRUE and give an error if not.
21593 */
21594 static int
21595var_check_func_name(name, new_var)
21596 char_u *name; /* points to start of variable name */
21597 int new_var; /* TRUE when creating the variable */
21598{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021599 /* Allow for w: b: s: and t:. */
21600 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021601 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21602 ? name[2] : name[0]))
21603 {
21604 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21605 name);
21606 return TRUE;
21607 }
21608 /* Don't allow hiding a function. When "v" is not NULL we might be
21609 * assigning another function to the same var, the type is checked
21610 * below. */
21611 if (new_var && function_exists(name))
21612 {
21613 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21614 name);
21615 return TRUE;
21616 }
21617 return FALSE;
21618}
21619
21620/*
21621 * Check if a variable name is valid.
21622 * Return FALSE and give an error if not.
21623 */
21624 static int
21625valid_varname(varname)
21626 char_u *varname;
21627{
21628 char_u *p;
21629
21630 for (p = varname; *p != NUL; ++p)
21631 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21632 && *p != AUTOLOAD_CHAR)
21633 {
21634 EMSG2(_(e_illvar), varname);
21635 return FALSE;
21636 }
21637 return TRUE;
21638}
21639
21640/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021641 * Return TRUE if typeval "tv" is set to be locked (immutable).
21642 * Also give an error message, using "name".
21643 */
21644 static int
21645tv_check_lock(lock, name)
21646 int lock;
21647 char_u *name;
21648{
21649 if (lock & VAR_LOCKED)
21650 {
21651 EMSG2(_("E741: Value is locked: %s"),
21652 name == NULL ? (char_u *)_("Unknown") : name);
21653 return TRUE;
21654 }
21655 if (lock & VAR_FIXED)
21656 {
21657 EMSG2(_("E742: Cannot change value of %s"),
21658 name == NULL ? (char_u *)_("Unknown") : name);
21659 return TRUE;
21660 }
21661 return FALSE;
21662}
21663
21664/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021666 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021667 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021668 * It is OK for "from" and "to" to point to the same item. This is used to
21669 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021670 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021671 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021672copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 typval_T *from;
21674 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021676 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021677 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021678 switch (from->v_type)
21679 {
21680 case VAR_NUMBER:
21681 to->vval.v_number = from->vval.v_number;
21682 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021683#ifdef FEAT_FLOAT
21684 case VAR_FLOAT:
21685 to->vval.v_float = from->vval.v_float;
21686 break;
21687#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021688 case VAR_STRING:
21689 case VAR_FUNC:
21690 if (from->vval.v_string == NULL)
21691 to->vval.v_string = NULL;
21692 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021693 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021694 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021695 if (from->v_type == VAR_FUNC)
21696 func_ref(to->vval.v_string);
21697 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021698 break;
21699 case VAR_LIST:
21700 if (from->vval.v_list == NULL)
21701 to->vval.v_list = NULL;
21702 else
21703 {
21704 to->vval.v_list = from->vval.v_list;
21705 ++to->vval.v_list->lv_refcount;
21706 }
21707 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021708 case VAR_DICT:
21709 if (from->vval.v_dict == NULL)
21710 to->vval.v_dict = NULL;
21711 else
21712 {
21713 to->vval.v_dict = from->vval.v_dict;
21714 ++to->vval.v_dict->dv_refcount;
21715 }
21716 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021717 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021718 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021719 break;
21720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721}
21722
21723/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021724 * Make a copy of an item.
21725 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021726 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21727 * reference to an already copied list/dict can be used.
21728 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021729 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021730 static int
21731item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021732 typval_T *from;
21733 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021734 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021735 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021736{
21737 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021738 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021739
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021741 {
21742 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021743 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021744 }
21745 ++recurse;
21746
21747 switch (from->v_type)
21748 {
21749 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021750#ifdef FEAT_FLOAT
21751 case VAR_FLOAT:
21752#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021753 case VAR_STRING:
21754 case VAR_FUNC:
21755 copy_tv(from, to);
21756 break;
21757 case VAR_LIST:
21758 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021759 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021760 if (from->vval.v_list == NULL)
21761 to->vval.v_list = NULL;
21762 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21763 {
21764 /* use the copy made earlier */
21765 to->vval.v_list = from->vval.v_list->lv_copylist;
21766 ++to->vval.v_list->lv_refcount;
21767 }
21768 else
21769 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21770 if (to->vval.v_list == NULL)
21771 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021772 break;
21773 case VAR_DICT:
21774 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021775 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021776 if (from->vval.v_dict == NULL)
21777 to->vval.v_dict = NULL;
21778 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21779 {
21780 /* use the copy made earlier */
21781 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21782 ++to->vval.v_dict->dv_refcount;
21783 }
21784 else
21785 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21786 if (to->vval.v_dict == NULL)
21787 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021788 break;
21789 default:
21790 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021791 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021792 }
21793 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021794 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021795}
21796
21797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 * ":echo expr1 ..." print each argument separated with a space, add a
21799 * newline at the end.
21800 * ":echon expr1 ..." print each argument plain.
21801 */
21802 void
21803ex_echo(eap)
21804 exarg_T *eap;
21805{
21806 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021808 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 char_u *p;
21810 int needclr = TRUE;
21811 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021812 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813
21814 if (eap->skip)
21815 ++emsg_skip;
21816 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21817 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021818 /* If eval1() causes an error message the text from the command may
21819 * still need to be cleared. E.g., "echo 22,44". */
21820 need_clr_eos = needclr;
21821
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 {
21825 /*
21826 * Report the invalid expression unless the expression evaluation
21827 * has been cancelled due to an aborting error, an interrupt, or an
21828 * exception.
21829 */
21830 if (!aborting())
21831 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021832 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 break;
21834 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021835 need_clr_eos = FALSE;
21836
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 if (!eap->skip)
21838 {
21839 if (atstart)
21840 {
21841 atstart = FALSE;
21842 /* Call msg_start() after eval1(), evaluating the expression
21843 * may cause a message to appear. */
21844 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021845 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021846 /* Mark the saved text as finishing the line, so that what
21847 * follows is displayed on a new line when scrolling back
21848 * at the more prompt. */
21849 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 }
21853 else if (eap->cmdidx == CMD_echo)
21854 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021855 current_copyID += COPYID_INC;
21856 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021857 if (p != NULL)
21858 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021860 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021862 if (*p != TAB && needclr)
21863 {
21864 /* remove any text still there from the command */
21865 msg_clr_eos();
21866 needclr = FALSE;
21867 }
21868 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 }
21870 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021871 {
21872#ifdef FEAT_MBYTE
21873 if (has_mbyte)
21874 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021875 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021876
21877 (void)msg_outtrans_len_attr(p, i, echo_attr);
21878 p += i - 1;
21879 }
21880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021882 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021885 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021887 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 arg = skipwhite(arg);
21889 }
21890 eap->nextcmd = check_nextcmd(arg);
21891
21892 if (eap->skip)
21893 --emsg_skip;
21894 else
21895 {
21896 /* remove text that may still be there from the command */
21897 if (needclr)
21898 msg_clr_eos();
21899 if (eap->cmdidx == CMD_echo)
21900 msg_end();
21901 }
21902}
21903
21904/*
21905 * ":echohl {name}".
21906 */
21907 void
21908ex_echohl(eap)
21909 exarg_T *eap;
21910{
21911 int id;
21912
21913 id = syn_name2id(eap->arg);
21914 if (id == 0)
21915 echo_attr = 0;
21916 else
21917 echo_attr = syn_id2attr(id);
21918}
21919
21920/*
21921 * ":execute expr1 ..." execute the result of an expression.
21922 * ":echomsg expr1 ..." Print a message
21923 * ":echoerr expr1 ..." Print an error
21924 * Each gets spaces around each argument and a newline at the end for
21925 * echo commands
21926 */
21927 void
21928ex_execute(eap)
21929 exarg_T *eap;
21930{
21931 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 int ret = OK;
21934 char_u *p;
21935 garray_T ga;
21936 int len;
21937 int save_did_emsg;
21938
21939 ga_init2(&ga, 1, 80);
21940
21941 if (eap->skip)
21942 ++emsg_skip;
21943 while (*arg != NUL && *arg != '|' && *arg != '\n')
21944 {
21945 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021946 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 {
21948 /*
21949 * Report the invalid expression unless the expression evaluation
21950 * has been cancelled due to an aborting error, an interrupt, or an
21951 * exception.
21952 */
21953 if (!aborting())
21954 EMSG2(_(e_invexpr2), p);
21955 ret = FAIL;
21956 break;
21957 }
21958
21959 if (!eap->skip)
21960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021961 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 len = (int)STRLEN(p);
21963 if (ga_grow(&ga, len + 2) == FAIL)
21964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 ret = FAIL;
21967 break;
21968 }
21969 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 ga.ga_len += len;
21973 }
21974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 arg = skipwhite(arg);
21977 }
21978
21979 if (ret != FAIL && ga.ga_data != NULL)
21980 {
21981 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021984 out_flush();
21985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 else if (eap->cmdidx == CMD_echoerr)
21987 {
21988 /* We don't want to abort following commands, restore did_emsg. */
21989 save_did_emsg = did_emsg;
21990 EMSG((char_u *)ga.ga_data);
21991 if (!force_abort)
21992 did_emsg = save_did_emsg;
21993 }
21994 else if (eap->cmdidx == CMD_execute)
21995 do_cmdline((char_u *)ga.ga_data,
21996 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21997 }
21998
21999 ga_clear(&ga);
22000
22001 if (eap->skip)
22002 --emsg_skip;
22003
22004 eap->nextcmd = check_nextcmd(arg);
22005}
22006
22007/*
22008 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22009 * "arg" points to the "&" or '+' when called, to "option" when returning.
22010 * Returns NULL when no option name found. Otherwise pointer to the char
22011 * after the option name.
22012 */
22013 static char_u *
22014find_option_end(arg, opt_flags)
22015 char_u **arg;
22016 int *opt_flags;
22017{
22018 char_u *p = *arg;
22019
22020 ++p;
22021 if (*p == 'g' && p[1] == ':')
22022 {
22023 *opt_flags = OPT_GLOBAL;
22024 p += 2;
22025 }
22026 else if (*p == 'l' && p[1] == ':')
22027 {
22028 *opt_flags = OPT_LOCAL;
22029 p += 2;
22030 }
22031 else
22032 *opt_flags = 0;
22033
22034 if (!ASCII_ISALPHA(*p))
22035 return NULL;
22036 *arg = p;
22037
22038 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22039 p += 4; /* termcap option */
22040 else
22041 while (ASCII_ISALPHA(*p))
22042 ++p;
22043 return p;
22044}
22045
22046/*
22047 * ":function"
22048 */
22049 void
22050ex_function(eap)
22051 exarg_T *eap;
22052{
22053 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022054 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 int j;
22056 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022058 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 char_u *name = NULL;
22060 char_u *p;
22061 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022062 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 garray_T newargs;
22064 garray_T newlines;
22065 int varargs = FALSE;
22066 int mustend = FALSE;
22067 int flags = 0;
22068 ufunc_T *fp;
22069 int indent;
22070 int nesting;
22071 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 dictitem_T *v;
22073 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022074 static int func_nr = 0; /* number for nameless function */
22075 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022076 hashtab_T *ht;
22077 int todo;
22078 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022079 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080
22081 /*
22082 * ":function" without argument: list functions.
22083 */
22084 if (ends_excmd(*eap->arg))
22085 {
22086 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022087 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022088 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022089 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022090 {
22091 if (!HASHITEM_EMPTY(hi))
22092 {
22093 --todo;
22094 fp = HI2UF(hi);
22095 if (!isdigit(*fp->uf_name))
22096 list_func_head(fp, FALSE);
22097 }
22098 }
22099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 eap->nextcmd = check_nextcmd(eap->arg);
22101 return;
22102 }
22103
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022104 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022105 * ":function /pat": list functions matching pattern.
22106 */
22107 if (*eap->arg == '/')
22108 {
22109 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22110 if (!eap->skip)
22111 {
22112 regmatch_T regmatch;
22113
22114 c = *p;
22115 *p = NUL;
22116 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22117 *p = c;
22118 if (regmatch.regprog != NULL)
22119 {
22120 regmatch.rm_ic = p_ic;
22121
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022122 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022123 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22124 {
22125 if (!HASHITEM_EMPTY(hi))
22126 {
22127 --todo;
22128 fp = HI2UF(hi);
22129 if (!isdigit(*fp->uf_name)
22130 && vim_regexec(&regmatch, fp->uf_name, 0))
22131 list_func_head(fp, FALSE);
22132 }
22133 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022134 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022135 }
22136 }
22137 if (*p == '/')
22138 ++p;
22139 eap->nextcmd = check_nextcmd(p);
22140 return;
22141 }
22142
22143 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022144 * Get the function name. There are these situations:
22145 * func normal function name
22146 * "name" == func, "fudi.fd_dict" == NULL
22147 * dict.func new dictionary entry
22148 * "name" == NULL, "fudi.fd_dict" set,
22149 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22150 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022151 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022152 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22153 * dict.func existing dict entry that's not a Funcref
22154 * "name" == NULL, "fudi.fd_dict" set,
22155 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022156 * s:func script-local function name
22157 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 name = trans_function_name(&p, eap->skip, 0, &fudi);
22161 paren = (vim_strchr(p, '(') != NULL);
22162 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 {
22164 /*
22165 * Return on an invalid expression in braces, unless the expression
22166 * evaluation has been cancelled due to an aborting error, an
22167 * interrupt, or an exception.
22168 */
22169 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022171 if (!eap->skip && fudi.fd_newkey != NULL)
22172 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022173 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 else
22177 eap->skip = TRUE;
22178 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022179
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 /* An error in a function call during evaluation of an expression in magic
22181 * braces should not cause the function not to be defined. */
22182 saved_did_emsg = did_emsg;
22183 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184
22185 /*
22186 * ":function func" with only function name: list function.
22187 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022188 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 {
22190 if (!ends_excmd(*skipwhite(p)))
22191 {
22192 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022193 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 }
22195 eap->nextcmd = check_nextcmd(p);
22196 if (eap->nextcmd != NULL)
22197 *p = NUL;
22198 if (!eap->skip && !got_int)
22199 {
22200 fp = find_func(name);
22201 if (fp != NULL)
22202 {
22203 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022204 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022206 if (FUNCLINE(fp, j) == NULL)
22207 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 msg_putchar('\n');
22209 msg_outnum((long)(j + 1));
22210 if (j < 9)
22211 msg_putchar(' ');
22212 if (j < 99)
22213 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022214 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 out_flush(); /* show a line at a time */
22216 ui_breakcheck();
22217 }
22218 if (!got_int)
22219 {
22220 msg_putchar('\n');
22221 msg_puts((char_u *)" endfunction");
22222 }
22223 }
22224 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022225 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022227 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 }
22229
22230 /*
22231 * ":function name(arg1, arg2)" Define function.
22232 */
22233 p = skipwhite(p);
22234 if (*p != '(')
22235 {
22236 if (!eap->skip)
22237 {
22238 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022239 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 }
22241 /* attempt to continue by skipping some text */
22242 if (vim_strchr(p, '(') != NULL)
22243 p = vim_strchr(p, '(');
22244 }
22245 p = skipwhite(p + 1);
22246
22247 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22248 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22249
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022250 if (!eap->skip)
22251 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022252 /* Check the name of the function. Unless it's a dictionary function
22253 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022254 if (name != NULL)
22255 arg = name;
22256 else
22257 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022258 if (arg != NULL && (fudi.fd_di == NULL
22259 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022260 {
22261 if (*arg == K_SPECIAL)
22262 j = 3;
22263 else
22264 j = 0;
22265 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22266 : eval_isnamec(arg[j])))
22267 ++j;
22268 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022269 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022270 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022271 /* Disallow using the g: dict. */
22272 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22273 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022274 }
22275
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 /*
22277 * Isolate the arguments: "arg1, arg2, ...)"
22278 */
22279 while (*p != ')')
22280 {
22281 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22282 {
22283 varargs = TRUE;
22284 p += 3;
22285 mustend = TRUE;
22286 }
22287 else
22288 {
22289 arg = p;
22290 while (ASCII_ISALNUM(*p) || *p == '_')
22291 ++p;
22292 if (arg == p || isdigit(*arg)
22293 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22294 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22295 {
22296 if (!eap->skip)
22297 EMSG2(_("E125: Illegal argument: %s"), arg);
22298 break;
22299 }
22300 if (ga_grow(&newargs, 1) == FAIL)
22301 goto erret;
22302 c = *p;
22303 *p = NUL;
22304 arg = vim_strsave(arg);
22305 if (arg == NULL)
22306 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022307
22308 /* Check for duplicate argument name. */
22309 for (i = 0; i < newargs.ga_len; ++i)
22310 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22311 {
22312 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022313 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022314 goto erret;
22315 }
22316
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22318 *p = c;
22319 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 if (*p == ',')
22321 ++p;
22322 else
22323 mustend = TRUE;
22324 }
22325 p = skipwhite(p);
22326 if (mustend && *p != ')')
22327 {
22328 if (!eap->skip)
22329 EMSG2(_(e_invarg2), eap->arg);
22330 break;
22331 }
22332 }
22333 ++p; /* skip the ')' */
22334
Bram Moolenaare9a41262005-01-15 22:18:47 +000022335 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 for (;;)
22337 {
22338 p = skipwhite(p);
22339 if (STRNCMP(p, "range", 5) == 0)
22340 {
22341 flags |= FC_RANGE;
22342 p += 5;
22343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022344 else if (STRNCMP(p, "dict", 4) == 0)
22345 {
22346 flags |= FC_DICT;
22347 p += 4;
22348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 else if (STRNCMP(p, "abort", 5) == 0)
22350 {
22351 flags |= FC_ABORT;
22352 p += 5;
22353 }
22354 else
22355 break;
22356 }
22357
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022358 /* When there is a line break use what follows for the function body.
22359 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22360 if (*p == '\n')
22361 line_arg = p + 1;
22362 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 EMSG(_(e_trailing));
22364
22365 /*
22366 * Read the body of the function, until ":endfunction" is found.
22367 */
22368 if (KeyTyped)
22369 {
22370 /* Check if the function already exists, don't let the user type the
22371 * whole function before telling him it doesn't work! For a script we
22372 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022373 if (!eap->skip && !eap->forceit)
22374 {
22375 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22376 EMSG(_(e_funcdict));
22377 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022378 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022381 if (!eap->skip && did_emsg)
22382 goto erret;
22383
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 msg_putchar('\n'); /* don't overwrite the function name */
22385 cmdline_row = msg_row;
22386 }
22387
22388 indent = 2;
22389 nesting = 0;
22390 for (;;)
22391 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022392 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022393 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022394 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022395 saved_wait_return = FALSE;
22396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022398 sourcing_lnum_off = sourcing_lnum;
22399
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022400 if (line_arg != NULL)
22401 {
22402 /* Use eap->arg, split up in parts by line breaks. */
22403 theline = line_arg;
22404 p = vim_strchr(theline, '\n');
22405 if (p == NULL)
22406 line_arg += STRLEN(line_arg);
22407 else
22408 {
22409 *p = NUL;
22410 line_arg = p + 1;
22411 }
22412 }
22413 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 theline = getcmdline(':', 0L, indent);
22415 else
22416 theline = eap->getline(':', eap->cookie, indent);
22417 if (KeyTyped)
22418 lines_left = Rows - 1;
22419 if (theline == NULL)
22420 {
22421 EMSG(_("E126: Missing :endfunction"));
22422 goto erret;
22423 }
22424
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022425 /* Detect line continuation: sourcing_lnum increased more than one. */
22426 if (sourcing_lnum > sourcing_lnum_off + 1)
22427 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22428 else
22429 sourcing_lnum_off = 0;
22430
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 if (skip_until != NULL)
22432 {
22433 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22434 * don't check for ":endfunc". */
22435 if (STRCMP(theline, skip_until) == 0)
22436 {
22437 vim_free(skip_until);
22438 skip_until = NULL;
22439 }
22440 }
22441 else
22442 {
22443 /* skip ':' and blanks*/
22444 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22445 ;
22446
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022447 /* Check for "endfunction". */
22448 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022450 if (line_arg == NULL)
22451 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 break;
22453 }
22454
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022455 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 * at "end". */
22457 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22458 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022459 else if (STRNCMP(p, "if", 2) == 0
22460 || STRNCMP(p, "wh", 2) == 0
22461 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 || STRNCMP(p, "try", 3) == 0)
22463 indent += 2;
22464
22465 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022466 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022468 if (*p == '!')
22469 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022471 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22472 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022474 ++nesting;
22475 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 }
22477 }
22478
22479 /* Check for ":append" or ":insert". */
22480 p = skip_range(p, NULL);
22481 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22482 || (p[0] == 'i'
22483 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22484 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22485 skip_until = vim_strsave((char_u *)".");
22486
22487 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22488 arg = skipwhite(skiptowhite(p));
22489 if (arg[0] == '<' && arg[1] =='<'
22490 && ((p[0] == 'p' && p[1] == 'y'
22491 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22492 || (p[0] == 'p' && p[1] == 'e'
22493 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22494 || (p[0] == 't' && p[1] == 'c'
22495 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022496 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22497 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22499 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022500 || (p[0] == 'm' && p[1] == 'z'
22501 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 ))
22503 {
22504 /* ":python <<" continues until a dot, like ":append" */
22505 p = skipwhite(arg + 2);
22506 if (*p == NUL)
22507 skip_until = vim_strsave((char_u *)".");
22508 else
22509 skip_until = vim_strsave(p);
22510 }
22511 }
22512
22513 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022514 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022515 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022516 if (line_arg == NULL)
22517 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022519 }
22520
22521 /* Copy the line to newly allocated memory. get_one_sourceline()
22522 * allocates 250 bytes per line, this saves 80% on average. The cost
22523 * is an extra alloc/free. */
22524 p = vim_strsave(theline);
22525 if (p != NULL)
22526 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022527 if (line_arg == NULL)
22528 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022529 theline = p;
22530 }
22531
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022532 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22533
22534 /* Add NULL lines for continuation lines, so that the line count is
22535 * equal to the index in the growarray. */
22536 while (sourcing_lnum_off-- > 0)
22537 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022538
22539 /* Check for end of eap->arg. */
22540 if (line_arg != NULL && *line_arg == NUL)
22541 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 }
22543
22544 /* Don't define the function when skipping commands or when an error was
22545 * detected. */
22546 if (eap->skip || did_emsg)
22547 goto erret;
22548
22549 /*
22550 * If there are no errors, add the function
22551 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022552 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022553 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022554 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022557 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022558 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022559 goto erret;
22560 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022561
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022562 fp = find_func(name);
22563 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022565 if (!eap->forceit)
22566 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022567 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022568 goto erret;
22569 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022570 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022571 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022572 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022573 name);
22574 goto erret;
22575 }
22576 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022577 ga_clear_strings(&(fp->uf_args));
22578 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022579 vim_free(name);
22580 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 }
22583 else
22584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022585 char numbuf[20];
22586
22587 fp = NULL;
22588 if (fudi.fd_newkey == NULL && !eap->forceit)
22589 {
22590 EMSG(_(e_funcdict));
22591 goto erret;
22592 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022593 if (fudi.fd_di == NULL)
22594 {
22595 /* Can't add a function to a locked dictionary */
22596 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22597 goto erret;
22598 }
22599 /* Can't change an existing function if it is locked */
22600 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22601 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022602
22603 /* Give the function a sequential number. Can only be used with a
22604 * Funcref! */
22605 vim_free(name);
22606 sprintf(numbuf, "%d", ++func_nr);
22607 name = vim_strsave((char_u *)numbuf);
22608 if (name == NULL)
22609 goto erret;
22610 }
22611
22612 if (fp == NULL)
22613 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022614 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022615 {
22616 int slen, plen;
22617 char_u *scriptname;
22618
22619 /* Check that the autoload name matches the script name. */
22620 j = FAIL;
22621 if (sourcing_name != NULL)
22622 {
22623 scriptname = autoload_name(name);
22624 if (scriptname != NULL)
22625 {
22626 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022627 plen = (int)STRLEN(p);
22628 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022629 if (slen > plen && fnamecmp(p,
22630 sourcing_name + slen - plen) == 0)
22631 j = OK;
22632 vim_free(scriptname);
22633 }
22634 }
22635 if (j == FAIL)
22636 {
22637 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22638 goto erret;
22639 }
22640 }
22641
22642 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 if (fp == NULL)
22644 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645
22646 if (fudi.fd_dict != NULL)
22647 {
22648 if (fudi.fd_di == NULL)
22649 {
22650 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022651 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022652 if (fudi.fd_di == NULL)
22653 {
22654 vim_free(fp);
22655 goto erret;
22656 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022657 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22658 {
22659 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022660 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022661 goto erret;
22662 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022663 }
22664 else
22665 /* overwrite existing dict entry */
22666 clear_tv(&fudi.fd_di->di_tv);
22667 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022668 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022669 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022670 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022671
22672 /* behave like "dict" was used */
22673 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 }
22675
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022677 STRCPY(fp->uf_name, name);
22678 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022680 fp->uf_args = newargs;
22681 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022682#ifdef FEAT_PROFILE
22683 fp->uf_tml_count = NULL;
22684 fp->uf_tml_total = NULL;
22685 fp->uf_tml_self = NULL;
22686 fp->uf_profiling = FALSE;
22687 if (prof_def_func())
22688 func_do_profile(fp);
22689#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022690 fp->uf_varargs = varargs;
22691 fp->uf_flags = flags;
22692 fp->uf_calls = 0;
22693 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022694 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695
22696erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 ga_clear_strings(&newargs);
22698 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022699ret_free:
22700 vim_free(skip_until);
22701 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022704 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705}
22706
22707/*
22708 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022709 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022711 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022712 * TFN_INT: internal function name OK
22713 * TFN_QUIET: be quiet
22714 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 * Advances "pp" to just after the function name (if no error).
22716 */
22717 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022718trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 char_u **pp;
22720 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022721 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022722 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022724 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 char_u *start;
22726 char_u *end;
22727 int lead;
22728 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022731
22732 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022735
22736 /* Check for hard coded <SNR>: already translated function ID (from a user
22737 * command). */
22738 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22739 && (*pp)[2] == (int)KE_SNR)
22740 {
22741 *pp += 3;
22742 len = get_id_len(pp) + 3;
22743 return vim_strnsave(start, len);
22744 }
22745
22746 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22747 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022749 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022751
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022752 /* Note that TFN_ flags use the same values as GLV_ flags. */
22753 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022754 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022755 if (end == start)
22756 {
22757 if (!skip)
22758 EMSG(_("E129: Function name required"));
22759 goto theend;
22760 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022761 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022762 {
22763 /*
22764 * Report an invalid expression in braces, unless the expression
22765 * evaluation has been cancelled due to an aborting error, an
22766 * interrupt, or an exception.
22767 */
22768 if (!aborting())
22769 {
22770 if (end != NULL)
22771 EMSG2(_(e_invarg2), start);
22772 }
22773 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022774 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022775 goto theend;
22776 }
22777
22778 if (lv.ll_tv != NULL)
22779 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022780 if (fdp != NULL)
22781 {
22782 fdp->fd_dict = lv.ll_dict;
22783 fdp->fd_newkey = lv.ll_newkey;
22784 lv.ll_newkey = NULL;
22785 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022787 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22788 {
22789 name = vim_strsave(lv.ll_tv->vval.v_string);
22790 *pp = end;
22791 }
22792 else
22793 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022794 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22795 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022796 EMSG(_(e_funcref));
22797 else
22798 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022799 name = NULL;
22800 }
22801 goto theend;
22802 }
22803
22804 if (lv.ll_name == NULL)
22805 {
22806 /* Error found, but continue after the function name. */
22807 *pp = end;
22808 goto theend;
22809 }
22810
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022811 /* Check if the name is a Funcref. If so, use the value. */
22812 if (lv.ll_exp_name != NULL)
22813 {
22814 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022815 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022816 if (name == lv.ll_exp_name)
22817 name = NULL;
22818 }
22819 else
22820 {
22821 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022822 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022823 if (name == *pp)
22824 name = NULL;
22825 }
22826 if (name != NULL)
22827 {
22828 name = vim_strsave(name);
22829 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022830 if (STRNCMP(name, "<SNR>", 5) == 0)
22831 {
22832 /* Change "<SNR>" to the byte sequence. */
22833 name[0] = K_SPECIAL;
22834 name[1] = KS_EXTRA;
22835 name[2] = (int)KE_SNR;
22836 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22837 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022838 goto theend;
22839 }
22840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022841 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022842 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022843 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022844 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22845 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22846 {
22847 /* When there was "s:" already or the name expanded to get a
22848 * leading "s:" then remove it. */
22849 lv.ll_name += 2;
22850 len -= 2;
22851 lead = 2;
22852 }
22853 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022854 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022855 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022856 /* skip over "s:" and "g:" */
22857 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022858 lv.ll_name += 2;
22859 len = (int)(end - lv.ll_name);
22860 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022861
22862 /*
22863 * Copy the function name to allocated memory.
22864 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22865 * Accept <SNR>123_name() outside a script.
22866 */
22867 if (skip)
22868 lead = 0; /* do nothing */
22869 else if (lead > 0)
22870 {
22871 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022872 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22873 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022874 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022875 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022876 if (current_SID <= 0)
22877 {
22878 EMSG(_(e_usingsid));
22879 goto theend;
22880 }
22881 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22882 lead += (int)STRLEN(sid_buf);
22883 }
22884 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022885 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022886 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022887 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022888 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022889 goto theend;
22890 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022891 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022892 {
22893 char_u *cp = vim_strchr(lv.ll_name, ':');
22894
22895 if (cp != NULL && cp < end)
22896 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022897 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022898 goto theend;
22899 }
22900 }
22901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022902 name = alloc((unsigned)(len + lead + 1));
22903 if (name != NULL)
22904 {
22905 if (lead > 0)
22906 {
22907 name[0] = K_SPECIAL;
22908 name[1] = KS_EXTRA;
22909 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022910 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022911 STRCPY(name + 3, sid_buf);
22912 }
22913 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022914 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022915 }
22916 *pp = end;
22917
22918theend:
22919 clear_lval(&lv);
22920 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921}
22922
22923/*
22924 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22925 * Return 2 if "p" starts with "s:".
22926 * Return 0 otherwise.
22927 */
22928 static int
22929eval_fname_script(p)
22930 char_u *p;
22931{
22932 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22933 || STRNICMP(p + 1, "SNR>", 4) == 0))
22934 return 5;
22935 if (p[0] == 's' && p[1] == ':')
22936 return 2;
22937 return 0;
22938}
22939
22940/*
22941 * Return TRUE if "p" starts with "<SID>" or "s:".
22942 * Only works if eval_fname_script() returned non-zero for "p"!
22943 */
22944 static int
22945eval_fname_sid(p)
22946 char_u *p;
22947{
22948 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22949}
22950
22951/*
22952 * List the head of the function: "name(arg1, arg2)".
22953 */
22954 static void
22955list_func_head(fp, indent)
22956 ufunc_T *fp;
22957 int indent;
22958{
22959 int j;
22960
22961 msg_start();
22962 if (indent)
22963 MSG_PUTS(" ");
22964 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022965 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 {
22967 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022968 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 }
22970 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022971 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022973 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 {
22975 if (j)
22976 MSG_PUTS(", ");
22977 msg_puts(FUNCARG(fp, j));
22978 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022979 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 {
22981 if (j)
22982 MSG_PUTS(", ");
22983 MSG_PUTS("...");
22984 }
22985 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022986 if (fp->uf_flags & FC_ABORT)
22987 MSG_PUTS(" abort");
22988 if (fp->uf_flags & FC_RANGE)
22989 MSG_PUTS(" range");
22990 if (fp->uf_flags & FC_DICT)
22991 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022992 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022993 if (p_verbose > 0)
22994 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995}
22996
22997/*
22998 * Find a function by name, return pointer to it in ufuncs.
22999 * Return NULL for unknown function.
23000 */
23001 static ufunc_T *
23002find_func(name)
23003 char_u *name;
23004{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023005 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023007 hi = hash_find(&func_hashtab, name);
23008 if (!HASHITEM_EMPTY(hi))
23009 return HI2UF(hi);
23010 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011}
23012
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023013#if defined(EXITFREE) || defined(PROTO)
23014 void
23015free_all_functions()
23016{
23017 hashitem_T *hi;
23018
23019 /* Need to start all over every time, because func_free() may change the
23020 * hash table. */
23021 while (func_hashtab.ht_used > 0)
23022 for (hi = func_hashtab.ht_array; ; ++hi)
23023 if (!HASHITEM_EMPTY(hi))
23024 {
23025 func_free(HI2UF(hi));
23026 break;
23027 }
23028}
23029#endif
23030
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023031 int
23032translated_function_exists(name)
23033 char_u *name;
23034{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023035 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023036 return find_internal_func(name) >= 0;
23037 return find_func(name) != NULL;
23038}
23039
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023040/*
23041 * Return TRUE if a function "name" exists.
23042 */
23043 static int
23044function_exists(name)
23045 char_u *name;
23046{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023047 char_u *nm = name;
23048 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049 int n = FALSE;
23050
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023051 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23052 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023053 nm = skipwhite(nm);
23054
23055 /* Only accept "funcname", "funcname ", "funcname (..." and
23056 * "funcname(...", not "funcname!...". */
23057 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023058 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023059 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023060 return n;
23061}
23062
Bram Moolenaara1544c02013-05-30 12:35:52 +020023063 char_u *
23064get_expanded_name(name, check)
23065 char_u *name;
23066 int check;
23067{
23068 char_u *nm = name;
23069 char_u *p;
23070
23071 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23072
23073 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023074 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023075 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023076
Bram Moolenaara1544c02013-05-30 12:35:52 +020023077 vim_free(p);
23078 return NULL;
23079}
23080
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023081/*
23082 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023083 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23084 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023085 */
23086 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023087builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023088 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023089 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023090{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023091 char_u *p;
23092
23093 if (!ASCII_ISLOWER(name[0]))
23094 return FALSE;
23095 p = vim_strchr(name, AUTOLOAD_CHAR);
23096 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023097}
23098
Bram Moolenaar05159a02005-02-26 23:04:13 +000023099#if defined(FEAT_PROFILE) || defined(PROTO)
23100/*
23101 * Start profiling function "fp".
23102 */
23103 static void
23104func_do_profile(fp)
23105 ufunc_T *fp;
23106{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023107 int len = fp->uf_lines.ga_len;
23108
23109 if (len == 0)
23110 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023111 fp->uf_tm_count = 0;
23112 profile_zero(&fp->uf_tm_self);
23113 profile_zero(&fp->uf_tm_total);
23114 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023115 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023116 if (fp->uf_tml_total == NULL)
23117 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023118 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023119 if (fp->uf_tml_self == NULL)
23120 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023121 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023122 fp->uf_tml_idx = -1;
23123 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23124 || fp->uf_tml_self == NULL)
23125 return; /* out of memory */
23126
23127 fp->uf_profiling = TRUE;
23128}
23129
23130/*
23131 * Dump the profiling results for all functions in file "fd".
23132 */
23133 void
23134func_dump_profile(fd)
23135 FILE *fd;
23136{
23137 hashitem_T *hi;
23138 int todo;
23139 ufunc_T *fp;
23140 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023141 ufunc_T **sorttab;
23142 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023144 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023145 if (todo == 0)
23146 return; /* nothing to dump */
23147
Bram Moolenaar73830342005-02-28 22:48:19 +000023148 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
23149
Bram Moolenaar05159a02005-02-26 23:04:13 +000023150 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23151 {
23152 if (!HASHITEM_EMPTY(hi))
23153 {
23154 --todo;
23155 fp = HI2UF(hi);
23156 if (fp->uf_profiling)
23157 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023158 if (sorttab != NULL)
23159 sorttab[st_len++] = fp;
23160
Bram Moolenaar05159a02005-02-26 23:04:13 +000023161 if (fp->uf_name[0] == K_SPECIAL)
23162 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23163 else
23164 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23165 if (fp->uf_tm_count == 1)
23166 fprintf(fd, "Called 1 time\n");
23167 else
23168 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23169 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23170 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23171 fprintf(fd, "\n");
23172 fprintf(fd, "count total (s) self (s)\n");
23173
23174 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23175 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023176 if (FUNCLINE(fp, i) == NULL)
23177 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023178 prof_func_line(fd, fp->uf_tml_count[i],
23179 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023180 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23181 }
23182 fprintf(fd, "\n");
23183 }
23184 }
23185 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023186
23187 if (sorttab != NULL && st_len > 0)
23188 {
23189 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23190 prof_total_cmp);
23191 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23192 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23193 prof_self_cmp);
23194 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23195 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023196
23197 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023198}
Bram Moolenaar73830342005-02-28 22:48:19 +000023199
23200 static void
23201prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23202 FILE *fd;
23203 ufunc_T **sorttab;
23204 int st_len;
23205 char *title;
23206 int prefer_self; /* when equal print only self time */
23207{
23208 int i;
23209 ufunc_T *fp;
23210
23211 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23212 fprintf(fd, "count total (s) self (s) function\n");
23213 for (i = 0; i < 20 && i < st_len; ++i)
23214 {
23215 fp = sorttab[i];
23216 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23217 prefer_self);
23218 if (fp->uf_name[0] == K_SPECIAL)
23219 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23220 else
23221 fprintf(fd, " %s()\n", fp->uf_name);
23222 }
23223 fprintf(fd, "\n");
23224}
23225
23226/*
23227 * Print the count and times for one function or function line.
23228 */
23229 static void
23230prof_func_line(fd, count, total, self, prefer_self)
23231 FILE *fd;
23232 int count;
23233 proftime_T *total;
23234 proftime_T *self;
23235 int prefer_self; /* when equal print only self time */
23236{
23237 if (count > 0)
23238 {
23239 fprintf(fd, "%5d ", count);
23240 if (prefer_self && profile_equal(total, self))
23241 fprintf(fd, " ");
23242 else
23243 fprintf(fd, "%s ", profile_msg(total));
23244 if (!prefer_self && profile_equal(total, self))
23245 fprintf(fd, " ");
23246 else
23247 fprintf(fd, "%s ", profile_msg(self));
23248 }
23249 else
23250 fprintf(fd, " ");
23251}
23252
23253/*
23254 * Compare function for total time sorting.
23255 */
23256 static int
23257#ifdef __BORLANDC__
23258_RTLENTRYF
23259#endif
23260prof_total_cmp(s1, s2)
23261 const void *s1;
23262 const void *s2;
23263{
23264 ufunc_T *p1, *p2;
23265
23266 p1 = *(ufunc_T **)s1;
23267 p2 = *(ufunc_T **)s2;
23268 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23269}
23270
23271/*
23272 * Compare function for self time sorting.
23273 */
23274 static int
23275#ifdef __BORLANDC__
23276_RTLENTRYF
23277#endif
23278prof_self_cmp(s1, s2)
23279 const void *s1;
23280 const void *s2;
23281{
23282 ufunc_T *p1, *p2;
23283
23284 p1 = *(ufunc_T **)s1;
23285 p2 = *(ufunc_T **)s2;
23286 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23287}
23288
Bram Moolenaar05159a02005-02-26 23:04:13 +000023289#endif
23290
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023291/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023292 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023293 * Return TRUE if a package was loaded.
23294 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023295 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023296script_autoload(name, reload)
23297 char_u *name;
23298 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023299{
23300 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023301 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023302 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023303 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023304
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023305 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023306 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023307 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023308 return FALSE;
23309
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023310 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023311
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023312 /* Find the name in the list of previously loaded package names. Skip
23313 * "autoload/", it's always the same. */
23314 for (i = 0; i < ga_loaded.ga_len; ++i)
23315 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23316 break;
23317 if (!reload && i < ga_loaded.ga_len)
23318 ret = FALSE; /* was loaded already */
23319 else
23320 {
23321 /* Remember the name if it wasn't loaded already. */
23322 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23323 {
23324 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23325 tofree = NULL;
23326 }
23327
23328 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023329 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023330 ret = TRUE;
23331 }
23332
23333 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023334 return ret;
23335}
23336
23337/*
23338 * Return the autoload script name for a function or variable name.
23339 * Returns NULL when out of memory.
23340 */
23341 static char_u *
23342autoload_name(name)
23343 char_u *name;
23344{
23345 char_u *p;
23346 char_u *scriptname;
23347
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023348 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023349 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23350 if (scriptname == NULL)
23351 return FALSE;
23352 STRCPY(scriptname, "autoload/");
23353 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023354 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023355 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023356 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023357 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023358 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023359}
23360
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23362
23363/*
23364 * Function given to ExpandGeneric() to obtain the list of user defined
23365 * function names.
23366 */
23367 char_u *
23368get_user_func_name(xp, idx)
23369 expand_T *xp;
23370 int idx;
23371{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023372 static long_u done;
23373 static hashitem_T *hi;
23374 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375
23376 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023378 done = 0;
23379 hi = func_hashtab.ht_array;
23380 }
23381 if (done < func_hashtab.ht_used)
23382 {
23383 if (done++ > 0)
23384 ++hi;
23385 while (HASHITEM_EMPTY(hi))
23386 ++hi;
23387 fp = HI2UF(hi);
23388
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023389 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023390 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023391
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023392 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23393 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394
23395 cat_func_name(IObuff, fp);
23396 if (xp->xp_context != EXPAND_USER_FUNC)
23397 {
23398 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023399 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 STRCAT(IObuff, ")");
23401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 return IObuff;
23403 }
23404 return NULL;
23405}
23406
23407#endif /* FEAT_CMDL_COMPL */
23408
23409/*
23410 * Copy the function name of "fp" to buffer "buf".
23411 * "buf" must be able to hold the function name plus three bytes.
23412 * Takes care of script-local function names.
23413 */
23414 static void
23415cat_func_name(buf, fp)
23416 char_u *buf;
23417 ufunc_T *fp;
23418{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023419 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 {
23421 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023422 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 }
23424 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023425 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426}
23427
23428/*
23429 * ":delfunction {name}"
23430 */
23431 void
23432ex_delfunction(eap)
23433 exarg_T *eap;
23434{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023435 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 char_u *p;
23437 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439
23440 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023441 name = trans_function_name(&p, eap->skip, 0, &fudi);
23442 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023444 {
23445 if (fudi.fd_dict != NULL && !eap->skip)
23446 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 if (!ends_excmd(*skipwhite(p)))
23450 {
23451 vim_free(name);
23452 EMSG(_(e_trailing));
23453 return;
23454 }
23455 eap->nextcmd = check_nextcmd(p);
23456 if (eap->nextcmd != NULL)
23457 *p = NUL;
23458
23459 if (!eap->skip)
23460 fp = find_func(name);
23461 vim_free(name);
23462
23463 if (!eap->skip)
23464 {
23465 if (fp == NULL)
23466 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023467 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 return;
23469 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023470 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 {
23472 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23473 return;
23474 }
23475
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023476 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023478 /* Delete the dict item that refers to the function, it will
23479 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023480 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023482 else
23483 func_free(fp);
23484 }
23485}
23486
23487/*
23488 * Free a function and remove it from the list of functions.
23489 */
23490 static void
23491func_free(fp)
23492 ufunc_T *fp;
23493{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023494 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495
23496 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 ga_clear_strings(&(fp->uf_args));
23498 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023499#ifdef FEAT_PROFILE
23500 vim_free(fp->uf_tml_count);
23501 vim_free(fp->uf_tml_total);
23502 vim_free(fp->uf_tml_self);
23503#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023504
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023505 /* remove the function from the function hashtable */
23506 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23507 if (HASHITEM_EMPTY(hi))
23508 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023509 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023510 hash_remove(&func_hashtab, hi);
23511
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023512 vim_free(fp);
23513}
23514
23515/*
23516 * Unreference a Function: decrement the reference count and free it when it
23517 * becomes zero. Only for numbered functions.
23518 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023519 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023520func_unref(name)
23521 char_u *name;
23522{
23523 ufunc_T *fp;
23524
23525 if (name != NULL && isdigit(*name))
23526 {
23527 fp = find_func(name);
23528 if (fp == NULL)
23529 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023530 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023531 {
23532 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023533 * when "uf_calls" becomes zero. */
23534 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023535 func_free(fp);
23536 }
23537 }
23538}
23539
23540/*
23541 * Count a reference to a Function.
23542 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023543 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023544func_ref(name)
23545 char_u *name;
23546{
23547 ufunc_T *fp;
23548
23549 if (name != NULL && isdigit(*name))
23550 {
23551 fp = find_func(name);
23552 if (fp == NULL)
23553 EMSG2(_(e_intern2), "func_ref()");
23554 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023555 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 }
23557}
23558
23559/*
23560 * Call a user function.
23561 */
23562 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023563call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 ufunc_T *fp; /* pointer to function */
23565 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023566 typval_T *argvars; /* arguments */
23567 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 linenr_T firstline; /* first line of range */
23569 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023570 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571{
Bram Moolenaar33570922005-01-25 22:26:29 +000023572 char_u *save_sourcing_name;
23573 linenr_T save_sourcing_lnum;
23574 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023575 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023576 int save_did_emsg;
23577 static int depth = 0;
23578 dictitem_T *v;
23579 int fixvar_idx = 0; /* index in fixvar[] */
23580 int i;
23581 int ai;
23582 char_u numbuf[NUMBUFLEN];
23583 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023584#ifdef FEAT_PROFILE
23585 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023586 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588
23589 /* If depth of calling is getting too high, don't execute the function */
23590 if (depth >= p_mfd)
23591 {
23592 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023593 rettv->v_type = VAR_NUMBER;
23594 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 return;
23596 }
23597 ++depth;
23598
23599 line_breakcheck(); /* check for CTRL-C hit */
23600
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023601 fc = (funccall_T *)alloc(sizeof(funccall_T));
23602 fc->caller = current_funccal;
23603 current_funccal = fc;
23604 fc->func = fp;
23605 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023606 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023607 fc->linenr = 0;
23608 fc->returned = FALSE;
23609 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023611 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23612 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613
Bram Moolenaar33570922005-01-25 22:26:29 +000023614 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023615 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023616 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23617 * each argument variable and saves a lot of time.
23618 */
23619 /*
23620 * Init l: variables.
23621 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023622 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023623 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023624 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023625 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23626 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023627 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023628 name = v->di_key;
23629 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023630 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023631 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023632 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023633 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023634 v->di_tv.vval.v_dict = selfdict;
23635 ++selfdict->dv_refcount;
23636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023637
Bram Moolenaar33570922005-01-25 22:26:29 +000023638 /*
23639 * Init a: variables.
23640 * Set a:0 to "argcount".
23641 * Set a:000 to a list with room for the "..." arguments.
23642 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023643 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023644 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023645 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023646 /* Use "name" to avoid a warning from some compiler that checks the
23647 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023648 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023649 name = v->di_key;
23650 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023651 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023652 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023653 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023654 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023655 v->di_tv.vval.v_list = &fc->l_varlist;
23656 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23657 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23658 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023659
23660 /*
23661 * Set a:firstline to "firstline" and a:lastline to "lastline".
23662 * Set a:name to named arguments.
23663 * Set a:N to the "..." arguments.
23664 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023665 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023666 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023667 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023668 (varnumber_T)lastline);
23669 for (i = 0; i < argcount; ++i)
23670 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023671 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023672 if (ai < 0)
23673 /* named argument a:name */
23674 name = FUNCARG(fp, i);
23675 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023676 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023677 /* "..." argument a:1, a:2, etc. */
23678 sprintf((char *)numbuf, "%d", ai + 1);
23679 name = numbuf;
23680 }
23681 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23682 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023683 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023684 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23685 }
23686 else
23687 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023688 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23689 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023690 if (v == NULL)
23691 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023692 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023693 }
23694 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023695 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023696
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023697 /* Note: the values are copied directly to avoid alloc/free.
23698 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023699 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023700 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023701
23702 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23703 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023704 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23705 fc->l_listitems[ai].li_tv = argvars[i];
23706 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023707 }
23708 }
23709
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 /* Don't redraw while executing the function. */
23711 ++RedrawingDisabled;
23712 save_sourcing_name = sourcing_name;
23713 save_sourcing_lnum = sourcing_lnum;
23714 sourcing_lnum = 1;
23715 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023716 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 if (sourcing_name != NULL)
23718 {
23719 if (save_sourcing_name != NULL
23720 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23721 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23722 else
23723 STRCPY(sourcing_name, "function ");
23724 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23725
23726 if (p_verbose >= 12)
23727 {
23728 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023729 verbose_enter_scroll();
23730
Bram Moolenaar555b2802005-05-19 21:08:39 +000023731 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 if (p_verbose >= 14)
23733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023735 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023736 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023737 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738
23739 msg_puts((char_u *)"(");
23740 for (i = 0; i < argcount; ++i)
23741 {
23742 if (i > 0)
23743 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023744 if (argvars[i].v_type == VAR_NUMBER)
23745 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 else
23747 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023748 /* Do not want errors such as E724 here. */
23749 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023750 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023751 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023752 if (s != NULL)
23753 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023754 if (vim_strsize(s) > MSG_BUF_CLEN)
23755 {
23756 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23757 s = buf;
23758 }
23759 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023760 vim_free(tofree);
23761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 }
23763 }
23764 msg_puts((char_u *)")");
23765 }
23766 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023767
23768 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 --no_wait_return;
23770 }
23771 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023772#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023773 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023774 {
23775 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23776 func_do_profile(fp);
23777 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023778 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023779 {
23780 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023781 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023782 profile_zero(&fp->uf_tm_children);
23783 }
23784 script_prof_save(&wait_start);
23785 }
23786#endif
23787
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023789 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 save_did_emsg = did_emsg;
23791 did_emsg = FALSE;
23792
23793 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023794 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23796
23797 --RedrawingDisabled;
23798
23799 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023800 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023802 clear_tv(rettv);
23803 rettv->v_type = VAR_NUMBER;
23804 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 }
23806
Bram Moolenaar05159a02005-02-26 23:04:13 +000023807#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023808 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023809 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023810 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023811 profile_end(&call_start);
23812 profile_sub_wait(&wait_start, &call_start);
23813 profile_add(&fp->uf_tm_total, &call_start);
23814 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023815 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023816 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023817 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23818 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023819 }
23820 }
23821#endif
23822
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 /* when being verbose, mention the return value */
23824 if (p_verbose >= 12)
23825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023827 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023830 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023831 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023832 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023833 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023836 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023837 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023838 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023839 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023840
Bram Moolenaar555b2802005-05-19 21:08:39 +000023841 /* The value may be very long. Skip the middle part, so that we
23842 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023843 * truncate it at the end. Don't want errors such as E724 here. */
23844 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023845 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023846 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023847 if (s != NULL)
23848 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023849 if (vim_strsize(s) > MSG_BUF_CLEN)
23850 {
23851 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23852 s = buf;
23853 }
23854 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023855 vim_free(tofree);
23856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 }
23858 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023859
23860 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 --no_wait_return;
23862 }
23863
23864 vim_free(sourcing_name);
23865 sourcing_name = save_sourcing_name;
23866 sourcing_lnum = save_sourcing_lnum;
23867 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023868#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023869 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023870 script_prof_restore(&wait_start);
23871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872
23873 if (p_verbose >= 12 && sourcing_name != NULL)
23874 {
23875 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023876 verbose_enter_scroll();
23877
Bram Moolenaar555b2802005-05-19 21:08:39 +000023878 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023880
23881 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 --no_wait_return;
23883 }
23884
23885 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023886 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023888
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023889 /* If the a:000 list and the l: and a: dicts are not referenced we can
23890 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023891 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23892 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23893 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23894 {
23895 free_funccal(fc, FALSE);
23896 }
23897 else
23898 {
23899 hashitem_T *hi;
23900 listitem_T *li;
23901 int todo;
23902
23903 /* "fc" is still in use. This can happen when returning "a:000" or
23904 * assigning "l:" to a global variable.
23905 * Link "fc" in the list for garbage collection later. */
23906 fc->caller = previous_funccal;
23907 previous_funccal = fc;
23908
23909 /* Make a copy of the a: variables, since we didn't do that above. */
23910 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23911 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23912 {
23913 if (!HASHITEM_EMPTY(hi))
23914 {
23915 --todo;
23916 v = HI2DI(hi);
23917 copy_tv(&v->di_tv, &v->di_tv);
23918 }
23919 }
23920
23921 /* Make a copy of the a:000 items, since we didn't do that above. */
23922 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23923 copy_tv(&li->li_tv, &li->li_tv);
23924 }
23925}
23926
23927/*
23928 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023929 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023930 */
23931 static int
23932can_free_funccal(fc, copyID)
23933 funccall_T *fc;
23934 int copyID;
23935{
23936 return (fc->l_varlist.lv_copyID != copyID
23937 && fc->l_vars.dv_copyID != copyID
23938 && fc->l_avars.dv_copyID != copyID);
23939}
23940
23941/*
23942 * Free "fc" and what it contains.
23943 */
23944 static void
23945free_funccal(fc, free_val)
23946 funccall_T *fc;
23947 int free_val; /* a: vars were allocated */
23948{
23949 listitem_T *li;
23950
23951 /* The a: variables typevals may not have been allocated, only free the
23952 * allocated variables. */
23953 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23954
23955 /* free all l: variables */
23956 vars_clear(&fc->l_vars.dv_hashtab);
23957
23958 /* Free the a:000 variables if they were allocated. */
23959 if (free_val)
23960 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23961 clear_tv(&li->li_tv);
23962
23963 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964}
23965
23966/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023967 * Add a number variable "name" to dict "dp" with value "nr".
23968 */
23969 static void
23970add_nr_var(dp, v, name, nr)
23971 dict_T *dp;
23972 dictitem_T *v;
23973 char *name;
23974 varnumber_T nr;
23975{
23976 STRCPY(v->di_key, name);
23977 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23978 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23979 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023980 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023981 v->di_tv.vval.v_number = nr;
23982}
23983
23984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 * ":return [expr]"
23986 */
23987 void
23988ex_return(eap)
23989 exarg_T *eap;
23990{
23991 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023992 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 int returning = FALSE;
23994
23995 if (current_funccal == NULL)
23996 {
23997 EMSG(_("E133: :return not inside a function"));
23998 return;
23999 }
24000
24001 if (eap->skip)
24002 ++emsg_skip;
24003
24004 eap->nextcmd = NULL;
24005 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024006 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 {
24008 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024009 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024011 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 }
24013 /* It's safer to return also on error. */
24014 else if (!eap->skip)
24015 {
24016 /*
24017 * Return unless the expression evaluation has been cancelled due to an
24018 * aborting error, an interrupt, or an exception.
24019 */
24020 if (!aborting())
24021 returning = do_return(eap, FALSE, TRUE, NULL);
24022 }
24023
24024 /* When skipping or the return gets pending, advance to the next command
24025 * in this line (!returning). Otherwise, ignore the rest of the line.
24026 * Following lines will be ignored by get_func_line(). */
24027 if (returning)
24028 eap->nextcmd = NULL;
24029 else if (eap->nextcmd == NULL) /* no argument */
24030 eap->nextcmd = check_nextcmd(arg);
24031
24032 if (eap->skip)
24033 --emsg_skip;
24034}
24035
24036/*
24037 * Return from a function. Possibly makes the return pending. Also called
24038 * for a pending return at the ":endtry" or after returning from an extra
24039 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024040 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024041 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042 * FALSE when the return gets pending.
24043 */
24044 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024045do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 exarg_T *eap;
24047 int reanimate;
24048 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024049 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050{
24051 int idx;
24052 struct condstack *cstack = eap->cstack;
24053
24054 if (reanimate)
24055 /* Undo the return. */
24056 current_funccal->returned = FALSE;
24057
24058 /*
24059 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24060 * not in its finally clause (which then is to be executed next) is found.
24061 * In this case, make the ":return" pending for execution at the ":endtry".
24062 * Otherwise, return normally.
24063 */
24064 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24065 if (idx >= 0)
24066 {
24067 cstack->cs_pending[idx] = CSTP_RETURN;
24068
24069 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024070 /* A pending return again gets pending. "rettv" points to an
24071 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024073 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 else
24075 {
24076 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024077 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024079 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024081 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 {
24083 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024084 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024085 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 else
24087 EMSG(_(e_outofmem));
24088 }
24089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024090 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091
24092 if (reanimate)
24093 {
24094 /* The pending return value could be overwritten by a ":return"
24095 * without argument in a finally clause; reset the default
24096 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024097 current_funccal->rettv->v_type = VAR_NUMBER;
24098 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 }
24100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024101 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102 }
24103 else
24104 {
24105 current_funccal->returned = TRUE;
24106
24107 /* If the return is carried out now, store the return value. For
24108 * a return immediately after reanimation, the value is already
24109 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024110 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024112 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024113 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024115 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 }
24117 }
24118
24119 return idx < 0;
24120}
24121
24122/*
24123 * Free the variable with a pending return value.
24124 */
24125 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024126discard_pending_return(rettv)
24127 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128{
Bram Moolenaar33570922005-01-25 22:26:29 +000024129 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130}
24131
24132/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024133 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 * is an allocated string. Used by report_pending() for verbose messages.
24135 */
24136 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024137get_return_cmd(rettv)
24138 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024140 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024141 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024142 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024144 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024145 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024146 if (s == NULL)
24147 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024148
24149 STRCPY(IObuff, ":return ");
24150 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24151 if (STRLEN(s) + 8 >= IOSIZE)
24152 STRCPY(IObuff + IOSIZE - 4, "...");
24153 vim_free(tofree);
24154 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155}
24156
24157/*
24158 * Get next function line.
24159 * Called by do_cmdline() to get the next line.
24160 * Returns allocated string, or NULL for end of function.
24161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 char_u *
24163get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024164 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024166 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167{
Bram Moolenaar33570922005-01-25 22:26:29 +000024168 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024169 ufunc_T *fp = fcp->func;
24170 char_u *retval;
24171 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172
24173 /* If breakpoints have been added/deleted need to check for it. */
24174 if (fcp->dbg_tick != debug_tick)
24175 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024176 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 sourcing_lnum);
24178 fcp->dbg_tick = debug_tick;
24179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024180#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024181 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024182 func_line_end(cookie);
24183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184
Bram Moolenaar05159a02005-02-26 23:04:13 +000024185 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024186 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24187 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 retval = NULL;
24189 else
24190 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024191 /* Skip NULL lines (continuation lines). */
24192 while (fcp->linenr < gap->ga_len
24193 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24194 ++fcp->linenr;
24195 if (fcp->linenr >= gap->ga_len)
24196 retval = NULL;
24197 else
24198 {
24199 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24200 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024201#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024202 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024203 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024204#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 }
24207
24208 /* Did we encounter a breakpoint? */
24209 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24210 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024211 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024213 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 sourcing_lnum);
24215 fcp->dbg_tick = debug_tick;
24216 }
24217
24218 return retval;
24219}
24220
Bram Moolenaar05159a02005-02-26 23:04:13 +000024221#if defined(FEAT_PROFILE) || defined(PROTO)
24222/*
24223 * Called when starting to read a function line.
24224 * "sourcing_lnum" must be correct!
24225 * When skipping lines it may not actually be executed, but we won't find out
24226 * until later and we need to store the time now.
24227 */
24228 void
24229func_line_start(cookie)
24230 void *cookie;
24231{
24232 funccall_T *fcp = (funccall_T *)cookie;
24233 ufunc_T *fp = fcp->func;
24234
24235 if (fp->uf_profiling && sourcing_lnum >= 1
24236 && sourcing_lnum <= fp->uf_lines.ga_len)
24237 {
24238 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024239 /* Skip continuation lines. */
24240 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24241 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024242 fp->uf_tml_execed = FALSE;
24243 profile_start(&fp->uf_tml_start);
24244 profile_zero(&fp->uf_tml_children);
24245 profile_get_wait(&fp->uf_tml_wait);
24246 }
24247}
24248
24249/*
24250 * Called when actually executing a function line.
24251 */
24252 void
24253func_line_exec(cookie)
24254 void *cookie;
24255{
24256 funccall_T *fcp = (funccall_T *)cookie;
24257 ufunc_T *fp = fcp->func;
24258
24259 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24260 fp->uf_tml_execed = TRUE;
24261}
24262
24263/*
24264 * Called when done with a function line.
24265 */
24266 void
24267func_line_end(cookie)
24268 void *cookie;
24269{
24270 funccall_T *fcp = (funccall_T *)cookie;
24271 ufunc_T *fp = fcp->func;
24272
24273 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24274 {
24275 if (fp->uf_tml_execed)
24276 {
24277 ++fp->uf_tml_count[fp->uf_tml_idx];
24278 profile_end(&fp->uf_tml_start);
24279 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024280 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024281 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24282 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024283 }
24284 fp->uf_tml_idx = -1;
24285 }
24286}
24287#endif
24288
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289/*
24290 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024291 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 */
24293 int
24294func_has_ended(cookie)
24295 void *cookie;
24296{
Bram Moolenaar33570922005-01-25 22:26:29 +000024297 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298
24299 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24300 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024301 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 || fcp->returned);
24303}
24304
24305/*
24306 * return TRUE if cookie indicates a function which "abort"s on errors.
24307 */
24308 int
24309func_has_abort(cookie)
24310 void *cookie;
24311{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024312 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313}
24314
24315#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24316typedef enum
24317{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024318 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24319 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24320 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321} var_flavour_T;
24322
24323static var_flavour_T var_flavour __ARGS((char_u *varname));
24324
24325 static var_flavour_T
24326var_flavour(varname)
24327 char_u *varname;
24328{
24329 char_u *p = varname;
24330
24331 if (ASCII_ISUPPER(*p))
24332 {
24333 while (*(++p))
24334 if (ASCII_ISLOWER(*p))
24335 return VAR_FLAVOUR_SESSION;
24336 return VAR_FLAVOUR_VIMINFO;
24337 }
24338 else
24339 return VAR_FLAVOUR_DEFAULT;
24340}
24341#endif
24342
24343#if defined(FEAT_VIMINFO) || defined(PROTO)
24344/*
24345 * Restore global vars that start with a capital from the viminfo file
24346 */
24347 int
24348read_viminfo_varlist(virp, writing)
24349 vir_T *virp;
24350 int writing;
24351{
24352 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024353 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355
24356 if (!writing && (find_viminfo_parameter('!') != NULL))
24357 {
24358 tab = vim_strchr(virp->vir_line + 1, '\t');
24359 if (tab != NULL)
24360 {
24361 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024362 switch (*tab)
24363 {
24364 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024365#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024366 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024367#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024368 case 'D': type = VAR_DICT; break;
24369 case 'L': type = VAR_LIST; break;
24370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371
24372 tab = vim_strchr(tab, '\t');
24373 if (tab != NULL)
24374 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024375 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024376 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024377 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024379#ifdef FEAT_FLOAT
24380 else if (type == VAR_FLOAT)
24381 (void)string2float(tab + 1, &tv.vval.v_float);
24382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024384 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024385 if (type == VAR_DICT || type == VAR_LIST)
24386 {
24387 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24388
24389 if (etv == NULL)
24390 /* Failed to parse back the dict or list, use it as a
24391 * string. */
24392 tv.v_type = VAR_STRING;
24393 else
24394 {
24395 vim_free(tv.vval.v_string);
24396 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024397 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024398 }
24399 }
24400
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024401 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024402
24403 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024404 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024405 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24406 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 }
24408 }
24409 }
24410
24411 return viminfo_readline(virp);
24412}
24413
24414/*
24415 * Write global vars that start with a capital to the viminfo file
24416 */
24417 void
24418write_viminfo_varlist(fp)
24419 FILE *fp;
24420{
Bram Moolenaar33570922005-01-25 22:26:29 +000024421 hashitem_T *hi;
24422 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024423 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024424 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024425 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024426 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428
24429 if (find_viminfo_parameter('!') == NULL)
24430 return;
24431
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024432 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024433
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024434 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024435 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024437 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024439 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024440 this_var = HI2DI(hi);
24441 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024443 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024444 {
24445 case VAR_STRING: s = "STR"; break;
24446 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024447#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024448 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024449#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024450 case VAR_DICT: s = "DIC"; break;
24451 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024452 default: continue;
24453 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024454 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024455 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024456 if (p != NULL)
24457 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024458 vim_free(tofree);
24459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 }
24461 }
24462}
24463#endif
24464
24465#if defined(FEAT_SESSION) || defined(PROTO)
24466 int
24467store_session_globals(fd)
24468 FILE *fd;
24469{
Bram Moolenaar33570922005-01-25 22:26:29 +000024470 hashitem_T *hi;
24471 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024472 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 char_u *p, *t;
24474
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024475 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024476 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024478 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024480 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024481 this_var = HI2DI(hi);
24482 if ((this_var->di_tv.v_type == VAR_NUMBER
24483 || this_var->di_tv.v_type == VAR_STRING)
24484 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024485 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024486 /* Escape special characters with a backslash. Turn a LF and
24487 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024488 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024489 (char_u *)"\\\"\n\r");
24490 if (p == NULL) /* out of memory */
24491 break;
24492 for (t = p; *t != NUL; ++t)
24493 if (*t == '\n')
24494 *t = 'n';
24495 else if (*t == '\r')
24496 *t = 'r';
24497 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024498 this_var->di_key,
24499 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24500 : ' ',
24501 p,
24502 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24503 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024504 || put_eol(fd) == FAIL)
24505 {
24506 vim_free(p);
24507 return FAIL;
24508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 vim_free(p);
24510 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024511#ifdef FEAT_FLOAT
24512 else if (this_var->di_tv.v_type == VAR_FLOAT
24513 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24514 {
24515 float_T f = this_var->di_tv.vval.v_float;
24516 int sign = ' ';
24517
24518 if (f < 0)
24519 {
24520 f = -f;
24521 sign = '-';
24522 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024523 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024524 this_var->di_key, sign, f) < 0)
24525 || put_eol(fd) == FAIL)
24526 return FAIL;
24527 }
24528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 }
24530 }
24531 return OK;
24532}
24533#endif
24534
Bram Moolenaar661b1822005-07-28 22:36:45 +000024535/*
24536 * Display script name where an item was last set.
24537 * Should only be invoked when 'verbose' is non-zero.
24538 */
24539 void
24540last_set_msg(scriptID)
24541 scid_T scriptID;
24542{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024543 char_u *p;
24544
Bram Moolenaar661b1822005-07-28 22:36:45 +000024545 if (scriptID != 0)
24546 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024547 p = home_replace_save(NULL, get_scriptname(scriptID));
24548 if (p != NULL)
24549 {
24550 verbose_enter();
24551 MSG_PUTS(_("\n\tLast set from "));
24552 MSG_PUTS(p);
24553 vim_free(p);
24554 verbose_leave();
24555 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024556 }
24557}
24558
Bram Moolenaard812df62008-11-09 12:46:09 +000024559/*
24560 * List v:oldfiles in a nice way.
24561 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024562 void
24563ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024564 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024565{
24566 list_T *l = vimvars[VV_OLDFILES].vv_list;
24567 listitem_T *li;
24568 int nr = 0;
24569
24570 if (l == NULL)
24571 msg((char_u *)_("No old files"));
24572 else
24573 {
24574 msg_start();
24575 msg_scroll = TRUE;
24576 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24577 {
24578 msg_outnum((long)++nr);
24579 MSG_PUTS(": ");
24580 msg_outtrans(get_tv_string(&li->li_tv));
24581 msg_putchar('\n');
24582 out_flush(); /* output one line at a time */
24583 ui_breakcheck();
24584 }
24585 /* Assume "got_int" was set to truncate the listing. */
24586 got_int = FALSE;
24587
24588#ifdef FEAT_BROWSE_CMD
24589 if (cmdmod.browse)
24590 {
24591 quit_more = FALSE;
24592 nr = prompt_for_number(FALSE);
24593 msg_starthere();
24594 if (nr > 0)
24595 {
24596 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24597 (long)nr);
24598
24599 if (p != NULL)
24600 {
24601 p = expand_env_save(p);
24602 eap->arg = p;
24603 eap->cmdidx = CMD_edit;
24604 cmdmod.browse = FALSE;
24605 do_exedit(eap, NULL);
24606 vim_free(p);
24607 }
24608 }
24609 }
24610#endif
24611 }
24612}
24613
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614#endif /* FEAT_EVAL */
24615
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024617#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618
24619#ifdef WIN3264
24620/*
24621 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24622 */
24623static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24624static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24625static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24626
24627/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024628 * Get the short path (8.3) for the filename in "fnamep".
24629 * Only works for a valid file name.
24630 * When the path gets longer "fnamep" is changed and the allocated buffer
24631 * is put in "bufp".
24632 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24633 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 */
24635 static int
24636get_short_pathname(fnamep, bufp, fnamelen)
24637 char_u **fnamep;
24638 char_u **bufp;
24639 int *fnamelen;
24640{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024641 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 char_u *newbuf;
24643
24644 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 l = GetShortPathName(*fnamep, *fnamep, len);
24646 if (l > len - 1)
24647 {
24648 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024649 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 newbuf = vim_strnsave(*fnamep, l);
24651 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024652 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653
24654 vim_free(*bufp);
24655 *fnamep = *bufp = newbuf;
24656
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024657 /* Really should always succeed, as the buffer is big enough. */
24658 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 }
24660
24661 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024662 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663}
24664
24665/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024666 * Get the short path (8.3) for the filename in "fname". The converted
24667 * path is returned in "bufp".
24668 *
24669 * Some of the directories specified in "fname" may not exist. This function
24670 * will shorten the existing directories at the beginning of the path and then
24671 * append the remaining non-existing path.
24672 *
24673 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024674 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024675 * bufp - Pointer to an allocated buffer for the filename.
24676 * fnamelen - Length of the filename pointed to by fname
24677 *
24678 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 */
24680 static int
24681shortpath_for_invalid_fname(fname, bufp, fnamelen)
24682 char_u **fname;
24683 char_u **bufp;
24684 int *fnamelen;
24685{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024686 char_u *short_fname, *save_fname, *pbuf_unused;
24687 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024689 int old_len, len;
24690 int new_len, sfx_len;
24691 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692
24693 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024694 old_len = *fnamelen;
24695 save_fname = vim_strnsave(*fname, old_len);
24696 pbuf_unused = NULL;
24697 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024699 endp = save_fname + old_len - 1; /* Find the end of the copy */
24700 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024702 /*
24703 * Try shortening the supplied path till it succeeds by removing one
24704 * directory at a time from the tail of the path.
24705 */
24706 len = 0;
24707 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024709 /* go back one path-separator */
24710 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24711 --endp;
24712 if (endp <= save_fname)
24713 break; /* processed the complete path */
24714
24715 /*
24716 * Replace the path separator with a NUL and try to shorten the
24717 * resulting path.
24718 */
24719 ch = *endp;
24720 *endp = 0;
24721 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024722 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024723 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24724 {
24725 retval = FAIL;
24726 goto theend;
24727 }
24728 *endp = ch; /* preserve the string */
24729
24730 if (len > 0)
24731 break; /* successfully shortened the path */
24732
24733 /* failed to shorten the path. Skip the path separator */
24734 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735 }
24736
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024737 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024739 /*
24740 * Succeeded in shortening the path. Now concatenate the shortened
24741 * path with the remaining path at the tail.
24742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024744 /* Compute the length of the new path. */
24745 sfx_len = (int)(save_endp - endp) + 1;
24746 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024748 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024750 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024752 /* There is not enough space in the currently allocated string,
24753 * copy it to a buffer big enough. */
24754 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024756 {
24757 retval = FAIL;
24758 goto theend;
24759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024760 }
24761 else
24762 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024763 /* Transfer short_fname to the main buffer (it's big enough),
24764 * unless get_short_pathname() did its work in-place. */
24765 *fname = *bufp = save_fname;
24766 if (short_fname != save_fname)
24767 vim_strncpy(save_fname, short_fname, len);
24768 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024770
24771 /* concat the not-shortened part of the path */
24772 vim_strncpy(*fname + len, endp, sfx_len);
24773 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024775
24776theend:
24777 vim_free(pbuf_unused);
24778 vim_free(save_fname);
24779
24780 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781}
24782
24783/*
24784 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024785 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 */
24787 static int
24788shortpath_for_partial(fnamep, bufp, fnamelen)
24789 char_u **fnamep;
24790 char_u **bufp;
24791 int *fnamelen;
24792{
24793 int sepcount, len, tflen;
24794 char_u *p;
24795 char_u *pbuf, *tfname;
24796 int hasTilde;
24797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024798 /* Count up the path separators from the RHS.. so we know which part
24799 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024801 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 if (vim_ispathsep(*p))
24803 ++sepcount;
24804
24805 /* Need full path first (use expand_env() to remove a "~/") */
24806 hasTilde = (**fnamep == '~');
24807 if (hasTilde)
24808 pbuf = tfname = expand_env_save(*fnamep);
24809 else
24810 pbuf = tfname = FullName_save(*fnamep, FALSE);
24811
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024812 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024814 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816
24817 if (len == 0)
24818 {
24819 /* Don't have a valid filename, so shorten the rest of the
24820 * path if we can. This CAN give us invalid 8.3 filenames, but
24821 * there's not a lot of point in guessing what it might be.
24822 */
24823 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024824 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24825 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826 }
24827
24828 /* Count the paths backward to find the beginning of the desired string. */
24829 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024830 {
24831#ifdef FEAT_MBYTE
24832 if (has_mbyte)
24833 p -= mb_head_off(tfname, p);
24834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835 if (vim_ispathsep(*p))
24836 {
24837 if (sepcount == 0 || (hasTilde && sepcount == 1))
24838 break;
24839 else
24840 sepcount --;
24841 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 if (hasTilde)
24844 {
24845 --p;
24846 if (p >= tfname)
24847 *p = '~';
24848 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 }
24851 else
24852 ++p;
24853
24854 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24855 vim_free(*bufp);
24856 *fnamelen = (int)STRLEN(p);
24857 *bufp = pbuf;
24858 *fnamep = p;
24859
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024860 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861}
24862#endif /* WIN3264 */
24863
24864/*
24865 * Adjust a filename, according to a string of modifiers.
24866 * *fnamep must be NUL terminated when called. When returning, the length is
24867 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024868 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 * When there is an error, *fnamep is set to NULL.
24870 */
24871 int
24872modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24873 char_u *src; /* string with modifiers */
24874 int *usedlen; /* characters after src that are used */
24875 char_u **fnamep; /* file name so far */
24876 char_u **bufp; /* buffer for allocated file name or NULL */
24877 int *fnamelen; /* length of fnamep */
24878{
24879 int valid = 0;
24880 char_u *tail;
24881 char_u *s, *p, *pbuf;
24882 char_u dirname[MAXPATHL];
24883 int c;
24884 int has_fullname = 0;
24885#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024886 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 int has_shortname = 0;
24888#endif
24889
24890repeat:
24891 /* ":p" - full path/file_name */
24892 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24893 {
24894 has_fullname = 1;
24895
24896 valid |= VALID_PATH;
24897 *usedlen += 2;
24898
24899 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24900 if ((*fnamep)[0] == '~'
24901#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24902 && ((*fnamep)[1] == '/'
24903# ifdef BACKSLASH_IN_FILENAME
24904 || (*fnamep)[1] == '\\'
24905# endif
24906 || (*fnamep)[1] == NUL)
24907
24908#endif
24909 )
24910 {
24911 *fnamep = expand_env_save(*fnamep);
24912 vim_free(*bufp); /* free any allocated file name */
24913 *bufp = *fnamep;
24914 if (*fnamep == NULL)
24915 return -1;
24916 }
24917
24918 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024919 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 {
24921 if (vim_ispathsep(*p)
24922 && p[1] == '.'
24923 && (p[2] == NUL
24924 || vim_ispathsep(p[2])
24925 || (p[2] == '.'
24926 && (p[3] == NUL || vim_ispathsep(p[3])))))
24927 break;
24928 }
24929
24930 /* FullName_save() is slow, don't use it when not needed. */
24931 if (*p != NUL || !vim_isAbsName(*fnamep))
24932 {
24933 *fnamep = FullName_save(*fnamep, *p != NUL);
24934 vim_free(*bufp); /* free any allocated file name */
24935 *bufp = *fnamep;
24936 if (*fnamep == NULL)
24937 return -1;
24938 }
24939
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024940#ifdef WIN3264
24941# if _WIN32_WINNT >= 0x0500
24942 if (vim_strchr(*fnamep, '~') != NULL)
24943 {
24944 /* Expand 8.3 filename to full path. Needed to make sure the same
24945 * file does not have two different names.
24946 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24947 p = alloc(_MAX_PATH + 1);
24948 if (p != NULL)
24949 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024950 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024951 {
24952 vim_free(*bufp);
24953 *bufp = *fnamep = p;
24954 }
24955 else
24956 vim_free(p);
24957 }
24958 }
24959# endif
24960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 /* Append a path separator to a directory. */
24962 if (mch_isdir(*fnamep))
24963 {
24964 /* Make room for one or two extra characters. */
24965 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24966 vim_free(*bufp); /* free any allocated file name */
24967 *bufp = *fnamep;
24968 if (*fnamep == NULL)
24969 return -1;
24970 add_pathsep(*fnamep);
24971 }
24972 }
24973
24974 /* ":." - path relative to the current directory */
24975 /* ":~" - path relative to the home directory */
24976 /* ":8" - shortname path - postponed till after */
24977 while (src[*usedlen] == ':'
24978 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24979 {
24980 *usedlen += 2;
24981 if (c == '8')
24982 {
24983#ifdef WIN3264
24984 has_shortname = 1; /* Postpone this. */
24985#endif
24986 continue;
24987 }
24988 pbuf = NULL;
24989 /* Need full path first (use expand_env() to remove a "~/") */
24990 if (!has_fullname)
24991 {
24992 if (c == '.' && **fnamep == '~')
24993 p = pbuf = expand_env_save(*fnamep);
24994 else
24995 p = pbuf = FullName_save(*fnamep, FALSE);
24996 }
24997 else
24998 p = *fnamep;
24999
25000 has_fullname = 0;
25001
25002 if (p != NULL)
25003 {
25004 if (c == '.')
25005 {
25006 mch_dirname(dirname, MAXPATHL);
25007 s = shorten_fname(p, dirname);
25008 if (s != NULL)
25009 {
25010 *fnamep = s;
25011 if (pbuf != NULL)
25012 {
25013 vim_free(*bufp); /* free any allocated file name */
25014 *bufp = pbuf;
25015 pbuf = NULL;
25016 }
25017 }
25018 }
25019 else
25020 {
25021 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25022 /* Only replace it when it starts with '~' */
25023 if (*dirname == '~')
25024 {
25025 s = vim_strsave(dirname);
25026 if (s != NULL)
25027 {
25028 *fnamep = s;
25029 vim_free(*bufp);
25030 *bufp = s;
25031 }
25032 }
25033 }
25034 vim_free(pbuf);
25035 }
25036 }
25037
25038 tail = gettail(*fnamep);
25039 *fnamelen = (int)STRLEN(*fnamep);
25040
25041 /* ":h" - head, remove "/file_name", can be repeated */
25042 /* Don't remove the first "/" or "c:\" */
25043 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25044 {
25045 valid |= VALID_HEAD;
25046 *usedlen += 2;
25047 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025048 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025049 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 *fnamelen = (int)(tail - *fnamep);
25051#ifdef VMS
25052 if (*fnamelen > 0)
25053 *fnamelen += 1; /* the path separator is part of the path */
25054#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025055 if (*fnamelen == 0)
25056 {
25057 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25058 p = vim_strsave((char_u *)".");
25059 if (p == NULL)
25060 return -1;
25061 vim_free(*bufp);
25062 *bufp = *fnamep = tail = p;
25063 *fnamelen = 1;
25064 }
25065 else
25066 {
25067 while (tail > s && !after_pathsep(s, tail))
25068 mb_ptr_back(*fnamep, tail);
25069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 }
25071
25072 /* ":8" - shortname */
25073 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25074 {
25075 *usedlen += 2;
25076#ifdef WIN3264
25077 has_shortname = 1;
25078#endif
25079 }
25080
25081#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025082 /*
25083 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 */
25085 if (has_shortname)
25086 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025087 /* Copy the string if it is shortened by :h and when it wasn't copied
25088 * yet, because we are going to change it in place. Avoids changing
25089 * the buffer name for "%:8". */
25090 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 {
25092 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025093 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025094 return -1;
25095 vim_free(*bufp);
25096 *bufp = *fnamep = p;
25097 }
25098
25099 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025100 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 if (!has_fullname && !vim_isAbsName(*fnamep))
25102 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025103 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 return -1;
25105 }
25106 else
25107 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025108 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109
Bram Moolenaardc935552011-08-17 15:23:23 +020025110 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025112 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 return -1;
25114
25115 if (l == 0)
25116 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025117 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025118 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025119 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 return -1;
25121 }
25122 *fnamelen = l;
25123 }
25124 }
25125#endif /* WIN3264 */
25126
25127 /* ":t" - tail, just the basename */
25128 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25129 {
25130 *usedlen += 2;
25131 *fnamelen -= (int)(tail - *fnamep);
25132 *fnamep = tail;
25133 }
25134
25135 /* ":e" - extension, can be repeated */
25136 /* ":r" - root, without extension, can be repeated */
25137 while (src[*usedlen] == ':'
25138 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25139 {
25140 /* find a '.' in the tail:
25141 * - for second :e: before the current fname
25142 * - otherwise: The last '.'
25143 */
25144 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25145 s = *fnamep - 2;
25146 else
25147 s = *fnamep + *fnamelen - 1;
25148 for ( ; s > tail; --s)
25149 if (s[0] == '.')
25150 break;
25151 if (src[*usedlen + 1] == 'e') /* :e */
25152 {
25153 if (s > tail)
25154 {
25155 *fnamelen += (int)(*fnamep - (s + 1));
25156 *fnamep = s + 1;
25157#ifdef VMS
25158 /* cut version from the extension */
25159 s = *fnamep + *fnamelen - 1;
25160 for ( ; s > *fnamep; --s)
25161 if (s[0] == ';')
25162 break;
25163 if (s > *fnamep)
25164 *fnamelen = s - *fnamep;
25165#endif
25166 }
25167 else if (*fnamep <= tail)
25168 *fnamelen = 0;
25169 }
25170 else /* :r */
25171 {
25172 if (s > tail) /* remove one extension */
25173 *fnamelen = (int)(s - *fnamep);
25174 }
25175 *usedlen += 2;
25176 }
25177
25178 /* ":s?pat?foo?" - substitute */
25179 /* ":gs?pat?foo?" - global substitute */
25180 if (src[*usedlen] == ':'
25181 && (src[*usedlen + 1] == 's'
25182 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25183 {
25184 char_u *str;
25185 char_u *pat;
25186 char_u *sub;
25187 int sep;
25188 char_u *flags;
25189 int didit = FALSE;
25190
25191 flags = (char_u *)"";
25192 s = src + *usedlen + 2;
25193 if (src[*usedlen + 1] == 'g')
25194 {
25195 flags = (char_u *)"g";
25196 ++s;
25197 }
25198
25199 sep = *s++;
25200 if (sep)
25201 {
25202 /* find end of pattern */
25203 p = vim_strchr(s, sep);
25204 if (p != NULL)
25205 {
25206 pat = vim_strnsave(s, (int)(p - s));
25207 if (pat != NULL)
25208 {
25209 s = p + 1;
25210 /* find end of substitution */
25211 p = vim_strchr(s, sep);
25212 if (p != NULL)
25213 {
25214 sub = vim_strnsave(s, (int)(p - s));
25215 str = vim_strnsave(*fnamep, *fnamelen);
25216 if (sub != NULL && str != NULL)
25217 {
25218 *usedlen = (int)(p + 1 - src);
25219 s = do_string_sub(str, pat, sub, flags);
25220 if (s != NULL)
25221 {
25222 *fnamep = s;
25223 *fnamelen = (int)STRLEN(s);
25224 vim_free(*bufp);
25225 *bufp = s;
25226 didit = TRUE;
25227 }
25228 }
25229 vim_free(sub);
25230 vim_free(str);
25231 }
25232 vim_free(pat);
25233 }
25234 }
25235 /* after using ":s", repeat all the modifiers */
25236 if (didit)
25237 goto repeat;
25238 }
25239 }
25240
Bram Moolenaar26df0922014-02-23 23:39:13 +010025241 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25242 {
25243 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25244 if (p == NULL)
25245 return -1;
25246 vim_free(*bufp);
25247 *bufp = *fnamep = p;
25248 *fnamelen = (int)STRLEN(p);
25249 *usedlen += 2;
25250 }
25251
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 return valid;
25253}
25254
25255/*
25256 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25257 * "flags" can be "g" to do a global substitute.
25258 * Returns an allocated string, NULL for error.
25259 */
25260 char_u *
25261do_string_sub(str, pat, sub, flags)
25262 char_u *str;
25263 char_u *pat;
25264 char_u *sub;
25265 char_u *flags;
25266{
25267 int sublen;
25268 regmatch_T regmatch;
25269 int i;
25270 int do_all;
25271 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025272 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 garray_T ga;
25274 char_u *ret;
25275 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025276 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277
25278 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25279 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025280 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281
25282 ga_init2(&ga, 1, 200);
25283
25284 do_all = (flags[0] == 'g');
25285
25286 regmatch.rm_ic = p_ic;
25287 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25288 if (regmatch.regprog != NULL)
25289 {
25290 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025291 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25293 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025294 /* Skip empty match except for first match. */
25295 if (regmatch.startp[0] == regmatch.endp[0])
25296 {
25297 if (zero_width == regmatch.startp[0])
25298 {
25299 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025300 i = MB_PTR2LEN(tail);
25301 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25302 (size_t)i);
25303 ga.ga_len += i;
25304 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025305 continue;
25306 }
25307 zero_width = regmatch.startp[0];
25308 }
25309
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 /*
25311 * Get some space for a temporary buffer to do the substitution
25312 * into. It will contain:
25313 * - The text up to where the match is.
25314 * - The substituted text.
25315 * - The text after the match.
25316 */
25317 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025318 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25320 {
25321 ga_clear(&ga);
25322 break;
25323 }
25324
25325 /* copy the text up to where the match is */
25326 i = (int)(regmatch.startp[0] - tail);
25327 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25328 /* add the substituted text */
25329 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25330 + ga.ga_len + i, TRUE, TRUE, FALSE);
25331 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025332 tail = regmatch.endp[0];
25333 if (*tail == NUL)
25334 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 if (!do_all)
25336 break;
25337 }
25338
25339 if (ga.ga_data != NULL)
25340 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25341
Bram Moolenaar473de612013-06-08 18:19:48 +020025342 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 }
25344
25345 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25346 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025347 if (p_cpo == empty_option)
25348 p_cpo = save_cpo;
25349 else
25350 /* Darn, evaluating {sub} expression changed the value. */
25351 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352
25353 return ret;
25354}
25355
25356#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */