blob: b80762e12d7f25f88128c5161ed8e3a64195d0da [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 Moolenaard39a7512015-04-16 22:51:22 +02006783 if (l->lv_len < 1)
6784 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6786 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6787
6788 /* Dispose each item in join_ga. */
6789 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 p = (join_T *)join_ga.ga_data;
6792 for (i = 0; i < join_ga.ga_len; ++i)
6793 {
6794 vim_free(p->tofree);
6795 ++p;
6796 }
6797 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006799
6800 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801}
6802
6803/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804 * Garbage collection for lists and dictionaries.
6805 *
6806 * We use reference counts to be able to free most items right away when they
6807 * are no longer used. But for composite items it's possible that it becomes
6808 * unused while the reference count is > 0: When there is a recursive
6809 * reference. Example:
6810 * :let l = [1, 2, 3]
6811 * :let d = {9: l}
6812 * :let l[1] = d
6813 *
6814 * Since this is quite unusual we handle this with garbage collection: every
6815 * once in a while find out which lists and dicts are not referenced from any
6816 * variable.
6817 *
6818 * Here is a good reference text about garbage collection (refers to Python
6819 * but it applies to all reference-counting mechanisms):
6820 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006821 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822
6823/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824 * Do garbage collection for lists and dicts.
6825 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 int
6828garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006831 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 buf_T *buf;
6833 win_T *wp;
6834 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006835 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006836 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006838#ifdef FEAT_WINDOWS
6839 tabpage_T *tp;
6840#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006842 /* Only do this once. */
6843 want_garbage_collect = FALSE;
6844 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006845 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006846
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 /* We advance by two because we add one for items referenced through
6848 * previous_funccal. */
6849 current_copyID += COPYID_INC;
6850 copyID = current_copyID;
6851
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 /*
6853 * 1. Go through all accessible variables and mark all lists and dicts
6854 * with copyID.
6855 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856
6857 /* Don't free variables in the previous_funccal list unless they are only
6858 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006859 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6861 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6863 NULL);
6864 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6865 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 }
6867
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868 /* script-local variables */
6869 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871
6872 /* buffer-local variables */
6873 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6875 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876
6877 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6880 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006881#ifdef FEAT_AUTOCMD
6882 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006885#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887#ifdef FEAT_WINDOWS
6888 /* tabpage-local variables */
6889 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6891 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006892#endif
6893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
6897 /* function-local variables */
6898 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6899 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6901 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 }
6903
Bram Moolenaard812df62008-11-09 12:46:09 +00006904 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006906
Bram Moolenaar1dced572012-04-05 16:54:08 +02006907#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006909#endif
6910
Bram Moolenaardb913952012-06-29 12:54:53 +02006911#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006913#endif
6914
6915#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#endif
6918
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006920 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 /*
6922 * 2. Free lists and dictionaries that are not referenced.
6923 */
6924 did_free = free_unref_items(copyID);
6925
6926 /*
6927 * 3. Check if any funccal can be freed now.
6928 */
6929 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 if (can_free_funccal(*pfc, copyID))
6932 {
6933 fc = *pfc;
6934 *pfc = fc->caller;
6935 free_funccal(fc, TRUE);
6936 did_free = TRUE;
6937 did_free_funccal = TRUE;
6938 }
6939 else
6940 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 if (did_free_funccal)
6943 /* When a funccal was freed some more items might be garbage
6944 * collected, so run again. */
6945 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 else if (p_verbose > 0)
6948 {
6949 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6950 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951
6952 return did_free;
6953}
6954
6955/*
6956 * Free lists and dictionaries that are no longer referenced.
6957 */
6958 static int
6959free_unref_items(copyID)
6960 int copyID;
6961{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006962 dict_T *dd, *dd_next;
6963 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 int did_free = FALSE;
6965
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 */
6969 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006970 {
6971 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006974 /* Free the Dictionary and ordinary items it contains, but don't
6975 * recurse into Lists and Dictionaries, they will be in the list
6976 * of dicts or list of lists. */
6977 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 dd = dd_next;
6981 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
6983 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 * Go through the list of lists and free items without the copyID.
6985 * But don't free a list that has a watcher (used in a for loop), these
6986 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 */
6988 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 {
6990 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6992 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006994 /* Free the List and ordinary items it contains, but don't recurse
6995 * into Lists and Dictionaries, they will be in the list of dicts
6996 * or list of lists. */
6997 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007000 ll = ll_next;
7001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 return did_free;
7003}
7004
7005/*
7006 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 * "list_stack" is used to add lists to be marked. Can be NULL.
7008 *
7009 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 int
7012set_ref_in_ht(ht, copyID, list_stack)
7013 hashtab_T *ht;
7014 int copyID;
7015 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016{
7017 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 hashtab_T *cur_ht;
7021 ht_stack_T *ht_stack = NULL;
7022 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 cur_ht = ht;
7025 for (;;)
7026 {
7027 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 /* Mark each item in the hashtab. If the item contains a hashtab
7030 * it is added to ht_stack, if it contains a list it is added to
7031 * list_stack. */
7032 todo = (int)cur_ht->ht_used;
7033 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7034 if (!HASHITEM_EMPTY(hi))
7035 {
7036 --todo;
7037 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7038 &ht_stack, list_stack);
7039 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041
7042 if (ht_stack == NULL)
7043 break;
7044
7045 /* take an item from the stack */
7046 cur_ht = ht_stack->ht;
7047 tempitem = ht_stack;
7048 ht_stack = ht_stack->prev;
7049 free(tempitem);
7050 }
7051
7052 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053}
7054
7055/*
7056 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7058 *
7059 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 int
7062set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 list_T *l;
7064 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 listitem_T *li;
7068 int abort = FALSE;
7069 list_T *cur_l;
7070 list_stack_T *list_stack = NULL;
7071 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 cur_l = l;
7074 for (;;)
7075 {
7076 if (!abort)
7077 /* Mark each item in the list. If the item contains a hashtab
7078 * it is added to ht_stack, if it contains a list it is added to
7079 * list_stack. */
7080 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7081 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7082 ht_stack, &list_stack);
7083 if (list_stack == NULL)
7084 break;
7085
7086 /* take an item from the stack */
7087 cur_l = list_stack->list;
7088 tempitem = list_stack;
7089 list_stack = list_stack->prev;
7090 free(tempitem);
7091 }
7092
7093 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094}
7095
7096/*
7097 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 * "list_stack" is used to add lists to be marked. Can be NULL.
7099 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7100 *
7101 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 int
7104set_ref_in_item(tv, copyID, ht_stack, list_stack)
7105 typval_T *tv;
7106 int copyID;
7107 ht_stack_T **ht_stack;
7108 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109{
7110 dict_T *dd;
7111 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113
7114 switch (tv->v_type)
7115 {
7116 case VAR_DICT:
7117 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007118 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007119 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 /* Didn't see this dict yet. */
7121 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 if (ht_stack == NULL)
7123 {
7124 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7125 }
7126 else
7127 {
7128 ht_stack_T *newitem = (ht_stack_T*)malloc(
7129 sizeof(ht_stack_T));
7130 if (newitem == NULL)
7131 abort = TRUE;
7132 else
7133 {
7134 newitem->ht = &dd->dv_hashtab;
7135 newitem->prev = *ht_stack;
7136 *ht_stack = newitem;
7137 }
7138 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007139 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007140 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141
7142 case VAR_LIST:
7143 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007144 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007145 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007146 /* Didn't see this list yet. */
7147 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007148 if (list_stack == NULL)
7149 {
7150 abort = set_ref_in_list(ll, copyID, ht_stack);
7151 }
7152 else
7153 {
7154 list_stack_T *newitem = (list_stack_T*)malloc(
7155 sizeof(list_stack_T));
7156 if (newitem == NULL)
7157 abort = TRUE;
7158 else
7159 {
7160 newitem->list = ll;
7161 newitem->prev = *list_stack;
7162 *list_stack = newitem;
7163 }
7164 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169}
7170
7171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 * Allocate an empty header for a dictionary.
7173 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007174 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175dict_alloc()
7176{
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007181 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007182 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007183 if (first_dict != NULL)
7184 first_dict->dv_used_prev = d;
7185 d->dv_used_next = first_dict;
7186 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007187 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007190 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007191 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007192 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196}
7197
7198/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007199 * Allocate an empty dict for a return value.
7200 * Returns OK or FAIL.
7201 */
7202 static int
7203rettv_dict_alloc(rettv)
7204 typval_T *rettv;
7205{
7206 dict_T *d = dict_alloc();
7207
7208 if (d == NULL)
7209 return FAIL;
7210
7211 rettv->vval.v_dict = d;
7212 rettv->v_type = VAR_DICT;
7213 ++d->dv_refcount;
7214 return OK;
7215}
7216
7217
7218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 * Unreference a Dictionary: decrement the reference count and free it when it
7220 * becomes zero.
7221 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007222 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007226 if (d != NULL && --d->dv_refcount <= 0)
7227 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228}
7229
7230/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007231 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 * Ignores the reference count.
7233 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007234 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007235dict_free(d, recurse)
7236 dict_T *d;
7237 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007241 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243 /* Remove the dict from the list of dicts for garbage collection. */
7244 if (d->dv_used_prev == NULL)
7245 first_dict = d->dv_used_next;
7246 else
7247 d->dv_used_prev->dv_used_next = d->dv_used_next;
7248 if (d->dv_used_next != NULL)
7249 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7250
7251 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007252 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007253 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (!HASHITEM_EMPTY(hi))
7257 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007258 /* Remove the item before deleting it, just in case there is
7259 * something recursive causing trouble. */
7260 di = HI2DI(hi);
7261 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262 if (recurse || (di->di_tv.v_type != VAR_LIST
7263 && di->di_tv.v_type != VAR_DICT))
7264 clear_tv(&di->di_tv);
7265 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 --todo;
7267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 vim_free(d);
7271}
7272
7273/*
7274 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 * The "key" is copied to the new item.
7276 * Note that the value of the item "di_tv" still needs to be initialized!
7277 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007279 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280dictitem_alloc(key)
7281 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007289 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292}
7293
7294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 * Make a copy of a Dictionary item.
7296 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7304 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (di != NULL)
7306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 copy_tv(&org->di_tv, &di->di_tv);
7310 }
7311 return di;
7312}
7313
7314/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 * Remove item "item" from Dictionary "dict" and free it.
7316 */
7317 static void
7318dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dict_T *dict;
7320 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321{
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if (HASHITEM_EMPTY(hi))
7326 EMSG2(_(e_intern2), "dictitem_remove()");
7327 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 dictitem_free(item);
7330}
7331
7332/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 * Free a dict item. Also clears the value.
7334 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007335 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007340 if (item->di_flags & DI_FLAGS_ALLOC)
7341 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342}
7343
7344/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7346 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007347 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 * Returns NULL when out of memory.
7349 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007351dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 dict_T *copy;
7357 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360
7361 if (orig == NULL)
7362 return NULL;
7363
7364 copy = dict_alloc();
7365 if (copy != NULL)
7366 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 if (copyID != 0)
7368 {
7369 orig->dv_copyID = copyID;
7370 orig->dv_copydict = copy;
7371 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007372 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007373 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 --todo;
7378
7379 di = dictitem_alloc(hi->hi_key);
7380 if (di == NULL)
7381 break;
7382 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 {
7384 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7385 copyID) == FAIL)
7386 {
7387 vim_free(di);
7388 break;
7389 }
7390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 else
7392 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7393 if (dict_add(copy, di) == FAIL)
7394 {
7395 dictitem_free(di);
7396 break;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007402 if (todo > 0)
7403 {
7404 dict_unref(copy);
7405 copy = NULL;
7406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407 }
7408
7409 return copy;
7410}
7411
7412/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007414 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007416 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 dict_T *d;
7419 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420{
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422}
7423
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007425 * Add a number or string entry to dictionary "d".
7426 * When "str" is NULL use number "nr", otherwise use "str".
7427 * Returns FAIL when out of memory and when key already exists.
7428 */
7429 int
7430dict_add_nr_str(d, key, nr, str)
7431 dict_T *d;
7432 char *key;
7433 long nr;
7434 char_u *str;
7435{
7436 dictitem_T *item;
7437
7438 item = dictitem_alloc((char_u *)key);
7439 if (item == NULL)
7440 return FAIL;
7441 item->di_tv.v_lock = 0;
7442 if (str == NULL)
7443 {
7444 item->di_tv.v_type = VAR_NUMBER;
7445 item->di_tv.vval.v_number = nr;
7446 }
7447 else
7448 {
7449 item->di_tv.v_type = VAR_STRING;
7450 item->di_tv.vval.v_string = vim_strsave(str);
7451 }
7452 if (dict_add(d, item) == FAIL)
7453 {
7454 dictitem_free(item);
7455 return FAIL;
7456 }
7457 return OK;
7458}
7459
7460/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007461 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007462 * Returns FAIL when out of memory and when key already exists.
7463 */
7464 int
7465dict_add_list(d, key, list)
7466 dict_T *d;
7467 char *key;
7468 list_T *list;
7469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 item->di_tv.v_type = VAR_LIST;
7477 item->di_tv.vval.v_list = list;
7478 if (dict_add(d, item) == FAIL)
7479 {
7480 dictitem_free(item);
7481 return FAIL;
7482 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007483 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007484 return OK;
7485}
7486
7487/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 * Get the number of items in a Dictionary.
7489 */
7490 static long
7491dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 if (d == NULL)
7495 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007496 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497}
7498
7499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 * Find item "key[len]" in Dictionary "d".
7501 * If "len" is negative use strlen(key).
7502 * Returns NULL when not found.
7503 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007504 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 char_u *key;
7508 int len;
7509{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510#define AKEYLEN 200
7511 char_u buf[AKEYLEN];
7512 char_u *akey;
7513 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 if (len < 0)
7517 akey = key;
7518 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 tofree = akey = vim_strnsave(key, len);
7521 if (akey == NULL)
7522 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 else
7525 {
7526 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007527 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528 akey = buf;
7529 }
7530
Bram Moolenaar33570922005-01-25 22:26:29 +00007531 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 vim_free(tofree);
7533 if (HASHITEM_EMPTY(hi))
7534 return NULL;
7535 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536}
7537
7538/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007539 * Get a string item from a dictionary.
7540 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541 * Returns NULL if the entry doesn't exist or out of memory.
7542 */
7543 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007544get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007545 dict_T *d;
7546 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007547 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007548{
7549 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007550 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007555 s = get_tv_string(&di->di_tv);
7556 if (save && s != NULL)
7557 s = vim_strsave(s);
7558 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007559}
7560
7561/*
7562 * Get a number item from a dictionary.
7563 * Returns 0 if the entry doesn't exist or out of memory.
7564 */
7565 long
7566get_dict_number(d, key)
7567 dict_T *d;
7568 char_u *key;
7569{
7570 dictitem_T *di;
7571
7572 di = dict_find(d, key, -1);
7573 if (di == NULL)
7574 return 0;
7575 return get_tv_number(&di->di_tv);
7576}
7577
7578/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 * Return an allocated string with the string representation of a Dictionary.
7580 * May return NULL.
7581 */
7582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007585 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586{
7587 garray_T ga;
7588 int first = TRUE;
7589 char_u *tofree;
7590 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 return NULL;
7598 ga_init2(&ga, (int)sizeof(char), 80);
7599 ga_append(&ga, '{');
7600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007601 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007602 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 --todo;
7607
7608 if (first)
7609 first = FALSE;
7610 else
7611 ga_concat(&ga, (char_u *)", ");
7612
7613 tofree = string_quote(hi->hi_key, FALSE);
7614 if (tofree != NULL)
7615 {
7616 ga_concat(&ga, tofree);
7617 vim_free(tofree);
7618 }
7619 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 if (s != NULL)
7622 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007624 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007626 line_breakcheck();
7627
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007630 if (todo > 0)
7631 {
7632 vim_free(ga.ga_data);
7633 return NULL;
7634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635
7636 ga_append(&ga, '}');
7637 ga_append(&ga, NUL);
7638 return (char_u *)ga.ga_data;
7639}
7640
7641/*
7642 * Allocate a variable for a Dictionary and fill it from "*arg".
7643 * Return OK or FAIL. Returns NOTDONE for {expr}.
7644 */
7645 static int
7646get_dict_tv(arg, rettv, evaluate)
7647 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 int evaluate;
7650{
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 dict_T *d = NULL;
7652 typval_T tvkey;
7653 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007654 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 /*
7660 * First check if it's not a curly-braces thing: {expr}.
7661 * Must do this without evaluating, otherwise a function may be called
7662 * twice. Unfortunately this means we need to call eval1() twice for the
7663 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 if (*start != '}')
7667 {
7668 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7669 return FAIL;
7670 if (*start == '}')
7671 return NOTDONE;
7672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673
7674 if (evaluate)
7675 {
7676 d = dict_alloc();
7677 if (d == NULL)
7678 return FAIL;
7679 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007680 tvkey.v_type = VAR_UNKNOWN;
7681 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682
7683 *arg = skipwhite(*arg + 1);
7684 while (**arg != '}' && **arg != NUL)
7685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 goto failret;
7688 if (**arg != ':')
7689 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007690 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 goto failret;
7693 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007694 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007696 key = get_tv_string_buf_chk(&tvkey, buf);
7697 if (key == NULL || *key == NUL)
7698 {
7699 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7700 if (key != NULL)
7701 EMSG(_(e_emptykey));
7702 clear_tv(&tvkey);
7703 goto failret;
7704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706
7707 *arg = skipwhite(*arg + 1);
7708 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7709 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007710 if (evaluate)
7711 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 goto failret;
7713 }
7714 if (evaluate)
7715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 if (item != NULL)
7718 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007719 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 clear_tv(&tv);
7722 goto failret;
7723 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 item = dictitem_alloc(key);
7725 clear_tv(&tvkey);
7726 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007729 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 if (dict_add(d, item) == FAIL)
7731 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 }
7733 }
7734
7735 if (**arg == '}')
7736 break;
7737 if (**arg != ',')
7738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007739 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 goto failret;
7741 }
7742 *arg = skipwhite(*arg + 1);
7743 }
7744
7745 if (**arg != '}')
7746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007747 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748failret:
7749 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007750 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 return FAIL;
7752 }
7753
7754 *arg = skipwhite(*arg + 1);
7755 if (evaluate)
7756 {
7757 rettv->v_type = VAR_DICT;
7758 rettv->vval.v_dict = d;
7759 ++d->dv_refcount;
7760 }
7761
7762 return OK;
7763}
7764
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 * Return a string with the string representation of a variable.
7767 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007768 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007770 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007771 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772 */
7773 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007774echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007777 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007778 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007780 static int recurse = 0;
7781 char_u *r = NULL;
7782
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007785 if (!did_echo_string_emsg)
7786 {
7787 /* Only give this message once for a recursive call to avoid
7788 * flooding the user with errors. And stop iterating over lists
7789 * and dicts. */
7790 did_echo_string_emsg = TRUE;
7791 EMSG(_("E724: variable nested too deep for displaying"));
7792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007794 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 }
7796 ++recurse;
7797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 switch (tv->v_type)
7799 {
7800 case VAR_FUNC:
7801 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 r = tv->vval.v_string;
7803 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007805 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007806 if (tv->vval.v_list == NULL)
7807 {
7808 *tofree = NULL;
7809 r = NULL;
7810 }
7811 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7812 {
7813 *tofree = NULL;
7814 r = (char_u *)"[...]";
7815 }
7816 else
7817 {
7818 tv->vval.v_list->lv_copyID = copyID;
7819 *tofree = list2string(tv, copyID);
7820 r = *tofree;
7821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007823
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007825 if (tv->vval.v_dict == NULL)
7826 {
7827 *tofree = NULL;
7828 r = NULL;
7829 }
7830 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7831 {
7832 *tofree = NULL;
7833 r = (char_u *)"{...}";
7834 }
7835 else
7836 {
7837 tv->vval.v_dict->dv_copyID = copyID;
7838 *tofree = dict2string(tv, copyID);
7839 r = *tofree;
7840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 case VAR_STRING:
7844 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 *tofree = NULL;
7846 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#ifdef FEAT_FLOAT
7850 case VAR_FLOAT:
7851 *tofree = NULL;
7852 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7853 r = numbuf;
7854 break;
7855#endif
7856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007858 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007860 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861
Bram Moolenaar8502c702014-06-17 12:51:16 +02007862 if (--recurse == 0)
7863 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865}
7866
7867/*
7868 * Return a string with the string representation of a variable.
7869 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7870 * "numbuf" is used for a number.
7871 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007872 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 */
7874 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 char_u **tofree;
7878 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880{
7881 switch (tv->v_type)
7882 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 case VAR_FUNC:
7884 *tofree = string_quote(tv->vval.v_string, TRUE);
7885 return *tofree;
7886 case VAR_STRING:
7887 *tofree = string_quote(tv->vval.v_string, FALSE);
7888 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#ifdef FEAT_FLOAT
7890 case VAR_FLOAT:
7891 *tofree = NULL;
7892 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7893 return numbuf;
7894#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007897 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007900 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903}
7904
7905/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 * Return string "str" in ' quotes, doubling ' characters.
7907 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 */
7910 static char_u *
7911string_quote(str, function)
7912 char_u *str;
7913 int function;
7914{
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 char_u *p, *r, *s;
7917
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 len = (function ? 13 : 3);
7919 if (str != NULL)
7920 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007921 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 for (p = str; *p != NUL; mb_ptr_adv(p))
7923 if (*p == '\'')
7924 ++len;
7925 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 s = r = alloc(len);
7927 if (r != NULL)
7928 {
7929 if (function)
7930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 r += 10;
7933 }
7934 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 if (str != NULL)
7937 for (p = str; *p != NUL; )
7938 {
7939 if (*p == '\'')
7940 *r++ = '\'';
7941 MB_COPY_CHAR(p, r);
7942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 if (function)
7945 *r++ = ')';
7946 *r++ = NUL;
7947 }
7948 return s;
7949}
7950
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007951#ifdef FEAT_FLOAT
7952/*
7953 * Convert the string "text" to a floating point number.
7954 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7955 * this always uses a decimal point.
7956 * Returns the length of the text that was consumed.
7957 */
7958 static int
7959string2float(text, value)
7960 char_u *text;
7961 float_T *value; /* result stored here */
7962{
7963 char *s = (char *)text;
7964 float_T f;
7965
7966 f = strtod(s, &s);
7967 *value = f;
7968 return (int)((char_u *)s - text);
7969}
7970#endif
7971
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 * Get the value of an environment variable.
7974 * "arg" is pointing to the '$'. It is advanced to after the name.
7975 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007976 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 */
7978 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int evaluate;
7983{
7984 char_u *string = NULL;
7985 int len;
7986 int cc;
7987 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007988 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 ++*arg;
7991 name = *arg;
7992 len = get_env_len(arg);
7993 if (evaluate)
7994 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007995 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007996 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007997
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007998 cc = name[len];
7999 name[len] = NUL;
8000 /* first try vim_getenv(), fast for normal environment vars */
8001 string = vim_getenv(name, &mustfree);
8002 if (string != NULL && *string != NUL)
8003 {
8004 if (!mustfree)
8005 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008007 else
8008 {
8009 if (mustfree)
8010 vim_free(string);
8011
8012 /* next try expanding things like $VIM and ${HOME} */
8013 string = expand_env_save(name - 1);
8014 if (string != NULL && *string == '$')
8015 {
8016 vim_free(string);
8017 string = NULL;
8018 }
8019 }
8020 name[len] = cc;
8021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 rettv->v_type = VAR_STRING;
8023 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025
8026 return OK;
8027}
8028
8029/*
8030 * Array with names and number of arguments of all internal functions
8031 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8032 */
8033static struct fst
8034{
8035 char *f_name; /* function name */
8036 char f_min_argc; /* minimal number of arguments */
8037 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008039 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040} functions[] =
8041{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008044 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008047 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"append", 2, 2, f_append},
8049 {"argc", 0, 0, f_argc},
8050 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008051 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008052 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008056 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008059 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"bufexists", 1, 1, f_bufexists},
8061 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8062 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8063 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8064 {"buflisted", 1, 1, f_buflisted},
8065 {"bufloaded", 1, 1, f_bufloaded},
8066 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008067 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"bufwinnr", 1, 1, f_bufwinnr},
8069 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008070 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008071 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008072 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#ifdef FEAT_FLOAT
8074 {"ceil", 1, 1, f_ceil},
8075#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008076 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008077 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008079 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008081#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008082 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008083 {"complete_add", 1, 1, f_complete_add},
8084 {"complete_check", 0, 0, f_complete_check},
8085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#ifdef FEAT_FLOAT
8089 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008090 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008091#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008092 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008094 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008095 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"delete", 1, 1, f_delete},
8097 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008098 {"diff_filler", 1, 1, f_diff_filler},
8099 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008100 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008102 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"eventhandler", 0, 0, f_eventhandler},
8104 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008105 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107#ifdef FEAT_FLOAT
8108 {"exp", 1, 1, f_exp},
8109#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008110 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008111 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008112 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8114 {"filereadable", 1, 1, f_filereadable},
8115 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008116 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008117 {"finddir", 1, 3, f_finddir},
8118 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#ifdef FEAT_FLOAT
8120 {"float2nr", 1, 1, f_float2nr},
8121 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008124 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"fnamemodify", 2, 2, f_fnamemodify},
8126 {"foldclosed", 1, 1, f_foldclosed},
8127 {"foldclosedend", 1, 1, f_foldclosedend},
8128 {"foldlevel", 1, 1, f_foldlevel},
8129 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008130 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008133 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008134 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008135 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008136 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"getchar", 0, 1, f_getchar},
8138 {"getcharmod", 0, 0, f_getcharmod},
8139 {"getcmdline", 0, 0, f_getcmdline},
8140 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008141 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008142 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008143 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008145 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008146 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"getfsize", 1, 1, f_getfsize},
8148 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008149 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008150 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008151 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008152 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008153 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008154 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008155 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008156 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008158 {"gettabvar", 2, 3, f_gettabvar},
8159 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"getwinposx", 0, 0, f_getwinposx},
8161 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008162 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008163 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008164 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008165 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008168 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008169 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8171 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8172 {"histadd", 2, 2, f_histadd},
8173 {"histdel", 1, 2, f_histdel},
8174 {"histget", 1, 2, f_histget},
8175 {"histnr", 1, 1, f_histnr},
8176 {"hlID", 1, 1, f_hlID},
8177 {"hlexists", 1, 1, f_hlexists},
8178 {"hostname", 0, 0, f_hostname},
8179 {"iconv", 3, 3, f_iconv},
8180 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008181 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008182 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008184 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"inputrestore", 0, 0, f_inputrestore},
8186 {"inputsave", 0, 0, f_inputsave},
8187 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008189 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008191 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008192 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008194 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"libcall", 3, 3, f_libcall},
8198 {"libcallnr", 3, 3, f_libcallnr},
8199 {"line", 1, 1, f_line},
8200 {"line2byte", 1, 1, f_line2byte},
8201 {"lispindent", 1, 1, f_lispindent},
8202 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008204 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008205 {"log10", 1, 1, f_log10},
8206#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008207#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008208 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008209#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008211 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008212 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008213 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008214 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008215 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008216 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008217 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008218 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008219 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008220 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008221 {"max", 1, 1, f_max},
8222 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008223#ifdef vim_mkdir
8224 {"mkdir", 1, 3, f_mkdir},
8225#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008226 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008227#ifdef FEAT_MZSCHEME
8228 {"mzeval", 1, 1, f_mzeval},
8229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008231 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008232 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008233 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234#ifdef FEAT_FLOAT
8235 {"pow", 2, 2, f_pow},
8236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008238 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008239 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008240#ifdef FEAT_PYTHON3
8241 {"py3eval", 1, 1, f_py3eval},
8242#endif
8243#ifdef FEAT_PYTHON
8244 {"pyeval", 1, 1, f_pyeval},
8245#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008247 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008248 {"reltime", 0, 2, f_reltime},
8249 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"remote_expr", 2, 3, f_remote_expr},
8251 {"remote_foreground", 1, 1, f_remote_foreground},
8252 {"remote_peek", 1, 2, f_remote_peek},
8253 {"remote_read", 1, 1, f_remote_read},
8254 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008255 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008257 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008259 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008260#ifdef FEAT_FLOAT
8261 {"round", 1, 1, f_round},
8262#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008263 {"screenattr", 2, 2, f_screenattr},
8264 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008265 {"screencol", 0, 0, f_screencol},
8266 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008267 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008268 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008269 {"searchpair", 3, 7, f_searchpair},
8270 {"searchpairpos", 3, 7, f_searchpairpos},
8271 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"server2client", 2, 2, f_server2client},
8273 {"serverlist", 0, 0, f_serverlist},
8274 {"setbufvar", 3, 3, f_setbufvar},
8275 {"setcmdpos", 1, 1, f_setcmdpos},
8276 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008277 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008278 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008279 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008280 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008282 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008283 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008285#ifdef FEAT_CRYPT
8286 {"sha256", 1, 1, f_sha256},
8287#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008288 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008289 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008291#ifdef FEAT_FLOAT
8292 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008293 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008295 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008296 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008297 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008298 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008299 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300#ifdef FEAT_FLOAT
8301 {"sqrt", 1, 1, f_sqrt},
8302 {"str2float", 1, 1, f_str2float},
8303#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008304 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008305 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008306 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#ifdef HAVE_STRFTIME
8308 {"strftime", 1, 2, f_strftime},
8309#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"strlen", 1, 1, f_strlen},
8313 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008314 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008316 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008317 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"substitute", 4, 4, f_substitute},
8319 {"synID", 3, 3, f_synID},
8320 {"synIDattr", 2, 3, f_synIDattr},
8321 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008322 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008323 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008324 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008325 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008326 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008327 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008328 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008329 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008330 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008331#ifdef FEAT_FLOAT
8332 {"tan", 1, 1, f_tan},
8333 {"tanh", 1, 1, f_tanh},
8334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008336 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"tolower", 1, 1, f_tolower},
8338 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008339 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"trunc", 1, 1, f_trunc},
8342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008344 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008345 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008346 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"virtcol", 1, 1, f_virtcol},
8349 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008350 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"winbufnr", 1, 1, f_winbufnr},
8352 {"wincol", 0, 0, f_wincol},
8353 {"winheight", 1, 1, f_winheight},
8354 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008355 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008357 {"winrestview", 1, 1, f_winrestview},
8358 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008360 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008361 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362};
8363
8364#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8365
8366/*
8367 * Function given to ExpandGeneric() to obtain the list of internal
8368 * or user defined function names.
8369 */
8370 char_u *
8371get_function_name(xp, idx)
8372 expand_T *xp;
8373 int idx;
8374{
8375 static int intidx = -1;
8376 char_u *name;
8377
8378 if (idx == 0)
8379 intidx = -1;
8380 if (intidx < 0)
8381 {
8382 name = get_user_func_name(xp, idx);
8383 if (name != NULL)
8384 return name;
8385 }
8386 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8387 {
8388 STRCPY(IObuff, functions[intidx].f_name);
8389 STRCAT(IObuff, "(");
8390 if (functions[intidx].f_max_argc == 0)
8391 STRCAT(IObuff, ")");
8392 return IObuff;
8393 }
8394
8395 return NULL;
8396}
8397
8398/*
8399 * Function given to ExpandGeneric() to obtain the list of internal or
8400 * user defined variable or function names.
8401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 char_u *
8403get_expr_name(xp, idx)
8404 expand_T *xp;
8405 int idx;
8406{
8407 static int intidx = -1;
8408 char_u *name;
8409
8410 if (idx == 0)
8411 intidx = -1;
8412 if (intidx < 0)
8413 {
8414 name = get_function_name(xp, idx);
8415 if (name != NULL)
8416 return name;
8417 }
8418 return get_user_var_name(xp, ++intidx);
8419}
8420
8421#endif /* FEAT_CMDL_COMPL */
8422
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008423#if defined(EBCDIC) || defined(PROTO)
8424/*
8425 * Compare struct fst by function name.
8426 */
8427 static int
8428compare_func_name(s1, s2)
8429 const void *s1;
8430 const void *s2;
8431{
8432 struct fst *p1 = (struct fst *)s1;
8433 struct fst *p2 = (struct fst *)s2;
8434
8435 return STRCMP(p1->f_name, p2->f_name);
8436}
8437
8438/*
8439 * Sort the function table by function name.
8440 * The sorting of the table above is ASCII dependant.
8441 * On machines using EBCDIC we have to sort it.
8442 */
8443 static void
8444sortFunctions()
8445{
8446 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8447
8448 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8449}
8450#endif
8451
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453/*
8454 * Find internal function in table above.
8455 * Return index, or -1 if not found
8456 */
8457 static int
8458find_internal_func(name)
8459 char_u *name; /* name of the function */
8460{
8461 int first = 0;
8462 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8463 int cmp;
8464 int x;
8465
8466 /*
8467 * Find the function name in the table. Binary search.
8468 */
8469 while (first <= last)
8470 {
8471 x = first + ((unsigned)(last - first) >> 1);
8472 cmp = STRCMP(name, functions[x].f_name);
8473 if (cmp < 0)
8474 last = x - 1;
8475 else if (cmp > 0)
8476 first = x + 1;
8477 else
8478 return x;
8479 }
8480 return -1;
8481}
8482
8483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8485 * name it contains, otherwise return "name".
8486 */
8487 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008488deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008489 char_u *name;
8490 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008491 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008492{
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008494 int cc;
8495
8496 cc = name[*lenp];
8497 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008498 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 {
8504 *lenp = 0;
8505 return (char_u *)""; /* just in case */
8506 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008507 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008508 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 }
8510
8511 return name;
8512}
8513
8514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 * Allocate a variable for the result of a function.
8516 * Return OK or FAIL.
8517 */
8518 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8520 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 char_u *name; /* name of the function */
8522 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 char_u **arg; /* argument, pointing to the '(' */
8525 linenr_T firstline; /* first line of range */
8526 linenr_T lastline; /* last line of range */
8527 int *doesrange; /* return: function handled range */
8528 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 char_u *argp;
8532 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008533 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 int argcount = 0; /* number of arguments found */
8535
8536 /*
8537 * Get the arguments.
8538 */
8539 argp = *arg;
8540 while (argcount < MAX_FUNC_ARGS)
8541 {
8542 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8543 if (*argp == ')' || *argp == ',' || *argp == NUL)
8544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8546 {
8547 ret = FAIL;
8548 break;
8549 }
8550 ++argcount;
8551 if (*argp != ',')
8552 break;
8553 }
8554 if (*argp == ')')
8555 ++argp;
8556 else
8557 ret = FAIL;
8558
8559 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 {
8564 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008565 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008567 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
8573 *arg = skipwhite(argp);
8574 return ret;
8575}
8576
8577
8578/*
8579 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008580 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008581 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 */
8583 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008584call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008586 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008590 typval_T *argvars; /* vars for arguments, must have "argcount"
8591 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 linenr_T firstline; /* first line of range */
8593 linenr_T lastline; /* last line of range */
8594 int *doesrange; /* return: function handled range */
8595 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599#define ERROR_UNKNOWN 0
8600#define ERROR_TOOMANY 1
8601#define ERROR_TOOFEW 2
8602#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603#define ERROR_DICT 4
8604#define ERROR_NONE 5
8605#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int error = ERROR_NONE;
8607 int i;
8608 int llen;
8609 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#define FLEN_FIXED 40
8611 char_u fname_buf[FLEN_FIXED + 1];
8612 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008613 char_u *name;
8614
8615 /* Make a copy of the name, if it comes from a funcref variable it could
8616 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008617 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008618 if (name == NULL)
8619 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 /*
8622 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8623 * Change <SNR>123_name() to K_SNR 123_name().
8624 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 llen = eval_fname_script(name);
8627 if (llen > 0)
8628 {
8629 fname_buf[0] = K_SPECIAL;
8630 fname_buf[1] = KS_EXTRA;
8631 fname_buf[2] = (int)KE_SNR;
8632 i = 3;
8633 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8634 {
8635 if (current_SID <= 0)
8636 error = ERROR_SCRIPT;
8637 else
8638 {
8639 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8640 i = (int)STRLEN(fname_buf);
8641 }
8642 }
8643 if (i + STRLEN(name + llen) < FLEN_FIXED)
8644 {
8645 STRCPY(fname_buf + i, name + llen);
8646 fname = fname_buf;
8647 }
8648 else
8649 {
8650 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8651 if (fname == NULL)
8652 error = ERROR_OTHER;
8653 else
8654 {
8655 mch_memmove(fname, fname_buf, (size_t)i);
8656 STRCPY(fname + i, name + llen);
8657 }
8658 }
8659 }
8660 else
8661 fname = name;
8662
8663 *doesrange = FALSE;
8664
8665
8666 /* execute the function if no errors detected and executing */
8667 if (evaluate && error == ERROR_NONE)
8668 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008669 char_u *rfname = fname;
8670
8671 /* Ignore "g:" before a function name. */
8672 if (fname[0] == 'g' && fname[1] == ':')
8673 rfname = fname + 2;
8674
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008675 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8676 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 error = ERROR_UNKNOWN;
8678
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008679 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 /*
8682 * User defined function.
8683 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008684 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008687 /* Trigger FuncUndefined event, may load the function. */
8688 if (fp == NULL
8689 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008690 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008691 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008693 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008694 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 }
8696#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008697 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008698 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008699 {
8700 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702 }
8703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 if (fp != NULL)
8705 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008706 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008708 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008710 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008712 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else
8715 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008716 int did_save_redo = FALSE;
8717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 /*
8719 * Call the user function.
8720 * Save and restore search patterns, script variables and
8721 * redo buffer.
8722 */
8723 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008724 if (!ins_compl_active())
8725 {
8726 saveRedobuff();
8727 did_save_redo = TRUE;
8728 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008731 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8733 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8734 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008735 /* Function was unreferenced while being used, free it
8736 * now. */
8737 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008738 if (did_save_redo)
8739 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 restore_search_patterns();
8741 error = ERROR_NONE;
8742 }
8743 }
8744 }
8745 else
8746 {
8747 /*
8748 * Find the function name in the table, call its implementation.
8749 */
8750 i = find_internal_func(fname);
8751 if (i >= 0)
8752 {
8753 if (argcount < functions[i].f_min_argc)
8754 error = ERROR_TOOFEW;
8755 else if (argcount > functions[i].f_max_argc)
8756 error = ERROR_TOOMANY;
8757 else
8758 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008759 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 error = ERROR_NONE;
8762 }
8763 }
8764 }
8765 /*
8766 * The function call (or "FuncUndefined" autocommand sequence) might
8767 * have been aborted by an error, an interrupt, or an explicitly thrown
8768 * exception that has not been caught so far. This situation can be
8769 * tested for by calling aborting(). For an error in an internal
8770 * function or for the "E132" error in call_user_func(), however, the
8771 * throw point at which the "force_abort" flag (temporarily reset by
8772 * emsg()) is normally updated has not been reached yet. We need to
8773 * update that flag first to make aborting() reliable.
8774 */
8775 update_force_abort();
8776 }
8777 if (error == ERROR_NONE)
8778 ret = OK;
8779
8780 /*
8781 * Report an error unless the argument evaluation or function call has been
8782 * cancelled due to an aborting error, an interrupt, or an exception.
8783 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008784 if (!aborting())
8785 {
8786 switch (error)
8787 {
8788 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008789 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008790 break;
8791 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008792 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008793 break;
8794 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008795 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008796 name);
8797 break;
8798 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008799 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008800 name);
8801 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008803 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 name);
8805 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008806 }
8807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (fname != name && fname != fname_buf)
8810 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008811 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 return ret;
8814}
8815
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008816/*
8817 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008818 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008819 */
8820 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008821emsg_funcname(ermsg, name)
8822 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008823 char_u *name;
8824{
8825 char_u *p;
8826
8827 if (*name == K_SPECIAL)
8828 p = concat_str((char_u *)"<SNR>", name + 3);
8829 else
8830 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008831 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 if (p != name)
8833 vim_free(p);
8834}
8835
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008836/*
8837 * Return TRUE for a non-zero Number and a non-empty String.
8838 */
8839 static int
8840non_zero_arg(argvars)
8841 typval_T *argvars;
8842{
8843 return ((argvars[0].v_type == VAR_NUMBER
8844 && argvars[0].vval.v_number != 0)
8845 || (argvars[0].v_type == VAR_STRING
8846 && argvars[0].vval.v_string != NULL
8847 && *argvars[0].vval.v_string != NUL));
8848}
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*********************************************
8851 * Implementation of the built-in functions
8852 */
8853
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008854#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008855static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8856
8857/*
8858 * Get the float value of "argvars[0]" into "f".
8859 * Returns FAIL when the argument is not a Number or Float.
8860 */
8861 static int
8862get_float_arg(argvars, f)
8863 typval_T *argvars;
8864 float_T *f;
8865{
8866 if (argvars[0].v_type == VAR_FLOAT)
8867 {
8868 *f = argvars[0].vval.v_float;
8869 return OK;
8870 }
8871 if (argvars[0].v_type == VAR_NUMBER)
8872 {
8873 *f = (float_T)argvars[0].vval.v_number;
8874 return OK;
8875 }
8876 EMSG(_("E808: Number or Float required"));
8877 return FAIL;
8878}
8879
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880/*
8881 * "abs(expr)" function
8882 */
8883 static void
8884f_abs(argvars, rettv)
8885 typval_T *argvars;
8886 typval_T *rettv;
8887{
8888 if (argvars[0].v_type == VAR_FLOAT)
8889 {
8890 rettv->v_type = VAR_FLOAT;
8891 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8892 }
8893 else
8894 {
8895 varnumber_T n;
8896 int error = FALSE;
8897
8898 n = get_tv_number_chk(&argvars[0], &error);
8899 if (error)
8900 rettv->vval.v_number = -1;
8901 else if (n > 0)
8902 rettv->vval.v_number = n;
8903 else
8904 rettv->vval.v_number = -n;
8905 }
8906}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008907
8908/*
8909 * "acos()" function
8910 */
8911 static void
8912f_acos(argvars, rettv)
8913 typval_T *argvars;
8914 typval_T *rettv;
8915{
8916 float_T f;
8917
8918 rettv->v_type = VAR_FLOAT;
8919 if (get_float_arg(argvars, &f) == OK)
8920 rettv->vval.v_float = acos(f);
8921 else
8922 rettv->vval.v_float = 0.0;
8923}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#endif
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008927 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 */
8929 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008930f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008931 typval_T *argvars;
8932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008937 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008939 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008940 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008941 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008943 }
8944 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008945 EMSG(_(e_listreq));
8946}
8947
8948/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008949 * "and(expr, expr)" function
8950 */
8951 static void
8952f_and(argvars, rettv)
8953 typval_T *argvars;
8954 typval_T *rettv;
8955{
8956 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8957 & get_tv_number_chk(&argvars[1], NULL);
8958}
8959
8960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008961 * "append(lnum, string/list)" function
8962 */
8963 static void
8964f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008967{
8968 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 list_T *l = NULL;
8971 listitem_T *li = NULL;
8972 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008973 long added = 0;
8974
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008975 /* When coming here from Insert mode, sync undo, so that this can be
8976 * undone separately from what was previously inserted. */
8977 if (u_sync_once == 2)
8978 {
8979 u_sync_once = 1; /* notify that u_sync() was called */
8980 u_sync(TRUE);
8981 }
8982
Bram Moolenaar0d660222005-01-07 21:51:51 +00008983 lnum = get_tv_lnum(argvars);
8984 if (lnum >= 0
8985 && lnum <= curbuf->b_ml.ml_line_count
8986 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008988 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008990 l = argvars[1].vval.v_list;
8991 if (l == NULL)
8992 return;
8993 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995 for (;;)
8996 {
8997 if (l == NULL)
8998 tv = &argvars[1]; /* append a string */
8999 else if (li == NULL)
9000 break; /* end of list */
9001 else
9002 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009003 line = get_tv_string_chk(tv);
9004 if (line == NULL) /* type error */
9005 {
9006 rettv->vval.v_number = 1; /* Failed */
9007 break;
9008 }
9009 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009010 ++added;
9011 if (l == NULL)
9012 break;
9013 li = li->li_next;
9014 }
9015
9016 appended_lines_mark(lnum, added);
9017 if (curwin->w_cursor.lnum > lnum)
9018 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 else
9021 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023
9024/*
9025 * "argc()" function
9026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033}
9034
9035/*
9036 * "argidx()" function
9037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009040 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009047 * "arglistid()" function
9048 */
9049 static void
9050f_arglistid(argvars, rettv)
9051 typval_T *argvars UNUSED;
9052 typval_T *rettv;
9053{
9054 win_T *wp;
9055 tabpage_T *tp = NULL;
9056 long n;
9057
9058 rettv->vval.v_number = -1;
9059 if (argvars[0].v_type != VAR_UNKNOWN)
9060 {
9061 if (argvars[1].v_type != VAR_UNKNOWN)
9062 {
9063 n = get_tv_number(&argvars[1]);
9064 if (n >= 0)
9065 tp = find_tabpage(n);
9066 }
9067 else
9068 tp = curtab;
9069
9070 if (tp != NULL)
9071 {
9072 wp = find_win_by_nr(&argvars[0], tp);
9073 if (wp != NULL)
9074 rettv->vval.v_number = wp->w_alist->id;
9075 }
9076 }
9077 else
9078 rettv->vval.v_number = curwin->w_alist->id;
9079}
9080
9081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * "argv(nr)" function
9083 */
9084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
9089 int idx;
9090
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009091 if (argvars[0].v_type != VAR_UNKNOWN)
9092 {
9093 idx = get_tv_number_chk(&argvars[0], NULL);
9094 if (idx >= 0 && idx < ARGCOUNT)
9095 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9096 else
9097 rettv->vval.v_string = NULL;
9098 rettv->v_type = VAR_STRING;
9099 }
9100 else if (rettv_list_alloc(rettv) == OK)
9101 for (idx = 0; idx < ARGCOUNT; ++idx)
9102 list_append_string(rettv->vval.v_list,
9103 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009106#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009107/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009108 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009109 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009110 static void
9111f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009112 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009113 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009114{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009115 float_T f;
9116
9117 rettv->v_type = VAR_FLOAT;
9118 if (get_float_arg(argvars, &f) == OK)
9119 rettv->vval.v_float = asin(f);
9120 else
9121 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009122}
9123
9124/*
9125 * "atan()" function
9126 */
9127 static void
9128f_atan(argvars, rettv)
9129 typval_T *argvars;
9130 typval_T *rettv;
9131{
9132 float_T f;
9133
9134 rettv->v_type = VAR_FLOAT;
9135 if (get_float_arg(argvars, &f) == OK)
9136 rettv->vval.v_float = atan(f);
9137 else
9138 rettv->vval.v_float = 0.0;
9139}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009140
9141/*
9142 * "atan2()" function
9143 */
9144 static void
9145f_atan2(argvars, rettv)
9146 typval_T *argvars;
9147 typval_T *rettv;
9148{
9149 float_T fx, fy;
9150
9151 rettv->v_type = VAR_FLOAT;
9152 if (get_float_arg(argvars, &fx) == OK
9153 && get_float_arg(&argvars[1], &fy) == OK)
9154 rettv->vval.v_float = atan2(fx, fy);
9155 else
9156 rettv->vval.v_float = 0.0;
9157}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009158#endif
9159
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160/*
9161 * "browse(save, title, initdir, default)" function
9162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009165 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168#ifdef FEAT_BROWSE
9169 int save;
9170 char_u *title;
9171 char_u *initdir;
9172 char_u *defname;
9173 char_u buf[NUMBUFLEN];
9174 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 save = get_tv_number_chk(&argvars[0], &error);
9178 title = get_tv_string_chk(&argvars[1]);
9179 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9180 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 if (error || title == NULL || initdir == NULL || defname == NULL)
9183 rettv->vval.v_string = NULL;
9184 else
9185 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009186 do_browse(save ? BROWSE_SAVE : 0,
9187 title, defname, NULL, initdir, NULL, curbuf);
9188#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009192}
9193
9194/*
9195 * "browsedir(title, initdir)" function
9196 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009200 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009201{
9202#ifdef FEAT_BROWSE
9203 char_u *title;
9204 char_u *initdir;
9205 char_u buf[NUMBUFLEN];
9206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 title = get_tv_string_chk(&argvars[0]);
9208 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009210 if (title == NULL || initdir == NULL)
9211 rettv->vval.v_string = NULL;
9212 else
9213 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009214 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220
Bram Moolenaar33570922005-01-25 22:26:29 +00009221static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223/*
9224 * Find a buffer by number or exact name.
9225 */
9226 static buf_T *
9227find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
9230 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009232 if (avar->v_type == VAR_NUMBER)
9233 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009234 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009236 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009237 if (buf == NULL)
9238 {
9239 /* No full path name match, try a match with a URL or a "nofile"
9240 * buffer, these don't use the full path. */
9241 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9242 if (buf->b_fname != NULL
9243 && (path_with_url(buf->b_fname)
9244#ifdef FEAT_QUICKFIX
9245 || bt_nofile(buf)
9246#endif
9247 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009248 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009249 break;
9250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 }
9252 return buf;
9253}
9254
9255/*
9256 * "bufexists(expr)" function
9257 */
9258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009260 typval_T *argvars;
9261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264}
9265
9266/*
9267 * "buflisted(expr)" function
9268 */
9269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009271 typval_T *argvars;
9272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274 buf_T *buf;
9275
9276 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
9281 * "bufloaded(expr)" function
9282 */
9283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 typval_T *argvars;
9286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 buf_T *buf;
9289
9290 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292}
9293
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009294static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296/*
9297 * Get buffer by number or pattern.
9298 */
9299 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009300get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009301 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009302 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 int save_magic;
9306 char_u *save_cpo;
9307 buf_T *buf;
9308
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 if (tv->v_type == VAR_NUMBER)
9310 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009311 if (tv->v_type != VAR_STRING)
9312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 if (name == NULL || *name == NUL)
9314 return curbuf;
9315 if (name[0] == '$' && name[1] == NUL)
9316 return lastbuf;
9317
9318 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9319 save_magic = p_magic;
9320 p_magic = TRUE;
9321 save_cpo = p_cpo;
9322 p_cpo = (char_u *)"";
9323
9324 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009325 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326
9327 p_magic = save_magic;
9328 p_cpo = save_cpo;
9329
9330 /* If not found, try expanding the name, like done for bufexists(). */
9331 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
9334 return buf;
9335}
9336
9337/*
9338 * "bufname(expr)" function
9339 */
9340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009342 typval_T *argvars;
9343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
9345 buf_T *buf;
9346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009349 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 --emsg_off;
9356}
9357
9358/*
9359 * "bufnr(expr)" function
9360 */
9361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009367 int error = FALSE;
9368 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009370 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009372 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009373 --emsg_off;
9374
9375 /* If the buffer isn't found and the second argument is not zero create a
9376 * new buffer. */
9377 if (buf == NULL
9378 && argvars[1].v_type != VAR_UNKNOWN
9379 && get_tv_number_chk(&argvars[1], &error) != 0
9380 && !error
9381 && (name = get_tv_string_chk(&argvars[0])) != NULL
9382 && !error)
9383 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9384
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * "bufwinnr(nr)" function
9393 */
9394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T *argvars;
9397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398{
9399#ifdef FEAT_WINDOWS
9400 win_T *wp;
9401 int winnr = 0;
9402#endif
9403 buf_T *buf;
9404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009407 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408#ifdef FEAT_WINDOWS
9409 for (wp = firstwin; wp; wp = wp->w_next)
9410 {
9411 ++winnr;
9412 if (wp->w_buffer == buf)
9413 break;
9414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418#endif
9419 --emsg_off;
9420}
9421
9422/*
9423 * "byte2line(byte)" function
9424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009427 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#else
9433 long boff = 0;
9434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 (linenr_T)0, &boff);
9441#endif
9442}
9443
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009444 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009445byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009448 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009449{
9450#ifdef FEAT_MBYTE
9451 char_u *t;
9452#endif
9453 char_u *str;
9454 long idx;
9455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 str = get_tv_string_chk(&argvars[0]);
9457 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009460 return;
9461
9462#ifdef FEAT_MBYTE
9463 t = str;
9464 for ( ; idx > 0; idx--)
9465 {
9466 if (*t == NUL) /* EOL reached */
9467 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009468 if (enc_utf8 && comp)
9469 t += utf_ptr2len(t);
9470 else
9471 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009472 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009473 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009474#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009475 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009477#endif
9478}
9479
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009480/*
9481 * "byteidx()" function
9482 */
9483 static void
9484f_byteidx(argvars, rettv)
9485 typval_T *argvars;
9486 typval_T *rettv;
9487{
9488 byteidx(argvars, rettv, FALSE);
9489}
9490
9491/*
9492 * "byteidxcomp()" function
9493 */
9494 static void
9495f_byteidxcomp(argvars, rettv)
9496 typval_T *argvars;
9497 typval_T *rettv;
9498{
9499 byteidx(argvars, rettv, TRUE);
9500}
9501
Bram Moolenaardb913952012-06-29 12:54:53 +02009502 int
9503func_call(name, args, selfdict, rettv)
9504 char_u *name;
9505 typval_T *args;
9506 dict_T *selfdict;
9507 typval_T *rettv;
9508{
9509 listitem_T *item;
9510 typval_T argv[MAX_FUNC_ARGS + 1];
9511 int argc = 0;
9512 int dummy;
9513 int r = 0;
9514
9515 for (item = args->vval.v_list->lv_first; item != NULL;
9516 item = item->li_next)
9517 {
9518 if (argc == MAX_FUNC_ARGS)
9519 {
9520 EMSG(_("E699: Too many arguments"));
9521 break;
9522 }
9523 /* Make a copy of each argument. This is needed to be able to set
9524 * v_lock to VAR_FIXED in the copy without changing the original list.
9525 */
9526 copy_tv(&item->li_tv, &argv[argc++]);
9527 }
9528
9529 if (item == NULL)
9530 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9531 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9532 &dummy, TRUE, selfdict);
9533
9534 /* Free the arguments. */
9535 while (argc > 0)
9536 clear_tv(&argv[--argc]);
9537
9538 return r;
9539}
9540
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009541/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009542 * "call(func, arglist)" function
9543 */
9544 static void
9545f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009548{
9549 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009551
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009552 if (argvars[1].v_type != VAR_LIST)
9553 {
9554 EMSG(_(e_listreq));
9555 return;
9556 }
9557 if (argvars[1].vval.v_list == NULL)
9558 return;
9559
9560 if (argvars[0].v_type == VAR_FUNC)
9561 func = argvars[0].vval.v_string;
9562 else
9563 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 if (*func == NUL)
9565 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566
Bram Moolenaare9a41262005-01-15 22:18:47 +00009567 if (argvars[2].v_type != VAR_UNKNOWN)
9568 {
9569 if (argvars[2].v_type != VAR_DICT)
9570 {
9571 EMSG(_(e_dictreq));
9572 return;
9573 }
9574 selfdict = argvars[2].vval.v_dict;
9575 }
9576
Bram Moolenaardb913952012-06-29 12:54:53 +02009577 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009578}
9579
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009580#ifdef FEAT_FLOAT
9581/*
9582 * "ceil({float})" function
9583 */
9584 static void
9585f_ceil(argvars, rettv)
9586 typval_T *argvars;
9587 typval_T *rettv;
9588{
9589 float_T f;
9590
9591 rettv->v_type = VAR_FLOAT;
9592 if (get_float_arg(argvars, &f) == OK)
9593 rettv->vval.v_float = ceil(f);
9594 else
9595 rettv->vval.v_float = 0.0;
9596}
9597#endif
9598
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009600 * "changenr()" function
9601 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009602 static void
9603f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009604 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009605 typval_T *rettv;
9606{
9607 rettv->vval.v_number = curbuf->b_u_seq_cur;
9608}
9609
9610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 * "char2nr(string)" function
9612 */
9613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618#ifdef FEAT_MBYTE
9619 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009620 {
9621 int utf8 = 0;
9622
9623 if (argvars[1].v_type != VAR_UNKNOWN)
9624 utf8 = get_tv_number_chk(&argvars[1], NULL);
9625
9626 if (utf8)
9627 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9628 else
9629 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 else
9632#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634}
9635
9636/*
9637 * "cindent(lnum)" function
9638 */
9639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643{
9644#ifdef FEAT_CINDENT
9645 pos_T pos;
9646 linenr_T lnum;
9647
9648 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9651 {
9652 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 curwin->w_cursor = pos;
9655 }
9656 else
9657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659}
9660
9661/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009662 * "clearmatches()" function
9663 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009664 static void
9665f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009666 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009667 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009668{
9669#ifdef FEAT_SEARCH_EXTRA
9670 clear_matches(curwin);
9671#endif
9672}
9673
9674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 * "col(string)" function
9676 */
9677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009679 typval_T *argvars;
9680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682 colnr_T col = 0;
9683 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009684 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009686 fp = var2fpos(&argvars[0], FALSE, &fnum);
9687 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 {
9689 if (fp->col == MAXCOL)
9690 {
9691 /* '> can be MAXCOL, get the length of the line then */
9692 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009693 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 else
9695 col = MAXCOL;
9696 }
9697 else
9698 {
9699 col = fp->col + 1;
9700#ifdef FEAT_VIRTUALEDIT
9701 /* col(".") when the cursor is on the NUL at the end of the line
9702 * because of "coladd" can be seen as an extra column. */
9703 if (virtual_active() && fp == &curwin->w_cursor)
9704 {
9705 char_u *p = ml_get_cursor();
9706
9707 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9708 curwin->w_virtcol - curwin->w_cursor.coladd))
9709 {
9710# ifdef FEAT_MBYTE
9711 int l;
9712
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009713 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 col += l;
9715# else
9716 if (*p != NUL && p[1] == NUL)
9717 ++col;
9718# endif
9719 }
9720 }
9721#endif
9722 }
9723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
Bram Moolenaar572cb562005-08-05 21:35:02 +00009727#if defined(FEAT_INS_EXPAND)
9728/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009729 * "complete()" function
9730 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009731 static void
9732f_complete(argvars, rettv)
9733 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009734 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009735{
9736 int startcol;
9737
9738 if ((State & INSERT) == 0)
9739 {
9740 EMSG(_("E785: complete() can only be used in Insert mode"));
9741 return;
9742 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009743
9744 /* Check for undo allowed here, because if something was already inserted
9745 * the line was already saved for undo and this check isn't done. */
9746 if (!undo_allowed())
9747 return;
9748
Bram Moolenaarade00832006-03-10 21:46:58 +00009749 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9750 {
9751 EMSG(_(e_invarg));
9752 return;
9753 }
9754
9755 startcol = get_tv_number_chk(&argvars[0], NULL);
9756 if (startcol <= 0)
9757 return;
9758
9759 set_completion(startcol - 1, argvars[1].vval.v_list);
9760}
9761
9762/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009763 * "complete_add()" function
9764 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009765 static void
9766f_complete_add(argvars, rettv)
9767 typval_T *argvars;
9768 typval_T *rettv;
9769{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009770 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009771}
9772
9773/*
9774 * "complete_check()" function
9775 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009776 static void
9777f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009778 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009779 typval_T *rettv;
9780{
9781 int saved = RedrawingDisabled;
9782
9783 RedrawingDisabled = 0;
9784 ins_compl_check_keys(0);
9785 rettv->vval.v_number = compl_interrupted;
9786 RedrawingDisabled = saved;
9787}
9788#endif
9789
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790/*
9791 * "confirm(message, buttons[, default [, type]])" function
9792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009795 typval_T *argvars UNUSED;
9796 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9799 char_u *message;
9800 char_u *buttons = NULL;
9801 char_u buf[NUMBUFLEN];
9802 char_u buf2[NUMBUFLEN];
9803 int def = 1;
9804 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 char_u *typestr;
9806 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 message = get_tv_string_chk(&argvars[0]);
9809 if (message == NULL)
9810 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009811 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9814 if (buttons == NULL)
9815 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009816 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009819 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009821 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9822 if (typestr == NULL)
9823 error = TRUE;
9824 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 switch (TOUPPER_ASC(*typestr))
9827 {
9828 case 'E': type = VIM_ERROR; break;
9829 case 'Q': type = VIM_QUESTION; break;
9830 case 'I': type = VIM_INFO; break;
9831 case 'W': type = VIM_WARNING; break;
9832 case 'G': type = VIM_GENERIC; break;
9833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835 }
9836 }
9837 }
9838
9839 if (buttons == NULL || *buttons == NUL)
9840 buttons = (char_u *)_("&Ok");
9841
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009842 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009844 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845#endif
9846}
9847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009848/*
9849 * "copy()" function
9850 */
9851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009853 typval_T *argvars;
9854 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009855{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009856 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009859#ifdef FEAT_FLOAT
9860/*
9861 * "cos()" function
9862 */
9863 static void
9864f_cos(argvars, rettv)
9865 typval_T *argvars;
9866 typval_T *rettv;
9867{
9868 float_T f;
9869
9870 rettv->v_type = VAR_FLOAT;
9871 if (get_float_arg(argvars, &f) == OK)
9872 rettv->vval.v_float = cos(f);
9873 else
9874 rettv->vval.v_float = 0.0;
9875}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009876
9877/*
9878 * "cosh()" function
9879 */
9880 static void
9881f_cosh(argvars, rettv)
9882 typval_T *argvars;
9883 typval_T *rettv;
9884{
9885 float_T f;
9886
9887 rettv->v_type = VAR_FLOAT;
9888 if (get_float_arg(argvars, &f) == OK)
9889 rettv->vval.v_float = cosh(f);
9890 else
9891 rettv->vval.v_float = 0.0;
9892}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009893#endif
9894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009896 * "count()" function
9897 */
9898 static void
9899f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009900 typval_T *argvars;
9901 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009902{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009903 long n = 0;
9904 int ic = FALSE;
9905
Bram Moolenaare9a41262005-01-15 22:18:47 +00009906 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 listitem_T *li;
9909 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009911
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 if ((l = argvars[0].vval.v_list) != NULL)
9913 {
9914 li = l->lv_first;
9915 if (argvars[2].v_type != VAR_UNKNOWN)
9916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 int error = FALSE;
9918
9919 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 if (argvars[3].v_type != VAR_UNKNOWN)
9921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 idx = get_tv_number_chk(&argvars[3], &error);
9923 if (!error)
9924 {
9925 li = list_find(l, idx);
9926 if (li == NULL)
9927 EMSGN(_(e_listidx), idx);
9928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930 if (error)
9931 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 }
9933
9934 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009935 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936 ++n;
9937 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 else if (argvars[0].v_type == VAR_DICT)
9940 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009941 int todo;
9942 dict_T *d;
9943 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009945 if ((d = argvars[0].vval.v_dict) != NULL)
9946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 int error = FALSE;
9948
Bram Moolenaare9a41262005-01-15 22:18:47 +00009949 if (argvars[2].v_type != VAR_UNKNOWN)
9950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009952 if (argvars[3].v_type != VAR_UNKNOWN)
9953 EMSG(_(e_invarg));
9954 }
9955
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009956 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009958 {
9959 if (!HASHITEM_EMPTY(hi))
9960 {
9961 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009962 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009963 ++n;
9964 }
9965 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 }
9967 }
9968 else
9969 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009970 rettv->vval.v_number = n;
9971}
9972
9973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9975 *
9976 * Checks the existence of a cscope connection.
9977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009980 typval_T *argvars UNUSED;
9981 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
9983#ifdef FEAT_CSCOPE
9984 int num = 0;
9985 char_u *dbpath = NULL;
9986 char_u *prepend = NULL;
9987 char_u buf[NUMBUFLEN];
9988
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009989 if (argvars[0].v_type != VAR_UNKNOWN
9990 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 num = (int)get_tv_number(&argvars[0]);
9993 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009994 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 }
9997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999#endif
10000}
10001
10002/*
10003 * "cursor(lnum, col)" function
10004 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010005 * Moves the cursor to the specified line and column.
10006 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *argvars;
10011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
10013 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010014#ifdef FEAT_VIRTUALEDIT
10015 long coladd = 0;
10016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010018 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010019 if (argvars[1].v_type == VAR_UNKNOWN)
10020 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010021 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010022 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010023
Bram Moolenaar493c1782014-05-28 14:34:46 +020010024 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010025 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010026 line = pos.lnum;
10027 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010028#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010029 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010030#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010031 if (curswant >= 0)
10032 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010033 }
10034 else
10035 {
10036 line = get_tv_lnum(argvars);
10037 col = get_tv_number_chk(&argvars[1], NULL);
10038#ifdef FEAT_VIRTUALEDIT
10039 if (argvars[2].v_type != VAR_UNKNOWN)
10040 coladd = get_tv_number_chk(&argvars[2], NULL);
10041#endif
10042 }
10043 if (line < 0 || col < 0
10044#ifdef FEAT_VIRTUALEDIT
10045 || coladd < 0
10046#endif
10047 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010048 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (line > 0)
10050 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 if (col > 0)
10052 curwin->w_cursor.col = col - 1;
10053#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010054 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055#endif
10056
10057 /* Make sure the cursor is in a valid position. */
10058 check_cursor();
10059#ifdef FEAT_MBYTE
10060 /* Correct cursor for multi-byte character. */
10061 if (has_mbyte)
10062 mb_adjust_cursor();
10063#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010064
10065 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010066 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067}
10068
10069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010070 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 */
10072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010074 typval_T *argvars;
10075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010077 int noref = 0;
10078
10079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010080 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010081 if (noref < 0 || noref > 1)
10082 EMSG(_(e_invarg));
10083 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010084 {
10085 current_copyID += COPYID_INC;
10086 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088}
10089
10090/*
10091 * "delete()" function
10092 */
10093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010095 typval_T *argvars;
10096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
10104/*
10105 * "did_filetype()" function
10106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010109 typval_T *argvars UNUSED;
10110 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114#endif
10115}
10116
10117/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010118 * "diff_filler()" function
10119 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010122 typval_T *argvars UNUSED;
10123 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010124{
10125#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010127#endif
10128}
10129
10130/*
10131 * "diff_hlID()" function
10132 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010135 typval_T *argvars UNUSED;
10136 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010137{
10138#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010140 static linenr_T prev_lnum = 0;
10141 static int changedtick = 0;
10142 static int fnum = 0;
10143 static int change_start = 0;
10144 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010145 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010146 int filler_lines;
10147 int col;
10148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010149 if (lnum < 0) /* ignore type error in {lnum} arg */
10150 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010151 if (lnum != prev_lnum
10152 || changedtick != curbuf->b_changedtick
10153 || fnum != curbuf->b_fnum)
10154 {
10155 /* New line, buffer, change: need to get the values. */
10156 filler_lines = diff_check(curwin, lnum);
10157 if (filler_lines < 0)
10158 {
10159 if (filler_lines == -1)
10160 {
10161 change_start = MAXCOL;
10162 change_end = -1;
10163 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10164 hlID = HLF_ADD; /* added line */
10165 else
10166 hlID = HLF_CHD; /* changed line */
10167 }
10168 else
10169 hlID = HLF_ADD; /* added line */
10170 }
10171 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010172 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010173 prev_lnum = lnum;
10174 changedtick = curbuf->b_changedtick;
10175 fnum = curbuf->b_fnum;
10176 }
10177
10178 if (hlID == HLF_CHD || hlID == HLF_TXD)
10179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010180 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010181 if (col >= change_start && col <= change_end)
10182 hlID = HLF_TXD; /* changed text */
10183 else
10184 hlID = HLF_CHD; /* changed line */
10185 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010186 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010187#endif
10188}
10189
10190/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010191 * "empty({expr})" function
10192 */
10193 static void
10194f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010195 typval_T *argvars;
10196 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010197{
10198 int n;
10199
10200 switch (argvars[0].v_type)
10201 {
10202 case VAR_STRING:
10203 case VAR_FUNC:
10204 n = argvars[0].vval.v_string == NULL
10205 || *argvars[0].vval.v_string == NUL;
10206 break;
10207 case VAR_NUMBER:
10208 n = argvars[0].vval.v_number == 0;
10209 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010210#ifdef FEAT_FLOAT
10211 case VAR_FLOAT:
10212 n = argvars[0].vval.v_float == 0.0;
10213 break;
10214#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010215 case VAR_LIST:
10216 n = argvars[0].vval.v_list == NULL
10217 || argvars[0].vval.v_list->lv_first == NULL;
10218 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 case VAR_DICT:
10220 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010221 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010223 default:
10224 EMSG2(_(e_intern2), "f_empty()");
10225 n = 0;
10226 }
10227
10228 rettv->vval.v_number = n;
10229}
10230
10231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 * "escape({string}, {chars})" function
10233 */
10234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 typval_T *argvars;
10237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238{
10239 char_u buf[NUMBUFLEN];
10240
Bram Moolenaar758711c2005-02-02 23:11:38 +000010241 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10242 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244}
10245
10246/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010247 * "eval()" function
10248 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249 static void
10250f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010251 typval_T *argvars;
10252 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010253{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010254 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 s = get_tv_string_chk(&argvars[0]);
10257 if (s != NULL)
10258 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259
Bram Moolenaar615b9972015-01-14 17:15:05 +010010260 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10262 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010263 if (p != NULL && !aborting())
10264 EMSG2(_(e_invexpr2), p);
10265 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269 else if (*s != NUL)
10270 EMSG(_(e_trailing));
10271}
10272
10273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 * "eventhandler()" function
10275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010278 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282}
10283
10284/*
10285 * "executable()" function
10286 */
10287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010289 typval_T *argvars;
10290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010292 char_u *name = get_tv_string(&argvars[0]);
10293
10294 /* Check in $PATH and also check directly if there is a directory name. */
10295 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10296 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010297}
10298
10299/*
10300 * "exepath()" function
10301 */
10302 static void
10303f_exepath(argvars, rettv)
10304 typval_T *argvars;
10305 typval_T *rettv;
10306{
10307 char_u *p = NULL;
10308
Bram Moolenaarb5971142015-03-21 17:32:19 +010010309 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010310 rettv->v_type = VAR_STRING;
10311 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312}
10313
10314/*
10315 * "exists()" function
10316 */
10317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010319 typval_T *argvars;
10320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
10322 char_u *p;
10323 char_u *name;
10324 int n = FALSE;
10325 int len = 0;
10326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010327 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 if (*p == '$') /* environment variable */
10329 {
10330 /* first try "normal" environment variables (fast) */
10331 if (mch_getenv(p + 1) != NULL)
10332 n = TRUE;
10333 else
10334 {
10335 /* try expanding things like $VIM and ${HOME} */
10336 p = expand_env_save(p);
10337 if (p != NULL && *p != '$')
10338 n = TRUE;
10339 vim_free(p);
10340 }
10341 }
10342 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010345 if (*skipwhite(p) != NUL)
10346 n = FALSE; /* trailing garbage */
10347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 else if (*p == '*') /* internal or user defined function */
10349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010350 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 }
10352 else if (*p == ':')
10353 {
10354 n = cmd_exists(p + 1);
10355 }
10356 else if (*p == '#')
10357 {
10358#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010359 if (p[1] == '#')
10360 n = autocmd_supported(p + 2);
10361 else
10362 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363#endif
10364 }
10365 else /* internal variable */
10366 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010367 char_u *tofree;
10368 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010370 /* get_name_len() takes care of expanding curly braces */
10371 name = p;
10372 len = get_name_len(&p, &tofree, TRUE, FALSE);
10373 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010375 if (tofree != NULL)
10376 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010377 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010378 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010380 /* handle d.key, l[idx], f(expr) */
10381 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10382 if (n)
10383 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 }
10385 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010386 if (*p != NUL)
10387 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010389 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 }
10391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393}
10394
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010395#ifdef FEAT_FLOAT
10396/*
10397 * "exp()" function
10398 */
10399 static void
10400f_exp(argvars, rettv)
10401 typval_T *argvars;
10402 typval_T *rettv;
10403{
10404 float_T f;
10405
10406 rettv->v_type = VAR_FLOAT;
10407 if (get_float_arg(argvars, &f) == OK)
10408 rettv->vval.v_float = exp(f);
10409 else
10410 rettv->vval.v_float = 0.0;
10411}
10412#endif
10413
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414/*
10415 * "expand()" function
10416 */
10417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 typval_T *argvars;
10420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421{
10422 char_u *s;
10423 int len;
10424 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010425 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010428 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010431 if (argvars[1].v_type != VAR_UNKNOWN
10432 && argvars[2].v_type != VAR_UNKNOWN
10433 && get_tv_number_chk(&argvars[2], &error)
10434 && !error)
10435 {
10436 rettv->v_type = VAR_LIST;
10437 rettv->vval.v_list = NULL;
10438 }
10439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 if (*s == '%' || *s == '#' || *s == '<')
10442 {
10443 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010444 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010446 if (rettv->v_type == VAR_LIST)
10447 {
10448 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10449 list_append_string(rettv->vval.v_list, result, -1);
10450 else
10451 vim_free(result);
10452 }
10453 else
10454 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 }
10456 else
10457 {
10458 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010459 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 if (argvars[1].v_type != VAR_UNKNOWN
10461 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010462 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 if (!error)
10464 {
10465 ExpandInit(&xpc);
10466 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010467 if (p_wic)
10468 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010469 if (rettv->v_type == VAR_STRING)
10470 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10471 options, WILD_ALL);
10472 else if (rettv_list_alloc(rettv) != FAIL)
10473 {
10474 int i;
10475
10476 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10477 for (i = 0; i < xpc.xp_numfiles; i++)
10478 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10479 ExpandCleanup(&xpc);
10480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 }
10482 else
10483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 }
10485}
10486
10487/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010488 * Go over all entries in "d2" and add them to "d1".
10489 * When "action" is "error" then a duplicate key is an error.
10490 * When "action" is "force" then a duplicate key is overwritten.
10491 * Otherwise duplicate keys are ignored ("action" is "keep").
10492 */
10493 void
10494dict_extend(d1, d2, action)
10495 dict_T *d1;
10496 dict_T *d2;
10497 char_u *action;
10498{
10499 dictitem_T *di1;
10500 hashitem_T *hi2;
10501 int todo;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010502 char *arg_errmsg = N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010503
10504 todo = (int)d2->dv_hashtab.ht_used;
10505 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10506 {
10507 if (!HASHITEM_EMPTY(hi2))
10508 {
10509 --todo;
10510 di1 = dict_find(d1, hi2->hi_key, -1);
10511 if (d1->dv_scope != 0)
10512 {
10513 /* Disallow replacing a builtin function in l: and g:.
10514 * Check the key to be valid when adding to any
10515 * scope. */
10516 if (d1->dv_scope == VAR_DEF_SCOPE
10517 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10518 && var_check_func_name(hi2->hi_key,
10519 di1 == NULL))
10520 break;
10521 if (!valid_varname(hi2->hi_key))
10522 break;
10523 }
10524 if (di1 == NULL)
10525 {
10526 di1 = dictitem_copy(HI2DI(hi2));
10527 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10528 dictitem_free(di1);
10529 }
10530 else if (*action == 'e')
10531 {
10532 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10533 break;
10534 }
10535 else if (*action == 'f' && HI2DI(hi2) != di1)
10536 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010537 if (tv_check_lock(di1->di_tv.v_lock, (char_u *)_(arg_errmsg))
10538 || var_check_ro(di1->di_flags, (char_u *)_(arg_errmsg)))
10539 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010540 clear_tv(&di1->di_tv);
10541 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10542 }
10543 }
10544 }
10545}
10546
10547/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010548 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010549 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010550 */
10551 static void
10552f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010553 typval_T *argvars;
10554 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010555{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010556 char *arg_errmsg = N_("extend() argument");
10557
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010559 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 list_T *l1, *l2;
10561 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010564
Bram Moolenaare9a41262005-01-15 22:18:47 +000010565 l1 = argvars[0].vval.v_list;
10566 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010567 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010568 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569 {
10570 if (argvars[2].v_type != VAR_UNKNOWN)
10571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 before = get_tv_number_chk(&argvars[2], &error);
10573 if (error)
10574 return; /* type error; errmsg already given */
10575
Bram Moolenaar758711c2005-02-02 23:11:38 +000010576 if (before == l1->lv_len)
10577 item = NULL;
10578 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010580 item = list_find(l1, before);
10581 if (item == NULL)
10582 {
10583 EMSGN(_(e_listidx), before);
10584 return;
10585 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 }
10587 }
10588 else
10589 item = NULL;
10590 list_extend(l1, l2, item);
10591
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 copy_tv(&argvars[0], rettv);
10593 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010594 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10596 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010597 dict_T *d1, *d2;
10598 char_u *action;
10599 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600
10601 d1 = argvars[0].vval.v_dict;
10602 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010603 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010604 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 {
10606 /* Check the third argument. */
10607 if (argvars[2].v_type != VAR_UNKNOWN)
10608 {
10609 static char *(av[]) = {"keep", "force", "error"};
10610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 action = get_tv_string_chk(&argvars[2]);
10612 if (action == NULL)
10613 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614 for (i = 0; i < 3; ++i)
10615 if (STRCMP(action, av[i]) == 0)
10616 break;
10617 if (i == 3)
10618 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010619 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010620 return;
10621 }
10622 }
10623 else
10624 action = (char_u *)"force";
10625
Bram Moolenaara9922d62013-05-30 13:01:18 +020010626 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010627
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 copy_tv(&argvars[0], rettv);
10629 }
10630 }
10631 else
10632 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010633}
10634
10635/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010636 * "feedkeys()" function
10637 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010638 static void
10639f_feedkeys(argvars, rettv)
10640 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010641 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010642{
10643 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010644 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010645 char_u *keys, *flags;
10646 char_u nbuf[NUMBUFLEN];
10647 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010648 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010649
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010650 /* This is not allowed in the sandbox. If the commands would still be
10651 * executed in the sandbox it would be OK, but it probably happens later,
10652 * when "sandbox" is no longer set. */
10653 if (check_secure())
10654 return;
10655
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010656 keys = get_tv_string(&argvars[0]);
10657 if (*keys != NUL)
10658 {
10659 if (argvars[1].v_type != VAR_UNKNOWN)
10660 {
10661 flags = get_tv_string_buf(&argvars[1], nbuf);
10662 for ( ; *flags != NUL; ++flags)
10663 {
10664 switch (*flags)
10665 {
10666 case 'n': remap = FALSE; break;
10667 case 'm': remap = TRUE; break;
10668 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010669 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010670 }
10671 }
10672 }
10673
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010674 /* Need to escape K_SPECIAL and CSI before putting the string in the
10675 * typeahead buffer. */
10676 keys_esc = vim_strsave_escape_csi(keys);
10677 if (keys_esc != NULL)
10678 {
10679 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010680 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010681 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010682 if (vgetc_busy)
10683 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010684 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010685 }
10686}
10687
10688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 * "filereadable()" function
10690 */
10691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010693 typval_T *argvars;
10694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010696 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 char_u *p;
10698 int n;
10699
Bram Moolenaarc236c162008-07-13 17:41:49 +000010700#ifndef O_NONBLOCK
10701# define O_NONBLOCK 0
10702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010704 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10705 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
10707 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010708 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 }
10710 else
10711 n = FALSE;
10712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714}
10715
10716/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010717 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 * rights to write into.
10719 */
10720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 typval_T *argvars;
10723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010725 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726}
10727
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010728static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010729
10730 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010731findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010733 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010734 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010735{
10736#ifdef FEAT_SEARCHPATH
10737 char_u *fname;
10738 char_u *fresult = NULL;
10739 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10740 char_u *p;
10741 char_u pathbuf[NUMBUFLEN];
10742 int count = 1;
10743 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010744 int error = FALSE;
10745#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010746
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010747 rettv->vval.v_string = NULL;
10748 rettv->v_type = VAR_STRING;
10749
10750#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010752
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010753 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10756 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010757 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 else
10759 {
10760 if (*p != NUL)
10761 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010764 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010765 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010766 }
10767
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010768 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10769 error = TRUE;
10770
10771 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 do
10774 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010775 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010776 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 fresult = find_file_in_path_option(first ? fname : NULL,
10778 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010779 0, first, path,
10780 find_what,
10781 curbuf->b_ffname,
10782 find_what == FINDFILE_DIR
10783 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010785
10786 if (fresult != NULL && rettv->v_type == VAR_LIST)
10787 list_append_string(rettv->vval.v_list, fresult, -1);
10788
10789 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010791
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010792 if (rettv->v_type == VAR_STRING)
10793 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010794#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010795}
10796
Bram Moolenaar33570922005-01-25 22:26:29 +000010797static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10798static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010799
10800/*
10801 * Implementation of map() and filter().
10802 */
10803 static void
10804filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010805 typval_T *argvars;
10806 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010807 int map;
10808{
10809 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010810 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 listitem_T *li, *nli;
10812 list_T *l = NULL;
10813 dictitem_T *di;
10814 hashtab_T *ht;
10815 hashitem_T *hi;
10816 dict_T *d = NULL;
10817 typval_T save_val;
10818 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010819 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010820 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010821 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10822 char *arg_errmsg = (map ? N_("map() argument")
10823 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010824 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010825 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010826
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010828 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010829 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010830 || (!map && tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg))))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 return;
10832 }
10833 else if (argvars[0].v_type == VAR_DICT)
10834 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010835 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010836 || (!map && tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg))))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 return;
10838 }
10839 else
10840 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010841 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 return;
10843 }
10844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 expr = get_tv_string_buf_chk(&argvars[1], buf);
10846 /* On type errors, the preceding call has already displayed an error
10847 * message. Avoid a misleading error message for an empty string that
10848 * was not passed as argument. */
10849 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 prepare_vimvar(VV_VAL, &save_val);
10852 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010853
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010854 /* We reset "did_emsg" to be able to detect whether an error
10855 * occurred during evaluation of the expression. */
10856 save_did_emsg = did_emsg;
10857 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010858
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010859 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010860 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 vimvars[VV_KEY].vv_type = VAR_STRING;
10863
10864 ht = &d->dv_hashtab;
10865 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010866 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010867 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 if (!HASHITEM_EMPTY(hi))
10870 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010871 int r;
10872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 --todo;
10874 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010875 if (map &&
10876 (tv_check_lock(di->di_tv.v_lock,
10877 (char_u *)_(arg_errmsg))
10878 || var_check_ro(di->di_flags,
10879 (char_u *)_(arg_errmsg))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 break;
10881 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010882 r = filter_map_one(&di->di_tv, expr, map, &rem);
10883 clear_tv(&vimvars[VV_KEY].vv_tv);
10884 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 break;
10886 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010887 {
10888 if (var_check_fixed(di->di_flags,
10889 (char_u *)_(arg_errmsg))
10890 || var_check_ro(di->di_flags,
10891 (char_u *)_(arg_errmsg)))
10892 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010894 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 }
10896 }
10897 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010898 }
10899 else
10900 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010901 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 for (li = l->lv_first; li != NULL; li = nli)
10904 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010905 if (map && tv_check_lock(li->li_tv.v_lock,
10906 (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010907 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010909 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010910 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010911 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010912 break;
10913 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010915 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010916 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010917 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010918
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010919 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010921
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010922 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010923 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924
10925 copy_tv(&argvars[0], rettv);
10926}
10927
10928 static int
10929filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010930 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010931 char_u *expr;
10932 int map;
10933 int *remp;
10934{
Bram Moolenaar33570922005-01-25 22:26:29 +000010935 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010936 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010937 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010938
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 s = expr;
10941 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010942 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 if (*s != NUL) /* check for trailing chars after expr */
10944 {
10945 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010946 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010947 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010948 }
10949 if (map)
10950 {
10951 /* map(): replace the list item value */
10952 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010953 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954 *tv = rettv;
10955 }
10956 else
10957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 int error = FALSE;
10959
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010962 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 /* On type error, nothing has been removed; return FAIL to stop the
10964 * loop. The error message was given by get_tv_number_chk(). */
10965 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010966 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010968 retval = OK;
10969theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010970 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010971 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010972}
10973
10974/*
10975 * "filter()" function
10976 */
10977 static void
10978f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010979 typval_T *argvars;
10980 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010981{
10982 filter_map(argvars, rettv, FALSE);
10983}
10984
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010985/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010986 * "finddir({fname}[, {path}[, {count}]])" function
10987 */
10988 static void
10989f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 typval_T *argvars;
10991 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010993 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994}
10995
10996/*
10997 * "findfile({fname}[, {path}[, {count}]])" function
10998 */
10999 static void
11000f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011001 typval_T *argvars;
11002 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011004 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005}
11006
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011007#ifdef FEAT_FLOAT
11008/*
11009 * "float2nr({float})" function
11010 */
11011 static void
11012f_float2nr(argvars, rettv)
11013 typval_T *argvars;
11014 typval_T *rettv;
11015{
11016 float_T f;
11017
11018 if (get_float_arg(argvars, &f) == OK)
11019 {
11020 if (f < -0x7fffffff)
11021 rettv->vval.v_number = -0x7fffffff;
11022 else if (f > 0x7fffffff)
11023 rettv->vval.v_number = 0x7fffffff;
11024 else
11025 rettv->vval.v_number = (varnumber_T)f;
11026 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011027}
11028
11029/*
11030 * "floor({float})" function
11031 */
11032 static void
11033f_floor(argvars, rettv)
11034 typval_T *argvars;
11035 typval_T *rettv;
11036{
11037 float_T f;
11038
11039 rettv->v_type = VAR_FLOAT;
11040 if (get_float_arg(argvars, &f) == OK)
11041 rettv->vval.v_float = floor(f);
11042 else
11043 rettv->vval.v_float = 0.0;
11044}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011045
11046/*
11047 * "fmod()" function
11048 */
11049 static void
11050f_fmod(argvars, rettv)
11051 typval_T *argvars;
11052 typval_T *rettv;
11053{
11054 float_T fx, fy;
11055
11056 rettv->v_type = VAR_FLOAT;
11057 if (get_float_arg(argvars, &fx) == OK
11058 && get_float_arg(&argvars[1], &fy) == OK)
11059 rettv->vval.v_float = fmod(fx, fy);
11060 else
11061 rettv->vval.v_float = 0.0;
11062}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011063#endif
11064
Bram Moolenaar0d660222005-01-07 21:51:51 +000011065/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011066 * "fnameescape({string})" function
11067 */
11068 static void
11069f_fnameescape(argvars, rettv)
11070 typval_T *argvars;
11071 typval_T *rettv;
11072{
11073 rettv->vval.v_string = vim_strsave_fnameescape(
11074 get_tv_string(&argvars[0]), FALSE);
11075 rettv->v_type = VAR_STRING;
11076}
11077
11078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 * "fnamemodify({fname}, {mods})" function
11080 */
11081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011083 typval_T *argvars;
11084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085{
11086 char_u *fname;
11087 char_u *mods;
11088 int usedlen = 0;
11089 int len;
11090 char_u *fbuf = NULL;
11091 char_u buf[NUMBUFLEN];
11092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011093 fname = get_tv_string_chk(&argvars[0]);
11094 mods = get_tv_string_buf_chk(&argvars[1], buf);
11095 if (fname == NULL || mods == NULL)
11096 fname = NULL;
11097 else
11098 {
11099 len = (int)STRLEN(fname);
11100 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 vim_free(fbuf);
11109}
11110
Bram Moolenaar33570922005-01-25 22:26:29 +000011111static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112
11113/*
11114 * "foldclosed()" function
11115 */
11116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011120 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121{
11122#ifdef FEAT_FOLDING
11123 linenr_T lnum;
11124 linenr_T first, last;
11125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11128 {
11129 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11130 {
11131 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 return;
11136 }
11137 }
11138#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140}
11141
11142/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143 * "foldclosed()" function
11144 */
11145 static void
11146f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149{
11150 foldclosed_both(argvars, rettv, FALSE);
11151}
11152
11153/*
11154 * "foldclosedend()" function
11155 */
11156 static void
11157f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 typval_T *argvars;
11159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011160{
11161 foldclosed_both(argvars, rettv, TRUE);
11162}
11163
11164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 * "foldlevel()" function
11166 */
11167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011169 typval_T *argvars UNUSED;
11170 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
11172#ifdef FEAT_FOLDING
11173 linenr_T lnum;
11174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179}
11180
11181/*
11182 * "foldtext()" function
11183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188{
11189#ifdef FEAT_FOLDING
11190 linenr_T lnum;
11191 char_u *s;
11192 char_u *r;
11193 int len;
11194 char *txt;
11195#endif
11196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011197 rettv->v_type = VAR_STRING;
11198 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011200 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11201 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11202 <= curbuf->b_ml.ml_line_count
11203 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 {
11205 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011206 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11207 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 {
11209 if (!linewhite(lnum))
11210 break;
11211 ++lnum;
11212 }
11213
11214 /* Find interesting text in this line. */
11215 s = skipwhite(ml_get(lnum));
11216 /* skip C comment-start */
11217 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011220 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011222 {
11223 s = skipwhite(ml_get(lnum + 1));
11224 if (*s == '*')
11225 s = skipwhite(s + 1);
11226 }
11227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 txt = _("+-%s%3ld lines: ");
11229 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011230 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 + 20 /* for %3ld */
11232 + STRLEN(s))); /* concatenated */
11233 if (r != NULL)
11234 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011235 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11236 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11237 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 len = (int)STRLEN(r);
11239 STRCAT(r, s);
11240 /* remove 'foldmarker' and 'commentstring' */
11241 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 }
11244 }
11245#endif
11246}
11247
11248/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011249 * "foldtextresult(lnum)" function
11250 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011254 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011255{
11256#ifdef FEAT_FOLDING
11257 linenr_T lnum;
11258 char_u *text;
11259 char_u buf[51];
11260 foldinfo_T foldinfo;
11261 int fold_count;
11262#endif
11263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->v_type = VAR_STRING;
11265 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011266#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011268 /* treat illegal types and illegal string values for {lnum} the same */
11269 if (lnum < 0)
11270 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011271 fold_count = foldedCount(curwin, lnum, &foldinfo);
11272 if (fold_count > 0)
11273 {
11274 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11275 &foldinfo, buf);
11276 if (text == buf)
11277 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011279 }
11280#endif
11281}
11282
11283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 * "foreground()" function
11285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011288 typval_T *argvars UNUSED;
11289 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291#ifdef FEAT_GUI
11292 if (gui.in_use)
11293 gui_mch_set_foreground();
11294#else
11295# ifdef WIN32
11296 win32_set_foreground();
11297# endif
11298#endif
11299}
11300
11301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011302 * "function()" function
11303 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011306 typval_T *argvars;
11307 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308{
11309 char_u *s;
11310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011312 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011313 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011314 /* Don't check an autoload name for existence here. */
11315 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011316 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011317 else
11318 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011319 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011320 {
11321 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011322 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011323
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011324 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11325 * also be called from another script. Using trans_function_name()
11326 * would also work, but some plugins depend on the name being
11327 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011328 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011329 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011330 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011331 if (rettv->vval.v_string != NULL)
11332 {
11333 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011334 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011335 }
11336 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011337 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011338 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011340 }
11341}
11342
11343/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011344 * "garbagecollect()" function
11345 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011346 static void
11347f_garbagecollect(argvars, rettv)
11348 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011349 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011350{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011351 /* This is postponed until we are back at the toplevel, because we may be
11352 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11353 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011354
11355 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11356 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011357}
11358
11359/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360 * "get()" function
11361 */
11362 static void
11363f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *argvars;
11365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366{
Bram Moolenaar33570922005-01-25 22:26:29 +000011367 listitem_T *li;
11368 list_T *l;
11369 dictitem_T *di;
11370 dict_T *d;
11371 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372
Bram Moolenaare9a41262005-01-15 22:18:47 +000011373 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011374 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011375 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 int error = FALSE;
11378
11379 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11380 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 else if (argvars[0].v_type == VAR_DICT)
11385 {
11386 if ((d = argvars[0].vval.v_dict) != NULL)
11387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011388 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 if (di != NULL)
11390 tv = &di->di_tv;
11391 }
11392 }
11393 else
11394 EMSG2(_(e_listdictarg), "get()");
11395
11396 if (tv == NULL)
11397 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011399 copy_tv(&argvars[2], rettv);
11400 }
11401 else
11402 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011403}
11404
Bram Moolenaar342337a2005-07-21 21:11:17 +000011405static 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 +000011406
11407/*
11408 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011409 * Return a range (from start to end) of lines in rettv from the specified
11410 * buffer.
11411 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011412 */
11413 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011414get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011415 buf_T *buf;
11416 linenr_T start;
11417 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011418 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011419 typval_T *rettv;
11420{
11421 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011422
Bram Moolenaar959a1432013-12-14 12:17:38 +010011423 rettv->v_type = VAR_STRING;
11424 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011425 if (retlist && rettv_list_alloc(rettv) == FAIL)
11426 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011427
11428 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11429 return;
11430
11431 if (!retlist)
11432 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011433 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11434 p = ml_get_buf(buf, start, FALSE);
11435 else
11436 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011437 rettv->vval.v_string = vim_strsave(p);
11438 }
11439 else
11440 {
11441 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011442 return;
11443
11444 if (start < 1)
11445 start = 1;
11446 if (end > buf->b_ml.ml_line_count)
11447 end = buf->b_ml.ml_line_count;
11448 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011449 if (list_append_string(rettv->vval.v_list,
11450 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011451 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011452 }
11453}
11454
11455/*
11456 * "getbufline()" function
11457 */
11458 static void
11459f_getbufline(argvars, rettv)
11460 typval_T *argvars;
11461 typval_T *rettv;
11462{
11463 linenr_T lnum;
11464 linenr_T end;
11465 buf_T *buf;
11466
11467 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11468 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011469 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011470 --emsg_off;
11471
Bram Moolenaar661b1822005-07-28 22:36:45 +000011472 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011473 if (argvars[2].v_type == VAR_UNKNOWN)
11474 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011475 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011476 end = get_tv_lnum_buf(&argvars[2], buf);
11477
Bram Moolenaar342337a2005-07-21 21:11:17 +000011478 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011479}
11480
Bram Moolenaar0d660222005-01-07 21:51:51 +000011481/*
11482 * "getbufvar()" function
11483 */
11484 static void
11485f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011486 typval_T *argvars;
11487 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011488{
11489 buf_T *buf;
11490 buf_T *save_curbuf;
11491 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011493 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011495 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11496 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011498 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011499
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011500 rettv->v_type = VAR_STRING;
11501 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502
11503 if (buf != NULL && varname != NULL)
11504 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011505 /* set curbuf to be our buf, temporarily */
11506 save_curbuf = curbuf;
11507 curbuf = buf;
11508
Bram Moolenaar0d660222005-01-07 21:51:51 +000011509 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011510 {
11511 if (get_option_tv(&varname, rettv, TRUE) == OK)
11512 done = TRUE;
11513 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011514 else if (STRCMP(varname, "changedtick") == 0)
11515 {
11516 rettv->v_type = VAR_NUMBER;
11517 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011518 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011520 else
11521 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011522 /* Look up the variable. */
11523 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11524 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11525 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011526 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011527 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011529 done = TRUE;
11530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011532
11533 /* restore previous notion of curbuf */
11534 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 }
11536
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011537 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11538 /* use the default value */
11539 copy_tv(&argvars[2], rettv);
11540
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541 --emsg_off;
11542}
11543
11544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 * "getchar()" function
11546 */
11547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T *argvars;
11550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551{
11552 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011553 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011555 /* Position the cursor. Needed after a message that ends in a space. */
11556 windgoto(msg_row, msg_col);
11557
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 ++no_mapping;
11559 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011560 for (;;)
11561 {
11562 if (argvars[0].v_type == VAR_UNKNOWN)
11563 /* getchar(): blocking wait. */
11564 n = safe_vgetc();
11565 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11566 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011567 n = vpeekc_any();
11568 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011569 /* illegal argument or getchar(0) and no char avail: return zero */
11570 n = 0;
11571 else
11572 /* getchar(0) and char avail: return char */
11573 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011574
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011575 if (n == K_IGNORE)
11576 continue;
11577 break;
11578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 --no_mapping;
11580 --allow_keys;
11581
Bram Moolenaar219b8702006-11-01 14:32:36 +000011582 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11583 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11584 vimvars[VV_MOUSE_COL].vv_nr = 0;
11585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 if (IS_SPECIAL(n) || mod_mask != 0)
11588 {
11589 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11590 int i = 0;
11591
11592 /* Turn a special key into three bytes, plus modifier. */
11593 if (mod_mask != 0)
11594 {
11595 temp[i++] = K_SPECIAL;
11596 temp[i++] = KS_MODIFIER;
11597 temp[i++] = mod_mask;
11598 }
11599 if (IS_SPECIAL(n))
11600 {
11601 temp[i++] = K_SPECIAL;
11602 temp[i++] = K_SECOND(n);
11603 temp[i++] = K_THIRD(n);
11604 }
11605#ifdef FEAT_MBYTE
11606 else if (has_mbyte)
11607 i += (*mb_char2bytes)(n, temp + i);
11608#endif
11609 else
11610 temp[i++] = n;
11611 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612 rettv->v_type = VAR_STRING;
11613 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011614
11615#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011616 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011617 {
11618 int row = mouse_row;
11619 int col = mouse_col;
11620 win_T *win;
11621 linenr_T lnum;
11622# ifdef FEAT_WINDOWS
11623 win_T *wp;
11624# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011625 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011626
11627 if (row >= 0 && col >= 0)
11628 {
11629 /* Find the window at the mouse coordinates and compute the
11630 * text position. */
11631 win = mouse_find_win(&row, &col);
11632 (void)mouse_comp_pos(win, &row, &col, &lnum);
11633# ifdef FEAT_WINDOWS
11634 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011635 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011636# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011637 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011638 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11639 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11640 }
11641 }
11642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 }
11644}
11645
11646/*
11647 * "getcharmod()" function
11648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011651 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655}
11656
11657/*
11658 * "getcmdline()" function
11659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 rettv->v_type = VAR_STRING;
11666 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667}
11668
11669/*
11670 * "getcmdpos()" function
11671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011674 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678}
11679
11680/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011681 * "getcmdtype()" function
11682 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011683 static void
11684f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011685 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011686 typval_T *rettv;
11687{
11688 rettv->v_type = VAR_STRING;
11689 rettv->vval.v_string = alloc(2);
11690 if (rettv->vval.v_string != NULL)
11691 {
11692 rettv->vval.v_string[0] = get_cmdline_type();
11693 rettv->vval.v_string[1] = NUL;
11694 }
11695}
11696
11697/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011698 * "getcmdwintype()" function
11699 */
11700 static void
11701f_getcmdwintype(argvars, rettv)
11702 typval_T *argvars UNUSED;
11703 typval_T *rettv;
11704{
11705 rettv->v_type = VAR_STRING;
11706 rettv->vval.v_string = NULL;
11707#ifdef FEAT_CMDWIN
11708 rettv->vval.v_string = alloc(2);
11709 if (rettv->vval.v_string != NULL)
11710 {
11711 rettv->vval.v_string[0] = cmdwin_type;
11712 rettv->vval.v_string[1] = NUL;
11713 }
11714#endif
11715}
11716
11717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 * "getcwd()" function
11719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011725 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011728 rettv->vval.v_string = NULL;
11729 cwd = alloc(MAXPATHL);
11730 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011732 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11733 {
11734 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011736 if (rettv->vval.v_string != NULL)
11737 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011739 }
11740 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 }
11742}
11743
11744/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011745 * "getfontname()" function
11746 */
11747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011750 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->v_type = VAR_STRING;
11753 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011754#ifdef FEAT_GUI
11755 if (gui.in_use)
11756 {
11757 GuiFont font;
11758 char_u *name = NULL;
11759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011760 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011761 {
11762 /* Get the "Normal" font. Either the name saved by
11763 * hl_set_font_name() or from the font ID. */
11764 font = gui.norm_font;
11765 name = hl_get_font_name();
11766 }
11767 else
11768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011770 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11771 return;
11772 font = gui_mch_get_font(name, FALSE);
11773 if (font == NOFONT)
11774 return; /* Invalid font name, return empty string. */
11775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011777 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011778 gui_mch_free_font(font);
11779 }
11780#endif
11781}
11782
11783/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011784 * "getfperm({fname})" function
11785 */
11786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011788 typval_T *argvars;
11789 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011790{
11791 char_u *fname;
11792 struct stat st;
11793 char_u *perm = NULL;
11794 char_u flags[] = "rwx";
11795 int i;
11796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011800 if (mch_stat((char *)fname, &st) >= 0)
11801 {
11802 perm = vim_strsave((char_u *)"---------");
11803 if (perm != NULL)
11804 {
11805 for (i = 0; i < 9; i++)
11806 {
11807 if (st.st_mode & (1 << (8 - i)))
11808 perm[i] = flags[i % 3];
11809 }
11810 }
11811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011813}
11814
11815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 * "getfsize({fname})" function
11817 */
11818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011819f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011820 typval_T *argvars;
11821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822{
11823 char_u *fname;
11824 struct stat st;
11825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829
11830 if (mch_stat((char *)fname, &st) >= 0)
11831 {
11832 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011837
11838 /* non-perfect check for overflow */
11839 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11840 rettv->vval.v_number = -2;
11841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 }
11843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845}
11846
11847/*
11848 * "getftime({fname})" function
11849 */
11850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011852 typval_T *argvars;
11853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854{
11855 char_u *fname;
11856 struct stat st;
11857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859
11860 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864}
11865
11866/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011867 * "getftype({fname})" function
11868 */
11869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011871 typval_T *argvars;
11872 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011873{
11874 char_u *fname;
11875 struct stat st;
11876 char_u *type = NULL;
11877 char *t;
11878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011881 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011882 if (mch_lstat((char *)fname, &st) >= 0)
11883 {
11884#ifdef S_ISREG
11885 if (S_ISREG(st.st_mode))
11886 t = "file";
11887 else if (S_ISDIR(st.st_mode))
11888 t = "dir";
11889# ifdef S_ISLNK
11890 else if (S_ISLNK(st.st_mode))
11891 t = "link";
11892# endif
11893# ifdef S_ISBLK
11894 else if (S_ISBLK(st.st_mode))
11895 t = "bdev";
11896# endif
11897# ifdef S_ISCHR
11898 else if (S_ISCHR(st.st_mode))
11899 t = "cdev";
11900# endif
11901# ifdef S_ISFIFO
11902 else if (S_ISFIFO(st.st_mode))
11903 t = "fifo";
11904# endif
11905# ifdef S_ISSOCK
11906 else if (S_ISSOCK(st.st_mode))
11907 t = "fifo";
11908# endif
11909 else
11910 t = "other";
11911#else
11912# ifdef S_IFMT
11913 switch (st.st_mode & S_IFMT)
11914 {
11915 case S_IFREG: t = "file"; break;
11916 case S_IFDIR: t = "dir"; break;
11917# ifdef S_IFLNK
11918 case S_IFLNK: t = "link"; break;
11919# endif
11920# ifdef S_IFBLK
11921 case S_IFBLK: t = "bdev"; break;
11922# endif
11923# ifdef S_IFCHR
11924 case S_IFCHR: t = "cdev"; break;
11925# endif
11926# ifdef S_IFIFO
11927 case S_IFIFO: t = "fifo"; break;
11928# endif
11929# ifdef S_IFSOCK
11930 case S_IFSOCK: t = "socket"; break;
11931# endif
11932 default: t = "other";
11933 }
11934# else
11935 if (mch_isdir(fname))
11936 t = "dir";
11937 else
11938 t = "file";
11939# endif
11940#endif
11941 type = vim_strsave((char_u *)t);
11942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011944}
11945
11946/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011947 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948 */
11949 static void
11950f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 typval_T *argvars;
11952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011953{
11954 linenr_T lnum;
11955 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011956 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011957
11958 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011959 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011960 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011961 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011962 retlist = FALSE;
11963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011964 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011965 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011966 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011967 retlist = TRUE;
11968 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011969
Bram Moolenaar342337a2005-07-21 21:11:17 +000011970 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011971}
11972
11973/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011974 * "getmatches()" function
11975 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011976 static void
11977f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011978 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011979 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011980{
11981#ifdef FEAT_SEARCH_EXTRA
11982 dict_T *dict;
11983 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011984 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011985
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011986 if (rettv_list_alloc(rettv) == OK)
11987 {
11988 while (cur != NULL)
11989 {
11990 dict = dict_alloc();
11991 if (dict == NULL)
11992 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011993 if (cur->match.regprog == NULL)
11994 {
11995 /* match added with matchaddpos() */
11996 for (i = 0; i < MAXPOSMATCH; ++i)
11997 {
11998 llpos_T *llpos;
11999 char buf[6];
12000 list_T *l;
12001
12002 llpos = &cur->pos.pos[i];
12003 if (llpos->lnum == 0)
12004 break;
12005 l = list_alloc();
12006 if (l == NULL)
12007 break;
12008 list_append_number(l, (varnumber_T)llpos->lnum);
12009 if (llpos->col > 0)
12010 {
12011 list_append_number(l, (varnumber_T)llpos->col);
12012 list_append_number(l, (varnumber_T)llpos->len);
12013 }
12014 sprintf(buf, "pos%d", i + 1);
12015 dict_add_list(dict, buf, l);
12016 }
12017 }
12018 else
12019 {
12020 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12021 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012022 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012023 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12024 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12025 list_append_dict(rettv->vval.v_list, dict);
12026 cur = cur->next;
12027 }
12028 }
12029#endif
12030}
12031
12032/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012033 * "getpid()" function
12034 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012035 static void
12036f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012037 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012038 typval_T *rettv;
12039{
12040 rettv->vval.v_number = mch_get_pid();
12041}
12042
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012043static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12044
12045/*
12046 * "getcurpos()" function
12047 */
12048 static void
12049f_getcurpos(argvars, rettv)
12050 typval_T *argvars;
12051 typval_T *rettv;
12052{
12053 getpos_both(argvars, rettv, TRUE);
12054}
12055
Bram Moolenaar18081e32008-02-20 19:11:07 +000012056/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012057 * "getpos(string)" function
12058 */
12059 static void
12060f_getpos(argvars, rettv)
12061 typval_T *argvars;
12062 typval_T *rettv;
12063{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012064 getpos_both(argvars, rettv, FALSE);
12065}
12066
12067 static void
12068getpos_both(argvars, rettv, getcurpos)
12069 typval_T *argvars;
12070 typval_T *rettv;
12071 int getcurpos;
12072{
Bram Moolenaara5525202006-03-02 22:52:09 +000012073 pos_T *fp;
12074 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012075 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012076
12077 if (rettv_list_alloc(rettv) == OK)
12078 {
12079 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012080 if (getcurpos)
12081 fp = &curwin->w_cursor;
12082 else
12083 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012084 if (fnum != -1)
12085 list_append_number(l, (varnumber_T)fnum);
12086 else
12087 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012088 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12089 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012090 list_append_number(l, (fp != NULL)
12091 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012092 : (varnumber_T)0);
12093 list_append_number(l,
12094#ifdef FEAT_VIRTUALEDIT
12095 (fp != NULL) ? (varnumber_T)fp->coladd :
12096#endif
12097 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012098 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012099 list_append_number(l, curwin->w_curswant == MAXCOL ?
12100 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012101 }
12102 else
12103 rettv->vval.v_number = FALSE;
12104}
12105
12106/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012107 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012108 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012109 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012110f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012111 typval_T *argvars UNUSED;
12112 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012113{
12114#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012115 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012116#endif
12117
Bram Moolenaar2641f772005-03-25 21:58:17 +000012118#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012119 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012120 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012121 wp = NULL;
12122 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12123 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012124 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012125 if (wp == NULL)
12126 return;
12127 }
12128
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012129 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012130 }
12131#endif
12132}
12133
12134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 * "getreg()" function
12136 */
12137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 typval_T *argvars;
12140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141{
12142 char_u *strregname;
12143 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012144 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012145 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012148 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 strregname = get_tv_string_chk(&argvars[0]);
12151 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012152 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012154 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012155 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12156 return_list = get_tv_number_chk(&argvars[2], &error);
12157 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012160 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012161
12162 if (error)
12163 return;
12164
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 regname = (strregname == NULL ? '"' : *strregname);
12166 if (regname == 0)
12167 regname = '"';
12168
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012169 if (return_list)
12170 {
12171 rettv->v_type = VAR_LIST;
12172 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12173 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012174 if (rettv->vval.v_list != NULL)
12175 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012176 }
12177 else
12178 {
12179 rettv->v_type = VAR_STRING;
12180 rettv->vval.v_string = get_reg_contents(regname,
12181 arg2 ? GREG_EXPR_SRC : 0);
12182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183}
12184
12185/*
12186 * "getregtype()" function
12187 */
12188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 typval_T *argvars;
12191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192{
12193 char_u *strregname;
12194 int regname;
12195 char_u buf[NUMBUFLEN + 2];
12196 long reglen = 0;
12197
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012198 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012199 {
12200 strregname = get_tv_string_chk(&argvars[0]);
12201 if (strregname == NULL) /* type error; errmsg already given */
12202 {
12203 rettv->v_type = VAR_STRING;
12204 rettv->vval.v_string = NULL;
12205 return;
12206 }
12207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 else
12209 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012210 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211
12212 regname = (strregname == NULL ? '"' : *strregname);
12213 if (regname == 0)
12214 regname = '"';
12215
12216 buf[0] = NUL;
12217 buf[1] = NUL;
12218 switch (get_reg_type(regname, &reglen))
12219 {
12220 case MLINE: buf[0] = 'V'; break;
12221 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 case MBLOCK:
12223 buf[0] = Ctrl_V;
12224 sprintf((char *)buf + 1, "%ld", reglen + 1);
12225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->v_type = VAR_STRING;
12228 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229}
12230
12231/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012232 * "gettabvar()" function
12233 */
12234 static void
12235f_gettabvar(argvars, rettv)
12236 typval_T *argvars;
12237 typval_T *rettv;
12238{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012239 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012240 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012241 dictitem_T *v;
12242 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012243 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012244
12245 rettv->v_type = VAR_STRING;
12246 rettv->vval.v_string = NULL;
12247
12248 varname = get_tv_string_chk(&argvars[1]);
12249 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12250 if (tp != NULL && varname != NULL)
12251 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012252 /* Set tp to be our tabpage, temporarily. Also set the window to the
12253 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012254 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12255 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012256 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012257 /* look up the variable */
12258 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12259 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12260 if (v != NULL)
12261 {
12262 copy_tv(&v->di_tv, rettv);
12263 done = TRUE;
12264 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012265 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012266
12267 /* restore previous notion of curwin */
12268 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012269 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012270
12271 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012272 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012273}
12274
12275/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012276 * "gettabwinvar()" function
12277 */
12278 static void
12279f_gettabwinvar(argvars, rettv)
12280 typval_T *argvars;
12281 typval_T *rettv;
12282{
12283 getwinvar(argvars, rettv, 1);
12284}
12285
12286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 * "getwinposx()" function
12288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295#ifdef FEAT_GUI
12296 if (gui.in_use)
12297 {
12298 int x, y;
12299
12300 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 }
12303#endif
12304}
12305
12306/*
12307 * "getwinposy()" function
12308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012311 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315#ifdef FEAT_GUI
12316 if (gui.in_use)
12317 {
12318 int x, y;
12319
12320 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 }
12323#endif
12324}
12325
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012326/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012327 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012328 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012329 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012330find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012331 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012332 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012333{
12334#ifdef FEAT_WINDOWS
12335 win_T *wp;
12336#endif
12337 int nr;
12338
12339 nr = get_tv_number_chk(vp, NULL);
12340
12341#ifdef FEAT_WINDOWS
12342 if (nr < 0)
12343 return NULL;
12344 if (nr == 0)
12345 return curwin;
12346
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012347 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12348 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012349 if (--nr <= 0)
12350 break;
12351 return wp;
12352#else
12353 if (nr == 0 || nr == 1)
12354 return curwin;
12355 return NULL;
12356#endif
12357}
12358
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359/*
12360 * "getwinvar()" function
12361 */
12362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012364 typval_T *argvars;
12365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012367 getwinvar(argvars, rettv, 0);
12368}
12369
12370/*
12371 * getwinvar() and gettabwinvar()
12372 */
12373 static void
12374getwinvar(argvars, rettv, off)
12375 typval_T *argvars;
12376 typval_T *rettv;
12377 int off; /* 1 for gettabwinvar() */
12378{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 win_T *win, *oldcurwin;
12380 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012382 tabpage_T *tp = NULL;
12383 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012384 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012386#ifdef FEAT_WINDOWS
12387 if (off == 1)
12388 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12389 else
12390 tp = curtab;
12391#endif
12392 win = find_win_by_nr(&argvars[off], tp);
12393 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012394 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012396 rettv->v_type = VAR_STRING;
12397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398
12399 if (win != NULL && varname != NULL)
12400 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012401 /* Set curwin to be our win, temporarily. Also set the tabpage,
12402 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012403 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012404 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012405 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012406 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012407 if (get_option_tv(&varname, rettv, 1) == OK)
12408 done = TRUE;
12409 }
12410 else
12411 {
12412 /* Look up the variable. */
12413 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12414 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12415 varname, FALSE);
12416 if (v != NULL)
12417 {
12418 copy_tv(&v->di_tv, rettv);
12419 done = TRUE;
12420 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012423
12424 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012425 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 }
12427
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012428 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12429 /* use the default return value */
12430 copy_tv(&argvars[off + 2], rettv);
12431
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 --emsg_off;
12433}
12434
12435/*
12436 * "glob()" function
12437 */
12438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 typval_T *argvars;
12441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012443 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012445 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012447 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012448 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012450 if (argvars[1].v_type != VAR_UNKNOWN)
12451 {
12452 if (get_tv_number_chk(&argvars[1], &error))
12453 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012454 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012455 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012456 if (get_tv_number_chk(&argvars[2], &error))
12457 {
12458 rettv->v_type = VAR_LIST;
12459 rettv->vval.v_list = NULL;
12460 }
12461 if (argvars[3].v_type != VAR_UNKNOWN
12462 && get_tv_number_chk(&argvars[3], &error))
12463 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012464 }
12465 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012466 if (!error)
12467 {
12468 ExpandInit(&xpc);
12469 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012470 if (p_wic)
12471 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012472 if (rettv->v_type == VAR_STRING)
12473 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012474 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012475 else if (rettv_list_alloc(rettv) != FAIL)
12476 {
12477 int i;
12478
12479 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12480 NULL, options, WILD_ALL_KEEP);
12481 for (i = 0; i < xpc.xp_numfiles; i++)
12482 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12483
12484 ExpandCleanup(&xpc);
12485 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012486 }
12487 else
12488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489}
12490
12491/*
12492 * "globpath()" function
12493 */
12494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 typval_T *argvars;
12497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012499 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012502 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012503 garray_T ga;
12504 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012506 /* When the optional second argument is non-zero, don't remove matches
12507 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012509 if (argvars[2].v_type != VAR_UNKNOWN)
12510 {
12511 if (get_tv_number_chk(&argvars[2], &error))
12512 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012513 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012514 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012515 if (get_tv_number_chk(&argvars[3], &error))
12516 {
12517 rettv->v_type = VAR_LIST;
12518 rettv->vval.v_list = NULL;
12519 }
12520 if (argvars[4].v_type != VAR_UNKNOWN
12521 && get_tv_number_chk(&argvars[4], &error))
12522 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012523 }
12524 }
12525 if (file != NULL && !error)
12526 {
12527 ga_init2(&ga, (int)sizeof(char_u *), 10);
12528 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12529 if (rettv->v_type == VAR_STRING)
12530 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12531 else if (rettv_list_alloc(rettv) != FAIL)
12532 for (i = 0; i < ga.ga_len; ++i)
12533 list_append_string(rettv->vval.v_list,
12534 ((char_u **)(ga.ga_data))[i], -1);
12535 ga_clear_strings(&ga);
12536 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012538 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539}
12540
12541/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012542 * "glob2regpat()" function
12543 */
12544 static void
12545f_glob2regpat(argvars, rettv)
12546 typval_T *argvars;
12547 typval_T *rettv;
12548{
12549 char_u *pat = get_tv_string_chk(&argvars[0]);
12550
12551 rettv->v_type = VAR_STRING;
12552 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12553}
12554
12555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 * "has()" function
12557 */
12558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 typval_T *argvars;
12561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
12563 int i;
12564 char_u *name;
12565 int n = FALSE;
12566 static char *(has_list[]) =
12567 {
12568#ifdef AMIGA
12569 "amiga",
12570# ifdef FEAT_ARP
12571 "arp",
12572# endif
12573#endif
12574#ifdef __BEOS__
12575 "beos",
12576#endif
12577#ifdef MSDOS
12578# ifdef DJGPP
12579 "dos32",
12580# else
12581 "dos16",
12582# endif
12583#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012584#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 "mac",
12586#endif
12587#if defined(MACOS_X_UNIX)
12588 "macunix",
12589#endif
12590#ifdef OS2
12591 "os2",
12592#endif
12593#ifdef __QNX__
12594 "qnx",
12595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#ifdef UNIX
12597 "unix",
12598#endif
12599#ifdef VMS
12600 "vms",
12601#endif
12602#ifdef WIN16
12603 "win16",
12604#endif
12605#ifdef WIN32
12606 "win32",
12607#endif
12608#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12609 "win32unix",
12610#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012611#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 "win64",
12613#endif
12614#ifdef EBCDIC
12615 "ebcdic",
12616#endif
12617#ifndef CASE_INSENSITIVE_FILENAME
12618 "fname_case",
12619#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012620#ifdef HAVE_ACL
12621 "acl",
12622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623#ifdef FEAT_ARABIC
12624 "arabic",
12625#endif
12626#ifdef FEAT_AUTOCMD
12627 "autocmd",
12628#endif
12629#ifdef FEAT_BEVAL
12630 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012631# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12632 "balloon_multiline",
12633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#endif
12635#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12636 "builtin_terms",
12637# ifdef ALL_BUILTIN_TCAPS
12638 "all_builtin_terms",
12639# endif
12640#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012641#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12642 || defined(FEAT_GUI_W32) \
12643 || defined(FEAT_GUI_MOTIF))
12644 "browsefilter",
12645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646#ifdef FEAT_BYTEOFF
12647 "byte_offset",
12648#endif
12649#ifdef FEAT_CINDENT
12650 "cindent",
12651#endif
12652#ifdef FEAT_CLIENTSERVER
12653 "clientserver",
12654#endif
12655#ifdef FEAT_CLIPBOARD
12656 "clipboard",
12657#endif
12658#ifdef FEAT_CMDL_COMPL
12659 "cmdline_compl",
12660#endif
12661#ifdef FEAT_CMDHIST
12662 "cmdline_hist",
12663#endif
12664#ifdef FEAT_COMMENTS
12665 "comments",
12666#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012667#ifdef FEAT_CONCEAL
12668 "conceal",
12669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670#ifdef FEAT_CRYPT
12671 "cryptv",
12672#endif
12673#ifdef FEAT_CSCOPE
12674 "cscope",
12675#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012676#ifdef FEAT_CURSORBIND
12677 "cursorbind",
12678#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012679#ifdef CURSOR_SHAPE
12680 "cursorshape",
12681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682#ifdef DEBUG
12683 "debug",
12684#endif
12685#ifdef FEAT_CON_DIALOG
12686 "dialog_con",
12687#endif
12688#ifdef FEAT_GUI_DIALOG
12689 "dialog_gui",
12690#endif
12691#ifdef FEAT_DIFF
12692 "diff",
12693#endif
12694#ifdef FEAT_DIGRAPHS
12695 "digraphs",
12696#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012697#ifdef FEAT_DIRECTX
12698 "directx",
12699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700#ifdef FEAT_DND
12701 "dnd",
12702#endif
12703#ifdef FEAT_EMACS_TAGS
12704 "emacs_tags",
12705#endif
12706 "eval", /* always present, of course! */
12707#ifdef FEAT_EX_EXTRA
12708 "ex_extra",
12709#endif
12710#ifdef FEAT_SEARCH_EXTRA
12711 "extra_search",
12712#endif
12713#ifdef FEAT_FKMAP
12714 "farsi",
12715#endif
12716#ifdef FEAT_SEARCHPATH
12717 "file_in_path",
12718#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012719#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012720 "filterpipe",
12721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#ifdef FEAT_FIND_ID
12723 "find_in_path",
12724#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012725#ifdef FEAT_FLOAT
12726 "float",
12727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728#ifdef FEAT_FOLDING
12729 "folding",
12730#endif
12731#ifdef FEAT_FOOTER
12732 "footer",
12733#endif
12734#if !defined(USE_SYSTEM) && defined(UNIX)
12735 "fork",
12736#endif
12737#ifdef FEAT_GETTEXT
12738 "gettext",
12739#endif
12740#ifdef FEAT_GUI
12741 "gui",
12742#endif
12743#ifdef FEAT_GUI_ATHENA
12744# ifdef FEAT_GUI_NEXTAW
12745 "gui_neXtaw",
12746# else
12747 "gui_athena",
12748# endif
12749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750#ifdef FEAT_GUI_GTK
12751 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012754#ifdef FEAT_GUI_GNOME
12755 "gui_gnome",
12756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757#ifdef FEAT_GUI_MAC
12758 "gui_mac",
12759#endif
12760#ifdef FEAT_GUI_MOTIF
12761 "gui_motif",
12762#endif
12763#ifdef FEAT_GUI_PHOTON
12764 "gui_photon",
12765#endif
12766#ifdef FEAT_GUI_W16
12767 "gui_win16",
12768#endif
12769#ifdef FEAT_GUI_W32
12770 "gui_win32",
12771#endif
12772#ifdef FEAT_HANGULIN
12773 "hangul_input",
12774#endif
12775#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12776 "iconv",
12777#endif
12778#ifdef FEAT_INS_EXPAND
12779 "insert_expand",
12780#endif
12781#ifdef FEAT_JUMPLIST
12782 "jumplist",
12783#endif
12784#ifdef FEAT_KEYMAP
12785 "keymap",
12786#endif
12787#ifdef FEAT_LANGMAP
12788 "langmap",
12789#endif
12790#ifdef FEAT_LIBCALL
12791 "libcall",
12792#endif
12793#ifdef FEAT_LINEBREAK
12794 "linebreak",
12795#endif
12796#ifdef FEAT_LISP
12797 "lispindent",
12798#endif
12799#ifdef FEAT_LISTCMDS
12800 "listcmds",
12801#endif
12802#ifdef FEAT_LOCALMAP
12803 "localmap",
12804#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012805#ifdef FEAT_LUA
12806# ifndef DYNAMIC_LUA
12807 "lua",
12808# endif
12809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810#ifdef FEAT_MENU
12811 "menu",
12812#endif
12813#ifdef FEAT_SESSION
12814 "mksession",
12815#endif
12816#ifdef FEAT_MODIFY_FNAME
12817 "modify_fname",
12818#endif
12819#ifdef FEAT_MOUSE
12820 "mouse",
12821#endif
12822#ifdef FEAT_MOUSESHAPE
12823 "mouseshape",
12824#endif
12825#if defined(UNIX) || defined(VMS)
12826# ifdef FEAT_MOUSE_DEC
12827 "mouse_dec",
12828# endif
12829# ifdef FEAT_MOUSE_GPM
12830 "mouse_gpm",
12831# endif
12832# ifdef FEAT_MOUSE_JSB
12833 "mouse_jsbterm",
12834# endif
12835# ifdef FEAT_MOUSE_NET
12836 "mouse_netterm",
12837# endif
12838# ifdef FEAT_MOUSE_PTERM
12839 "mouse_pterm",
12840# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012841# ifdef FEAT_MOUSE_SGR
12842 "mouse_sgr",
12843# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012844# ifdef FEAT_SYSMOUSE
12845 "mouse_sysmouse",
12846# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012847# ifdef FEAT_MOUSE_URXVT
12848 "mouse_urxvt",
12849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850# ifdef FEAT_MOUSE_XTERM
12851 "mouse_xterm",
12852# endif
12853#endif
12854#ifdef FEAT_MBYTE
12855 "multi_byte",
12856#endif
12857#ifdef FEAT_MBYTE_IME
12858 "multi_byte_ime",
12859#endif
12860#ifdef FEAT_MULTI_LANG
12861 "multi_lang",
12862#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012863#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012864#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012865 "mzscheme",
12866#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868#ifdef FEAT_OLE
12869 "ole",
12870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871#ifdef FEAT_PATH_EXTRA
12872 "path_extra",
12873#endif
12874#ifdef FEAT_PERL
12875#ifndef DYNAMIC_PERL
12876 "perl",
12877#endif
12878#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012879#ifdef FEAT_PERSISTENT_UNDO
12880 "persistent_undo",
12881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882#ifdef FEAT_PYTHON
12883#ifndef DYNAMIC_PYTHON
12884 "python",
12885#endif
12886#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012887#ifdef FEAT_PYTHON3
12888#ifndef DYNAMIC_PYTHON3
12889 "python3",
12890#endif
12891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892#ifdef FEAT_POSTSCRIPT
12893 "postscript",
12894#endif
12895#ifdef FEAT_PRINTER
12896 "printer",
12897#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012898#ifdef FEAT_PROFILE
12899 "profile",
12900#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012901#ifdef FEAT_RELTIME
12902 "reltime",
12903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904#ifdef FEAT_QUICKFIX
12905 "quickfix",
12906#endif
12907#ifdef FEAT_RIGHTLEFT
12908 "rightleft",
12909#endif
12910#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12911 "ruby",
12912#endif
12913#ifdef FEAT_SCROLLBIND
12914 "scrollbind",
12915#endif
12916#ifdef FEAT_CMDL_INFO
12917 "showcmd",
12918 "cmdline_info",
12919#endif
12920#ifdef FEAT_SIGNS
12921 "signs",
12922#endif
12923#ifdef FEAT_SMARTINDENT
12924 "smartindent",
12925#endif
12926#ifdef FEAT_SNIFF
12927 "sniff",
12928#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012929#ifdef STARTUPTIME
12930 "startuptime",
12931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932#ifdef FEAT_STL_OPT
12933 "statusline",
12934#endif
12935#ifdef FEAT_SUN_WORKSHOP
12936 "sun_workshop",
12937#endif
12938#ifdef FEAT_NETBEANS_INTG
12939 "netbeans_intg",
12940#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012941#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012942 "spell",
12943#endif
12944#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 "syntax",
12946#endif
12947#if defined(USE_SYSTEM) || !defined(UNIX)
12948 "system",
12949#endif
12950#ifdef FEAT_TAG_BINS
12951 "tag_binary",
12952#endif
12953#ifdef FEAT_TAG_OLDSTATIC
12954 "tag_old_static",
12955#endif
12956#ifdef FEAT_TAG_ANYWHITE
12957 "tag_any_white",
12958#endif
12959#ifdef FEAT_TCL
12960# ifndef DYNAMIC_TCL
12961 "tcl",
12962# endif
12963#endif
12964#ifdef TERMINFO
12965 "terminfo",
12966#endif
12967#ifdef FEAT_TERMRESPONSE
12968 "termresponse",
12969#endif
12970#ifdef FEAT_TEXTOBJ
12971 "textobjects",
12972#endif
12973#ifdef HAVE_TGETENT
12974 "tgetent",
12975#endif
12976#ifdef FEAT_TITLE
12977 "title",
12978#endif
12979#ifdef FEAT_TOOLBAR
12980 "toolbar",
12981#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012982#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12983 "unnamedplus",
12984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985#ifdef FEAT_USR_CMDS
12986 "user-commands", /* was accidentally included in 5.4 */
12987 "user_commands",
12988#endif
12989#ifdef FEAT_VIMINFO
12990 "viminfo",
12991#endif
12992#ifdef FEAT_VERTSPLIT
12993 "vertsplit",
12994#endif
12995#ifdef FEAT_VIRTUALEDIT
12996 "virtualedit",
12997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999#ifdef FEAT_VISUALEXTRA
13000 "visualextra",
13001#endif
13002#ifdef FEAT_VREPLACE
13003 "vreplace",
13004#endif
13005#ifdef FEAT_WILDIGN
13006 "wildignore",
13007#endif
13008#ifdef FEAT_WILDMENU
13009 "wildmenu",
13010#endif
13011#ifdef FEAT_WINDOWS
13012 "windows",
13013#endif
13014#ifdef FEAT_WAK
13015 "winaltkeys",
13016#endif
13017#ifdef FEAT_WRITEBACKUP
13018 "writebackup",
13019#endif
13020#ifdef FEAT_XIM
13021 "xim",
13022#endif
13023#ifdef FEAT_XFONTSET
13024 "xfontset",
13025#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013026#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013027 "xpm",
13028 "xpm_w32", /* for backward compatibility */
13029#else
13030# if defined(HAVE_XPM)
13031 "xpm",
13032# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034#ifdef USE_XSMP
13035 "xsmp",
13036#endif
13037#ifdef USE_XSMP_INTERACT
13038 "xsmp_interact",
13039#endif
13040#ifdef FEAT_XCLIPBOARD
13041 "xterm_clipboard",
13042#endif
13043#ifdef FEAT_XTERM_SAVE
13044 "xterm_save",
13045#endif
13046#if defined(UNIX) && defined(FEAT_X11)
13047 "X11",
13048#endif
13049 NULL
13050 };
13051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 for (i = 0; has_list[i] != NULL; ++i)
13054 if (STRICMP(name, has_list[i]) == 0)
13055 {
13056 n = TRUE;
13057 break;
13058 }
13059
13060 if (n == FALSE)
13061 {
13062 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013063 {
13064 if (name[5] == '-'
13065 && STRLEN(name) > 11
13066 && vim_isdigit(name[6])
13067 && vim_isdigit(name[8])
13068 && vim_isdigit(name[10]))
13069 {
13070 int major = atoi((char *)name + 6);
13071 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013072
13073 /* Expect "patch-9.9.01234". */
13074 n = (major < VIM_VERSION_MAJOR
13075 || (major == VIM_VERSION_MAJOR
13076 && (minor < VIM_VERSION_MINOR
13077 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013078 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013079 }
13080 else
13081 n = has_patch(atoi((char *)name + 5));
13082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 else if (STRICMP(name, "vim_starting") == 0)
13084 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013085#ifdef FEAT_MBYTE
13086 else if (STRICMP(name, "multi_byte_encoding") == 0)
13087 n = has_mbyte;
13088#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013089#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13090 else if (STRICMP(name, "balloon_multiline") == 0)
13091 n = multiline_balloon_available();
13092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093#ifdef DYNAMIC_TCL
13094 else if (STRICMP(name, "tcl") == 0)
13095 n = tcl_enabled(FALSE);
13096#endif
13097#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13098 else if (STRICMP(name, "iconv") == 0)
13099 n = iconv_enabled(FALSE);
13100#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013101#ifdef DYNAMIC_LUA
13102 else if (STRICMP(name, "lua") == 0)
13103 n = lua_enabled(FALSE);
13104#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013105#ifdef DYNAMIC_MZSCHEME
13106 else if (STRICMP(name, "mzscheme") == 0)
13107 n = mzscheme_enabled(FALSE);
13108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109#ifdef DYNAMIC_RUBY
13110 else if (STRICMP(name, "ruby") == 0)
13111 n = ruby_enabled(FALSE);
13112#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013113#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114#ifdef DYNAMIC_PYTHON
13115 else if (STRICMP(name, "python") == 0)
13116 n = python_enabled(FALSE);
13117#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013118#endif
13119#ifdef FEAT_PYTHON3
13120#ifdef DYNAMIC_PYTHON3
13121 else if (STRICMP(name, "python3") == 0)
13122 n = python3_enabled(FALSE);
13123#endif
13124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125#ifdef DYNAMIC_PERL
13126 else if (STRICMP(name, "perl") == 0)
13127 n = perl_enabled(FALSE);
13128#endif
13129#ifdef FEAT_GUI
13130 else if (STRICMP(name, "gui_running") == 0)
13131 n = (gui.in_use || gui.starting);
13132# ifdef FEAT_GUI_W32
13133 else if (STRICMP(name, "gui_win32s") == 0)
13134 n = gui_is_win32s();
13135# endif
13136# ifdef FEAT_BROWSE
13137 else if (STRICMP(name, "browse") == 0)
13138 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13139# endif
13140#endif
13141#ifdef FEAT_SYN_HL
13142 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013143 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144#endif
13145#if defined(WIN3264)
13146 else if (STRICMP(name, "win95") == 0)
13147 n = mch_windows95();
13148#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013149#ifdef FEAT_NETBEANS_INTG
13150 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013151 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 }
13154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156}
13157
13158/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013159 * "has_key()" function
13160 */
13161 static void
13162f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *argvars;
13164 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013165{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013166 if (argvars[0].v_type != VAR_DICT)
13167 {
13168 EMSG(_(e_dictreq));
13169 return;
13170 }
13171 if (argvars[0].vval.v_dict == NULL)
13172 return;
13173
13174 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013175 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013176}
13177
13178/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013179 * "haslocaldir()" function
13180 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013181 static void
13182f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013183 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013184 typval_T *rettv;
13185{
13186 rettv->vval.v_number = (curwin->w_localdir != NULL);
13187}
13188
13189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 * "hasmapto()" function
13191 */
13192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013194 typval_T *argvars;
13195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196{
13197 char_u *name;
13198 char_u *mode;
13199 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013200 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 mode = (char_u *)"nvo";
13205 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013208 if (argvars[2].v_type != VAR_UNKNOWN)
13209 abbr = get_tv_number(&argvars[2]);
13210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211
Bram Moolenaar2c932302006-03-18 21:42:09 +000013212 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216}
13217
13218/*
13219 * "histadd()" function
13220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013223 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225{
13226#ifdef FEAT_CMDHIST
13227 int histype;
13228 char_u *str;
13229 char_u buf[NUMBUFLEN];
13230#endif
13231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 if (check_restricted() || check_secure())
13234 return;
13235#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13237 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 if (histype >= 0)
13239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 if (*str != NUL)
13242 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013243 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 return;
13247 }
13248 }
13249#endif
13250}
13251
13252/*
13253 * "histdel()" function
13254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013257 typval_T *argvars UNUSED;
13258 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259{
13260#ifdef FEAT_CMDHIST
13261 int n;
13262 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013265 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13266 if (str == NULL)
13267 n = 0;
13268 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013270 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013271 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013273 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 else
13276 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 get_tv_string_buf(&argvars[1], buf));
13279 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280#endif
13281}
13282
13283/*
13284 * "histget()" function
13285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013288 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290{
13291#ifdef FEAT_CMDHIST
13292 int type;
13293 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13297 if (str == NULL)
13298 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013300 {
13301 type = get_histtype(str);
13302 if (argvars[1].v_type == VAR_UNKNOWN)
13303 idx = get_history_idx(type);
13304 else
13305 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13306 /* -1 on type error */
13307 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313}
13314
13315/*
13316 * "histnr()" function
13317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322{
13323 int i;
13324
13325#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 char_u *history = get_tv_string_chk(&argvars[0]);
13327
13328 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 if (i >= HIST_CMD && i < HIST_COUNT)
13330 i = get_history_idx(i);
13331 else
13332#endif
13333 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335}
13336
13337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 * "highlightID(name)" function
13339 */
13340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013342 typval_T *argvars;
13343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346}
13347
13348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349 * "highlight_exists()" function
13350 */
13351 static void
13352f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355{
13356 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13357}
13358
13359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 * "hostname()" function
13361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366{
13367 char_u hostname[256];
13368
13369 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->v_type = VAR_STRING;
13371 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
13375 * iconv() function
13376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013379 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381{
13382#ifdef FEAT_MBYTE
13383 char_u buf1[NUMBUFLEN];
13384 char_u buf2[NUMBUFLEN];
13385 char_u *from, *to, *str;
13386 vimconv_T vimconv;
13387#endif
13388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->v_type = VAR_STRING;
13390 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391
13392#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013393 str = get_tv_string(&argvars[0]);
13394 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13395 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 vimconv.vc_type = CONV_NONE;
13397 convert_setup(&vimconv, from, to);
13398
13399 /* If the encodings are equal, no conversion needed. */
13400 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404
13405 convert_setup(&vimconv, NULL, NULL);
13406 vim_free(from);
13407 vim_free(to);
13408#endif
13409}
13410
13411/*
13412 * "indent()" function
13413 */
13414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 typval_T *argvars;
13417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418{
13419 linenr_T lnum;
13420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426}
13427
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013428/*
13429 * "index()" function
13430 */
13431 static void
13432f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013435{
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 list_T *l;
13437 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013438 long idx = 0;
13439 int ic = FALSE;
13440
13441 rettv->vval.v_number = -1;
13442 if (argvars[0].v_type != VAR_LIST)
13443 {
13444 EMSG(_(e_listreq));
13445 return;
13446 }
13447 l = argvars[0].vval.v_list;
13448 if (l != NULL)
13449 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013450 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 int error = FALSE;
13454
Bram Moolenaar758711c2005-02-02 23:11:38 +000013455 /* Start at specified item. Use the cached index that list_find()
13456 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013457 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013458 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013459 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013460 ic = get_tv_number_chk(&argvars[3], &error);
13461 if (error)
13462 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013463 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013464
Bram Moolenaar758711c2005-02-02 23:11:38 +000013465 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013466 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013467 {
13468 rettv->vval.v_number = idx;
13469 break;
13470 }
13471 }
13472}
13473
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474static int inputsecret_flag = 0;
13475
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013476static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13477
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013479 * This function is used by f_input() and f_inputdialog() functions. The third
13480 * argument to f_input() specifies the type of completion to use at the
13481 * prompt. The third argument to f_inputdialog() specifies the value to return
13482 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 */
13484 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013485get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
13487 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013488 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013490 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 char_u *p = NULL;
13492 int c;
13493 char_u buf[NUMBUFLEN];
13494 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013496 int xp_type = EXPAND_NOTHING;
13497 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501
13502#ifdef NO_CONSOLE_INPUT
13503 /* While starting up, there is no place to enter text. */
13504 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506#endif
13507
13508 cmd_silent = FALSE; /* Want to see the prompt. */
13509 if (prompt != NULL)
13510 {
13511 /* Only the part of the message after the last NL is considered as
13512 * prompt for the command line */
13513 p = vim_strrchr(prompt, '\n');
13514 if (p == NULL)
13515 p = prompt;
13516 else
13517 {
13518 ++p;
13519 c = *p;
13520 *p = NUL;
13521 msg_start();
13522 msg_clr_eos();
13523 msg_puts_attr(prompt, echo_attr);
13524 msg_didout = FALSE;
13525 msg_starthere();
13526 *p = c;
13527 }
13528 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 if (argvars[1].v_type != VAR_UNKNOWN)
13531 {
13532 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13533 if (defstr != NULL)
13534 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013536 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013537 {
13538 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013539 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013540 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013541
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013542 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013543 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013544
Bram Moolenaar4463f292005-09-25 22:20:24 +000013545 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13546 if (xp_name == NULL)
13547 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013549 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013550
Bram Moolenaar4463f292005-09-25 22:20:24 +000013551 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13552 &xp_arg) == FAIL)
13553 return;
13554 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013555 }
13556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013557 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013558 {
13559# ifdef FEAT_EX_EXTRA
13560 int save_ex_normal_busy = ex_normal_busy;
13561 ex_normal_busy = 0;
13562# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013564 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13565 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013566# ifdef FEAT_EX_EXTRA
13567 ex_normal_busy = save_ex_normal_busy;
13568# endif
13569 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013570 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013571 && argvars[1].v_type != VAR_UNKNOWN
13572 && argvars[2].v_type != VAR_UNKNOWN)
13573 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13574 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013575
13576 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 /* since the user typed this, no need to wait for return */
13579 need_wait_return = FALSE;
13580 msg_didout = FALSE;
13581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 cmd_silent = cmd_silent_save;
13583}
13584
13585/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013586 * "input()" function
13587 * Also handles inputsecret() when inputsecret is set.
13588 */
13589 static void
13590f_input(argvars, rettv)
13591 typval_T *argvars;
13592 typval_T *rettv;
13593{
13594 get_user_input(argvars, rettv, FALSE);
13595}
13596
13597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 * "inputdialog()" function
13599 */
13600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 typval_T *argvars;
13603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604{
13605#if defined(FEAT_GUI_TEXTDIALOG)
13606 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13607 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13608 {
13609 char_u *message;
13610 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 message = get_tv_string_chk(&argvars[0]);
13614 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013615 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013616 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 else
13618 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 if (message != NULL && defstr != NULL
13620 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013621 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 else
13624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 if (message != NULL && defstr != NULL
13626 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013627 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_string = vim_strsave(
13629 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 }
13635 else
13636#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013637 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638}
13639
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013640/*
13641 * "inputlist()" function
13642 */
13643 static void
13644f_inputlist(argvars, rettv)
13645 typval_T *argvars;
13646 typval_T *rettv;
13647{
13648 listitem_T *li;
13649 int selected;
13650 int mouse_used;
13651
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013652#ifdef NO_CONSOLE_INPUT
13653 /* While starting up, there is no place to enter text. */
13654 if (no_console_input())
13655 return;
13656#endif
13657 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13658 {
13659 EMSG2(_(e_listarg), "inputlist()");
13660 return;
13661 }
13662
13663 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013664 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013665 lines_left = Rows; /* avoid more prompt */
13666 msg_scroll = TRUE;
13667 msg_clr_eos();
13668
13669 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13670 {
13671 msg_puts(get_tv_string(&li->li_tv));
13672 msg_putchar('\n');
13673 }
13674
13675 /* Ask for choice. */
13676 selected = prompt_for_number(&mouse_used);
13677 if (mouse_used)
13678 selected -= lines_left;
13679
13680 rettv->vval.v_number = selected;
13681}
13682
13683
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13685
13686/*
13687 * "inputrestore()" function
13688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013691 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
13694 if (ga_userinput.ga_len > 0)
13695 {
13696 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13698 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013699 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 }
13701 else if (p_verbose > 1)
13702 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013703 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 }
13706}
13707
13708/*
13709 * "inputsave()" function
13710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013713 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013716 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 if (ga_grow(&ga_userinput, 1) == OK)
13718 {
13719 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13720 + ga_userinput.ga_len);
13721 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013722 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 }
13724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726}
13727
13728/*
13729 * "inputsecret()" function
13730 */
13731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 typval_T *argvars;
13734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735{
13736 ++cmdline_star;
13737 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 --cmdline_star;
13740 --inputsecret_flag;
13741}
13742
13743/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013744 * "insert()" function
13745 */
13746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013750{
13751 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 listitem_T *item;
13753 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013755
13756 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013757 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013758 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013759 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013760 {
13761 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013762 before = get_tv_number_chk(&argvars[2], &error);
13763 if (error)
13764 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013765
Bram Moolenaar758711c2005-02-02 23:11:38 +000013766 if (before == l->lv_len)
13767 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013768 else
13769 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013770 item = list_find(l, before);
13771 if (item == NULL)
13772 {
13773 EMSGN(_(e_listidx), before);
13774 l = NULL;
13775 }
13776 }
13777 if (l != NULL)
13778 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013779 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013780 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013781 }
13782 }
13783}
13784
13785/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013786 * "invert(expr)" function
13787 */
13788 static void
13789f_invert(argvars, rettv)
13790 typval_T *argvars;
13791 typval_T *rettv;
13792{
13793 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13794}
13795
13796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 * "isdirectory()" function
13798 */
13799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013804 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805}
13806
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013807/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013808 * "islocked()" function
13809 */
13810 static void
13811f_islocked(argvars, rettv)
13812 typval_T *argvars;
13813 typval_T *rettv;
13814{
13815 lval_T lv;
13816 char_u *end;
13817 dictitem_T *di;
13818
13819 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013820 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13821 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013822 if (end != NULL && lv.ll_name != NULL)
13823 {
13824 if (*end != NUL)
13825 EMSG(_(e_trailing));
13826 else
13827 {
13828 if (lv.ll_tv == NULL)
13829 {
13830 if (check_changedtick(lv.ll_name))
13831 rettv->vval.v_number = 1; /* always locked */
13832 else
13833 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013834 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013835 if (di != NULL)
13836 {
13837 /* Consider a variable locked when:
13838 * 1. the variable itself is locked
13839 * 2. the value of the variable is locked.
13840 * 3. the List or Dict value is locked.
13841 */
13842 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13843 || tv_islocked(&di->di_tv));
13844 }
13845 }
13846 }
13847 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013848 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013849 else if (lv.ll_newkey != NULL)
13850 EMSG2(_(e_dictkey), lv.ll_newkey);
13851 else if (lv.ll_list != NULL)
13852 /* List item. */
13853 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13854 else
13855 /* Dictionary item. */
13856 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13857 }
13858 }
13859
13860 clear_lval(&lv);
13861}
13862
Bram Moolenaar33570922005-01-25 22:26:29 +000013863static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013864
13865/*
13866 * Turn a dict into a list:
13867 * "what" == 0: list of keys
13868 * "what" == 1: list of values
13869 * "what" == 2: list of items
13870 */
13871 static void
13872dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013873 typval_T *argvars;
13874 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013875 int what;
13876{
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 list_T *l2;
13878 dictitem_T *di;
13879 hashitem_T *hi;
13880 listitem_T *li;
13881 listitem_T *li2;
13882 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013883 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013884
Bram Moolenaar8c711452005-01-14 21:53:12 +000013885 if (argvars[0].v_type != VAR_DICT)
13886 {
13887 EMSG(_(e_dictreq));
13888 return;
13889 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013890 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013891 return;
13892
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013893 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013894 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013895
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013896 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013897 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013898 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013899 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013901 --todo;
13902 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013903
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013904 li = listitem_alloc();
13905 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013906 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013907 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013908
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013909 if (what == 0)
13910 {
13911 /* keys() */
13912 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013913 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013914 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13915 }
13916 else if (what == 1)
13917 {
13918 /* values() */
13919 copy_tv(&di->di_tv, &li->li_tv);
13920 }
13921 else
13922 {
13923 /* items() */
13924 l2 = list_alloc();
13925 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013926 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013927 li->li_tv.vval.v_list = l2;
13928 if (l2 == NULL)
13929 break;
13930 ++l2->lv_refcount;
13931
13932 li2 = listitem_alloc();
13933 if (li2 == NULL)
13934 break;
13935 list_append(l2, li2);
13936 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013937 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013938 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13939
13940 li2 = listitem_alloc();
13941 if (li2 == NULL)
13942 break;
13943 list_append(l2, li2);
13944 copy_tv(&di->di_tv, &li2->li_tv);
13945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013946 }
13947 }
13948}
13949
13950/*
13951 * "items(dict)" function
13952 */
13953 static void
13954f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 typval_T *argvars;
13956 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013957{
13958 dict_list(argvars, rettv, 2);
13959}
13960
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013962 * "join()" function
13963 */
13964 static void
13965f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013966 typval_T *argvars;
13967 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968{
13969 garray_T ga;
13970 char_u *sep;
13971
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013972 if (argvars[0].v_type != VAR_LIST)
13973 {
13974 EMSG(_(e_listreq));
13975 return;
13976 }
13977 if (argvars[0].vval.v_list == NULL)
13978 return;
13979 if (argvars[1].v_type == VAR_UNKNOWN)
13980 sep = (char_u *)" ";
13981 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013983
13984 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985
13986 if (sep != NULL)
13987 {
13988 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013989 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 ga_append(&ga, NUL);
13991 rettv->vval.v_string = (char_u *)ga.ga_data;
13992 }
13993 else
13994 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013995}
13996
13997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013998 * "keys()" function
13999 */
14000 static void
14001f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014002 typval_T *argvars;
14003 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014004{
14005 dict_list(argvars, rettv, 0);
14006}
14007
14008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 * "last_buffer_nr()" function.
14010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014013 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015{
14016 int n = 0;
14017 buf_T *buf;
14018
14019 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14020 if (n < buf->b_fnum)
14021 n = buf->b_fnum;
14022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014024}
14025
14026/*
14027 * "len()" function
14028 */
14029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 typval_T *argvars;
14032 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014033{
14034 switch (argvars[0].v_type)
14035 {
14036 case VAR_STRING:
14037 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 rettv->vval.v_number = (varnumber_T)STRLEN(
14039 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014040 break;
14041 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014043 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014044 case VAR_DICT:
14045 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14046 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014048 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014049 break;
14050 }
14051}
14052
Bram Moolenaar33570922005-01-25 22:26:29 +000014053static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014054
14055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014057 typval_T *argvars;
14058 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014059 int type;
14060{
14061#ifdef FEAT_LIBCALL
14062 char_u *string_in;
14063 char_u **string_result;
14064 int nr_result;
14065#endif
14066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014068 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014070
14071 if (check_restricted() || check_secure())
14072 return;
14073
14074#ifdef FEAT_LIBCALL
14075 /* The first two args must be strings, otherwise its meaningless */
14076 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14077 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014078 string_in = NULL;
14079 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080 string_in = argvars[2].vval.v_string;
14081 if (type == VAR_NUMBER)
14082 string_result = NULL;
14083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014085 if (mch_libcall(argvars[0].vval.v_string,
14086 argvars[1].vval.v_string,
14087 string_in,
14088 argvars[2].vval.v_number,
14089 string_result,
14090 &nr_result) == OK
14091 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014093 }
14094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095}
14096
14097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 * "libcall()" function
14099 */
14100 static void
14101f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T *argvars;
14103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104{
14105 libcall_common(argvars, rettv, VAR_STRING);
14106}
14107
14108/*
14109 * "libcallnr()" function
14110 */
14111 static void
14112f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 typval_T *argvars;
14114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014115{
14116 libcall_common(argvars, rettv, VAR_NUMBER);
14117}
14118
14119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 * "line(string)" function
14121 */
14122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 typval_T *argvars;
14125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
14127 linenr_T lnum = 0;
14128 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014129 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014131 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 if (fp != NULL)
14133 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014134 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135}
14136
14137/*
14138 * "line2byte(lnum)" function
14139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014141f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014142 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144{
14145#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147#else
14148 linenr_T lnum;
14149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14155 if (rettv->vval.v_number >= 0)
14156 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157#endif
14158}
14159
14160/*
14161 * "lispindent(lnum)" function
14162 */
14163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014165 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167{
14168#ifdef FEAT_LISP
14169 pos_T pos;
14170 linenr_T lnum;
14171
14172 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14175 {
14176 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014177 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 curwin->w_cursor = pos;
14179 }
14180 else
14181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
14185/*
14186 * "localtime()" function
14187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014190 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014193 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194}
14195
Bram Moolenaar33570922005-01-25 22:26:29 +000014196static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197
14198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014199get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *argvars;
14201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 int exact;
14203{
14204 char_u *keys;
14205 char_u *which;
14206 char_u buf[NUMBUFLEN];
14207 char_u *keys_buf = NULL;
14208 char_u *rhs;
14209 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014210 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014211 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014212 mapblock_T *mp;
14213 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214
14215 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 rettv->v_type = VAR_STRING;
14217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 if (*keys == NUL)
14221 return;
14222
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014223 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014226 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014227 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014228 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014229 if (argvars[3].v_type != VAR_UNKNOWN)
14230 get_dict = get_tv_number(&argvars[3]);
14231 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 else
14234 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 if (which == NULL)
14236 return;
14237
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 mode = get_map_mode(&which, 0);
14239
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014240 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014241 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014243
14244 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014246 /* Return a string. */
14247 if (rhs != NULL)
14248 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249
Bram Moolenaarbd743252010-10-20 21:23:33 +020014250 }
14251 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14252 {
14253 /* Return a dictionary. */
14254 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14255 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14256 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257
Bram Moolenaarbd743252010-10-20 21:23:33 +020014258 dict_add_nr_str(dict, "lhs", 0L, lhs);
14259 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14260 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14261 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14262 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14263 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14264 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014265 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014266 dict_add_nr_str(dict, "mode", 0L, mapmode);
14267
14268 vim_free(lhs);
14269 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270 }
14271}
14272
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014273#ifdef FEAT_FLOAT
14274/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014275 * "log()" function
14276 */
14277 static void
14278f_log(argvars, rettv)
14279 typval_T *argvars;
14280 typval_T *rettv;
14281{
14282 float_T f;
14283
14284 rettv->v_type = VAR_FLOAT;
14285 if (get_float_arg(argvars, &f) == OK)
14286 rettv->vval.v_float = log(f);
14287 else
14288 rettv->vval.v_float = 0.0;
14289}
14290
14291/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014292 * "log10()" function
14293 */
14294 static void
14295f_log10(argvars, rettv)
14296 typval_T *argvars;
14297 typval_T *rettv;
14298{
14299 float_T f;
14300
14301 rettv->v_type = VAR_FLOAT;
14302 if (get_float_arg(argvars, &f) == OK)
14303 rettv->vval.v_float = log10(f);
14304 else
14305 rettv->vval.v_float = 0.0;
14306}
14307#endif
14308
Bram Moolenaar1dced572012-04-05 16:54:08 +020014309#ifdef FEAT_LUA
14310/*
14311 * "luaeval()" function
14312 */
14313 static void
14314f_luaeval(argvars, rettv)
14315 typval_T *argvars;
14316 typval_T *rettv;
14317{
14318 char_u *str;
14319 char_u buf[NUMBUFLEN];
14320
14321 str = get_tv_string_buf(&argvars[0], buf);
14322 do_luaeval(str, argvars + 1, rettv);
14323}
14324#endif
14325
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014327 * "map()" function
14328 */
14329 static void
14330f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014331 typval_T *argvars;
14332 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014333{
14334 filter_map(argvars, rettv, TRUE);
14335}
14336
14337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014338 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 */
14340 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 typval_T *argvars;
14343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346}
14347
14348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 */
14351 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 typval_T *argvars;
14354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357}
14358
Bram Moolenaar33570922005-01-25 22:26:29 +000014359static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
14361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014362find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014363 typval_T *argvars;
14364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 int type;
14366{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014368 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014369 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 char_u *pat;
14371 regmatch_T regmatch;
14372 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014373 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 char_u *save_cpo;
14375 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014376 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014377 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014378 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014379 list_T *l = NULL;
14380 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014381 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014382 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383
14384 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14385 save_cpo = p_cpo;
14386 p_cpo = (char_u *)"";
14387
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014388 rettv->vval.v_number = -1;
14389 if (type == 3)
14390 {
14391 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014392 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014393 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014394 }
14395 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->v_type = VAR_STRING;
14398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014401 if (argvars[0].v_type == VAR_LIST)
14402 {
14403 if ((l = argvars[0].vval.v_list) == NULL)
14404 goto theend;
14405 li = l->lv_first;
14406 }
14407 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014408 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014409 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014410 len = (long)STRLEN(str);
14411 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14414 if (pat == NULL)
14415 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014417 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 int error = FALSE;
14420
14421 start = get_tv_number_chk(&argvars[2], &error);
14422 if (error)
14423 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014424 if (l != NULL)
14425 {
14426 li = list_find(l, start);
14427 if (li == NULL)
14428 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014429 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014430 }
14431 else
14432 {
14433 if (start < 0)
14434 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014435 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014436 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014437 /* When "count" argument is there ignore matches before "start",
14438 * otherwise skip part of the string. Differs when pattern is "^"
14439 * or "\<". */
14440 if (argvars[3].v_type != VAR_UNKNOWN)
14441 startcol = start;
14442 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014443 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014444 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014445 len -= start;
14446 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014447 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014448
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014449 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 nth = get_tv_number_chk(&argvars[3], &error);
14451 if (error)
14452 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 }
14454
14455 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14456 if (regmatch.regprog != NULL)
14457 {
14458 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014459
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014460 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014461 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014462 if (l != NULL)
14463 {
14464 if (li == NULL)
14465 {
14466 match = FALSE;
14467 break;
14468 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014469 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014470 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014471 if (str == NULL)
14472 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014473 }
14474
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014475 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014477 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014478 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014479 if (l == NULL && !match)
14480 break;
14481
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014482 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014483 if (l != NULL)
14484 {
14485 li = li->li_next;
14486 ++idx;
14487 }
14488 else
14489 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014490#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014491 startcol = (colnr_T)(regmatch.startp[0]
14492 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014493#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014494 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014495#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014496 if (startcol > (colnr_T)len
14497 || str + startcol <= regmatch.startp[0])
14498 {
14499 match = FALSE;
14500 break;
14501 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014503 }
14504
14505 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014507 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014509 int i;
14510
14511 /* return list with matched string and submatches */
14512 for (i = 0; i < NSUBEXP; ++i)
14513 {
14514 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014515 {
14516 if (list_append_string(rettv->vval.v_list,
14517 (char_u *)"", 0) == FAIL)
14518 break;
14519 }
14520 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014521 regmatch.startp[i],
14522 (int)(regmatch.endp[i] - regmatch.startp[i]))
14523 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014524 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014525 }
14526 }
14527 else if (type == 2)
14528 {
14529 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014530 if (l != NULL)
14531 copy_tv(&li->li_tv, rettv);
14532 else
14533 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014535 }
14536 else if (l != NULL)
14537 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 else
14539 {
14540 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 (varnumber_T)(regmatch.startp[0] - str);
14543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014546 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 }
14548 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014549 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 }
14551
14552theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014553 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 p_cpo = save_cpo;
14555}
14556
14557/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014558 * "match()" function
14559 */
14560 static void
14561f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 typval_T *argvars;
14563 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564{
14565 find_some_match(argvars, rettv, 1);
14566}
14567
14568/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014569 * "matchadd()" function
14570 */
14571 static void
14572f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014573 typval_T *argvars UNUSED;
14574 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014575{
14576#ifdef FEAT_SEARCH_EXTRA
14577 char_u buf[NUMBUFLEN];
14578 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14579 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14580 int prio = 10; /* default priority */
14581 int id = -1;
14582 int error = FALSE;
14583
14584 rettv->vval.v_number = -1;
14585
14586 if (grp == NULL || pat == NULL)
14587 return;
14588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014589 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014590 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014591 if (argvars[3].v_type != VAR_UNKNOWN)
14592 id = get_tv_number_chk(&argvars[3], &error);
14593 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014594 if (error == TRUE)
14595 return;
14596 if (id >= 1 && id <= 3)
14597 {
14598 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14599 return;
14600 }
14601
Bram Moolenaarb3414592014-06-17 17:48:32 +020014602 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14603#endif
14604}
14605
14606/*
14607 * "matchaddpos()" function
14608 */
14609 static void
14610f_matchaddpos(argvars, rettv)
14611 typval_T *argvars UNUSED;
14612 typval_T *rettv UNUSED;
14613{
14614#ifdef FEAT_SEARCH_EXTRA
14615 char_u buf[NUMBUFLEN];
14616 char_u *group;
14617 int prio = 10;
14618 int id = -1;
14619 int error = FALSE;
14620 list_T *l;
14621
14622 rettv->vval.v_number = -1;
14623
14624 group = get_tv_string_buf_chk(&argvars[0], buf);
14625 if (group == NULL)
14626 return;
14627
14628 if (argvars[1].v_type != VAR_LIST)
14629 {
14630 EMSG2(_(e_listarg), "matchaddpos()");
14631 return;
14632 }
14633 l = argvars[1].vval.v_list;
14634 if (l == NULL)
14635 return;
14636
14637 if (argvars[2].v_type != VAR_UNKNOWN)
14638 {
14639 prio = get_tv_number_chk(&argvars[2], &error);
14640 if (argvars[3].v_type != VAR_UNKNOWN)
14641 id = get_tv_number_chk(&argvars[3], &error);
14642 }
14643 if (error == TRUE)
14644 return;
14645
14646 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14647 if (id == 1 || id == 2)
14648 {
14649 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14650 return;
14651 }
14652
14653 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014654#endif
14655}
14656
14657/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014658 * "matcharg()" function
14659 */
14660 static void
14661f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014662 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014663 typval_T *rettv;
14664{
14665 if (rettv_list_alloc(rettv) == OK)
14666 {
14667#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014668 int id = get_tv_number(&argvars[0]);
14669 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014670
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014671 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014672 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014673 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14674 {
14675 list_append_string(rettv->vval.v_list,
14676 syn_id2name(m->hlg_id), -1);
14677 list_append_string(rettv->vval.v_list, m->pattern, -1);
14678 }
14679 else
14680 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014681 list_append_string(rettv->vval.v_list, NULL, -1);
14682 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014683 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014684 }
14685#endif
14686 }
14687}
14688
14689/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014690 * "matchdelete()" function
14691 */
14692 static void
14693f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014694 typval_T *argvars UNUSED;
14695 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014696{
14697#ifdef FEAT_SEARCH_EXTRA
14698 rettv->vval.v_number = match_delete(curwin,
14699 (int)get_tv_number(&argvars[0]), TRUE);
14700#endif
14701}
14702
14703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704 * "matchend()" function
14705 */
14706 static void
14707f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014708 typval_T *argvars;
14709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014710{
14711 find_some_match(argvars, rettv, 0);
14712}
14713
14714/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014715 * "matchlist()" function
14716 */
14717 static void
14718f_matchlist(argvars, rettv)
14719 typval_T *argvars;
14720 typval_T *rettv;
14721{
14722 find_some_match(argvars, rettv, 3);
14723}
14724
14725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 * "matchstr()" function
14727 */
14728 static void
14729f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014730 typval_T *argvars;
14731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732{
14733 find_some_match(argvars, rettv, 2);
14734}
14735
Bram Moolenaar33570922005-01-25 22:26:29 +000014736static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014737
14738 static void
14739max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014740 typval_T *argvars;
14741 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014742 int domax;
14743{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014744 long n = 0;
14745 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014747
14748 if (argvars[0].v_type == VAR_LIST)
14749 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 list_T *l;
14751 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014752
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014753 l = argvars[0].vval.v_list;
14754 if (l != NULL)
14755 {
14756 li = l->lv_first;
14757 if (li != NULL)
14758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014759 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014760 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014761 {
14762 li = li->li_next;
14763 if (li == NULL)
14764 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014765 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014766 if (domax ? i > n : i < n)
14767 n = i;
14768 }
14769 }
14770 }
14771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014772 else if (argvars[0].v_type == VAR_DICT)
14773 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014775 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014777 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014778
14779 d = argvars[0].vval.v_dict;
14780 if (d != NULL)
14781 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014782 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014784 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014785 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014786 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014787 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014788 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014789 if (first)
14790 {
14791 n = i;
14792 first = FALSE;
14793 }
14794 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014795 n = i;
14796 }
14797 }
14798 }
14799 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014800 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014801 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014803}
14804
14805/*
14806 * "max()" function
14807 */
14808 static void
14809f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 typval_T *argvars;
14811 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014812{
14813 max_min(argvars, rettv, TRUE);
14814}
14815
14816/*
14817 * "min()" function
14818 */
14819 static void
14820f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014821 typval_T *argvars;
14822 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014823{
14824 max_min(argvars, rettv, FALSE);
14825}
14826
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014827static int mkdir_recurse __ARGS((char_u *dir, int prot));
14828
14829/*
14830 * Create the directory in which "dir" is located, and higher levels when
14831 * needed.
14832 */
14833 static int
14834mkdir_recurse(dir, prot)
14835 char_u *dir;
14836 int prot;
14837{
14838 char_u *p;
14839 char_u *updir;
14840 int r = FAIL;
14841
14842 /* Get end of directory name in "dir".
14843 * We're done when it's "/" or "c:/". */
14844 p = gettail_sep(dir);
14845 if (p <= get_past_head(dir))
14846 return OK;
14847
14848 /* If the directory exists we're done. Otherwise: create it.*/
14849 updir = vim_strnsave(dir, (int)(p - dir));
14850 if (updir == NULL)
14851 return FAIL;
14852 if (mch_isdir(updir))
14853 r = OK;
14854 else if (mkdir_recurse(updir, prot) == OK)
14855 r = vim_mkdir_emsg(updir, prot);
14856 vim_free(updir);
14857 return r;
14858}
14859
14860#ifdef vim_mkdir
14861/*
14862 * "mkdir()" function
14863 */
14864 static void
14865f_mkdir(argvars, rettv)
14866 typval_T *argvars;
14867 typval_T *rettv;
14868{
14869 char_u *dir;
14870 char_u buf[NUMBUFLEN];
14871 int prot = 0755;
14872
14873 rettv->vval.v_number = FAIL;
14874 if (check_restricted() || check_secure())
14875 return;
14876
14877 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014878 if (*dir == NUL)
14879 rettv->vval.v_number = FAIL;
14880 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014881 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014882 if (*gettail(dir) == NUL)
14883 /* remove trailing slashes */
14884 *gettail_sep(dir) = NUL;
14885
14886 if (argvars[1].v_type != VAR_UNKNOWN)
14887 {
14888 if (argvars[2].v_type != VAR_UNKNOWN)
14889 prot = get_tv_number_chk(&argvars[2], NULL);
14890 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14891 mkdir_recurse(dir, prot);
14892 }
14893 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014894 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014895}
14896#endif
14897
Bram Moolenaar0d660222005-01-07 21:51:51 +000014898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 * "mode()" function
14900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014902f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014903 typval_T *argvars;
14904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014906 char_u buf[3];
14907
14908 buf[1] = NUL;
14909 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 if (VIsual_active)
14912 {
14913 if (VIsual_select)
14914 buf[0] = VIsual_mode + 's' - 'v';
14915 else
14916 buf[0] = VIsual_mode;
14917 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014918 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014919 || State == CONFIRM)
14920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014922 if (State == ASKMORE)
14923 buf[1] = 'm';
14924 else if (State == CONFIRM)
14925 buf[1] = '?';
14926 }
14927 else if (State == EXTERNCMD)
14928 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 else if (State & INSERT)
14930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014931#ifdef FEAT_VREPLACE
14932 if (State & VREPLACE_FLAG)
14933 {
14934 buf[0] = 'R';
14935 buf[1] = 'v';
14936 }
14937 else
14938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 if (State & REPLACE_FLAG)
14940 buf[0] = 'R';
14941 else
14942 buf[0] = 'i';
14943 }
14944 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014947 if (exmode_active)
14948 buf[1] = 'v';
14949 }
14950 else if (exmode_active)
14951 {
14952 buf[0] = 'c';
14953 buf[1] = 'e';
14954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014958 if (finish_op)
14959 buf[1] = 'o';
14960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014962 /* Clear out the minor mode when the argument is not a non-zero number or
14963 * non-empty string. */
14964 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014965 buf[1] = NUL;
14966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 rettv->vval.v_string = vim_strsave(buf);
14968 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969}
14970
Bram Moolenaar429fa852013-04-15 12:27:36 +020014971#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014972/*
14973 * "mzeval()" function
14974 */
14975 static void
14976f_mzeval(argvars, rettv)
14977 typval_T *argvars;
14978 typval_T *rettv;
14979{
14980 char_u *str;
14981 char_u buf[NUMBUFLEN];
14982
14983 str = get_tv_string_buf(&argvars[0], buf);
14984 do_mzeval(str, rettv);
14985}
Bram Moolenaar75676462013-01-30 14:55:42 +010014986
14987 void
14988mzscheme_call_vim(name, args, rettv)
14989 char_u *name;
14990 typval_T *args;
14991 typval_T *rettv;
14992{
14993 typval_T argvars[3];
14994
14995 argvars[0].v_type = VAR_STRING;
14996 argvars[0].vval.v_string = name;
14997 copy_tv(args, &argvars[1]);
14998 argvars[2].v_type = VAR_UNKNOWN;
14999 f_call(argvars, rettv);
15000 clear_tv(&argvars[1]);
15001}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015002#endif
15003
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015005 * "nextnonblank()" function
15006 */
15007 static void
15008f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015009 typval_T *argvars;
15010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011{
15012 linenr_T lnum;
15013
15014 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017 {
15018 lnum = 0;
15019 break;
15020 }
15021 if (*skipwhite(ml_get(lnum)) != NUL)
15022 break;
15023 }
15024 rettv->vval.v_number = lnum;
15025}
15026
15027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 * "nr2char()" function
15029 */
15030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 typval_T *argvars;
15033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034{
15035 char_u buf[NUMBUFLEN];
15036
15037#ifdef FEAT_MBYTE
15038 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015039 {
15040 int utf8 = 0;
15041
15042 if (argvars[1].v_type != VAR_UNKNOWN)
15043 utf8 = get_tv_number_chk(&argvars[1], NULL);
15044 if (utf8)
15045 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15046 else
15047 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 else
15050#endif
15051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 buf[1] = NUL;
15054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 rettv->v_type = VAR_STRING;
15056 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015057}
15058
15059/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015060 * "or(expr, expr)" function
15061 */
15062 static void
15063f_or(argvars, rettv)
15064 typval_T *argvars;
15065 typval_T *rettv;
15066{
15067 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15068 | get_tv_number_chk(&argvars[1], NULL);
15069}
15070
15071/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015072 * "pathshorten()" function
15073 */
15074 static void
15075f_pathshorten(argvars, rettv)
15076 typval_T *argvars;
15077 typval_T *rettv;
15078{
15079 char_u *p;
15080
15081 rettv->v_type = VAR_STRING;
15082 p = get_tv_string_chk(&argvars[0]);
15083 if (p == NULL)
15084 rettv->vval.v_string = NULL;
15085 else
15086 {
15087 p = vim_strsave(p);
15088 rettv->vval.v_string = p;
15089 if (p != NULL)
15090 shorten_dir(p);
15091 }
15092}
15093
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015094#ifdef FEAT_FLOAT
15095/*
15096 * "pow()" function
15097 */
15098 static void
15099f_pow(argvars, rettv)
15100 typval_T *argvars;
15101 typval_T *rettv;
15102{
15103 float_T fx, fy;
15104
15105 rettv->v_type = VAR_FLOAT;
15106 if (get_float_arg(argvars, &fx) == OK
15107 && get_float_arg(&argvars[1], &fy) == OK)
15108 rettv->vval.v_float = pow(fx, fy);
15109 else
15110 rettv->vval.v_float = 0.0;
15111}
15112#endif
15113
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015114/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115 * "prevnonblank()" function
15116 */
15117 static void
15118f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015119 typval_T *argvars;
15120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121{
15122 linenr_T lnum;
15123
15124 lnum = get_tv_lnum(argvars);
15125 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15126 lnum = 0;
15127 else
15128 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15129 --lnum;
15130 rettv->vval.v_number = lnum;
15131}
15132
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015133#ifdef HAVE_STDARG_H
15134/* This dummy va_list is here because:
15135 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15136 * - locally in the function results in a "used before set" warning
15137 * - using va_start() to initialize it gives "function with fixed args" error */
15138static va_list ap;
15139#endif
15140
Bram Moolenaar8c711452005-01-14 21:53:12 +000015141/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015142 * "printf()" function
15143 */
15144 static void
15145f_printf(argvars, rettv)
15146 typval_T *argvars;
15147 typval_T *rettv;
15148{
15149 rettv->v_type = VAR_STRING;
15150 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015151#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015152 {
15153 char_u buf[NUMBUFLEN];
15154 int len;
15155 char_u *s;
15156 int saved_did_emsg = did_emsg;
15157 char *fmt;
15158
15159 /* Get the required length, allocate the buffer and do it for real. */
15160 did_emsg = FALSE;
15161 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015162 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015163 if (!did_emsg)
15164 {
15165 s = alloc(len + 1);
15166 if (s != NULL)
15167 {
15168 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015169 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015170 }
15171 }
15172 did_emsg |= saved_did_emsg;
15173 }
15174#endif
15175}
15176
15177/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015178 * "pumvisible()" function
15179 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015180 static void
15181f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015182 typval_T *argvars UNUSED;
15183 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015184{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015185#ifdef FEAT_INS_EXPAND
15186 if (pum_visible())
15187 rettv->vval.v_number = 1;
15188#endif
15189}
15190
Bram Moolenaardb913952012-06-29 12:54:53 +020015191#ifdef FEAT_PYTHON3
15192/*
15193 * "py3eval()" function
15194 */
15195 static void
15196f_py3eval(argvars, rettv)
15197 typval_T *argvars;
15198 typval_T *rettv;
15199{
15200 char_u *str;
15201 char_u buf[NUMBUFLEN];
15202
15203 str = get_tv_string_buf(&argvars[0], buf);
15204 do_py3eval(str, rettv);
15205}
15206#endif
15207
15208#ifdef FEAT_PYTHON
15209/*
15210 * "pyeval()" function
15211 */
15212 static void
15213f_pyeval(argvars, rettv)
15214 typval_T *argvars;
15215 typval_T *rettv;
15216{
15217 char_u *str;
15218 char_u buf[NUMBUFLEN];
15219
15220 str = get_tv_string_buf(&argvars[0], buf);
15221 do_pyeval(str, rettv);
15222}
15223#endif
15224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226 * "range()" function
15227 */
15228 static void
15229f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 typval_T *argvars;
15231 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015232{
15233 long start;
15234 long end;
15235 long stride = 1;
15236 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 if (argvars[1].v_type == VAR_UNKNOWN)
15241 {
15242 end = start - 1;
15243 start = 0;
15244 }
15245 else
15246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015248 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015250 }
15251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 if (error)
15253 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015254 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015255 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015256 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015257 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015258 else
15259 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015260 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015261 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015262 if (list_append_number(rettv->vval.v_list,
15263 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015264 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015265 }
15266}
15267
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015268/*
15269 * "readfile()" function
15270 */
15271 static void
15272f_readfile(argvars, rettv)
15273 typval_T *argvars;
15274 typval_T *rettv;
15275{
15276 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015277 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015278 char_u *fname;
15279 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015280 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15281 int io_size = sizeof(buf);
15282 int readlen; /* size of last fread() */
15283 char_u *prev = NULL; /* previously read bytes, if any */
15284 long prevlen = 0; /* length of data in prev */
15285 long prevsize = 0; /* size of prev buffer */
15286 long maxline = MAXLNUM;
15287 long cnt = 0;
15288 char_u *p; /* position in buf */
15289 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015290
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015291 if (argvars[1].v_type != VAR_UNKNOWN)
15292 {
15293 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15294 binary = TRUE;
15295 if (argvars[2].v_type != VAR_UNKNOWN)
15296 maxline = get_tv_number(&argvars[2]);
15297 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015298
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015299 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015300 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015301
15302 /* Always open the file in binary mode, library functions have a mind of
15303 * their own about CR-LF conversion. */
15304 fname = get_tv_string(&argvars[0]);
15305 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15306 {
15307 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15308 return;
15309 }
15310
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015311 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015312 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015313 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015314
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015315 /* This for loop processes what was read, but is also entered at end
15316 * of file so that either:
15317 * - an incomplete line gets written
15318 * - a "binary" file gets an empty line at the end if it ends in a
15319 * newline. */
15320 for (p = buf, start = buf;
15321 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15322 ++p)
15323 {
15324 if (*p == '\n' || readlen <= 0)
15325 {
15326 listitem_T *li;
15327 char_u *s = NULL;
15328 long_u len = p - start;
15329
15330 /* Finished a line. Remove CRs before NL. */
15331 if (readlen > 0 && !binary)
15332 {
15333 while (len > 0 && start[len - 1] == '\r')
15334 --len;
15335 /* removal may cross back to the "prev" string */
15336 if (len == 0)
15337 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15338 --prevlen;
15339 }
15340 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015341 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015342 else
15343 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015344 /* Change "prev" buffer to be the right size. This way
15345 * the bytes are only copied once, and very long lines are
15346 * allocated only once. */
15347 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015349 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015351 prev = NULL; /* the list will own the string */
15352 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015353 }
15354 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015355 if (s == NULL)
15356 {
15357 do_outofmem_msg((long_u) prevlen + len + 1);
15358 failed = TRUE;
15359 break;
15360 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015361
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015362 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015363 {
15364 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015365 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 break;
15367 }
15368 li->li_tv.v_type = VAR_STRING;
15369 li->li_tv.v_lock = 0;
15370 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015371 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015373 start = p + 1; /* step over newline */
15374 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375 break;
15376 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015377 else if (*p == NUL)
15378 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015379#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015380 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15381 * when finding the BF and check the previous two bytes. */
15382 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015383 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015384 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15385 * + 1, these may be in the "prev" string. */
15386 char_u back1 = p >= buf + 1 ? p[-1]
15387 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15388 char_u back2 = p >= buf + 2 ? p[-2]
15389 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15390 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015391
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015392 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015393 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015394 char_u *dest = p - 2;
15395
15396 /* Usually a BOM is at the beginning of a file, and so at
15397 * the beginning of a line; then we can just step over it.
15398 */
15399 if (start == dest)
15400 start = p + 1;
15401 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015402 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015403 /* have to shuffle buf to close gap */
15404 int adjust_prevlen = 0;
15405
15406 if (dest < buf)
15407 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015408 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015409 dest = buf;
15410 }
15411 if (readlen > p - buf + 1)
15412 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15413 readlen -= 3 - adjust_prevlen;
15414 prevlen -= adjust_prevlen;
15415 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015416 }
15417 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015418 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015419#endif
15420 } /* for */
15421
15422 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15423 break;
15424 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015426 /* There's part of a line in buf, store it in "prev". */
15427 if (p - start + prevlen >= prevsize)
15428 {
15429 /* need bigger "prev" buffer */
15430 char_u *newprev;
15431
15432 /* A common use case is ordinary text files and "prev" gets a
15433 * fragment of a line, so the first allocation is made
15434 * small, to avoid repeatedly 'allocing' large and
15435 * 'reallocing' small. */
15436 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015437 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015438 else
15439 {
15440 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015441 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015442 prevsize = grow50pc > growmin ? grow50pc : growmin;
15443 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015444 newprev = prev == NULL ? alloc(prevsize)
15445 : vim_realloc(prev, prevsize);
15446 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015447 {
15448 do_outofmem_msg((long_u)prevsize);
15449 failed = TRUE;
15450 break;
15451 }
15452 prev = newprev;
15453 }
15454 /* Add the line part to end of "prev". */
15455 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015456 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015458 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015459
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015460 /*
15461 * For a negative line count use only the lines at the end of the file,
15462 * free the rest.
15463 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015464 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015465 while (cnt > -maxline)
15466 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015467 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015468 --cnt;
15469 }
15470
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015471 if (failed)
15472 {
15473 list_free(rettv->vval.v_list, TRUE);
15474 /* readfile doc says an empty list is returned on error */
15475 rettv->vval.v_list = list_alloc();
15476 }
15477
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015478 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015479 fclose(fd);
15480}
15481
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015482#if defined(FEAT_RELTIME)
15483static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15484
15485/*
15486 * Convert a List to proftime_T.
15487 * Return FAIL when there is something wrong.
15488 */
15489 static int
15490list2proftime(arg, tm)
15491 typval_T *arg;
15492 proftime_T *tm;
15493{
15494 long n1, n2;
15495 int error = FALSE;
15496
15497 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15498 || arg->vval.v_list->lv_len != 2)
15499 return FAIL;
15500 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15501 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15502# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015503 tm->HighPart = n1;
15504 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015505# else
15506 tm->tv_sec = n1;
15507 tm->tv_usec = n2;
15508# endif
15509 return error ? FAIL : OK;
15510}
15511#endif /* FEAT_RELTIME */
15512
15513/*
15514 * "reltime()" function
15515 */
15516 static void
15517f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015518 typval_T *argvars UNUSED;
15519 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015520{
15521#ifdef FEAT_RELTIME
15522 proftime_T res;
15523 proftime_T start;
15524
15525 if (argvars[0].v_type == VAR_UNKNOWN)
15526 {
15527 /* No arguments: get current time. */
15528 profile_start(&res);
15529 }
15530 else if (argvars[1].v_type == VAR_UNKNOWN)
15531 {
15532 if (list2proftime(&argvars[0], &res) == FAIL)
15533 return;
15534 profile_end(&res);
15535 }
15536 else
15537 {
15538 /* Two arguments: compute the difference. */
15539 if (list2proftime(&argvars[0], &start) == FAIL
15540 || list2proftime(&argvars[1], &res) == FAIL)
15541 return;
15542 profile_sub(&res, &start);
15543 }
15544
15545 if (rettv_list_alloc(rettv) == OK)
15546 {
15547 long n1, n2;
15548
15549# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015550 n1 = res.HighPart;
15551 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015552# else
15553 n1 = res.tv_sec;
15554 n2 = res.tv_usec;
15555# endif
15556 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15557 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15558 }
15559#endif
15560}
15561
15562/*
15563 * "reltimestr()" function
15564 */
15565 static void
15566f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015567 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015568 typval_T *rettv;
15569{
15570#ifdef FEAT_RELTIME
15571 proftime_T tm;
15572#endif
15573
15574 rettv->v_type = VAR_STRING;
15575 rettv->vval.v_string = NULL;
15576#ifdef FEAT_RELTIME
15577 if (list2proftime(&argvars[0], &tm) == OK)
15578 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15579#endif
15580}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015581
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15583static void make_connection __ARGS((void));
15584static int check_connection __ARGS((void));
15585
15586 static void
15587make_connection()
15588{
15589 if (X_DISPLAY == NULL
15590# ifdef FEAT_GUI
15591 && !gui.in_use
15592# endif
15593 )
15594 {
15595 x_force_connect = TRUE;
15596 setup_term_clip();
15597 x_force_connect = FALSE;
15598 }
15599}
15600
15601 static int
15602check_connection()
15603{
15604 make_connection();
15605 if (X_DISPLAY == NULL)
15606 {
15607 EMSG(_("E240: No connection to Vim server"));
15608 return FAIL;
15609 }
15610 return OK;
15611}
15612#endif
15613
15614#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015615static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015616
15617 static void
15618remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015619 typval_T *argvars;
15620 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015621 int expr;
15622{
15623 char_u *server_name;
15624 char_u *keys;
15625 char_u *r = NULL;
15626 char_u buf[NUMBUFLEN];
15627# ifdef WIN32
15628 HWND w;
15629# else
15630 Window w;
15631# endif
15632
15633 if (check_restricted() || check_secure())
15634 return;
15635
15636# ifdef FEAT_X11
15637 if (check_connection() == FAIL)
15638 return;
15639# endif
15640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 server_name = get_tv_string_chk(&argvars[0]);
15642 if (server_name == NULL)
15643 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015644 keys = get_tv_string_buf(&argvars[1], buf);
15645# ifdef WIN32
15646 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15647# else
15648 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15649 < 0)
15650# endif
15651 {
15652 if (r != NULL)
15653 EMSG(r); /* sending worked but evaluation failed */
15654 else
15655 EMSG2(_("E241: Unable to send to %s"), server_name);
15656 return;
15657 }
15658
15659 rettv->vval.v_string = r;
15660
15661 if (argvars[2].v_type != VAR_UNKNOWN)
15662 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015663 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015664 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015667 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015668 v.di_tv.v_type = VAR_STRING;
15669 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015670 idvar = get_tv_string_chk(&argvars[2]);
15671 if (idvar != NULL)
15672 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015673 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015674 }
15675}
15676#endif
15677
15678/*
15679 * "remote_expr()" function
15680 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015681 static void
15682f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685{
15686 rettv->v_type = VAR_STRING;
15687 rettv->vval.v_string = NULL;
15688#ifdef FEAT_CLIENTSERVER
15689 remote_common(argvars, rettv, TRUE);
15690#endif
15691}
15692
15693/*
15694 * "remote_foreground()" function
15695 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015696 static void
15697f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015698 typval_T *argvars UNUSED;
15699 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015700{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701#ifdef FEAT_CLIENTSERVER
15702# ifdef WIN32
15703 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015704 {
15705 char_u *server_name = get_tv_string_chk(&argvars[0]);
15706
15707 if (server_name != NULL)
15708 serverForeground(server_name);
15709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015710# else
15711 /* Send a foreground() expression to the server. */
15712 argvars[1].v_type = VAR_STRING;
15713 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15714 argvars[2].v_type = VAR_UNKNOWN;
15715 remote_common(argvars, rettv, TRUE);
15716 vim_free(argvars[1].vval.v_string);
15717# endif
15718#endif
15719}
15720
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721 static void
15722f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015723 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725{
15726#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015727 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728 char_u *s = NULL;
15729# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015730 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015732 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015733
15734 if (check_restricted() || check_secure())
15735 {
15736 rettv->vval.v_number = -1;
15737 return;
15738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015739 serverid = get_tv_string_chk(&argvars[0]);
15740 if (serverid == NULL)
15741 {
15742 rettv->vval.v_number = -1;
15743 return; /* type error; errmsg already given */
15744 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015746 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747 if (n == 0)
15748 rettv->vval.v_number = -1;
15749 else
15750 {
15751 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15752 rettv->vval.v_number = (s != NULL);
15753 }
15754# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 if (check_connection() == FAIL)
15756 return;
15757
15758 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015759 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760# endif
15761
15762 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 char_u *retvar;
15765
Bram Moolenaar33570922005-01-25 22:26:29 +000015766 v.di_tv.v_type = VAR_STRING;
15767 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 retvar = get_tv_string_chk(&argvars[1]);
15769 if (retvar != NULL)
15770 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 }
15773#else
15774 rettv->vval.v_number = -1;
15775#endif
15776}
15777
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 static void
15779f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782{
15783 char_u *r = NULL;
15784
15785#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015786 char_u *serverid = get_tv_string_chk(&argvars[0]);
15787
15788 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789 {
15790# ifdef WIN32
15791 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015792 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015794 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795 if (n != 0)
15796 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15797 if (r == NULL)
15798# else
15799 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801# endif
15802 EMSG(_("E277: Unable to read a server reply"));
15803 }
15804#endif
15805 rettv->v_type = VAR_STRING;
15806 rettv->vval.v_string = r;
15807}
15808
15809/*
15810 * "remote_send()" function
15811 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812 static void
15813f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816{
15817 rettv->v_type = VAR_STRING;
15818 rettv->vval.v_string = NULL;
15819#ifdef FEAT_CLIENTSERVER
15820 remote_common(argvars, rettv, FALSE);
15821#endif
15822}
15823
15824/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015825 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015826 */
15827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015828f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 typval_T *argvars;
15830 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015831{
Bram Moolenaar33570922005-01-25 22:26:29 +000015832 list_T *l;
15833 listitem_T *item, *item2;
15834 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015835 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015836 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015837 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 dict_T *d;
15839 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015840 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015841
Bram Moolenaar8c711452005-01-14 21:53:12 +000015842 if (argvars[0].v_type == VAR_DICT)
15843 {
15844 if (argvars[2].v_type != VAR_UNKNOWN)
15845 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015846 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015847 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015849 key = get_tv_string_chk(&argvars[1]);
15850 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015852 di = dict_find(d, key, -1);
15853 if (di == NULL)
15854 EMSG2(_(e_dictkey), key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020015855 else if (!var_check_fixed(di->di_flags, (char_u *)_(arg_errmsg))
15856 && !var_check_ro(di->di_flags,
15857 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 {
15859 *rettv = di->di_tv;
15860 init_tv(&di->di_tv);
15861 dictitem_remove(d, di);
15862 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015864 }
15865 }
15866 else if (argvars[0].v_type != VAR_LIST)
15867 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015868 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015869 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 int error = FALSE;
15872
15873 idx = get_tv_number_chk(&argvars[1], &error);
15874 if (error)
15875 ; /* type error: do nothing, errmsg already given */
15876 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877 EMSGN(_(e_listidx), idx);
15878 else
15879 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015880 if (argvars[2].v_type == VAR_UNKNOWN)
15881 {
15882 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015883 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015884 *rettv = item->li_tv;
15885 vim_free(item);
15886 }
15887 else
15888 {
15889 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015890 end = get_tv_number_chk(&argvars[2], &error);
15891 if (error)
15892 ; /* type error: do nothing */
15893 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015894 EMSGN(_(e_listidx), end);
15895 else
15896 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015897 int cnt = 0;
15898
15899 for (li = item; li != NULL; li = li->li_next)
15900 {
15901 ++cnt;
15902 if (li == item2)
15903 break;
15904 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015905 if (li == NULL) /* didn't find "item2" after "item" */
15906 EMSG(_(e_invrange));
15907 else
15908 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015909 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015910 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015911 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015912 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015913 l->lv_first = item;
15914 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015915 item->li_prev = NULL;
15916 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015917 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015918 }
15919 }
15920 }
15921 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015922 }
15923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924}
15925
15926/*
15927 * "rename({from}, {to})" function
15928 */
15929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015930f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015931 typval_T *argvars;
15932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933{
15934 char_u buf[NUMBUFLEN];
15935
15936 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15940 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941}
15942
15943/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015944 * "repeat()" function
15945 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015946 static void
15947f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015948 typval_T *argvars;
15949 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015950{
15951 char_u *p;
15952 int n;
15953 int slen;
15954 int len;
15955 char_u *r;
15956 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015957
15958 n = get_tv_number(&argvars[1]);
15959 if (argvars[0].v_type == VAR_LIST)
15960 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015962 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 if (list_extend(rettv->vval.v_list,
15964 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015965 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015966 }
15967 else
15968 {
15969 p = get_tv_string(&argvars[0]);
15970 rettv->v_type = VAR_STRING;
15971 rettv->vval.v_string = NULL;
15972
15973 slen = (int)STRLEN(p);
15974 len = slen * n;
15975 if (len <= 0)
15976 return;
15977
15978 r = alloc(len + 1);
15979 if (r != NULL)
15980 {
15981 for (i = 0; i < n; i++)
15982 mch_memmove(r + i * slen, p, (size_t)slen);
15983 r[len] = NUL;
15984 }
15985
15986 rettv->vval.v_string = r;
15987 }
15988}
15989
15990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 * "resolve()" function
15992 */
15993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *argvars;
15996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997{
15998 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015999#ifdef HAVE_READLINK
16000 char_u *buf = NULL;
16001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004#ifdef FEAT_SHORTCUT
16005 {
16006 char_u *v = NULL;
16007
16008 v = mch_resolve_shortcut(p);
16009 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 }
16014#else
16015# ifdef HAVE_READLINK
16016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 char_u *cpy;
16018 int len;
16019 char_u *remain = NULL;
16020 char_u *q;
16021 int is_relative_to_current = FALSE;
16022 int has_trailing_pathsep = FALSE;
16023 int limit = 100;
16024
16025 p = vim_strsave(p);
16026
16027 if (p[0] == '.' && (vim_ispathsep(p[1])
16028 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16029 is_relative_to_current = TRUE;
16030
16031 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016032 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016035 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037
16038 q = getnextcomp(p);
16039 if (*q != NUL)
16040 {
16041 /* Separate the first path component in "p", and keep the
16042 * remainder (beginning with the path separator). */
16043 remain = vim_strsave(q - 1);
16044 q[-1] = NUL;
16045 }
16046
Bram Moolenaard9462e32011-04-11 21:35:11 +020016047 buf = alloc(MAXPATHL + 1);
16048 if (buf == NULL)
16049 goto fail;
16050
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 for (;;)
16052 {
16053 for (;;)
16054 {
16055 len = readlink((char *)p, (char *)buf, MAXPATHL);
16056 if (len <= 0)
16057 break;
16058 buf[len] = NUL;
16059
16060 if (limit-- == 0)
16061 {
16062 vim_free(p);
16063 vim_free(remain);
16064 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 goto fail;
16067 }
16068
16069 /* Ensure that the result will have a trailing path separator
16070 * if the argument has one. */
16071 if (remain == NULL && has_trailing_pathsep)
16072 add_pathsep(buf);
16073
16074 /* Separate the first path component in the link value and
16075 * concatenate the remainders. */
16076 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16077 if (*q != NUL)
16078 {
16079 if (remain == NULL)
16080 remain = vim_strsave(q - 1);
16081 else
16082 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016083 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 if (cpy != NULL)
16085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 vim_free(remain);
16087 remain = cpy;
16088 }
16089 }
16090 q[-1] = NUL;
16091 }
16092
16093 q = gettail(p);
16094 if (q > p && *q == NUL)
16095 {
16096 /* Ignore trailing path separator. */
16097 q[-1] = NUL;
16098 q = gettail(p);
16099 }
16100 if (q > p && !mch_isFullName(buf))
16101 {
16102 /* symlink is relative to directory of argument */
16103 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16104 if (cpy != NULL)
16105 {
16106 STRCPY(cpy, p);
16107 STRCPY(gettail(cpy), buf);
16108 vim_free(p);
16109 p = cpy;
16110 }
16111 }
16112 else
16113 {
16114 vim_free(p);
16115 p = vim_strsave(buf);
16116 }
16117 }
16118
16119 if (remain == NULL)
16120 break;
16121
16122 /* Append the first path component of "remain" to "p". */
16123 q = getnextcomp(remain + 1);
16124 len = q - remain - (*q != NUL);
16125 cpy = vim_strnsave(p, STRLEN(p) + len);
16126 if (cpy != NULL)
16127 {
16128 STRNCAT(cpy, remain, len);
16129 vim_free(p);
16130 p = cpy;
16131 }
16132 /* Shorten "remain". */
16133 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016134 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 else
16136 {
16137 vim_free(remain);
16138 remain = NULL;
16139 }
16140 }
16141
16142 /* If the result is a relative path name, make it explicitly relative to
16143 * the current directory if and only if the argument had this form. */
16144 if (!vim_ispathsep(*p))
16145 {
16146 if (is_relative_to_current
16147 && *p != NUL
16148 && !(p[0] == '.'
16149 && (p[1] == NUL
16150 || vim_ispathsep(p[1])
16151 || (p[1] == '.'
16152 && (p[2] == NUL
16153 || vim_ispathsep(p[2]))))))
16154 {
16155 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016156 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 if (cpy != NULL)
16158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 vim_free(p);
16160 p = cpy;
16161 }
16162 }
16163 else if (!is_relative_to_current)
16164 {
16165 /* Strip leading "./". */
16166 q = p;
16167 while (q[0] == '.' && vim_ispathsep(q[1]))
16168 q += 2;
16169 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016170 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 }
16172 }
16173
16174 /* Ensure that the result will have no trailing path separator
16175 * if the argument had none. But keep "/" or "//". */
16176 if (!has_trailing_pathsep)
16177 {
16178 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016179 if (after_pathsep(p, q))
16180 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 }
16182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 }
16185# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187# endif
16188#endif
16189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191
16192#ifdef HAVE_READLINK
16193fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016194 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016196 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197}
16198
16199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 */
16202 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *argvars;
16205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206{
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 list_T *l;
16208 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 if (argvars[0].v_type != VAR_LIST)
16211 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016212 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016213 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214 {
16215 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016216 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016217 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 while (li != NULL)
16219 {
16220 ni = li->li_prev;
16221 list_append(l, li);
16222 li = ni;
16223 }
16224 rettv->vval.v_list = l;
16225 rettv->v_type = VAR_LIST;
16226 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016227 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229}
16230
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016231#define SP_NOMOVE 0x01 /* don't move cursor */
16232#define SP_REPEAT 0x02 /* repeat to find outer pair */
16233#define SP_RETCOUNT 0x04 /* return matchcount */
16234#define SP_SETPCMARK 0x08 /* set previous context mark */
16235#define SP_START 0x10 /* accept match at start position */
16236#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16237#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016238
Bram Moolenaar33570922005-01-25 22:26:29 +000016239static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240
16241/*
16242 * Get flags for a search function.
16243 * Possibly sets "p_ws".
16244 * Returns BACKWARD, FORWARD or zero (for an error).
16245 */
16246 static int
16247get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016248 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 int *flagsp;
16250{
16251 int dir = FORWARD;
16252 char_u *flags;
16253 char_u nbuf[NUMBUFLEN];
16254 int mask;
16255
16256 if (varp->v_type != VAR_UNKNOWN)
16257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 flags = get_tv_string_buf_chk(varp, nbuf);
16259 if (flags == NULL)
16260 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261 while (*flags != NUL)
16262 {
16263 switch (*flags)
16264 {
16265 case 'b': dir = BACKWARD; break;
16266 case 'w': p_ws = TRUE; break;
16267 case 'W': p_ws = FALSE; break;
16268 default: mask = 0;
16269 if (flagsp != NULL)
16270 switch (*flags)
16271 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016272 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016273 case 'e': mask = SP_END; break;
16274 case 'm': mask = SP_RETCOUNT; break;
16275 case 'n': mask = SP_NOMOVE; break;
16276 case 'p': mask = SP_SUBPAT; break;
16277 case 'r': mask = SP_REPEAT; break;
16278 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279 }
16280 if (mask == 0)
16281 {
16282 EMSG2(_(e_invarg2), flags);
16283 dir = 0;
16284 }
16285 else
16286 *flagsp |= mask;
16287 }
16288 if (dir == 0)
16289 break;
16290 ++flags;
16291 }
16292 }
16293 return dir;
16294}
16295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016297 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016299 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016300search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016302 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016303 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016305 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 char_u *pat;
16307 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016308 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 int save_p_ws = p_ws;
16310 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016311 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016312 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016313 proftime_T tm;
16314#ifdef FEAT_RELTIME
16315 long time_limit = 0;
16316#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016317 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016318 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016320 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016321 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016322 if (dir == 0)
16323 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016324 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016325 if (flags & SP_START)
16326 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016327 if (flags & SP_END)
16328 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016329
Bram Moolenaar76929292008-01-06 19:07:36 +000016330 /* Optional arguments: line number to stop searching and timeout. */
16331 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016332 {
16333 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16334 if (lnum_stop < 0)
16335 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016336#ifdef FEAT_RELTIME
16337 if (argvars[3].v_type != VAR_UNKNOWN)
16338 {
16339 time_limit = get_tv_number_chk(&argvars[3], NULL);
16340 if (time_limit < 0)
16341 goto theend;
16342 }
16343#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016344 }
16345
Bram Moolenaar76929292008-01-06 19:07:36 +000016346#ifdef FEAT_RELTIME
16347 /* Set the time limit, if there is one. */
16348 profile_setlimit(time_limit, &tm);
16349#endif
16350
Bram Moolenaar231334e2005-07-25 20:46:57 +000016351 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016352 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016353 * Check to make sure only those flags are set.
16354 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16355 * flags cannot be set. Check for that condition also.
16356 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016357 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016358 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016360 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016361 goto theend;
16362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016364 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016365 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016366 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016367 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016369 if (flags & SP_SUBPAT)
16370 retval = subpatnum;
16371 else
16372 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016373 if (flags & SP_SETPCMARK)
16374 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016376 if (match_pos != NULL)
16377 {
16378 /* Store the match cursor position */
16379 match_pos->lnum = pos.lnum;
16380 match_pos->col = pos.col + 1;
16381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 /* "/$" will put the cursor after the end of the line, may need to
16383 * correct that here */
16384 check_cursor();
16385 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016386
16387 /* If 'n' flag is used: restore cursor position. */
16388 if (flags & SP_NOMOVE)
16389 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016390 else
16391 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016392theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016394
16395 return retval;
16396}
16397
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016398#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016399
16400/*
16401 * round() is not in C90, use ceil() or floor() instead.
16402 */
16403 float_T
16404vim_round(f)
16405 float_T f;
16406{
16407 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16408}
16409
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016410/*
16411 * "round({float})" function
16412 */
16413 static void
16414f_round(argvars, rettv)
16415 typval_T *argvars;
16416 typval_T *rettv;
16417{
16418 float_T f;
16419
16420 rettv->v_type = VAR_FLOAT;
16421 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016422 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016423 else
16424 rettv->vval.v_float = 0.0;
16425}
16426#endif
16427
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016428/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016429 * "screenattr()" function
16430 */
16431 static void
16432f_screenattr(argvars, rettv)
16433 typval_T *argvars UNUSED;
16434 typval_T *rettv;
16435{
16436 int row;
16437 int col;
16438 int c;
16439
16440 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16441 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16442 if (row < 0 || row >= screen_Rows
16443 || col < 0 || col >= screen_Columns)
16444 c = -1;
16445 else
16446 c = ScreenAttrs[LineOffset[row] + col];
16447 rettv->vval.v_number = c;
16448}
16449
16450/*
16451 * "screenchar()" function
16452 */
16453 static void
16454f_screenchar(argvars, rettv)
16455 typval_T *argvars UNUSED;
16456 typval_T *rettv;
16457{
16458 int row;
16459 int col;
16460 int off;
16461 int c;
16462
16463 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16464 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16465 if (row < 0 || row >= screen_Rows
16466 || col < 0 || col >= screen_Columns)
16467 c = -1;
16468 else
16469 {
16470 off = LineOffset[row] + col;
16471#ifdef FEAT_MBYTE
16472 if (enc_utf8 && ScreenLinesUC[off] != 0)
16473 c = ScreenLinesUC[off];
16474 else
16475#endif
16476 c = ScreenLines[off];
16477 }
16478 rettv->vval.v_number = c;
16479}
16480
16481/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016482 * "screencol()" function
16483 *
16484 * First column is 1 to be consistent with virtcol().
16485 */
16486 static void
16487f_screencol(argvars, rettv)
16488 typval_T *argvars UNUSED;
16489 typval_T *rettv;
16490{
16491 rettv->vval.v_number = screen_screencol() + 1;
16492}
16493
16494/*
16495 * "screenrow()" function
16496 */
16497 static void
16498f_screenrow(argvars, rettv)
16499 typval_T *argvars UNUSED;
16500 typval_T *rettv;
16501{
16502 rettv->vval.v_number = screen_screenrow() + 1;
16503}
16504
16505/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016506 * "search()" function
16507 */
16508 static void
16509f_search(argvars, rettv)
16510 typval_T *argvars;
16511 typval_T *rettv;
16512{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016513 int flags = 0;
16514
16515 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516}
16517
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016519 * "searchdecl()" function
16520 */
16521 static void
16522f_searchdecl(argvars, rettv)
16523 typval_T *argvars;
16524 typval_T *rettv;
16525{
16526 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016527 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016528 int error = FALSE;
16529 char_u *name;
16530
16531 rettv->vval.v_number = 1; /* default: FAIL */
16532
16533 name = get_tv_string_chk(&argvars[0]);
16534 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016535 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016536 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016537 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16538 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16539 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016540 if (!error && name != NULL)
16541 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016542 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016543}
16544
16545/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016546 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016548 static int
16549searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016550 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016551 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552{
16553 char_u *spat, *mpat, *epat;
16554 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 int dir;
16557 int flags = 0;
16558 char_u nbuf1[NUMBUFLEN];
16559 char_u nbuf2[NUMBUFLEN];
16560 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016561 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016562 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016563 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016566 spat = get_tv_string_chk(&argvars[0]);
16567 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16568 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16569 if (spat == NULL || mpat == NULL || epat == NULL)
16570 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 /* Handle the optional fourth argument: flags */
16573 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016574 if (dir == 0)
16575 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016576
16577 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016578 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16579 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016580 if ((flags & (SP_END | SP_SUBPAT)) != 0
16581 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016582 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016583 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016584 goto theend;
16585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016587 /* Using 'r' implies 'W', otherwise it doesn't work. */
16588 if (flags & SP_REPEAT)
16589 p_ws = FALSE;
16590
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016591 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016592 if (argvars[3].v_type == VAR_UNKNOWN
16593 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 skip = (char_u *)"";
16595 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016597 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016598 if (argvars[5].v_type != VAR_UNKNOWN)
16599 {
16600 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16601 if (lnum_stop < 0)
16602 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016603#ifdef FEAT_RELTIME
16604 if (argvars[6].v_type != VAR_UNKNOWN)
16605 {
16606 time_limit = get_tv_number_chk(&argvars[6], NULL);
16607 if (time_limit < 0)
16608 goto theend;
16609 }
16610#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016611 }
16612 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 if (skip == NULL)
16614 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016616 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016617 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016618
16619theend:
16620 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016621
16622 return retval;
16623}
16624
16625/*
16626 * "searchpair()" function
16627 */
16628 static void
16629f_searchpair(argvars, rettv)
16630 typval_T *argvars;
16631 typval_T *rettv;
16632{
16633 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16634}
16635
16636/*
16637 * "searchpairpos()" function
16638 */
16639 static void
16640f_searchpairpos(argvars, rettv)
16641 typval_T *argvars;
16642 typval_T *rettv;
16643{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016644 pos_T match_pos;
16645 int lnum = 0;
16646 int col = 0;
16647
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016648 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016649 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016650
16651 if (searchpair_cmn(argvars, &match_pos) > 0)
16652 {
16653 lnum = match_pos.lnum;
16654 col = match_pos.col;
16655 }
16656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016657 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16658 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016659}
16660
16661/*
16662 * Search for a start/middle/end thing.
16663 * Used by searchpair(), see its documentation for the details.
16664 * Returns 0 or -1 for no match,
16665 */
16666 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016667do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16668 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016669 char_u *spat; /* start pattern */
16670 char_u *mpat; /* middle pattern */
16671 char_u *epat; /* end pattern */
16672 int dir; /* BACKWARD or FORWARD */
16673 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016674 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016675 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016676 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016677 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016678{
16679 char_u *save_cpo;
16680 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16681 long retval = 0;
16682 pos_T pos;
16683 pos_T firstpos;
16684 pos_T foundpos;
16685 pos_T save_cursor;
16686 pos_T save_pos;
16687 int n;
16688 int r;
16689 int nest = 1;
16690 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016691 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016692 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016693
16694 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16695 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016696 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016697
Bram Moolenaar76929292008-01-06 19:07:36 +000016698#ifdef FEAT_RELTIME
16699 /* Set the time limit, if there is one. */
16700 profile_setlimit(time_limit, &tm);
16701#endif
16702
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016703 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16704 * start/middle/end (pat3, for the top pair). */
16705 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16706 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16707 if (pat2 == NULL || pat3 == NULL)
16708 goto theend;
16709 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16710 if (*mpat == NUL)
16711 STRCPY(pat3, pat2);
16712 else
16713 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16714 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016715 if (flags & SP_START)
16716 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016717
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 save_cursor = curwin->w_cursor;
16719 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016720 clearpos(&firstpos);
16721 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 pat = pat3;
16723 for (;;)
16724 {
16725 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016726 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16728 /* didn't find it or found the first match again: FAIL */
16729 break;
16730
16731 if (firstpos.lnum == 0)
16732 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016733 if (equalpos(pos, foundpos))
16734 {
16735 /* Found the same position again. Can happen with a pattern that
16736 * has "\zs" at the end and searching backwards. Advance one
16737 * character and try again. */
16738 if (dir == BACKWARD)
16739 decl(&pos);
16740 else
16741 incl(&pos);
16742 }
16743 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016745 /* clear the start flag to avoid getting stuck here */
16746 options &= ~SEARCH_START;
16747
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 /* If the skip pattern matches, ignore this match. */
16749 if (*skip != NUL)
16750 {
16751 save_pos = curwin->w_cursor;
16752 curwin->w_cursor = pos;
16753 r = eval_to_bool(skip, &err, NULL, FALSE);
16754 curwin->w_cursor = save_pos;
16755 if (err)
16756 {
16757 /* Evaluating {skip} caused an error, break here. */
16758 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016759 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 break;
16761 }
16762 if (r)
16763 continue;
16764 }
16765
16766 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16767 {
16768 /* Found end when searching backwards or start when searching
16769 * forward: nested pair. */
16770 ++nest;
16771 pat = pat2; /* nested, don't search for middle */
16772 }
16773 else
16774 {
16775 /* Found end when searching forward or start when searching
16776 * backward: end of (nested) pair; or found middle in outer pair. */
16777 if (--nest == 1)
16778 pat = pat3; /* outer level, search for middle */
16779 }
16780
16781 if (nest == 0)
16782 {
16783 /* Found the match: return matchcount or line number. */
16784 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016785 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016787 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016788 if (flags & SP_SETPCMARK)
16789 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 curwin->w_cursor = pos;
16791 if (!(flags & SP_REPEAT))
16792 break;
16793 nest = 1; /* search for next unmatched */
16794 }
16795 }
16796
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016797 if (match_pos != NULL)
16798 {
16799 /* Store the match cursor position */
16800 match_pos->lnum = curwin->w_cursor.lnum;
16801 match_pos->col = curwin->w_cursor.col + 1;
16802 }
16803
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016805 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 curwin->w_cursor = save_cursor;
16807
16808theend:
16809 vim_free(pat2);
16810 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016811 if (p_cpo == empty_option)
16812 p_cpo = save_cpo;
16813 else
16814 /* Darn, evaluating the {skip} expression changed the value. */
16815 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016816
16817 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818}
16819
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820/*
16821 * "searchpos()" function
16822 */
16823 static void
16824f_searchpos(argvars, rettv)
16825 typval_T *argvars;
16826 typval_T *rettv;
16827{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016828 pos_T match_pos;
16829 int lnum = 0;
16830 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016831 int n;
16832 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016833
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016834 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016835 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016836
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016837 n = search_cmn(argvars, &match_pos, &flags);
16838 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016839 {
16840 lnum = match_pos.lnum;
16841 col = match_pos.col;
16842 }
16843
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016844 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16845 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016846 if (flags & SP_SUBPAT)
16847 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016848}
16849
16850
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851 static void
16852f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016856#ifdef FEAT_CLIENTSERVER
16857 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 char_u *server = get_tv_string_chk(&argvars[0]);
16859 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016862 if (server == NULL || reply == NULL)
16863 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 if (check_restricted() || check_secure())
16865 return;
16866# ifdef FEAT_X11
16867 if (check_connection() == FAIL)
16868 return;
16869# endif
16870
16871 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873 EMSG(_("E258: Unable to send to client"));
16874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876 rettv->vval.v_number = 0;
16877#else
16878 rettv->vval.v_number = -1;
16879#endif
16880}
16881
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 static void
16883f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016884 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016886{
16887 char_u *r = NULL;
16888
16889#ifdef FEAT_CLIENTSERVER
16890# ifdef WIN32
16891 r = serverGetVimNames();
16892# else
16893 make_connection();
16894 if (X_DISPLAY != NULL)
16895 r = serverGetVimNames(X_DISPLAY);
16896# endif
16897#endif
16898 rettv->v_type = VAR_STRING;
16899 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900}
16901
16902/*
16903 * "setbufvar()" function
16904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016906f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016907 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016908 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909{
16910 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 char_u nbuf[NUMBUFLEN];
16915
16916 if (check_restricted() || check_secure())
16917 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016918 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16919 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016920 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 varp = &argvars[2];
16922
16923 if (buf != NULL && varname != NULL && varp != NULL)
16924 {
16925 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927
16928 if (*varname == '&')
16929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 long numval;
16931 char_u *strval;
16932 int error = FALSE;
16933
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935 numval = get_tv_number_chk(varp, &error);
16936 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016937 if (!error && strval != NULL)
16938 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 }
16940 else
16941 {
16942 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16943 if (bufvarname != NULL)
16944 {
16945 STRCPY(bufvarname, "b:");
16946 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016947 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 vim_free(bufvarname);
16949 }
16950 }
16951
16952 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955}
16956
16957/*
16958 * "setcmdpos()" function
16959 */
16960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016961f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 typval_T *argvars;
16963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016965 int pos = (int)get_tv_number(&argvars[0]) - 1;
16966
16967 if (pos >= 0)
16968 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969}
16970
16971/*
16972 * "setline()" function
16973 */
16974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016975f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 typval_T *argvars;
16977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978{
16979 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016980 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016981 list_T *l = NULL;
16982 listitem_T *li = NULL;
16983 long added = 0;
16984 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016986 lnum = get_tv_lnum(&argvars[0]);
16987 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016989 l = argvars[1].vval.v_list;
16990 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016992 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016994
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016995 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016996 for (;;)
16997 {
16998 if (l != NULL)
16999 {
17000 /* list argument, get next string */
17001 if (li == NULL)
17002 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017004 li = li->li_next;
17005 }
17006
17007 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017009 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017010
17011 /* When coming here from Insert mode, sync undo, so that this can be
17012 * undone separately from what was previously inserted. */
17013 if (u_sync_once == 2)
17014 {
17015 u_sync_once = 1; /* notify that u_sync() was called */
17016 u_sync(TRUE);
17017 }
17018
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017019 if (lnum <= curbuf->b_ml.ml_line_count)
17020 {
17021 /* existing line, replace it */
17022 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17023 {
17024 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017025 if (lnum == curwin->w_cursor.lnum)
17026 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017027 rettv->vval.v_number = 0; /* OK */
17028 }
17029 }
17030 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17031 {
17032 /* lnum is one past the last line, append the line */
17033 ++added;
17034 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17035 rettv->vval.v_number = 0; /* OK */
17036 }
17037
17038 if (l == NULL) /* only one string argument */
17039 break;
17040 ++lnum;
17041 }
17042
17043 if (added > 0)
17044 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045}
17046
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017047static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17048
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017050 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017051 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017052 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017053set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017054 win_T *wp UNUSED;
17055 typval_T *list_arg UNUSED;
17056 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017057 typval_T *rettv;
17058{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017059#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017060 char_u *act;
17061 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017062#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017063
Bram Moolenaar2641f772005-03-25 21:58:17 +000017064 rettv->vval.v_number = -1;
17065
17066#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017067 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017068 EMSG(_(e_listreq));
17069 else
17070 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017071 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017072
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017073 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017074 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017075 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017076 if (act == NULL)
17077 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017078 if (*act == 'a' || *act == 'r')
17079 action = *act;
17080 }
17081
Bram Moolenaar81484f42012-12-05 15:16:47 +010017082 if (l != NULL && set_errorlist(wp, l, action,
17083 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017084 rettv->vval.v_number = 0;
17085 }
17086#endif
17087}
17088
17089/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017090 * "setloclist()" function
17091 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017092 static void
17093f_setloclist(argvars, rettv)
17094 typval_T *argvars;
17095 typval_T *rettv;
17096{
17097 win_T *win;
17098
17099 rettv->vval.v_number = -1;
17100
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017101 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017102 if (win != NULL)
17103 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17104}
17105
17106/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017107 * "setmatches()" function
17108 */
17109 static void
17110f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017111 typval_T *argvars UNUSED;
17112 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017113{
17114#ifdef FEAT_SEARCH_EXTRA
17115 list_T *l;
17116 listitem_T *li;
17117 dict_T *d;
17118
17119 rettv->vval.v_number = -1;
17120 if (argvars[0].v_type != VAR_LIST)
17121 {
17122 EMSG(_(e_listreq));
17123 return;
17124 }
17125 if ((l = argvars[0].vval.v_list) != NULL)
17126 {
17127
17128 /* To some extent make sure that we are dealing with a list from
17129 * "getmatches()". */
17130 li = l->lv_first;
17131 while (li != NULL)
17132 {
17133 if (li->li_tv.v_type != VAR_DICT
17134 || (d = li->li_tv.vval.v_dict) == NULL)
17135 {
17136 EMSG(_(e_invarg));
17137 return;
17138 }
17139 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17140 && dict_find(d, (char_u *)"pattern", -1) != NULL
17141 && dict_find(d, (char_u *)"priority", -1) != NULL
17142 && dict_find(d, (char_u *)"id", -1) != NULL))
17143 {
17144 EMSG(_(e_invarg));
17145 return;
17146 }
17147 li = li->li_next;
17148 }
17149
17150 clear_matches(curwin);
17151 li = l->lv_first;
17152 while (li != NULL)
17153 {
17154 d = li->li_tv.vval.v_dict;
17155 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17156 get_dict_string(d, (char_u *)"pattern", FALSE),
17157 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017158 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017159 li = li->li_next;
17160 }
17161 rettv->vval.v_number = 0;
17162 }
17163#endif
17164}
17165
17166/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017167 * "setpos()" function
17168 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017169 static void
17170f_setpos(argvars, rettv)
17171 typval_T *argvars;
17172 typval_T *rettv;
17173{
17174 pos_T pos;
17175 int fnum;
17176 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017177 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017178
Bram Moolenaar08250432008-02-13 11:42:46 +000017179 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017180 name = get_tv_string_chk(argvars);
17181 if (name != NULL)
17182 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017183 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017184 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017185 if (--pos.col < 0)
17186 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017187 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017188 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017189 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017190 if (fnum == curbuf->b_fnum)
17191 {
17192 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017193 if (curswant >= 0)
17194 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017195 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017196 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017197 }
17198 else
17199 EMSG(_(e_invarg));
17200 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017201 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17202 {
17203 /* set mark */
17204 if (setmark_pos(name[1], &pos, fnum) == OK)
17205 rettv->vval.v_number = 0;
17206 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017207 else
17208 EMSG(_(e_invarg));
17209 }
17210 }
17211}
17212
17213/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017214 * "setqflist()" function
17215 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017216 static void
17217f_setqflist(argvars, rettv)
17218 typval_T *argvars;
17219 typval_T *rettv;
17220{
17221 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17222}
17223
17224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 * "setreg()" function
17226 */
17227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017228f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 typval_T *argvars;
17230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231{
17232 int regname;
17233 char_u *strregname;
17234 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017235 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 int append;
17237 char_u yank_type;
17238 long block_len;
17239
17240 block_len = -1;
17241 yank_type = MAUTO;
17242 append = FALSE;
17243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017245 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017247 if (strregname == NULL)
17248 return; /* type error; errmsg already given */
17249 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 if (regname == 0 || regname == '@')
17251 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 stropt = get_tv_string_chk(&argvars[2]);
17256 if (stropt == NULL)
17257 return; /* type error */
17258 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 switch (*stropt)
17260 {
17261 case 'a': case 'A': /* append */
17262 append = TRUE;
17263 break;
17264 case 'v': case 'c': /* character-wise selection */
17265 yank_type = MCHAR;
17266 break;
17267 case 'V': case 'l': /* line-wise selection */
17268 yank_type = MLINE;
17269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 case 'b': case Ctrl_V: /* block-wise selection */
17271 yank_type = MBLOCK;
17272 if (VIM_ISDIGIT(stropt[1]))
17273 {
17274 ++stropt;
17275 block_len = getdigits(&stropt) - 1;
17276 --stropt;
17277 }
17278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 }
17280 }
17281
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017282 if (argvars[1].v_type == VAR_LIST)
17283 {
17284 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017285 char_u **allocval;
17286 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017287 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017288 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017289 int len = argvars[1].vval.v_list->lv_len;
17290 listitem_T *li;
17291
Bram Moolenaar7d647822014-04-05 21:28:56 +020017292 /* First half: use for pointers to result lines; second half: use for
17293 * pointers to allocated copies. */
17294 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017295 if (lstval == NULL)
17296 return;
17297 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017298 allocval = lstval + len + 2;
17299 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017300
17301 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17302 li = li->li_next)
17303 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017304 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017305 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017306 goto free_lstval;
17307 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017308 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017309 /* Need to make a copy, next get_tv_string_buf_chk() will
17310 * overwrite the string. */
17311 strval = vim_strsave(buf);
17312 if (strval == NULL)
17313 goto free_lstval;
17314 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017315 }
17316 *curval++ = strval;
17317 }
17318 *curval++ = NULL;
17319
17320 write_reg_contents_lst(regname, lstval, -1,
17321 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017322free_lstval:
17323 while (curallocval > allocval)
17324 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017325 vim_free(lstval);
17326 }
17327 else
17328 {
17329 strval = get_tv_string_chk(&argvars[1]);
17330 if (strval == NULL)
17331 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336}
17337
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017338/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017339 * "settabvar()" function
17340 */
17341 static void
17342f_settabvar(argvars, rettv)
17343 typval_T *argvars;
17344 typval_T *rettv;
17345{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017346#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017347 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017348 tabpage_T *tp;
17349#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017350 char_u *varname, *tabvarname;
17351 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017352
17353 rettv->vval.v_number = 0;
17354
17355 if (check_restricted() || check_secure())
17356 return;
17357
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017358#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017359 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017360#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017361 varname = get_tv_string_chk(&argvars[1]);
17362 varp = &argvars[2];
17363
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017364 if (varname != NULL && varp != NULL
17365#ifdef FEAT_WINDOWS
17366 && tp != NULL
17367#endif
17368 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017369 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017370#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017371 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017372 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017373#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017374
17375 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17376 if (tabvarname != NULL)
17377 {
17378 STRCPY(tabvarname, "t:");
17379 STRCPY(tabvarname + 2, varname);
17380 set_var(tabvarname, varp, TRUE);
17381 vim_free(tabvarname);
17382 }
17383
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017384#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017385 /* Restore current tabpage */
17386 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017387 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017388#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017389 }
17390}
17391
17392/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017393 * "settabwinvar()" function
17394 */
17395 static void
17396f_settabwinvar(argvars, rettv)
17397 typval_T *argvars;
17398 typval_T *rettv;
17399{
17400 setwinvar(argvars, rettv, 1);
17401}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402
17403/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017404 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 typval_T *argvars;
17409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017411 setwinvar(argvars, rettv, 0);
17412}
17413
17414/*
17415 * "setwinvar()" and "settabwinvar()" functions
17416 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017417
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017418 static void
17419setwinvar(argvars, rettv, off)
17420 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017421 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017422 int off;
17423{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 win_T *win;
17425#ifdef FEAT_WINDOWS
17426 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017427 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428#endif
17429 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017432 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433
17434 if (check_restricted() || check_secure())
17435 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017436
17437#ifdef FEAT_WINDOWS
17438 if (off == 1)
17439 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17440 else
17441 tp = curtab;
17442#endif
17443 win = find_win_by_nr(&argvars[off], tp);
17444 varname = get_tv_string_chk(&argvars[off + 1]);
17445 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446
17447 if (win != NULL && varname != NULL && varp != NULL)
17448 {
17449#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017450 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017453 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017455 long numval;
17456 char_u *strval;
17457 int error = FALSE;
17458
17459 ++varname;
17460 numval = get_tv_number_chk(varp, &error);
17461 strval = get_tv_string_buf_chk(varp, nbuf);
17462 if (!error && strval != NULL)
17463 set_option_value(varname, numval, strval, OPT_LOCAL);
17464 }
17465 else
17466 {
17467 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17468 if (winvarname != NULL)
17469 {
17470 STRCPY(winvarname, "w:");
17471 STRCPY(winvarname + 2, varname);
17472 set_var(winvarname, varp, TRUE);
17473 vim_free(winvarname);
17474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 }
17476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017478 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479#endif
17480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481}
17482
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017483#ifdef FEAT_CRYPT
17484/*
17485 * "sha256({string})" function
17486 */
17487 static void
17488f_sha256(argvars, rettv)
17489 typval_T *argvars;
17490 typval_T *rettv;
17491{
17492 char_u *p;
17493
17494 p = get_tv_string(&argvars[0]);
17495 rettv->vval.v_string = vim_strsave(
17496 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17497 rettv->v_type = VAR_STRING;
17498}
17499#endif /* FEAT_CRYPT */
17500
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017502 * "shellescape({string})" function
17503 */
17504 static void
17505f_shellescape(argvars, rettv)
17506 typval_T *argvars;
17507 typval_T *rettv;
17508{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017509 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017510 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017511 rettv->v_type = VAR_STRING;
17512}
17513
17514/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017515 * shiftwidth() function
17516 */
17517 static void
17518f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017519 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017520 typval_T *rettv;
17521{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017522 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017523}
17524
17525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 */
17528 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017530 typval_T *argvars;
17531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535 p = get_tv_string(&argvars[0]);
17536 rettv->vval.v_string = vim_strsave(p);
17537 simplify_filename(rettv->vval.v_string); /* simplify in place */
17538 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539}
17540
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017541#ifdef FEAT_FLOAT
17542/*
17543 * "sin()" function
17544 */
17545 static void
17546f_sin(argvars, rettv)
17547 typval_T *argvars;
17548 typval_T *rettv;
17549{
17550 float_T f;
17551
17552 rettv->v_type = VAR_FLOAT;
17553 if (get_float_arg(argvars, &f) == OK)
17554 rettv->vval.v_float = sin(f);
17555 else
17556 rettv->vval.v_float = 0.0;
17557}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017558
17559/*
17560 * "sinh()" function
17561 */
17562 static void
17563f_sinh(argvars, rettv)
17564 typval_T *argvars;
17565 typval_T *rettv;
17566{
17567 float_T f;
17568
17569 rettv->v_type = VAR_FLOAT;
17570 if (get_float_arg(argvars, &f) == OK)
17571 rettv->vval.v_float = sinh(f);
17572 else
17573 rettv->vval.v_float = 0.0;
17574}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017575#endif
17576
Bram Moolenaar0d660222005-01-07 21:51:51 +000017577static int
17578#ifdef __BORLANDC__
17579 _RTLENTRYF
17580#endif
17581 item_compare __ARGS((const void *s1, const void *s2));
17582static int
17583#ifdef __BORLANDC__
17584 _RTLENTRYF
17585#endif
17586 item_compare2 __ARGS((const void *s1, const void *s2));
17587
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017588/* struct used in the array that's given to qsort() */
17589typedef struct
17590{
17591 listitem_T *item;
17592 int idx;
17593} sortItem_T;
17594
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017596static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017598static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017599static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017600static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017601static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017602#define ITEM_COMPARE_FAIL 999
17603
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017605 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017607 static int
17608#ifdef __BORLANDC__
17609_RTLENTRYF
17610#endif
17611item_compare(s1, s2)
17612 const void *s1;
17613 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017615 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017616 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017618 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017619 int res;
17620 char_u numbuf1[NUMBUFLEN];
17621 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017623 si1 = (sortItem_T *)s1;
17624 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017625 tv1 = &si1->item->li_tv;
17626 tv2 = &si2->item->li_tv;
17627 /* tv2string() puts quotes around a string and allocates memory. Don't do
17628 * that for string variables. Use a single quote when comparing with a
17629 * non-string to do what the docs promise. */
17630 if (tv1->v_type == VAR_STRING)
17631 {
17632 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17633 p1 = (char_u *)"'";
17634 else
17635 p1 = tv1->vval.v_string;
17636 }
17637 else
17638 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17639 if (tv2->v_type == VAR_STRING)
17640 {
17641 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17642 p2 = (char_u *)"'";
17643 else
17644 p2 = tv2->vval.v_string;
17645 }
17646 else
17647 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017648 if (p1 == NULL)
17649 p1 = (char_u *)"";
17650 if (p2 == NULL)
17651 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017652 if (!item_compare_numeric)
17653 {
17654 if (item_compare_ic)
17655 res = STRICMP(p1, p2);
17656 else
17657 res = STRCMP(p1, p2);
17658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017660 {
17661 double n1, n2;
17662 n1 = strtod((char *)p1, (char **)&p1);
17663 n2 = strtod((char *)p2, (char **)&p2);
17664 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17665 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017666
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017667 /* When the result would be zero, compare the item indexes. Makes the
17668 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017669 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017670 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017671
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672 vim_free(tofree1);
17673 vim_free(tofree2);
17674 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675}
17676
17677 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017678#ifdef __BORLANDC__
17679_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017681item_compare2(s1, s2)
17682 const void *s1;
17683 const void *s2;
17684{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017685 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017686 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017688 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017691 /* shortcut after failure in previous call; compare all items equal */
17692 if (item_compare_func_err)
17693 return 0;
17694
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017695 si1 = (sortItem_T *)s1;
17696 si2 = (sortItem_T *)s2;
17697
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017698 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017699 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017700 copy_tv(&si1->item->li_tv, &argv[0]);
17701 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702
17703 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017704 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017705 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17706 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707 clear_tv(&argv[0]);
17708 clear_tv(&argv[1]);
17709
17710 if (res == FAIL)
17711 res = ITEM_COMPARE_FAIL;
17712 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017713 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17714 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017715 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017716 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017717
17718 /* When the result would be zero, compare the pointers themselves. Makes
17719 * the sort stable. */
17720 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017721 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017722
Bram Moolenaar0d660222005-01-07 21:51:51 +000017723 return res;
17724}
17725
17726/*
17727 * "sort({list})" function
17728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017730do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017731 typval_T *argvars;
17732 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017733 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734{
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 list_T *l;
17736 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017737 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738 long len;
17739 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740
Bram Moolenaar0d660222005-01-07 21:51:51 +000017741 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017742 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 else
17744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017746 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017747 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017748 return;
17749 rettv->vval.v_list = l;
17750 rettv->v_type = VAR_LIST;
17751 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752
Bram Moolenaar0d660222005-01-07 21:51:51 +000017753 len = list_len(l);
17754 if (len <= 1)
17755 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017758 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017759 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017760 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761 if (argvars[1].v_type != VAR_UNKNOWN)
17762 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017763 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017764 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017765 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766 else
17767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 int error = FALSE;
17769
17770 i = get_tv_number_chk(&argvars[1], &error);
17771 if (error)
17772 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773 if (i == 1)
17774 item_compare_ic = TRUE;
17775 else
17776 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017777 if (item_compare_func != NULL)
17778 {
17779 if (STRCMP(item_compare_func, "n") == 0)
17780 {
17781 item_compare_func = NULL;
17782 item_compare_numeric = TRUE;
17783 }
17784 else if (STRCMP(item_compare_func, "i") == 0)
17785 {
17786 item_compare_func = NULL;
17787 item_compare_ic = TRUE;
17788 }
17789 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017791
17792 if (argvars[2].v_type != VAR_UNKNOWN)
17793 {
17794 /* optional third argument: {dict} */
17795 if (argvars[2].v_type != VAR_DICT)
17796 {
17797 EMSG(_(e_dictreq));
17798 return;
17799 }
17800 item_compare_selfdict = argvars[2].vval.v_dict;
17801 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803
Bram Moolenaar0d660222005-01-07 21:51:51 +000017804 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017805 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017806 if (ptrs == NULL)
17807 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
Bram Moolenaar327aa022014-03-25 18:24:23 +010017809 i = 0;
17810 if (sort)
17811 {
17812 /* sort(): ptrs will be the list to sort */
17813 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017814 {
17815 ptrs[i].item = li;
17816 ptrs[i].idx = i;
17817 ++i;
17818 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017819
17820 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017821 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017822 /* test the compare function */
17823 if (item_compare_func != NULL
17824 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017826 EMSG(_("E702: Sort compare function failed"));
17827 else
17828 {
17829 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017830 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017831 item_compare_func == NULL ? item_compare : item_compare2);
17832
17833 if (!item_compare_func_err)
17834 {
17835 /* Clear the List and append the items in sorted order. */
17836 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17837 l->lv_len = 0;
17838 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017839 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017840 }
17841 }
17842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017845 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17846
17847 /* f_uniq(): ptrs will be a stack of items to remove */
17848 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017849 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017850 item_compare_func_ptr = item_compare_func
17851 ? item_compare2 : item_compare;
17852
17853 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17854 li = li->li_next)
17855 {
17856 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17857 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017858 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017859 if (item_compare_func_err)
17860 {
17861 EMSG(_("E882: Uniq compare function failed"));
17862 break;
17863 }
17864 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017866 if (!item_compare_func_err)
17867 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017868 while (--i >= 0)
17869 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017870 li = ptrs[i].item->li_next;
17871 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017872 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017873 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017874 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017875 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017876 list_fix_watch(l, li);
17877 listitem_free(li);
17878 l->lv_len--;
17879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017880 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881 }
17882
17883 vim_free(ptrs);
17884 }
17885}
17886
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017887/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017888 * "sort({list})" function
17889 */
17890 static void
17891f_sort(argvars, rettv)
17892 typval_T *argvars;
17893 typval_T *rettv;
17894{
17895 do_sort_uniq(argvars, rettv, TRUE);
17896}
17897
17898/*
17899 * "uniq({list})" function
17900 */
17901 static void
17902f_uniq(argvars, rettv)
17903 typval_T *argvars;
17904 typval_T *rettv;
17905{
17906 do_sort_uniq(argvars, rettv, FALSE);
17907}
17908
17909/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017910 * "soundfold({word})" function
17911 */
17912 static void
17913f_soundfold(argvars, rettv)
17914 typval_T *argvars;
17915 typval_T *rettv;
17916{
17917 char_u *s;
17918
17919 rettv->v_type = VAR_STRING;
17920 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017921#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017922 rettv->vval.v_string = eval_soundfold(s);
17923#else
17924 rettv->vval.v_string = vim_strsave(s);
17925#endif
17926}
17927
17928/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017929 * "spellbadword()" function
17930 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017931 static void
17932f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017933 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017934 typval_T *rettv;
17935{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017936 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017937 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017938 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017939
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017940 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017941 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017942
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017943#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017944 if (argvars[0].v_type == VAR_UNKNOWN)
17945 {
17946 /* Find the start and length of the badly spelled word. */
17947 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17948 if (len != 0)
17949 word = ml_get_cursor();
17950 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017951 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017952 {
17953 char_u *str = get_tv_string_chk(&argvars[0]);
17954 int capcol = -1;
17955
17956 if (str != NULL)
17957 {
17958 /* Check the argument for spelling. */
17959 while (*str != NUL)
17960 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017961 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017962 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017963 {
17964 word = str;
17965 break;
17966 }
17967 str += len;
17968 }
17969 }
17970 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017971#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017973 list_append_string(rettv->vval.v_list, word, len);
17974 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017975 attr == HLF_SPB ? "bad" :
17976 attr == HLF_SPR ? "rare" :
17977 attr == HLF_SPL ? "local" :
17978 attr == HLF_SPC ? "caps" :
17979 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017980}
17981
17982/*
17983 * "spellsuggest()" function
17984 */
17985 static void
17986f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017987 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017988 typval_T *rettv;
17989{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017990#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017991 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017992 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017993 int maxcount;
17994 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017995 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017996 listitem_T *li;
17997 int need_capital = FALSE;
17998#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017999
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018000 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018001 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018002
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018003#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018004 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018005 {
18006 str = get_tv_string(&argvars[0]);
18007 if (argvars[1].v_type != VAR_UNKNOWN)
18008 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018009 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018010 if (maxcount <= 0)
18011 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018012 if (argvars[2].v_type != VAR_UNKNOWN)
18013 {
18014 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18015 if (typeerr)
18016 return;
18017 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018018 }
18019 else
18020 maxcount = 25;
18021
Bram Moolenaar4770d092006-01-12 23:22:24 +000018022 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018023
18024 for (i = 0; i < ga.ga_len; ++i)
18025 {
18026 str = ((char_u **)ga.ga_data)[i];
18027
18028 li = listitem_alloc();
18029 if (li == NULL)
18030 vim_free(str);
18031 else
18032 {
18033 li->li_tv.v_type = VAR_STRING;
18034 li->li_tv.v_lock = 0;
18035 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018036 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018037 }
18038 }
18039 ga_clear(&ga);
18040 }
18041#endif
18042}
18043
Bram Moolenaar0d660222005-01-07 21:51:51 +000018044 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018045f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018046 typval_T *argvars;
18047 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018048{
18049 char_u *str;
18050 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018051 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018052 regmatch_T regmatch;
18053 char_u patbuf[NUMBUFLEN];
18054 char_u *save_cpo;
18055 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018056 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018057 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018058 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018059
18060 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18061 save_cpo = p_cpo;
18062 p_cpo = (char_u *)"";
18063
18064 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018065 if (argvars[1].v_type != VAR_UNKNOWN)
18066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018067 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18068 if (pat == NULL)
18069 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018070 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018071 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018072 }
18073 if (pat == NULL || *pat == NUL)
18074 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018075
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018076 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 if (typeerr)
18079 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18082 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018085 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018086 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018087 if (*str == NUL)
18088 match = FALSE; /* empty item at the end */
18089 else
18090 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091 if (match)
18092 end = regmatch.startp[0];
18093 else
18094 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018095 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18096 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018097 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018098 if (list_append_string(rettv->vval.v_list, str,
18099 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018100 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018101 }
18102 if (!match)
18103 break;
18104 /* Advance to just after the match. */
18105 if (regmatch.endp[0] > str)
18106 col = 0;
18107 else
18108 {
18109 /* Don't get stuck at the same match. */
18110#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018111 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018112#else
18113 col = 1;
18114#endif
18115 }
18116 str = regmatch.endp[0];
18117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118
Bram Moolenaar473de612013-06-08 18:19:48 +020018119 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121
Bram Moolenaar0d660222005-01-07 21:51:51 +000018122 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123}
18124
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018125#ifdef FEAT_FLOAT
18126/*
18127 * "sqrt()" function
18128 */
18129 static void
18130f_sqrt(argvars, rettv)
18131 typval_T *argvars;
18132 typval_T *rettv;
18133{
18134 float_T f;
18135
18136 rettv->v_type = VAR_FLOAT;
18137 if (get_float_arg(argvars, &f) == OK)
18138 rettv->vval.v_float = sqrt(f);
18139 else
18140 rettv->vval.v_float = 0.0;
18141}
18142
18143/*
18144 * "str2float()" function
18145 */
18146 static void
18147f_str2float(argvars, rettv)
18148 typval_T *argvars;
18149 typval_T *rettv;
18150{
18151 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18152
18153 if (*p == '+')
18154 p = skipwhite(p + 1);
18155 (void)string2float(p, &rettv->vval.v_float);
18156 rettv->v_type = VAR_FLOAT;
18157}
18158#endif
18159
Bram Moolenaar2c932302006-03-18 21:42:09 +000018160/*
18161 * "str2nr()" function
18162 */
18163 static void
18164f_str2nr(argvars, rettv)
18165 typval_T *argvars;
18166 typval_T *rettv;
18167{
18168 int base = 10;
18169 char_u *p;
18170 long n;
18171
18172 if (argvars[1].v_type != VAR_UNKNOWN)
18173 {
18174 base = get_tv_number(&argvars[1]);
18175 if (base != 8 && base != 10 && base != 16)
18176 {
18177 EMSG(_(e_invarg));
18178 return;
18179 }
18180 }
18181
18182 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018183 if (*p == '+')
18184 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018185 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18186 rettv->vval.v_number = n;
18187}
18188
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189#ifdef HAVE_STRFTIME
18190/*
18191 * "strftime({format}[, {time}])" function
18192 */
18193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018194f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018195 typval_T *argvars;
18196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197{
18198 char_u result_buf[256];
18199 struct tm *curtime;
18200 time_t seconds;
18201 char_u *p;
18202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018203 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018206 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 seconds = time(NULL);
18208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018209 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 curtime = localtime(&seconds);
18211 /* MSVC returns NULL for an invalid value of seconds. */
18212 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 else
18215 {
18216# ifdef FEAT_MBYTE
18217 vimconv_T conv;
18218 char_u *enc;
18219
18220 conv.vc_type = CONV_NONE;
18221 enc = enc_locale();
18222 convert_setup(&conv, p_enc, enc);
18223 if (conv.vc_type != CONV_NONE)
18224 p = string_convert(&conv, p, NULL);
18225# endif
18226 if (p != NULL)
18227 (void)strftime((char *)result_buf, sizeof(result_buf),
18228 (char *)p, curtime);
18229 else
18230 result_buf[0] = NUL;
18231
18232# ifdef FEAT_MBYTE
18233 if (conv.vc_type != CONV_NONE)
18234 vim_free(p);
18235 convert_setup(&conv, enc, p_enc);
18236 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018237 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 else
18239# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241
18242# ifdef FEAT_MBYTE
18243 /* Release conversion descriptors */
18244 convert_setup(&conv, NULL, NULL);
18245 vim_free(enc);
18246# endif
18247 }
18248}
18249#endif
18250
18251/*
18252 * "stridx()" function
18253 */
18254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018255f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018256 typval_T *argvars;
18257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258{
18259 char_u buf[NUMBUFLEN];
18260 char_u *needle;
18261 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018262 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018264 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018266 needle = get_tv_string_chk(&argvars[1]);
18267 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018269 if (needle == NULL || haystack == NULL)
18270 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271
Bram Moolenaar33570922005-01-25 22:26:29 +000018272 if (argvars[2].v_type != VAR_UNKNOWN)
18273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018274 int error = FALSE;
18275
18276 start_idx = get_tv_number_chk(&argvars[2], &error);
18277 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018278 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018279 if (start_idx >= 0)
18280 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018281 }
18282
18283 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18284 if (pos != NULL)
18285 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286}
18287
18288/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018289 * "string()" function
18290 */
18291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018292f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018293 typval_T *argvars;
18294 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018295{
18296 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018297 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018300 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018301 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018302 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018303 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304}
18305
18306/*
18307 * "strlen()" function
18308 */
18309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018310f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018311 typval_T *argvars;
18312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018314 rettv->vval.v_number = (varnumber_T)(STRLEN(
18315 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316}
18317
18318/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018319 * "strchars()" function
18320 */
18321 static void
18322f_strchars(argvars, rettv)
18323 typval_T *argvars;
18324 typval_T *rettv;
18325{
18326 char_u *s = get_tv_string(&argvars[0]);
18327#ifdef FEAT_MBYTE
18328 varnumber_T len = 0;
18329
18330 while (*s != NUL)
18331 {
18332 mb_cptr2char_adv(&s);
18333 ++len;
18334 }
18335 rettv->vval.v_number = len;
18336#else
18337 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18338#endif
18339}
18340
18341/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018342 * "strdisplaywidth()" function
18343 */
18344 static void
18345f_strdisplaywidth(argvars, rettv)
18346 typval_T *argvars;
18347 typval_T *rettv;
18348{
18349 char_u *s = get_tv_string(&argvars[0]);
18350 int col = 0;
18351
18352 if (argvars[1].v_type != VAR_UNKNOWN)
18353 col = get_tv_number(&argvars[1]);
18354
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018355 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018356}
18357
18358/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018359 * "strwidth()" function
18360 */
18361 static void
18362f_strwidth(argvars, rettv)
18363 typval_T *argvars;
18364 typval_T *rettv;
18365{
18366 char_u *s = get_tv_string(&argvars[0]);
18367
18368 rettv->vval.v_number = (varnumber_T)(
18369#ifdef FEAT_MBYTE
18370 mb_string2cells(s, -1)
18371#else
18372 STRLEN(s)
18373#endif
18374 );
18375}
18376
18377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 * "strpart()" function
18379 */
18380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 typval_T *argvars;
18383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384{
18385 char_u *p;
18386 int n;
18387 int len;
18388 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018389 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018391 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 slen = (int)STRLEN(p);
18393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018394 n = get_tv_number_chk(&argvars[1], &error);
18395 if (error)
18396 len = 0;
18397 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018398 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 else
18400 len = slen - n; /* default len: all bytes that are available. */
18401
18402 /*
18403 * Only return the overlap between the specified part and the actual
18404 * string.
18405 */
18406 if (n < 0)
18407 {
18408 len += n;
18409 n = 0;
18410 }
18411 else if (n > slen)
18412 n = slen;
18413 if (len < 0)
18414 len = 0;
18415 else if (n + len > slen)
18416 len = slen - n;
18417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 rettv->v_type = VAR_STRING;
18419 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420}
18421
18422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423 * "strridx()" function
18424 */
18425 static void
18426f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018427 typval_T *argvars;
18428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429{
18430 char_u buf[NUMBUFLEN];
18431 char_u *needle;
18432 char_u *haystack;
18433 char_u *rest;
18434 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018435 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018437 needle = get_tv_string_chk(&argvars[1]);
18438 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018439
18440 rettv->vval.v_number = -1;
18441 if (needle == NULL || haystack == NULL)
18442 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018443
18444 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018445 if (argvars[2].v_type != VAR_UNKNOWN)
18446 {
18447 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018448 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018449 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018451 }
18452 else
18453 end_idx = haystack_len;
18454
Bram Moolenaar0d660222005-01-07 21:51:51 +000018455 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018456 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018458 lastmatch = haystack + end_idx;
18459 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018461 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018462 for (rest = haystack; *rest != '\0'; ++rest)
18463 {
18464 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018465 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018466 break;
18467 lastmatch = rest;
18468 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018469 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470
18471 if (lastmatch == NULL)
18472 rettv->vval.v_number = -1;
18473 else
18474 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18475}
18476
18477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 * "strtrans()" function
18479 */
18480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018482 typval_T *argvars;
18483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018485 rettv->v_type = VAR_STRING;
18486 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487}
18488
18489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018490 * "submatch()" function
18491 */
18492 static void
18493f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018494 typval_T *argvars;
18495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496{
Bram Moolenaar41571762014-04-02 19:00:58 +020018497 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018498 int no;
18499 int retList = 0;
18500
18501 no = (int)get_tv_number_chk(&argvars[0], &error);
18502 if (error)
18503 return;
18504 error = FALSE;
18505 if (argvars[1].v_type != VAR_UNKNOWN)
18506 retList = get_tv_number_chk(&argvars[1], &error);
18507 if (error)
18508 return;
18509
18510 if (retList == 0)
18511 {
18512 rettv->v_type = VAR_STRING;
18513 rettv->vval.v_string = reg_submatch(no);
18514 }
18515 else
18516 {
18517 rettv->v_type = VAR_LIST;
18518 rettv->vval.v_list = reg_submatch_list(no);
18519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018520}
18521
18522/*
18523 * "substitute()" function
18524 */
18525 static void
18526f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018527 typval_T *argvars;
18528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529{
18530 char_u patbuf[NUMBUFLEN];
18531 char_u subbuf[NUMBUFLEN];
18532 char_u flagsbuf[NUMBUFLEN];
18533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018534 char_u *str = get_tv_string_chk(&argvars[0]);
18535 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18536 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18537 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18538
Bram Moolenaar0d660222005-01-07 21:51:51 +000018539 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018540 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18541 rettv->vval.v_string = NULL;
18542 else
18543 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018544}
18545
18546/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018547 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018551 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553{
18554 int id = 0;
18555#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018556 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 long col;
18558 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018559 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018561 lnum = get_tv_lnum(argvars); /* -1 on type error */
18562 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18563 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018565 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018566 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018567 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568#endif
18569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571}
18572
18573/*
18574 * "synIDattr(id, what [, mode])" function
18575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018578 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
18581 char_u *p = NULL;
18582#ifdef FEAT_SYN_HL
18583 int id;
18584 char_u *what;
18585 char_u *mode;
18586 char_u modebuf[NUMBUFLEN];
18587 int modec;
18588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589 id = get_tv_number(&argvars[0]);
18590 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018591 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018593 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018595 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 modec = 0; /* replace invalid with current */
18597 }
18598 else
18599 {
18600#ifdef FEAT_GUI
18601 if (gui.in_use)
18602 modec = 'g';
18603 else
18604#endif
18605 if (t_colors > 1)
18606 modec = 'c';
18607 else
18608 modec = 't';
18609 }
18610
18611
18612 switch (TOLOWER_ASC(what[0]))
18613 {
18614 case 'b':
18615 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18616 p = highlight_color(id, what, modec);
18617 else /* bold */
18618 p = highlight_has_attr(id, HL_BOLD, modec);
18619 break;
18620
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018621 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 p = highlight_color(id, what, modec);
18623 break;
18624
18625 case 'i':
18626 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18627 p = highlight_has_attr(id, HL_INVERSE, modec);
18628 else /* italic */
18629 p = highlight_has_attr(id, HL_ITALIC, modec);
18630 break;
18631
18632 case 'n': /* name */
18633 p = get_highlight_name(NULL, id - 1);
18634 break;
18635
18636 case 'r': /* reverse */
18637 p = highlight_has_attr(id, HL_INVERSE, modec);
18638 break;
18639
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018640 case 's':
18641 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18642 p = highlight_color(id, what, modec);
18643 else /* standout */
18644 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 break;
18646
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018647 case 'u':
18648 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18649 /* underline */
18650 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18651 else
18652 /* undercurl */
18653 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 break;
18655 }
18656
18657 if (p != NULL)
18658 p = vim_strsave(p);
18659#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->v_type = VAR_STRING;
18661 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662}
18663
18664/*
18665 * "synIDtrans(id)" function
18666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018668f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018669 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671{
18672 int id;
18673
18674#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676
18677 if (id > 0)
18678 id = syn_get_final_id(id);
18679 else
18680#endif
18681 id = 0;
18682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684}
18685
18686/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018687 * "synconcealed(lnum, col)" function
18688 */
18689 static void
18690f_synconcealed(argvars, rettv)
18691 typval_T *argvars UNUSED;
18692 typval_T *rettv;
18693{
18694#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18695 long lnum;
18696 long col;
18697 int syntax_flags = 0;
18698 int cchar;
18699 int matchid = 0;
18700 char_u str[NUMBUFLEN];
18701#endif
18702
18703 rettv->v_type = VAR_LIST;
18704 rettv->vval.v_list = NULL;
18705
18706#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18707 lnum = get_tv_lnum(argvars); /* -1 on type error */
18708 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18709
18710 vim_memset(str, NUL, sizeof(str));
18711
18712 if (rettv_list_alloc(rettv) != FAIL)
18713 {
18714 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18715 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18716 && curwin->w_p_cole > 0)
18717 {
18718 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18719 syntax_flags = get_syntax_info(&matchid);
18720
18721 /* get the conceal character */
18722 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18723 {
18724 cchar = syn_get_sub_char();
18725 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18726 cchar = lcs_conceal;
18727 if (cchar != NUL)
18728 {
18729# ifdef FEAT_MBYTE
18730 if (has_mbyte)
18731 (*mb_char2bytes)(cchar, str);
18732 else
18733# endif
18734 str[0] = cchar;
18735 }
18736 }
18737 }
18738
18739 list_append_number(rettv->vval.v_list,
18740 (syntax_flags & HL_CONCEAL) != 0);
18741 /* -1 to auto-determine strlen */
18742 list_append_string(rettv->vval.v_list, str, -1);
18743 list_append_number(rettv->vval.v_list, matchid);
18744 }
18745#endif
18746}
18747
18748/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018749 * "synstack(lnum, col)" function
18750 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018751 static void
18752f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018753 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018754 typval_T *rettv;
18755{
18756#ifdef FEAT_SYN_HL
18757 long lnum;
18758 long col;
18759 int i;
18760 int id;
18761#endif
18762
18763 rettv->v_type = VAR_LIST;
18764 rettv->vval.v_list = NULL;
18765
18766#ifdef FEAT_SYN_HL
18767 lnum = get_tv_lnum(argvars); /* -1 on type error */
18768 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18769
18770 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018771 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018772 && rettv_list_alloc(rettv) != FAIL)
18773 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018774 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018775 for (i = 0; ; ++i)
18776 {
18777 id = syn_get_stack_item(i);
18778 if (id < 0)
18779 break;
18780 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18781 break;
18782 }
18783 }
18784#endif
18785}
18786
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018788get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018789 typval_T *argvars;
18790 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018791 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018793 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018795 char_u *infile = NULL;
18796 char_u buf[NUMBUFLEN];
18797 int err = FALSE;
18798 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018799 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018800 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018802 rettv->v_type = VAR_STRING;
18803 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018804 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018805 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018806
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018807 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018808 {
18809 /*
18810 * Write the string to a temp file, to be used for input of the shell
18811 * command.
18812 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018813 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018814 {
18815 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018816 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018817 }
18818
18819 fd = mch_fopen((char *)infile, WRITEBIN);
18820 if (fd == NULL)
18821 {
18822 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018823 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018824 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018825 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018826 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018827 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18828 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018829 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018830 else
18831 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018832 size_t len;
18833
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018834 p = get_tv_string_buf_chk(&argvars[1], buf);
18835 if (p == NULL)
18836 {
18837 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018838 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018839 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018840 len = STRLEN(p);
18841 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018842 err = TRUE;
18843 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018844 if (fclose(fd) != 0)
18845 err = TRUE;
18846 if (err)
18847 {
18848 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018849 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018850 }
18851 }
18852
Bram Moolenaar52a72462014-08-29 15:53:52 +020018853 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18854 * echoes typeahead, that messes up the display. */
18855 if (!msg_silent)
18856 flags += SHELL_COOKED;
18857
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018858 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018860 int len;
18861 listitem_T *li;
18862 char_u *s = NULL;
18863 char_u *start;
18864 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018865 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866
Bram Moolenaar52a72462014-08-29 15:53:52 +020018867 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018868 if (res == NULL)
18869 goto errret;
18870
18871 list = list_alloc();
18872 if (list == NULL)
18873 goto errret;
18874
18875 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018877 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018878 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018879 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018880 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018881
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018882 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018883 if (s == NULL)
18884 goto errret;
18885
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018886 for (p = s; start < end; ++p, ++start)
18887 *p = *start == NUL ? NL : *start;
18888 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018889
18890 li = listitem_alloc();
18891 if (li == NULL)
18892 {
18893 vim_free(s);
18894 goto errret;
18895 }
18896 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018897 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018898 li->li_tv.vval.v_string = s;
18899 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018901
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018902 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018903 rettv->v_type = VAR_LIST;
18904 rettv->vval.v_list = list;
18905 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018907 else
18908 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018909 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018910#ifdef USE_CR
18911 /* translate <CR> into <NL> */
18912 if (res != NULL)
18913 {
18914 char_u *s;
18915
18916 for (s = res; *s; ++s)
18917 {
18918 if (*s == CAR)
18919 *s = NL;
18920 }
18921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922#else
18923# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018924 /* translate <CR><NL> into <NL> */
18925 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018927 char_u *s, *d;
18928
18929 d = res;
18930 for (s = res; *s; ++s)
18931 {
18932 if (s[0] == CAR && s[1] == NL)
18933 ++s;
18934 *d++ = *s;
18935 }
18936 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938# endif
18939#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018940 rettv->vval.v_string = res;
18941 res = NULL;
18942 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018943
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018944errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018945 if (infile != NULL)
18946 {
18947 mch_remove(infile);
18948 vim_free(infile);
18949 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018950 if (res != NULL)
18951 vim_free(res);
18952 if (list != NULL)
18953 list_free(list, TRUE);
18954}
18955
18956/*
18957 * "system()" function
18958 */
18959 static void
18960f_system(argvars, rettv)
18961 typval_T *argvars;
18962 typval_T *rettv;
18963{
18964 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18965}
18966
18967/*
18968 * "systemlist()" function
18969 */
18970 static void
18971f_systemlist(argvars, rettv)
18972 typval_T *argvars;
18973 typval_T *rettv;
18974{
18975 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976}
18977
18978/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018979 * "tabpagebuflist()" function
18980 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018981 static void
18982f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018983 typval_T *argvars UNUSED;
18984 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018985{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018986#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018987 tabpage_T *tp;
18988 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018989
18990 if (argvars[0].v_type == VAR_UNKNOWN)
18991 wp = firstwin;
18992 else
18993 {
18994 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18995 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018996 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018997 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018998 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018999 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019000 for (; wp != NULL; wp = wp->w_next)
19001 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019002 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019003 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019004 }
19005#endif
19006}
19007
19008
19009/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019010 * "tabpagenr()" function
19011 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019012 static void
19013f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019014 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019015 typval_T *rettv;
19016{
19017 int nr = 1;
19018#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019019 char_u *arg;
19020
19021 if (argvars[0].v_type != VAR_UNKNOWN)
19022 {
19023 arg = get_tv_string_chk(&argvars[0]);
19024 nr = 0;
19025 if (arg != NULL)
19026 {
19027 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019028 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019029 else
19030 EMSG2(_(e_invexpr2), arg);
19031 }
19032 }
19033 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019034 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019035#endif
19036 rettv->vval.v_number = nr;
19037}
19038
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019039
19040#ifdef FEAT_WINDOWS
19041static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19042
19043/*
19044 * Common code for tabpagewinnr() and winnr().
19045 */
19046 static int
19047get_winnr(tp, argvar)
19048 tabpage_T *tp;
19049 typval_T *argvar;
19050{
19051 win_T *twin;
19052 int nr = 1;
19053 win_T *wp;
19054 char_u *arg;
19055
19056 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19057 if (argvar->v_type != VAR_UNKNOWN)
19058 {
19059 arg = get_tv_string_chk(argvar);
19060 if (arg == NULL)
19061 nr = 0; /* type error; errmsg already given */
19062 else if (STRCMP(arg, "$") == 0)
19063 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19064 else if (STRCMP(arg, "#") == 0)
19065 {
19066 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19067 if (twin == NULL)
19068 nr = 0;
19069 }
19070 else
19071 {
19072 EMSG2(_(e_invexpr2), arg);
19073 nr = 0;
19074 }
19075 }
19076
19077 if (nr > 0)
19078 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19079 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019080 {
19081 if (wp == NULL)
19082 {
19083 /* didn't find it in this tabpage */
19084 nr = 0;
19085 break;
19086 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019087 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019088 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019089 return nr;
19090}
19091#endif
19092
19093/*
19094 * "tabpagewinnr()" function
19095 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019096 static void
19097f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019098 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019099 typval_T *rettv;
19100{
19101 int nr = 1;
19102#ifdef FEAT_WINDOWS
19103 tabpage_T *tp;
19104
19105 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19106 if (tp == NULL)
19107 nr = 0;
19108 else
19109 nr = get_winnr(tp, &argvars[1]);
19110#endif
19111 rettv->vval.v_number = nr;
19112}
19113
19114
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019115/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019116 * "tagfiles()" function
19117 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019118 static void
19119f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019120 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019121 typval_T *rettv;
19122{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019123 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019124 tagname_T tn;
19125 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019126
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019127 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019128 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019129 fname = alloc(MAXPATHL);
19130 if (fname == NULL)
19131 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019132
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019133 for (first = TRUE; ; first = FALSE)
19134 if (get_tagfname(&tn, first, fname) == FAIL
19135 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019136 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019137 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019138 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019139}
19140
19141/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019142 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019143 */
19144 static void
19145f_taglist(argvars, rettv)
19146 typval_T *argvars;
19147 typval_T *rettv;
19148{
19149 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019150
19151 tag_pattern = get_tv_string(&argvars[0]);
19152
19153 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019154 if (*tag_pattern == NUL)
19155 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019156
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019157 if (rettv_list_alloc(rettv) == OK)
19158 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019159}
19160
19161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 * "tempname()" function
19163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168{
19169 static int x = 'A';
19170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019172 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173
19174 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19175 * names. Skip 'I' and 'O', they are used for shell redirection. */
19176 do
19177 {
19178 if (x == 'Z')
19179 x = '0';
19180 else if (x == '9')
19181 x = 'A';
19182 else
19183 {
19184#ifdef EBCDIC
19185 if (x == 'I')
19186 x = 'J';
19187 else if (x == 'R')
19188 x = 'S';
19189 else
19190#endif
19191 ++x;
19192 }
19193 } while (x == 'I' || x == 'O');
19194}
19195
19196/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019197 * "test(list)" function: Just checking the walls...
19198 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019199 static void
19200f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019201 typval_T *argvars UNUSED;
19202 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019203{
19204 /* Used for unit testing. Change the code below to your liking. */
19205#if 0
19206 listitem_T *li;
19207 list_T *l;
19208 char_u *bad, *good;
19209
19210 if (argvars[0].v_type != VAR_LIST)
19211 return;
19212 l = argvars[0].vval.v_list;
19213 if (l == NULL)
19214 return;
19215 li = l->lv_first;
19216 if (li == NULL)
19217 return;
19218 bad = get_tv_string(&li->li_tv);
19219 li = li->li_next;
19220 if (li == NULL)
19221 return;
19222 good = get_tv_string(&li->li_tv);
19223 rettv->vval.v_number = test_edit_score(bad, good);
19224#endif
19225}
19226
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019227#ifdef FEAT_FLOAT
19228/*
19229 * "tan()" function
19230 */
19231 static void
19232f_tan(argvars, rettv)
19233 typval_T *argvars;
19234 typval_T *rettv;
19235{
19236 float_T f;
19237
19238 rettv->v_type = VAR_FLOAT;
19239 if (get_float_arg(argvars, &f) == OK)
19240 rettv->vval.v_float = tan(f);
19241 else
19242 rettv->vval.v_float = 0.0;
19243}
19244
19245/*
19246 * "tanh()" function
19247 */
19248 static void
19249f_tanh(argvars, rettv)
19250 typval_T *argvars;
19251 typval_T *rettv;
19252{
19253 float_T f;
19254
19255 rettv->v_type = VAR_FLOAT;
19256 if (get_float_arg(argvars, &f) == OK)
19257 rettv->vval.v_float = tanh(f);
19258 else
19259 rettv->vval.v_float = 0.0;
19260}
19261#endif
19262
Bram Moolenaard52d9742005-08-21 22:20:28 +000019263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 * "tolower(string)" function
19265 */
19266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019268 typval_T *argvars;
19269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270{
19271 char_u *p;
19272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273 p = vim_strsave(get_tv_string(&argvars[0]));
19274 rettv->v_type = VAR_STRING;
19275 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276
19277 if (p != NULL)
19278 while (*p != NUL)
19279 {
19280#ifdef FEAT_MBYTE
19281 int l;
19282
19283 if (enc_utf8)
19284 {
19285 int c, lc;
19286
19287 c = utf_ptr2char(p);
19288 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019289 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 /* TODO: reallocate string when byte count changes. */
19291 if (utf_char2len(lc) == l)
19292 utf_char2bytes(lc, p);
19293 p += l;
19294 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019295 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 p += l; /* skip multi-byte character */
19297 else
19298#endif
19299 {
19300 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19301 ++p;
19302 }
19303 }
19304}
19305
19306/*
19307 * "toupper(string)" function
19308 */
19309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019310f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019311 typval_T *argvars;
19312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019315 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316}
19317
19318/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019319 * "tr(string, fromstr, tostr)" function
19320 */
19321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019323 typval_T *argvars;
19324 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019325{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019326 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019327 char_u *fromstr;
19328 char_u *tostr;
19329 char_u *p;
19330#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019331 int inlen;
19332 int fromlen;
19333 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019334 int idx;
19335 char_u *cpstr;
19336 int cplen;
19337 int first = TRUE;
19338#endif
19339 char_u buf[NUMBUFLEN];
19340 char_u buf2[NUMBUFLEN];
19341 garray_T ga;
19342
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019343 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019344 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19345 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019346
19347 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019348 rettv->v_type = VAR_STRING;
19349 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019350 if (fromstr == NULL || tostr == NULL)
19351 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019352 ga_init2(&ga, (int)sizeof(char), 80);
19353
19354#ifdef FEAT_MBYTE
19355 if (!has_mbyte)
19356#endif
19357 /* not multi-byte: fromstr and tostr must be the same length */
19358 if (STRLEN(fromstr) != STRLEN(tostr))
19359 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019360#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019361error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019362#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019363 EMSG2(_(e_invarg2), fromstr);
19364 ga_clear(&ga);
19365 return;
19366 }
19367
19368 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019369 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019370 {
19371#ifdef FEAT_MBYTE
19372 if (has_mbyte)
19373 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019374 inlen = (*mb_ptr2len)(in_str);
19375 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019376 cplen = inlen;
19377 idx = 0;
19378 for (p = fromstr; *p != NUL; p += fromlen)
19379 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019380 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019381 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019382 {
19383 for (p = tostr; *p != NUL; p += tolen)
19384 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019385 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019386 if (idx-- == 0)
19387 {
19388 cplen = tolen;
19389 cpstr = p;
19390 break;
19391 }
19392 }
19393 if (*p == NUL) /* tostr is shorter than fromstr */
19394 goto error;
19395 break;
19396 }
19397 ++idx;
19398 }
19399
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019400 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019401 {
19402 /* Check that fromstr and tostr have the same number of
19403 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019404 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019405 first = FALSE;
19406 for (p = tostr; *p != NUL; p += tolen)
19407 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019408 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019409 --idx;
19410 }
19411 if (idx != 0)
19412 goto error;
19413 }
19414
19415 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019416 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019417 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019418
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019419 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019420 }
19421 else
19422#endif
19423 {
19424 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019425 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019426 if (p != NULL)
19427 ga_append(&ga, tostr[p - fromstr]);
19428 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019429 ga_append(&ga, *in_str);
19430 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019431 }
19432 }
19433
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019434 /* add a terminating NUL */
19435 ga_grow(&ga, 1);
19436 ga_append(&ga, NUL);
19437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019438 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019439}
19440
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019441#ifdef FEAT_FLOAT
19442/*
19443 * "trunc({float})" function
19444 */
19445 static void
19446f_trunc(argvars, rettv)
19447 typval_T *argvars;
19448 typval_T *rettv;
19449{
19450 float_T f;
19451
19452 rettv->v_type = VAR_FLOAT;
19453 if (get_float_arg(argvars, &f) == OK)
19454 /* trunc() is not in C90, use floor() or ceil() instead. */
19455 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19456 else
19457 rettv->vval.v_float = 0.0;
19458}
19459#endif
19460
Bram Moolenaar8299df92004-07-10 09:47:34 +000019461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 * "type(expr)" function
19463 */
19464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019465f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019466 typval_T *argvars;
19467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019469 int n;
19470
19471 switch (argvars[0].v_type)
19472 {
19473 case VAR_NUMBER: n = 0; break;
19474 case VAR_STRING: n = 1; break;
19475 case VAR_FUNC: n = 2; break;
19476 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019477 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019478#ifdef FEAT_FLOAT
19479 case VAR_FLOAT: n = 5; break;
19480#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019481 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19482 }
19483 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484}
19485
19486/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019487 * "undofile(name)" function
19488 */
19489 static void
19490f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019491 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019492 typval_T *rettv;
19493{
19494 rettv->v_type = VAR_STRING;
19495#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019496 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019497 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019498
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019499 if (*fname == NUL)
19500 {
19501 /* If there is no file name there will be no undo file. */
19502 rettv->vval.v_string = NULL;
19503 }
19504 else
19505 {
19506 char_u *ffname = FullName_save(fname, FALSE);
19507
19508 if (ffname != NULL)
19509 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19510 vim_free(ffname);
19511 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019512 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019513#else
19514 rettv->vval.v_string = NULL;
19515#endif
19516}
19517
19518/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019519 * "undotree()" function
19520 */
19521 static void
19522f_undotree(argvars, rettv)
19523 typval_T *argvars UNUSED;
19524 typval_T *rettv;
19525{
19526 if (rettv_dict_alloc(rettv) == OK)
19527 {
19528 dict_T *dict = rettv->vval.v_dict;
19529 list_T *list;
19530
Bram Moolenaar730cde92010-06-27 05:18:54 +020019531 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019532 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019533 dict_add_nr_str(dict, "save_last",
19534 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019535 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19536 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019537 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019538
19539 list = list_alloc();
19540 if (list != NULL)
19541 {
19542 u_eval_tree(curbuf->b_u_oldhead, list);
19543 dict_add_list(dict, "entries", list);
19544 }
19545 }
19546}
19547
19548/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019549 * "values(dict)" function
19550 */
19551 static void
19552f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 typval_T *argvars;
19554 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019555{
19556 dict_list(argvars, rettv, 1);
19557}
19558
19559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 * "virtcol(string)" function
19561 */
19562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 typval_T *argvars;
19565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566{
19567 colnr_T vcol = 0;
19568 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019569 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019571 fp = var2fpos(&argvars[0], FALSE, &fnum);
19572 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19573 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 {
19575 getvvcol(curwin, fp, NULL, NULL, &vcol);
19576 ++vcol;
19577 }
19578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019579 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580}
19581
19582/*
19583 * "visualmode()" function
19584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019586f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019587 typval_T *argvars UNUSED;
19588 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 char_u str[2];
19591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 str[0] = curbuf->b_visual_mode_eval;
19594 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596
19597 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019598 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601
19602/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019603 * "wildmenumode()" function
19604 */
19605 static void
19606f_wildmenumode(argvars, rettv)
19607 typval_T *argvars UNUSED;
19608 typval_T *rettv UNUSED;
19609{
19610#ifdef FEAT_WILDMENU
19611 if (wild_menu_showing)
19612 rettv->vval.v_number = 1;
19613#endif
19614}
19615
19616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 * "winbufnr(nr)" function
19618 */
19619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 typval_T *argvars;
19622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623{
19624 win_T *wp;
19625
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019626 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631}
19632
19633/*
19634 * "wincol()" function
19635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019637f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640{
19641 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019642 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643}
19644
19645/*
19646 * "winheight(nr)" function
19647 */
19648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 typval_T *argvars;
19651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652{
19653 win_T *wp;
19654
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019655 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019657 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660}
19661
19662/*
19663 * "winline()" function
19664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669{
19670 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672}
19673
19674/*
19675 * "winnr()" function
19676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019678f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681{
19682 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019683
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019685 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019687 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688}
19689
19690/*
19691 * "winrestcmd()" function
19692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019695 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697{
19698#ifdef FEAT_WINDOWS
19699 win_T *wp;
19700 int winnr = 1;
19701 garray_T ga;
19702 char_u buf[50];
19703
19704 ga_init2(&ga, (int)sizeof(char), 70);
19705 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19706 {
19707 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19708 ga_concat(&ga, buf);
19709# ifdef FEAT_VERTSPLIT
19710 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19711 ga_concat(&ga, buf);
19712# endif
19713 ++winnr;
19714 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019715 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722}
19723
19724/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019725 * "winrestview()" function
19726 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019727 static void
19728f_winrestview(argvars, rettv)
19729 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019730 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019731{
19732 dict_T *dict;
19733
19734 if (argvars[0].v_type != VAR_DICT
19735 || (dict = argvars[0].vval.v_dict) == NULL)
19736 EMSG(_(e_invarg));
19737 else
19738 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019739 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19740 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19741 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19742 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019743#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019744 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19745 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019746#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019747 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19748 {
19749 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19750 curwin->w_set_curswant = FALSE;
19751 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019752
Bram Moolenaar82c25852014-05-28 16:47:16 +020019753 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19754 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019755#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019756 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19757 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019758#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019759 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19760 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19761 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19762 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019763
19764 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019765 win_new_height(curwin, curwin->w_height);
19766# ifdef FEAT_VERTSPLIT
19767 win_new_width(curwin, W_WIDTH(curwin));
19768# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019769 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019770
Bram Moolenaarb851a962014-10-31 15:45:52 +010019771 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019772 curwin->w_topline = 1;
19773 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19774 curwin->w_topline = curbuf->b_ml.ml_line_count;
19775#ifdef FEAT_DIFF
19776 check_topfill(curwin, TRUE);
19777#endif
19778 }
19779}
19780
19781/*
19782 * "winsaveview()" function
19783 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019784 static void
19785f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019786 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019787 typval_T *rettv;
19788{
19789 dict_T *dict;
19790
Bram Moolenaara800b422010-06-27 01:15:55 +020019791 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019792 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019793 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019794
19795 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19796 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19797#ifdef FEAT_VIRTUALEDIT
19798 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19799#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019800 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019801 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19802
19803 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19804#ifdef FEAT_DIFF
19805 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19806#endif
19807 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19808 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19809}
19810
19811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 * "winwidth(nr)" function
19813 */
19814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 typval_T *argvars;
19817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
19819 win_T *wp;
19820
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019821 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 else
19825#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829#endif
19830}
19831
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019833 * Write list of strings to file
19834 */
19835 static int
19836write_list(fd, list, binary)
19837 FILE *fd;
19838 list_T *list;
19839 int binary;
19840{
19841 listitem_T *li;
19842 int c;
19843 int ret = OK;
19844 char_u *s;
19845
19846 for (li = list->lv_first; li != NULL; li = li->li_next)
19847 {
19848 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19849 {
19850 if (*s == '\n')
19851 c = putc(NUL, fd);
19852 else
19853 c = putc(*s, fd);
19854 if (c == EOF)
19855 {
19856 ret = FAIL;
19857 break;
19858 }
19859 }
19860 if (!binary || li->li_next != NULL)
19861 if (putc('\n', fd) == EOF)
19862 {
19863 ret = FAIL;
19864 break;
19865 }
19866 if (ret == FAIL)
19867 {
19868 EMSG(_(e_write));
19869 break;
19870 }
19871 }
19872 return ret;
19873}
19874
19875/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019876 * "writefile()" function
19877 */
19878 static void
19879f_writefile(argvars, rettv)
19880 typval_T *argvars;
19881 typval_T *rettv;
19882{
19883 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019884 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019885 char_u *fname;
19886 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019887 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019888
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019889 if (check_restricted() || check_secure())
19890 return;
19891
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019892 if (argvars[0].v_type != VAR_LIST)
19893 {
19894 EMSG2(_(e_listarg), "writefile()");
19895 return;
19896 }
19897 if (argvars[0].vval.v_list == NULL)
19898 return;
19899
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019900 if (argvars[2].v_type != VAR_UNKNOWN)
19901 {
19902 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19903 binary = TRUE;
19904 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19905 append = TRUE;
19906 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019907
19908 /* Always open the file in binary mode, library functions have a mind of
19909 * their own about CR-LF conversion. */
19910 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019911 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19912 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019913 {
19914 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19915 ret = -1;
19916 }
19917 else
19918 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019919 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19920 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019921 fclose(fd);
19922 }
19923
19924 rettv->vval.v_number = ret;
19925}
19926
19927/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019928 * "xor(expr, expr)" function
19929 */
19930 static void
19931f_xor(argvars, rettv)
19932 typval_T *argvars;
19933 typval_T *rettv;
19934{
19935 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19936 ^ get_tv_number_chk(&argvars[1], NULL);
19937}
19938
19939
19940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019942 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 */
19944 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019945var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019947 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019948 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019950 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019952 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953
Bram Moolenaara5525202006-03-02 22:52:09 +000019954 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019955 if (varp->v_type == VAR_LIST)
19956 {
19957 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019958 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019959 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019960 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019961
19962 l = varp->vval.v_list;
19963 if (l == NULL)
19964 return NULL;
19965
19966 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019967 pos.lnum = list_find_nr(l, 0L, &error);
19968 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019969 return NULL; /* invalid line number */
19970
19971 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019972 pos.col = list_find_nr(l, 1L, &error);
19973 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019974 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019975 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019976
19977 /* We accept "$" for the column number: last column. */
19978 li = list_find(l, 1L);
19979 if (li != NULL && li->li_tv.v_type == VAR_STRING
19980 && li->li_tv.vval.v_string != NULL
19981 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19982 pos.col = len + 1;
19983
Bram Moolenaara5525202006-03-02 22:52:09 +000019984 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019985 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019986 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019987 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019988
Bram Moolenaara5525202006-03-02 22:52:09 +000019989#ifdef FEAT_VIRTUALEDIT
19990 /* Get the virtual offset. Defaults to zero. */
19991 pos.coladd = list_find_nr(l, 2L, &error);
19992 if (error)
19993 pos.coladd = 0;
19994#endif
19995
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019996 return &pos;
19997 }
19998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 name = get_tv_string_chk(varp);
20000 if (name == NULL)
20001 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020002 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020004 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20005 {
20006 if (VIsual_active)
20007 return &VIsual;
20008 return &curwin->w_cursor;
20009 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020010 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020012 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20014 return NULL;
20015 return pp;
20016 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020017
20018#ifdef FEAT_VIRTUALEDIT
20019 pos.coladd = 0;
20020#endif
20021
Bram Moolenaar477933c2007-07-17 14:32:23 +000020022 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020023 {
20024 pos.col = 0;
20025 if (name[1] == '0') /* "w0": first visible line */
20026 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020027 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020028 pos.lnum = curwin->w_topline;
20029 return &pos;
20030 }
20031 else if (name[1] == '$') /* "w$": last visible line */
20032 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020033 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020034 pos.lnum = curwin->w_botline - 1;
20035 return &pos;
20036 }
20037 }
20038 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020040 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 {
20042 pos.lnum = curbuf->b_ml.ml_line_count;
20043 pos.col = 0;
20044 }
20045 else
20046 {
20047 pos.lnum = curwin->w_cursor.lnum;
20048 pos.col = (colnr_T)STRLEN(ml_get_curline());
20049 }
20050 return &pos;
20051 }
20052 return NULL;
20053}
20054
20055/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020056 * Convert list in "arg" into a position and optional file number.
20057 * When "fnump" is NULL there is no file number, only 3 items.
20058 * Note that the column is passed on as-is, the caller may want to decrement
20059 * it to use 1 for the first column.
20060 * Return FAIL when conversion is not possible, doesn't check the position for
20061 * validity.
20062 */
20063 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020064list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020065 typval_T *arg;
20066 pos_T *posp;
20067 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020068 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020069{
20070 list_T *l = arg->vval.v_list;
20071 long i = 0;
20072 long n;
20073
Bram Moolenaar493c1782014-05-28 14:34:46 +020020074 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20075 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020076 if (arg->v_type != VAR_LIST
20077 || l == NULL
20078 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020079 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020080 return FAIL;
20081
20082 if (fnump != NULL)
20083 {
20084 n = list_find_nr(l, i++, NULL); /* fnum */
20085 if (n < 0)
20086 return FAIL;
20087 if (n == 0)
20088 n = curbuf->b_fnum; /* current buffer */
20089 *fnump = n;
20090 }
20091
20092 n = list_find_nr(l, i++, NULL); /* lnum */
20093 if (n < 0)
20094 return FAIL;
20095 posp->lnum = n;
20096
20097 n = list_find_nr(l, i++, NULL); /* col */
20098 if (n < 0)
20099 return FAIL;
20100 posp->col = n;
20101
20102#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020103 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020104 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020105 posp->coladd = 0;
20106 else
20107 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020108#endif
20109
Bram Moolenaar493c1782014-05-28 14:34:46 +020020110 if (curswantp != NULL)
20111 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20112
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020113 return OK;
20114}
20115
20116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 * Get the length of an environment variable name.
20118 * Advance "arg" to the first character after the name.
20119 * Return 0 for error.
20120 */
20121 static int
20122get_env_len(arg)
20123 char_u **arg;
20124{
20125 char_u *p;
20126 int len;
20127
20128 for (p = *arg; vim_isIDc(*p); ++p)
20129 ;
20130 if (p == *arg) /* no name found */
20131 return 0;
20132
20133 len = (int)(p - *arg);
20134 *arg = p;
20135 return len;
20136}
20137
20138/*
20139 * Get the length of the name of a function or internal variable.
20140 * "arg" is advanced to the first non-white character after the name.
20141 * Return 0 if something is wrong.
20142 */
20143 static int
20144get_id_len(arg)
20145 char_u **arg;
20146{
20147 char_u *p;
20148 int len;
20149
20150 /* Find the end of the name. */
20151 for (p = *arg; eval_isnamec(*p); ++p)
20152 ;
20153 if (p == *arg) /* no name found */
20154 return 0;
20155
20156 len = (int)(p - *arg);
20157 *arg = skipwhite(p);
20158
20159 return len;
20160}
20161
20162/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020163 * Get the length of the name of a variable or function.
20164 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020166 * Return -1 if curly braces expansion failed.
20167 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 * If the name contains 'magic' {}'s, expand them and return the
20169 * expanded name in an allocated string via 'alias' - caller must free.
20170 */
20171 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020172get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 char_u **arg;
20174 char_u **alias;
20175 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020176 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177{
20178 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 char_u *p;
20180 char_u *expr_start;
20181 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182
20183 *alias = NULL; /* default to no alias */
20184
20185 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20186 && (*arg)[2] == (int)KE_SNR)
20187 {
20188 /* hard coded <SNR>, already translated */
20189 *arg += 3;
20190 return get_id_len(arg) + 3;
20191 }
20192 len = eval_fname_script(*arg);
20193 if (len > 0)
20194 {
20195 /* literal "<SID>", "s:" or "<SNR>" */
20196 *arg += len;
20197 }
20198
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020202 p = find_name_end(*arg, &expr_start, &expr_end,
20203 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 if (expr_start != NULL)
20205 {
20206 char_u *temp_string;
20207
20208 if (!evaluate)
20209 {
20210 len += (int)(p - *arg);
20211 *arg = skipwhite(p);
20212 return len;
20213 }
20214
20215 /*
20216 * Include any <SID> etc in the expanded string:
20217 * Thus the -len here.
20218 */
20219 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20220 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020221 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 *alias = temp_string;
20223 *arg = skipwhite(p);
20224 return (int)STRLEN(temp_string);
20225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226
20227 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020228 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 EMSG2(_(e_invexpr2), *arg);
20230
20231 return len;
20232}
20233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234/*
20235 * Find the end of a variable or function name, taking care of magic braces.
20236 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20237 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020238 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020239 * Return a pointer to just after the name. Equal to "arg" if there is no
20240 * valid name.
20241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020243find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 char_u *arg;
20245 char_u **expr_start;
20246 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020247 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 int mb_nest = 0;
20250 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 char_u *p;
20252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 *expr_start = NULL;
20256 *expr_end = NULL;
20257 }
20258
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020259 /* Quick check for valid starting character. */
20260 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20261 return arg;
20262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 for (p = arg; *p != NUL
20264 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020265 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020266 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020267 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020268 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020270 if (*p == '\'')
20271 {
20272 /* skip over 'string' to avoid counting [ and ] inside it. */
20273 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20274 ;
20275 if (*p == NUL)
20276 break;
20277 }
20278 else if (*p == '"')
20279 {
20280 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20281 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20282 if (*p == '\\' && p[1] != NUL)
20283 ++p;
20284 if (*p == NUL)
20285 break;
20286 }
20287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020288 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 if (*p == '[')
20291 ++br_nest;
20292 else if (*p == ']')
20293 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020296 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298 if (*p == '{')
20299 {
20300 mb_nest++;
20301 if (expr_start != NULL && *expr_start == NULL)
20302 *expr_start = p;
20303 }
20304 else if (*p == '}')
20305 {
20306 mb_nest--;
20307 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20308 *expr_end = p;
20309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 }
20312
20313 return p;
20314}
20315
20316/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020317 * Expands out the 'magic' {}'s in a variable/function name.
20318 * Note that this can call itself recursively, to deal with
20319 * constructs like foo{bar}{baz}{bam}
20320 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20321 * "in_start" ^
20322 * "expr_start" ^
20323 * "expr_end" ^
20324 * "in_end" ^
20325 *
20326 * Returns a new allocated string, which the caller must free.
20327 * Returns NULL for failure.
20328 */
20329 static char_u *
20330make_expanded_name(in_start, expr_start, expr_end, in_end)
20331 char_u *in_start;
20332 char_u *expr_start;
20333 char_u *expr_end;
20334 char_u *in_end;
20335{
20336 char_u c1;
20337 char_u *retval = NULL;
20338 char_u *temp_result;
20339 char_u *nextcmd = NULL;
20340
20341 if (expr_end == NULL || in_end == NULL)
20342 return NULL;
20343 *expr_start = NUL;
20344 *expr_end = NUL;
20345 c1 = *in_end;
20346 *in_end = NUL;
20347
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020348 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020349 if (temp_result != NULL && nextcmd == NULL)
20350 {
20351 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20352 + (in_end - expr_end) + 1));
20353 if (retval != NULL)
20354 {
20355 STRCPY(retval, in_start);
20356 STRCAT(retval, temp_result);
20357 STRCAT(retval, expr_end + 1);
20358 }
20359 }
20360 vim_free(temp_result);
20361
20362 *in_end = c1; /* put char back for error messages */
20363 *expr_start = '{';
20364 *expr_end = '}';
20365
20366 if (retval != NULL)
20367 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020368 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020369 if (expr_start != NULL)
20370 {
20371 /* Further expansion! */
20372 temp_result = make_expanded_name(retval, expr_start,
20373 expr_end, temp_result);
20374 vim_free(retval);
20375 retval = temp_result;
20376 }
20377 }
20378
20379 return retval;
20380}
20381
20382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020384 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 */
20386 static int
20387eval_isnamec(c)
20388 int c;
20389{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020390 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20391}
20392
20393/*
20394 * Return TRUE if character "c" can be used as the first character in a
20395 * variable or function name (excluding '{' and '}').
20396 */
20397 static int
20398eval_isnamec1(c)
20399 int c;
20400{
20401 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402}
20403
20404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 * Set number v: variable to "val".
20406 */
20407 void
20408set_vim_var_nr(idx, val)
20409 int idx;
20410 long val;
20411{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020412 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413}
20414
20415/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020416 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 */
20418 long
20419get_vim_var_nr(idx)
20420 int idx;
20421{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020422 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020425/*
20426 * Get string v: variable value. Uses a static buffer, can only be used once.
20427 */
20428 char_u *
20429get_vim_var_str(idx)
20430 int idx;
20431{
20432 return get_tv_string(&vimvars[idx].vv_tv);
20433}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020434
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020436 * Get List v: variable value. Caller must take care of reference count when
20437 * needed.
20438 */
20439 list_T *
20440get_vim_var_list(idx)
20441 int idx;
20442{
20443 return vimvars[idx].vv_list;
20444}
20445
20446/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020447 * Set v:char to character "c".
20448 */
20449 void
20450set_vim_var_char(c)
20451 int c;
20452{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020453 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020454
20455#ifdef FEAT_MBYTE
20456 if (has_mbyte)
20457 buf[(*mb_char2bytes)(c, buf)] = NUL;
20458 else
20459#endif
20460 {
20461 buf[0] = c;
20462 buf[1] = NUL;
20463 }
20464 set_vim_var_string(VV_CHAR, buf, -1);
20465}
20466
20467/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020468 * Set v:count to "count" and v:count1 to "count1".
20469 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 */
20471 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020472set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 long count;
20474 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020475 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020477 if (set_prevcount)
20478 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020479 vimvars[VV_COUNT].vv_nr = count;
20480 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481}
20482
20483/*
20484 * Set string v: variable to a copy of "val".
20485 */
20486 void
20487set_vim_var_string(idx, val, len)
20488 int idx;
20489 char_u *val;
20490 int len; /* length of "val" to use or -1 (whole string) */
20491{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020492 /* Need to do this (at least) once, since we can't initialize a union.
20493 * Will always be invoked when "v:progname" is set. */
20494 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20495
Bram Moolenaare9a41262005-01-15 22:18:47 +000020496 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020498 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020500 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020502 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503}
20504
20505/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020506 * Set List v: variable to "val".
20507 */
20508 void
20509set_vim_var_list(idx, val)
20510 int idx;
20511 list_T *val;
20512{
20513 list_unref(vimvars[idx].vv_list);
20514 vimvars[idx].vv_list = val;
20515 if (val != NULL)
20516 ++val->lv_refcount;
20517}
20518
20519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 * Set v:register if needed.
20521 */
20522 void
20523set_reg_var(c)
20524 int c;
20525{
20526 char_u regname;
20527
20528 if (c == 0 || c == ' ')
20529 regname = '"';
20530 else
20531 regname = c;
20532 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020533 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 set_vim_var_string(VV_REG, &regname, 1);
20535}
20536
20537/*
20538 * Get or set v:exception. If "oldval" == NULL, return the current value.
20539 * Otherwise, restore the value to "oldval" and return NULL.
20540 * Must always be called in pairs to save and restore v:exception! Does not
20541 * take care of memory allocations.
20542 */
20543 char_u *
20544v_exception(oldval)
20545 char_u *oldval;
20546{
20547 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020548 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549
Bram Moolenaare9a41262005-01-15 22:18:47 +000020550 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 return NULL;
20552}
20553
20554/*
20555 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20556 * Otherwise, restore the value to "oldval" and return NULL.
20557 * Must always be called in pairs to save and restore v:throwpoint! Does not
20558 * take care of memory allocations.
20559 */
20560 char_u *
20561v_throwpoint(oldval)
20562 char_u *oldval;
20563{
20564 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020565 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566
Bram Moolenaare9a41262005-01-15 22:18:47 +000020567 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 return NULL;
20569}
20570
20571#if defined(FEAT_AUTOCMD) || defined(PROTO)
20572/*
20573 * Set v:cmdarg.
20574 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20575 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20576 * Must always be called in pairs!
20577 */
20578 char_u *
20579set_cmdarg(eap, oldarg)
20580 exarg_T *eap;
20581 char_u *oldarg;
20582{
20583 char_u *oldval;
20584 char_u *newval;
20585 unsigned len;
20586
Bram Moolenaare9a41262005-01-15 22:18:47 +000020587 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020588 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020590 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020591 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020592 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593 }
20594
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020595 if (eap->force_bin == FORCE_BIN)
20596 len = 6;
20597 else if (eap->force_bin == FORCE_NOBIN)
20598 len = 8;
20599 else
20600 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020601
20602 if (eap->read_edit)
20603 len += 7;
20604
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020605 if (eap->force_ff != 0)
20606 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20607# ifdef FEAT_MBYTE
20608 if (eap->force_enc != 0)
20609 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020610 if (eap->bad_char != 0)
20611 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020612# endif
20613
20614 newval = alloc(len + 1);
20615 if (newval == NULL)
20616 return NULL;
20617
20618 if (eap->force_bin == FORCE_BIN)
20619 sprintf((char *)newval, " ++bin");
20620 else if (eap->force_bin == FORCE_NOBIN)
20621 sprintf((char *)newval, " ++nobin");
20622 else
20623 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020624
20625 if (eap->read_edit)
20626 STRCAT(newval, " ++edit");
20627
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020628 if (eap->force_ff != 0)
20629 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20630 eap->cmd + eap->force_ff);
20631# ifdef FEAT_MBYTE
20632 if (eap->force_enc != 0)
20633 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20634 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020635 if (eap->bad_char == BAD_KEEP)
20636 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20637 else if (eap->bad_char == BAD_DROP)
20638 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20639 else if (eap->bad_char != 0)
20640 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020641# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020642 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020643 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644}
20645#endif
20646
20647/*
20648 * Get the value of internal variable "name".
20649 * Return OK or FAIL.
20650 */
20651 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020652get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 char_u *name;
20654 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020655 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020656 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020657 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658{
20659 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020660 typval_T *tv = NULL;
20661 typval_T atv;
20662 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664
20665 /* truncate the name, so that we can use strcmp() */
20666 cc = name[len];
20667 name[len] = NUL;
20668
20669 /*
20670 * Check for "b:changedtick".
20671 */
20672 if (STRCMP(name, "b:changedtick") == 0)
20673 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020674 atv.v_type = VAR_NUMBER;
20675 atv.vval.v_number = curbuf->b_changedtick;
20676 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 }
20678
20679 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 * Check for user-defined variables.
20681 */
20682 else
20683 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020684 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020686 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 }
20688
Bram Moolenaare9a41262005-01-15 22:18:47 +000020689 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020691 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020692 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 ret = FAIL;
20694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020695 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020696 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697
20698 name[len] = cc;
20699
20700 return ret;
20701}
20702
20703/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020704 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20705 * Also handle function call with Funcref variable: func(expr)
20706 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20707 */
20708 static int
20709handle_subscript(arg, rettv, evaluate, verbose)
20710 char_u **arg;
20711 typval_T *rettv;
20712 int evaluate; /* do more than finding the end */
20713 int verbose; /* give error messages */
20714{
20715 int ret = OK;
20716 dict_T *selfdict = NULL;
20717 char_u *s;
20718 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020719 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020720
20721 while (ret == OK
20722 && (**arg == '['
20723 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020724 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020725 && !vim_iswhite(*(*arg - 1)))
20726 {
20727 if (**arg == '(')
20728 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020729 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020730 if (evaluate)
20731 {
20732 functv = *rettv;
20733 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020734
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020735 /* Invoke the function. Recursive! */
20736 s = functv.vval.v_string;
20737 }
20738 else
20739 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020740 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020741 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20742 &len, evaluate, selfdict);
20743
20744 /* Clear the funcref afterwards, so that deleting it while
20745 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020746 if (evaluate)
20747 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020748
20749 /* Stop the expression evaluation when immediately aborting on
20750 * error, or when an interrupt occurred or an exception was thrown
20751 * but not caught. */
20752 if (aborting())
20753 {
20754 if (ret == OK)
20755 clear_tv(rettv);
20756 ret = FAIL;
20757 }
20758 dict_unref(selfdict);
20759 selfdict = NULL;
20760 }
20761 else /* **arg == '[' || **arg == '.' */
20762 {
20763 dict_unref(selfdict);
20764 if (rettv->v_type == VAR_DICT)
20765 {
20766 selfdict = rettv->vval.v_dict;
20767 if (selfdict != NULL)
20768 ++selfdict->dv_refcount;
20769 }
20770 else
20771 selfdict = NULL;
20772 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20773 {
20774 clear_tv(rettv);
20775 ret = FAIL;
20776 }
20777 }
20778 }
20779 dict_unref(selfdict);
20780 return ret;
20781}
20782
20783/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020784 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020785 * value).
20786 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020787 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020788alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020789{
Bram Moolenaar33570922005-01-25 22:26:29 +000020790 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020791}
20792
20793/*
20794 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 * The string "s" must have been allocated, it is consumed.
20796 * Return NULL for out of memory, the variable otherwise.
20797 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020798 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020799alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 char_u *s;
20801{
Bram Moolenaar33570922005-01-25 22:26:29 +000020802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 rettv = alloc_tv();
20805 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020807 rettv->v_type = VAR_STRING;
20808 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 }
20810 else
20811 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813}
20814
20815/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020816 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020818 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020819free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020820 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821{
20822 if (varp != NULL)
20823 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 switch (varp->v_type)
20825 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020826 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020827 func_unref(varp->vval.v_string);
20828 /*FALLTHROUGH*/
20829 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020830 vim_free(varp->vval.v_string);
20831 break;
20832 case VAR_LIST:
20833 list_unref(varp->vval.v_list);
20834 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020835 case VAR_DICT:
20836 dict_unref(varp->vval.v_dict);
20837 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020838 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020839#ifdef FEAT_FLOAT
20840 case VAR_FLOAT:
20841#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020842 case VAR_UNKNOWN:
20843 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020844 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020845 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020846 break;
20847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 vim_free(varp);
20849 }
20850}
20851
20852/*
20853 * Free the memory for a variable value and set the value to NULL or 0.
20854 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020855 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020856clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020857 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858{
20859 if (varp != NULL)
20860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020861 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020863 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020864 func_unref(varp->vval.v_string);
20865 /*FALLTHROUGH*/
20866 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020867 vim_free(varp->vval.v_string);
20868 varp->vval.v_string = NULL;
20869 break;
20870 case VAR_LIST:
20871 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020872 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020873 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020874 case VAR_DICT:
20875 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020876 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020877 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020878 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020879 varp->vval.v_number = 0;
20880 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020881#ifdef FEAT_FLOAT
20882 case VAR_FLOAT:
20883 varp->vval.v_float = 0.0;
20884 break;
20885#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020886 case VAR_UNKNOWN:
20887 break;
20888 default:
20889 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020891 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 }
20893}
20894
20895/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020896 * Set the value of a variable to NULL without freeing items.
20897 */
20898 static void
20899init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020900 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020901{
20902 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020903 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904}
20905
20906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 * Get the number value of a variable.
20908 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020909 * For incompatible types, return 0.
20910 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20911 * caller of incompatible types: it sets *denote to TRUE if "denote"
20912 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 */
20914 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020916 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020918 int error = FALSE;
20919
20920 return get_tv_number_chk(varp, &error); /* return 0L on error */
20921}
20922
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020923 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020924get_tv_number_chk(varp, denote)
20925 typval_T *varp;
20926 int *denote;
20927{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020928 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020930 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020932 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020933 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020934#ifdef FEAT_FLOAT
20935 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020936 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020937 break;
20938#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020939 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020940 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020941 break;
20942 case VAR_STRING:
20943 if (varp->vval.v_string != NULL)
20944 vim_str2nr(varp->vval.v_string, NULL, NULL,
20945 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020946 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020947 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020948 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020949 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020951 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020953 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020954 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020957 if (denote == NULL) /* useful for values that must be unsigned */
20958 n = -1;
20959 else
20960 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020961 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962}
20963
20964/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020965 * Get the lnum from the first argument.
20966 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020967 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 */
20969 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020970get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020971 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972{
Bram Moolenaar33570922005-01-25 22:26:29 +000020973 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 linenr_T lnum;
20975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020976 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 if (lnum == 0) /* no valid number, try using line() */
20978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020979 rettv.v_type = VAR_NUMBER;
20980 f_line(argvars, &rettv);
20981 lnum = rettv.vval.v_number;
20982 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 }
20984 return lnum;
20985}
20986
20987/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020988 * Get the lnum from the first argument.
20989 * Also accepts "$", then "buf" is used.
20990 * Returns 0 on error.
20991 */
20992 static linenr_T
20993get_tv_lnum_buf(argvars, buf)
20994 typval_T *argvars;
20995 buf_T *buf;
20996{
20997 if (argvars[0].v_type == VAR_STRING
20998 && argvars[0].vval.v_string != NULL
20999 && argvars[0].vval.v_string[0] == '$'
21000 && buf != NULL)
21001 return buf->b_ml.ml_line_count;
21002 return get_tv_number_chk(&argvars[0], NULL);
21003}
21004
21005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 * Get the string value of a variable.
21007 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021008 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21009 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 * If the String variable has never been set, return an empty string.
21011 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021012 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21013 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 */
21015 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021017 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018{
21019 static char_u mybuf[NUMBUFLEN];
21020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021021 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022}
21023
21024 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021025get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021026 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021027 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021029 char_u *res = get_tv_string_buf_chk(varp, buf);
21030
21031 return res != NULL ? res : (char_u *)"";
21032}
21033
Bram Moolenaar7d647822014-04-05 21:28:56 +020021034/*
21035 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21036 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021037 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021038get_tv_string_chk(varp)
21039 typval_T *varp;
21040{
21041 static char_u mybuf[NUMBUFLEN];
21042
21043 return get_tv_string_buf_chk(varp, mybuf);
21044}
21045
21046 static char_u *
21047get_tv_string_buf_chk(varp, buf)
21048 typval_T *varp;
21049 char_u *buf;
21050{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021051 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021053 case VAR_NUMBER:
21054 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21055 return buf;
21056 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021057 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021058 break;
21059 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021061 break;
21062 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021063 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021064 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021065#ifdef FEAT_FLOAT
21066 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021067 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021068 break;
21069#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021070 case VAR_STRING:
21071 if (varp->vval.v_string != NULL)
21072 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021073 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021074 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021075 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079}
21080
21081/*
21082 * Find variable "name" in the list of variables.
21083 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021084 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021085 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021086 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021088 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021089find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021092 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021095 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096
Bram Moolenaara7043832005-01-21 11:56:39 +000021097 ht = find_var_ht(name, &varname);
21098 if (htp != NULL)
21099 *htp = ht;
21100 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021102 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103}
21104
21105/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021106 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021107 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021109 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021110find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021111 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021112 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021113 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021114 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021115{
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 hashitem_T *hi;
21117
21118 if (*varname == NUL)
21119 {
21120 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021121 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021123 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021124 case 'g': return &globvars_var;
21125 case 'v': return &vimvars_var;
21126 case 'b': return &curbuf->b_bufvar;
21127 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021128#ifdef FEAT_WINDOWS
21129 case 't': return &curtab->tp_winvar;
21130#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021131 case 'l': return current_funccal == NULL
21132 ? NULL : &current_funccal->l_vars_var;
21133 case 'a': return current_funccal == NULL
21134 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 }
21136 return NULL;
21137 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021138
21139 hi = hash_find(ht, varname);
21140 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021141 {
21142 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021143 * worked find the variable again. Don't auto-load a script if it was
21144 * loaded already, otherwise it would be loaded every time when
21145 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021146 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021147 {
21148 /* Note: script_autoload() may make "hi" invalid. It must either
21149 * be obtained again or not used. */
21150 if (!script_autoload(varname, FALSE) || aborting())
21151 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021152 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021153 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021154 if (HASHITEM_EMPTY(hi))
21155 return NULL;
21156 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021157 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021158}
21159
21160/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021161 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021162 * Set "varname" to the start of name without ':'.
21163 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021164 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021165find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 char_u *name;
21167 char_u **varname;
21168{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021169 hashitem_T *hi;
21170
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 if (name[1] != ':')
21172 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021173 /* The name must not start with a colon or #. */
21174 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 return NULL;
21176 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021177
21178 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021179 hi = hash_find(&compat_hashtab, name);
21180 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021181 return &compat_hashtab;
21182
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021184 return &globvarht; /* global variable */
21185 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 }
21187 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021188 if (*name == 'g') /* global variable */
21189 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021190 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21191 */
21192 if (vim_strchr(name + 2, ':') != NULL
21193 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021194 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021196 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021198 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021199#ifdef FEAT_WINDOWS
21200 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021201 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021202#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021203 if (*name == 'v') /* v: variable */
21204 return &vimvarht;
21205 if (*name == 'a' && current_funccal != NULL) /* function argument */
21206 return &current_funccal->l_avars.dv_hashtab;
21207 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21208 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 if (*name == 's' /* script variable */
21210 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21211 return &SCRIPT_VARS(current_SID);
21212 return NULL;
21213}
21214
21215/*
21216 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021217 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 * Returns NULL when it doesn't exist.
21219 */
21220 char_u *
21221get_var_value(name)
21222 char_u *name;
21223{
Bram Moolenaar33570922005-01-25 22:26:29 +000021224 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021226 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 if (v == NULL)
21228 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021229 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230}
21231
21232/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021233 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 * sourcing this script and when executing functions defined in the script.
21235 */
21236 void
21237new_script_vars(id)
21238 scid_T id;
21239{
Bram Moolenaara7043832005-01-21 11:56:39 +000021240 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021241 hashtab_T *ht;
21242 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021243
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21245 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021246 /* Re-allocating ga_data means that an ht_array pointing to
21247 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021248 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021249 for (i = 1; i <= ga_scripts.ga_len; ++i)
21250 {
21251 ht = &SCRIPT_VARS(i);
21252 if (ht->ht_mask == HT_INIT_SIZE - 1)
21253 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021254 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021255 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021256 }
21257
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 while (ga_scripts.ga_len < id)
21259 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021260 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021261 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021262 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 }
21265 }
21266}
21267
21268/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021269 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21270 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 */
21272 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021273init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021274 dict_T *dict;
21275 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021276 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277{
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021279 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021280 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021281 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021282 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021283 dict_var->di_tv.vval.v_dict = dict;
21284 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021285 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21287 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288}
21289
21290/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021291 * Unreference a dictionary initialized by init_var_dict().
21292 */
21293 void
21294unref_var_dict(dict)
21295 dict_T *dict;
21296{
21297 /* Now the dict needs to be freed if no one else is using it, go back to
21298 * normal reference counting. */
21299 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21300 dict_unref(dict);
21301}
21302
21303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021305 * Frees all allocated variables and the value they contain.
21306 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 */
21308 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021309vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 hashtab_T *ht;
21311{
21312 vars_clear_ext(ht, TRUE);
21313}
21314
21315/*
21316 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21317 */
21318 static void
21319vars_clear_ext(ht, free_val)
21320 hashtab_T *ht;
21321 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322{
Bram Moolenaara7043832005-01-21 11:56:39 +000021323 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021324 hashitem_T *hi;
21325 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326
Bram Moolenaar33570922005-01-25 22:26:29 +000021327 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021328 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021329 for (hi = ht->ht_array; todo > 0; ++hi)
21330 {
21331 if (!HASHITEM_EMPTY(hi))
21332 {
21333 --todo;
21334
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021336 * ht_array might change then. hash_clear() takes care of it
21337 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 v = HI2DI(hi);
21339 if (free_val)
21340 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021341 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021343 }
21344 }
21345 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021346 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347}
21348
Bram Moolenaara7043832005-01-21 11:56:39 +000021349/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021350 * Delete a variable from hashtab "ht" at item "hi".
21351 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021354delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021355 hashtab_T *ht;
21356 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357{
Bram Moolenaar33570922005-01-25 22:26:29 +000021358 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021359
21360 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021361 clear_tv(&di->di_tv);
21362 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363}
21364
21365/*
21366 * List the value of one internal variable.
21367 */
21368 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021369list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021370 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021372 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021374 char_u *tofree;
21375 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021376 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021377
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021378 current_copyID += COPYID_INC;
21379 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021381 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021382 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383}
21384
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021386list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 char_u *prefix;
21388 char_u *name;
21389 int type;
21390 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021391 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392{
Bram Moolenaar31859182007-08-14 20:41:13 +000021393 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21394 msg_start();
21395 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 if (name != NULL) /* "a:" vars don't have a name stored */
21397 msg_puts(name);
21398 msg_putchar(' ');
21399 msg_advance(22);
21400 if (type == VAR_NUMBER)
21401 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021402 else if (type == VAR_FUNC)
21403 msg_putchar('*');
21404 else if (type == VAR_LIST)
21405 {
21406 msg_putchar('[');
21407 if (*string == '[')
21408 ++string;
21409 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021410 else if (type == VAR_DICT)
21411 {
21412 msg_putchar('{');
21413 if (*string == '{')
21414 ++string;
21415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 else
21417 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021418
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021420
21421 if (type == VAR_FUNC)
21422 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021423 if (*first)
21424 {
21425 msg_clr_eos();
21426 *first = FALSE;
21427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428}
21429
21430/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 * If the variable already exists, the value is updated.
21433 * Otherwise the variable is created.
21434 */
21435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021439 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440{
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021443 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021445 ht = find_var_ht(name, &varname);
21446 if (ht == NULL || *varname == NUL)
21447 {
21448 EMSG2(_(e_illvar), name);
21449 return;
21450 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021451 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021452
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021453 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21454 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021455
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021459 if (var_check_ro(v->di_flags, name)
21460 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 return;
21462 if (v->di_tv.v_type != tv->v_type
21463 && !((v->di_tv.v_type == VAR_STRING
21464 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021466 || tv->v_type == VAR_NUMBER))
21467#ifdef FEAT_FLOAT
21468 && !((v->di_tv.v_type == VAR_NUMBER
21469 || v->di_tv.v_type == VAR_FLOAT)
21470 && (tv->v_type == VAR_NUMBER
21471 || tv->v_type == VAR_FLOAT))
21472#endif
21473 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021474 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021475 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021476 return;
21477 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021478
21479 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021480 * Handle setting internal v: variables separately: we don't change
21481 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021482 */
21483 if (ht == &vimvarht)
21484 {
21485 if (v->di_tv.v_type == VAR_STRING)
21486 {
21487 vim_free(v->di_tv.vval.v_string);
21488 if (copy || tv->v_type != VAR_STRING)
21489 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21490 else
21491 {
21492 /* Take over the string to avoid an extra alloc/free. */
21493 v->di_tv.vval.v_string = tv->vval.v_string;
21494 tv->vval.v_string = NULL;
21495 }
21496 }
21497 else if (v->di_tv.v_type != VAR_NUMBER)
21498 EMSG2(_(e_intern2), "set_var()");
21499 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021501 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021502 if (STRCMP(varname, "searchforward") == 0)
21503 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021504#ifdef FEAT_SEARCH_EXTRA
21505 else if (STRCMP(varname, "hlsearch") == 0)
21506 {
21507 no_hlsearch = !v->di_tv.vval.v_number;
21508 redraw_all_later(SOME_VALID);
21509 }
21510#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021511 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 return;
21513 }
21514
21515 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 }
21517 else /* add a new variable */
21518 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021519 /* Can't add "v:" variable. */
21520 if (ht == &vimvarht)
21521 {
21522 EMSG2(_(e_illvar), name);
21523 return;
21524 }
21525
Bram Moolenaar92124a32005-06-17 22:03:40 +000021526 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021527 if (!valid_varname(varname))
21528 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021529
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021530 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21531 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021532 if (v == NULL)
21533 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021537 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 return;
21539 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021540 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021542
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021543 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021544 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021545 else
21546 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021547 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021548 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021549 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551}
21552
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021553/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021554 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 * Also give an error message.
21556 */
21557 static int
21558var_check_ro(flags, name)
21559 int flags;
21560 char_u *name;
21561{
21562 if (flags & DI_FLAGS_RO)
21563 {
21564 EMSG2(_(e_readonlyvar), name);
21565 return TRUE;
21566 }
21567 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21568 {
21569 EMSG2(_(e_readonlysbx), name);
21570 return TRUE;
21571 }
21572 return FALSE;
21573}
21574
21575/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021576 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21577 * Also give an error message.
21578 */
21579 static int
21580var_check_fixed(flags, name)
21581 int flags;
21582 char_u *name;
21583{
21584 if (flags & DI_FLAGS_FIX)
21585 {
21586 EMSG2(_("E795: Cannot delete variable %s"), name);
21587 return TRUE;
21588 }
21589 return FALSE;
21590}
21591
21592/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021593 * Check if a funcref is assigned to a valid variable name.
21594 * Return TRUE and give an error if not.
21595 */
21596 static int
21597var_check_func_name(name, new_var)
21598 char_u *name; /* points to start of variable name */
21599 int new_var; /* TRUE when creating the variable */
21600{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021601 /* Allow for w: b: s: and t:. */
21602 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021603 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21604 ? name[2] : name[0]))
21605 {
21606 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21607 name);
21608 return TRUE;
21609 }
21610 /* Don't allow hiding a function. When "v" is not NULL we might be
21611 * assigning another function to the same var, the type is checked
21612 * below. */
21613 if (new_var && function_exists(name))
21614 {
21615 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21616 name);
21617 return TRUE;
21618 }
21619 return FALSE;
21620}
21621
21622/*
21623 * Check if a variable name is valid.
21624 * Return FALSE and give an error if not.
21625 */
21626 static int
21627valid_varname(varname)
21628 char_u *varname;
21629{
21630 char_u *p;
21631
21632 for (p = varname; *p != NUL; ++p)
21633 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21634 && *p != AUTOLOAD_CHAR)
21635 {
21636 EMSG2(_(e_illvar), varname);
21637 return FALSE;
21638 }
21639 return TRUE;
21640}
21641
21642/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021643 * Return TRUE if typeval "tv" is set to be locked (immutable).
21644 * Also give an error message, using "name".
21645 */
21646 static int
21647tv_check_lock(lock, name)
21648 int lock;
21649 char_u *name;
21650{
21651 if (lock & VAR_LOCKED)
21652 {
21653 EMSG2(_("E741: Value is locked: %s"),
21654 name == NULL ? (char_u *)_("Unknown") : name);
21655 return TRUE;
21656 }
21657 if (lock & VAR_FIXED)
21658 {
21659 EMSG2(_("E742: Cannot change value of %s"),
21660 name == NULL ? (char_u *)_("Unknown") : name);
21661 return TRUE;
21662 }
21663 return FALSE;
21664}
21665
21666/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021668 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021669 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021670 * It is OK for "from" and "to" to point to the same item. This is used to
21671 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021672 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021673 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021674copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 typval_T *from;
21676 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021678 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021679 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 switch (from->v_type)
21681 {
21682 case VAR_NUMBER:
21683 to->vval.v_number = from->vval.v_number;
21684 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021685#ifdef FEAT_FLOAT
21686 case VAR_FLOAT:
21687 to->vval.v_float = from->vval.v_float;
21688 break;
21689#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021690 case VAR_STRING:
21691 case VAR_FUNC:
21692 if (from->vval.v_string == NULL)
21693 to->vval.v_string = NULL;
21694 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021696 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021697 if (from->v_type == VAR_FUNC)
21698 func_ref(to->vval.v_string);
21699 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021700 break;
21701 case VAR_LIST:
21702 if (from->vval.v_list == NULL)
21703 to->vval.v_list = NULL;
21704 else
21705 {
21706 to->vval.v_list = from->vval.v_list;
21707 ++to->vval.v_list->lv_refcount;
21708 }
21709 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021710 case VAR_DICT:
21711 if (from->vval.v_dict == NULL)
21712 to->vval.v_dict = NULL;
21713 else
21714 {
21715 to->vval.v_dict = from->vval.v_dict;
21716 ++to->vval.v_dict->dv_refcount;
21717 }
21718 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021719 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021720 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021721 break;
21722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723}
21724
21725/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021726 * Make a copy of an item.
21727 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021728 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21729 * reference to an already copied list/dict can be used.
21730 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021731 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021732 static int
21733item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021734 typval_T *from;
21735 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021736 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021737 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021738{
21739 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021740 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021741
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021743 {
21744 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021745 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021746 }
21747 ++recurse;
21748
21749 switch (from->v_type)
21750 {
21751 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021752#ifdef FEAT_FLOAT
21753 case VAR_FLOAT:
21754#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021755 case VAR_STRING:
21756 case VAR_FUNC:
21757 copy_tv(from, to);
21758 break;
21759 case VAR_LIST:
21760 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021761 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021762 if (from->vval.v_list == NULL)
21763 to->vval.v_list = NULL;
21764 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21765 {
21766 /* use the copy made earlier */
21767 to->vval.v_list = from->vval.v_list->lv_copylist;
21768 ++to->vval.v_list->lv_refcount;
21769 }
21770 else
21771 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21772 if (to->vval.v_list == NULL)
21773 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021774 break;
21775 case VAR_DICT:
21776 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021777 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021778 if (from->vval.v_dict == NULL)
21779 to->vval.v_dict = NULL;
21780 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21781 {
21782 /* use the copy made earlier */
21783 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21784 ++to->vval.v_dict->dv_refcount;
21785 }
21786 else
21787 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21788 if (to->vval.v_dict == NULL)
21789 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021790 break;
21791 default:
21792 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021793 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021794 }
21795 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021796 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021797}
21798
21799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 * ":echo expr1 ..." print each argument separated with a space, add a
21801 * newline at the end.
21802 * ":echon expr1 ..." print each argument plain.
21803 */
21804 void
21805ex_echo(eap)
21806 exarg_T *eap;
21807{
21808 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021810 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 char_u *p;
21812 int needclr = TRUE;
21813 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021814 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815
21816 if (eap->skip)
21817 ++emsg_skip;
21818 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021820 /* If eval1() causes an error message the text from the command may
21821 * still need to be cleared. E.g., "echo 22,44". */
21822 need_clr_eos = needclr;
21823
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021825 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 {
21827 /*
21828 * Report the invalid expression unless the expression evaluation
21829 * has been cancelled due to an aborting error, an interrupt, or an
21830 * exception.
21831 */
21832 if (!aborting())
21833 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021834 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 break;
21836 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021837 need_clr_eos = FALSE;
21838
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 if (!eap->skip)
21840 {
21841 if (atstart)
21842 {
21843 atstart = FALSE;
21844 /* Call msg_start() after eval1(), evaluating the expression
21845 * may cause a message to appear. */
21846 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021847 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021848 /* Mark the saved text as finishing the line, so that what
21849 * follows is displayed on a new line when scrolling back
21850 * at the more prompt. */
21851 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 }
21855 else if (eap->cmdidx == CMD_echo)
21856 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021857 current_copyID += COPYID_INC;
21858 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021859 if (p != NULL)
21860 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021862 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021864 if (*p != TAB && needclr)
21865 {
21866 /* remove any text still there from the command */
21867 msg_clr_eos();
21868 needclr = FALSE;
21869 }
21870 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 }
21872 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021873 {
21874#ifdef FEAT_MBYTE
21875 if (has_mbyte)
21876 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021877 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021878
21879 (void)msg_outtrans_len_attr(p, i, echo_attr);
21880 p += i - 1;
21881 }
21882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021884 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 arg = skipwhite(arg);
21891 }
21892 eap->nextcmd = check_nextcmd(arg);
21893
21894 if (eap->skip)
21895 --emsg_skip;
21896 else
21897 {
21898 /* remove text that may still be there from the command */
21899 if (needclr)
21900 msg_clr_eos();
21901 if (eap->cmdidx == CMD_echo)
21902 msg_end();
21903 }
21904}
21905
21906/*
21907 * ":echohl {name}".
21908 */
21909 void
21910ex_echohl(eap)
21911 exarg_T *eap;
21912{
21913 int id;
21914
21915 id = syn_name2id(eap->arg);
21916 if (id == 0)
21917 echo_attr = 0;
21918 else
21919 echo_attr = syn_id2attr(id);
21920}
21921
21922/*
21923 * ":execute expr1 ..." execute the result of an expression.
21924 * ":echomsg expr1 ..." Print a message
21925 * ":echoerr expr1 ..." Print an error
21926 * Each gets spaces around each argument and a newline at the end for
21927 * echo commands
21928 */
21929 void
21930ex_execute(eap)
21931 exarg_T *eap;
21932{
21933 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021934 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935 int ret = OK;
21936 char_u *p;
21937 garray_T ga;
21938 int len;
21939 int save_did_emsg;
21940
21941 ga_init2(&ga, 1, 80);
21942
21943 if (eap->skip)
21944 ++emsg_skip;
21945 while (*arg != NUL && *arg != '|' && *arg != '\n')
21946 {
21947 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 {
21950 /*
21951 * Report the invalid expression unless the expression evaluation
21952 * has been cancelled due to an aborting error, an interrupt, or an
21953 * exception.
21954 */
21955 if (!aborting())
21956 EMSG2(_(e_invexpr2), p);
21957 ret = FAIL;
21958 break;
21959 }
21960
21961 if (!eap->skip)
21962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021963 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 len = (int)STRLEN(p);
21965 if (ga_grow(&ga, len + 2) == FAIL)
21966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021967 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 ret = FAIL;
21969 break;
21970 }
21971 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 ga.ga_len += len;
21975 }
21976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021977 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 arg = skipwhite(arg);
21979 }
21980
21981 if (ret != FAIL && ga.ga_data != NULL)
21982 {
21983 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021986 out_flush();
21987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 else if (eap->cmdidx == CMD_echoerr)
21989 {
21990 /* We don't want to abort following commands, restore did_emsg. */
21991 save_did_emsg = did_emsg;
21992 EMSG((char_u *)ga.ga_data);
21993 if (!force_abort)
21994 did_emsg = save_did_emsg;
21995 }
21996 else if (eap->cmdidx == CMD_execute)
21997 do_cmdline((char_u *)ga.ga_data,
21998 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21999 }
22000
22001 ga_clear(&ga);
22002
22003 if (eap->skip)
22004 --emsg_skip;
22005
22006 eap->nextcmd = check_nextcmd(arg);
22007}
22008
22009/*
22010 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22011 * "arg" points to the "&" or '+' when called, to "option" when returning.
22012 * Returns NULL when no option name found. Otherwise pointer to the char
22013 * after the option name.
22014 */
22015 static char_u *
22016find_option_end(arg, opt_flags)
22017 char_u **arg;
22018 int *opt_flags;
22019{
22020 char_u *p = *arg;
22021
22022 ++p;
22023 if (*p == 'g' && p[1] == ':')
22024 {
22025 *opt_flags = OPT_GLOBAL;
22026 p += 2;
22027 }
22028 else if (*p == 'l' && p[1] == ':')
22029 {
22030 *opt_flags = OPT_LOCAL;
22031 p += 2;
22032 }
22033 else
22034 *opt_flags = 0;
22035
22036 if (!ASCII_ISALPHA(*p))
22037 return NULL;
22038 *arg = p;
22039
22040 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22041 p += 4; /* termcap option */
22042 else
22043 while (ASCII_ISALPHA(*p))
22044 ++p;
22045 return p;
22046}
22047
22048/*
22049 * ":function"
22050 */
22051 void
22052ex_function(eap)
22053 exarg_T *eap;
22054{
22055 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022056 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 int j;
22058 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022060 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 char_u *name = NULL;
22062 char_u *p;
22063 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022064 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 garray_T newargs;
22066 garray_T newlines;
22067 int varargs = FALSE;
22068 int mustend = FALSE;
22069 int flags = 0;
22070 ufunc_T *fp;
22071 int indent;
22072 int nesting;
22073 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 dictitem_T *v;
22075 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022076 static int func_nr = 0; /* number for nameless function */
22077 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022078 hashtab_T *ht;
22079 int todo;
22080 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022081 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082
22083 /*
22084 * ":function" without argument: list functions.
22085 */
22086 if (ends_excmd(*eap->arg))
22087 {
22088 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022089 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022090 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022091 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022092 {
22093 if (!HASHITEM_EMPTY(hi))
22094 {
22095 --todo;
22096 fp = HI2UF(hi);
22097 if (!isdigit(*fp->uf_name))
22098 list_func_head(fp, FALSE);
22099 }
22100 }
22101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 eap->nextcmd = check_nextcmd(eap->arg);
22103 return;
22104 }
22105
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022106 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022107 * ":function /pat": list functions matching pattern.
22108 */
22109 if (*eap->arg == '/')
22110 {
22111 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22112 if (!eap->skip)
22113 {
22114 regmatch_T regmatch;
22115
22116 c = *p;
22117 *p = NUL;
22118 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22119 *p = c;
22120 if (regmatch.regprog != NULL)
22121 {
22122 regmatch.rm_ic = p_ic;
22123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022124 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022125 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22126 {
22127 if (!HASHITEM_EMPTY(hi))
22128 {
22129 --todo;
22130 fp = HI2UF(hi);
22131 if (!isdigit(*fp->uf_name)
22132 && vim_regexec(&regmatch, fp->uf_name, 0))
22133 list_func_head(fp, FALSE);
22134 }
22135 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022136 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022137 }
22138 }
22139 if (*p == '/')
22140 ++p;
22141 eap->nextcmd = check_nextcmd(p);
22142 return;
22143 }
22144
22145 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022146 * Get the function name. There are these situations:
22147 * func normal function name
22148 * "name" == func, "fudi.fd_dict" == NULL
22149 * dict.func new dictionary entry
22150 * "name" == NULL, "fudi.fd_dict" set,
22151 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22152 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022153 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022154 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22155 * dict.func existing dict entry that's not a Funcref
22156 * "name" == NULL, "fudi.fd_dict" set,
22157 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022158 * s:func script-local function name
22159 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022162 name = trans_function_name(&p, eap->skip, 0, &fudi);
22163 paren = (vim_strchr(p, '(') != NULL);
22164 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 {
22166 /*
22167 * Return on an invalid expression in braces, unless the expression
22168 * evaluation has been cancelled due to an aborting error, an
22169 * interrupt, or an exception.
22170 */
22171 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022173 if (!eap->skip && fudi.fd_newkey != NULL)
22174 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022175 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 else
22179 eap->skip = TRUE;
22180 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022181
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 /* An error in a function call during evaluation of an expression in magic
22183 * braces should not cause the function not to be defined. */
22184 saved_did_emsg = did_emsg;
22185 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186
22187 /*
22188 * ":function func" with only function name: list function.
22189 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022190 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 {
22192 if (!ends_excmd(*skipwhite(p)))
22193 {
22194 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022195 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 }
22197 eap->nextcmd = check_nextcmd(p);
22198 if (eap->nextcmd != NULL)
22199 *p = NUL;
22200 if (!eap->skip && !got_int)
22201 {
22202 fp = find_func(name);
22203 if (fp != NULL)
22204 {
22205 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022206 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022208 if (FUNCLINE(fp, j) == NULL)
22209 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 msg_putchar('\n');
22211 msg_outnum((long)(j + 1));
22212 if (j < 9)
22213 msg_putchar(' ');
22214 if (j < 99)
22215 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022216 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 out_flush(); /* show a line at a time */
22218 ui_breakcheck();
22219 }
22220 if (!got_int)
22221 {
22222 msg_putchar('\n');
22223 msg_puts((char_u *)" endfunction");
22224 }
22225 }
22226 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022227 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022229 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 }
22231
22232 /*
22233 * ":function name(arg1, arg2)" Define function.
22234 */
22235 p = skipwhite(p);
22236 if (*p != '(')
22237 {
22238 if (!eap->skip)
22239 {
22240 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022241 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 }
22243 /* attempt to continue by skipping some text */
22244 if (vim_strchr(p, '(') != NULL)
22245 p = vim_strchr(p, '(');
22246 }
22247 p = skipwhite(p + 1);
22248
22249 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22250 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22251
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022252 if (!eap->skip)
22253 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022254 /* Check the name of the function. Unless it's a dictionary function
22255 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022256 if (name != NULL)
22257 arg = name;
22258 else
22259 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022260 if (arg != NULL && (fudi.fd_di == NULL
22261 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022262 {
22263 if (*arg == K_SPECIAL)
22264 j = 3;
22265 else
22266 j = 0;
22267 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22268 : eval_isnamec(arg[j])))
22269 ++j;
22270 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022271 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022272 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022273 /* Disallow using the g: dict. */
22274 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22275 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022276 }
22277
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 /*
22279 * Isolate the arguments: "arg1, arg2, ...)"
22280 */
22281 while (*p != ')')
22282 {
22283 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22284 {
22285 varargs = TRUE;
22286 p += 3;
22287 mustend = TRUE;
22288 }
22289 else
22290 {
22291 arg = p;
22292 while (ASCII_ISALNUM(*p) || *p == '_')
22293 ++p;
22294 if (arg == p || isdigit(*arg)
22295 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22296 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22297 {
22298 if (!eap->skip)
22299 EMSG2(_("E125: Illegal argument: %s"), arg);
22300 break;
22301 }
22302 if (ga_grow(&newargs, 1) == FAIL)
22303 goto erret;
22304 c = *p;
22305 *p = NUL;
22306 arg = vim_strsave(arg);
22307 if (arg == NULL)
22308 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022309
22310 /* Check for duplicate argument name. */
22311 for (i = 0; i < newargs.ga_len; ++i)
22312 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22313 {
22314 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022315 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022316 goto erret;
22317 }
22318
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22320 *p = c;
22321 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 if (*p == ',')
22323 ++p;
22324 else
22325 mustend = TRUE;
22326 }
22327 p = skipwhite(p);
22328 if (mustend && *p != ')')
22329 {
22330 if (!eap->skip)
22331 EMSG2(_(e_invarg2), eap->arg);
22332 break;
22333 }
22334 }
22335 ++p; /* skip the ')' */
22336
Bram Moolenaare9a41262005-01-15 22:18:47 +000022337 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 for (;;)
22339 {
22340 p = skipwhite(p);
22341 if (STRNCMP(p, "range", 5) == 0)
22342 {
22343 flags |= FC_RANGE;
22344 p += 5;
22345 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022346 else if (STRNCMP(p, "dict", 4) == 0)
22347 {
22348 flags |= FC_DICT;
22349 p += 4;
22350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 else if (STRNCMP(p, "abort", 5) == 0)
22352 {
22353 flags |= FC_ABORT;
22354 p += 5;
22355 }
22356 else
22357 break;
22358 }
22359
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022360 /* When there is a line break use what follows for the function body.
22361 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22362 if (*p == '\n')
22363 line_arg = p + 1;
22364 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 EMSG(_(e_trailing));
22366
22367 /*
22368 * Read the body of the function, until ":endfunction" is found.
22369 */
22370 if (KeyTyped)
22371 {
22372 /* Check if the function already exists, don't let the user type the
22373 * whole function before telling him it doesn't work! For a script we
22374 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022375 if (!eap->skip && !eap->forceit)
22376 {
22377 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22378 EMSG(_(e_funcdict));
22379 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022380 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022383 if (!eap->skip && did_emsg)
22384 goto erret;
22385
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 msg_putchar('\n'); /* don't overwrite the function name */
22387 cmdline_row = msg_row;
22388 }
22389
22390 indent = 2;
22391 nesting = 0;
22392 for (;;)
22393 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022394 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022395 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022396 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022397 saved_wait_return = FALSE;
22398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022400 sourcing_lnum_off = sourcing_lnum;
22401
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022402 if (line_arg != NULL)
22403 {
22404 /* Use eap->arg, split up in parts by line breaks. */
22405 theline = line_arg;
22406 p = vim_strchr(theline, '\n');
22407 if (p == NULL)
22408 line_arg += STRLEN(line_arg);
22409 else
22410 {
22411 *p = NUL;
22412 line_arg = p + 1;
22413 }
22414 }
22415 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 theline = getcmdline(':', 0L, indent);
22417 else
22418 theline = eap->getline(':', eap->cookie, indent);
22419 if (KeyTyped)
22420 lines_left = Rows - 1;
22421 if (theline == NULL)
22422 {
22423 EMSG(_("E126: Missing :endfunction"));
22424 goto erret;
22425 }
22426
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022427 /* Detect line continuation: sourcing_lnum increased more than one. */
22428 if (sourcing_lnum > sourcing_lnum_off + 1)
22429 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22430 else
22431 sourcing_lnum_off = 0;
22432
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433 if (skip_until != NULL)
22434 {
22435 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22436 * don't check for ":endfunc". */
22437 if (STRCMP(theline, skip_until) == 0)
22438 {
22439 vim_free(skip_until);
22440 skip_until = NULL;
22441 }
22442 }
22443 else
22444 {
22445 /* skip ':' and blanks*/
22446 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22447 ;
22448
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022449 /* Check for "endfunction". */
22450 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022452 if (line_arg == NULL)
22453 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 break;
22455 }
22456
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022457 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 * at "end". */
22459 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22460 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022461 else if (STRNCMP(p, "if", 2) == 0
22462 || STRNCMP(p, "wh", 2) == 0
22463 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 || STRNCMP(p, "try", 3) == 0)
22465 indent += 2;
22466
22467 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022468 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022470 if (*p == '!')
22471 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022473 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22474 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022476 ++nesting;
22477 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 }
22479 }
22480
22481 /* Check for ":append" or ":insert". */
22482 p = skip_range(p, NULL);
22483 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22484 || (p[0] == 'i'
22485 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22486 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22487 skip_until = vim_strsave((char_u *)".");
22488
22489 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22490 arg = skipwhite(skiptowhite(p));
22491 if (arg[0] == '<' && arg[1] =='<'
22492 && ((p[0] == 'p' && p[1] == 'y'
22493 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22494 || (p[0] == 'p' && p[1] == 'e'
22495 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22496 || (p[0] == 't' && p[1] == 'c'
22497 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022498 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22499 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22501 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022502 || (p[0] == 'm' && p[1] == 'z'
22503 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 ))
22505 {
22506 /* ":python <<" continues until a dot, like ":append" */
22507 p = skipwhite(arg + 2);
22508 if (*p == NUL)
22509 skip_until = vim_strsave((char_u *)".");
22510 else
22511 skip_until = vim_strsave(p);
22512 }
22513 }
22514
22515 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022516 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022517 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022518 if (line_arg == NULL)
22519 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022521 }
22522
22523 /* Copy the line to newly allocated memory. get_one_sourceline()
22524 * allocates 250 bytes per line, this saves 80% on average. The cost
22525 * is an extra alloc/free. */
22526 p = vim_strsave(theline);
22527 if (p != NULL)
22528 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022529 if (line_arg == NULL)
22530 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022531 theline = p;
22532 }
22533
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022534 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22535
22536 /* Add NULL lines for continuation lines, so that the line count is
22537 * equal to the index in the growarray. */
22538 while (sourcing_lnum_off-- > 0)
22539 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022540
22541 /* Check for end of eap->arg. */
22542 if (line_arg != NULL && *line_arg == NUL)
22543 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 }
22545
22546 /* Don't define the function when skipping commands or when an error was
22547 * detected. */
22548 if (eap->skip || did_emsg)
22549 goto erret;
22550
22551 /*
22552 * If there are no errors, add the function
22553 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022554 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022555 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022556 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022558 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022559 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022560 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022561 goto erret;
22562 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022563
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022564 fp = find_func(name);
22565 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022567 if (!eap->forceit)
22568 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022569 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022570 goto erret;
22571 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022572 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022573 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022574 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575 name);
22576 goto erret;
22577 }
22578 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022579 ga_clear_strings(&(fp->uf_args));
22580 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022581 vim_free(name);
22582 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 }
22585 else
22586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022587 char numbuf[20];
22588
22589 fp = NULL;
22590 if (fudi.fd_newkey == NULL && !eap->forceit)
22591 {
22592 EMSG(_(e_funcdict));
22593 goto erret;
22594 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022595 if (fudi.fd_di == NULL)
22596 {
22597 /* Can't add a function to a locked dictionary */
22598 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22599 goto erret;
22600 }
22601 /* Can't change an existing function if it is locked */
22602 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22603 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604
22605 /* Give the function a sequential number. Can only be used with a
22606 * Funcref! */
22607 vim_free(name);
22608 sprintf(numbuf, "%d", ++func_nr);
22609 name = vim_strsave((char_u *)numbuf);
22610 if (name == NULL)
22611 goto erret;
22612 }
22613
22614 if (fp == NULL)
22615 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022616 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022617 {
22618 int slen, plen;
22619 char_u *scriptname;
22620
22621 /* Check that the autoload name matches the script name. */
22622 j = FAIL;
22623 if (sourcing_name != NULL)
22624 {
22625 scriptname = autoload_name(name);
22626 if (scriptname != NULL)
22627 {
22628 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022629 plen = (int)STRLEN(p);
22630 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022631 if (slen > plen && fnamecmp(p,
22632 sourcing_name + slen - plen) == 0)
22633 j = OK;
22634 vim_free(scriptname);
22635 }
22636 }
22637 if (j == FAIL)
22638 {
22639 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22640 goto erret;
22641 }
22642 }
22643
22644 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 if (fp == NULL)
22646 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022647
22648 if (fudi.fd_dict != NULL)
22649 {
22650 if (fudi.fd_di == NULL)
22651 {
22652 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022653 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022654 if (fudi.fd_di == NULL)
22655 {
22656 vim_free(fp);
22657 goto erret;
22658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022659 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22660 {
22661 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022662 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022663 goto erret;
22664 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 }
22666 else
22667 /* overwrite existing dict entry */
22668 clear_tv(&fudi.fd_di->di_tv);
22669 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022670 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022672 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022673
22674 /* behave like "dict" was used */
22675 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022676 }
22677
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022679 STRCPY(fp->uf_name, name);
22680 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022682 fp->uf_args = newargs;
22683 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022684#ifdef FEAT_PROFILE
22685 fp->uf_tml_count = NULL;
22686 fp->uf_tml_total = NULL;
22687 fp->uf_tml_self = NULL;
22688 fp->uf_profiling = FALSE;
22689 if (prof_def_func())
22690 func_do_profile(fp);
22691#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022692 fp->uf_varargs = varargs;
22693 fp->uf_flags = flags;
22694 fp->uf_calls = 0;
22695 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022696 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697
22698erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 ga_clear_strings(&newargs);
22700 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022701ret_free:
22702 vim_free(skip_until);
22703 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022706 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707}
22708
22709/*
22710 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022711 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022713 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022714 * TFN_INT: internal function name OK
22715 * TFN_QUIET: be quiet
22716 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 * Advances "pp" to just after the function name (if no error).
22718 */
22719 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022720trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 char_u **pp;
22722 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022723 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022726 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 char_u *start;
22728 char_u *end;
22729 int lead;
22730 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022733
22734 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022735 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022737
22738 /* Check for hard coded <SNR>: already translated function ID (from a user
22739 * command). */
22740 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22741 && (*pp)[2] == (int)KE_SNR)
22742 {
22743 *pp += 3;
22744 len = get_id_len(pp) + 3;
22745 return vim_strnsave(start, len);
22746 }
22747
22748 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22749 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022751 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022753
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022754 /* Note that TFN_ flags use the same values as GLV_ flags. */
22755 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022756 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022757 if (end == start)
22758 {
22759 if (!skip)
22760 EMSG(_("E129: Function name required"));
22761 goto theend;
22762 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022763 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022764 {
22765 /*
22766 * Report an invalid expression in braces, unless the expression
22767 * evaluation has been cancelled due to an aborting error, an
22768 * interrupt, or an exception.
22769 */
22770 if (!aborting())
22771 {
22772 if (end != NULL)
22773 EMSG2(_(e_invarg2), start);
22774 }
22775 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022776 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022777 goto theend;
22778 }
22779
22780 if (lv.ll_tv != NULL)
22781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022782 if (fdp != NULL)
22783 {
22784 fdp->fd_dict = lv.ll_dict;
22785 fdp->fd_newkey = lv.ll_newkey;
22786 lv.ll_newkey = NULL;
22787 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022788 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022789 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22790 {
22791 name = vim_strsave(lv.ll_tv->vval.v_string);
22792 *pp = end;
22793 }
22794 else
22795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022796 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22797 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022798 EMSG(_(e_funcref));
22799 else
22800 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022801 name = NULL;
22802 }
22803 goto theend;
22804 }
22805
22806 if (lv.ll_name == NULL)
22807 {
22808 /* Error found, but continue after the function name. */
22809 *pp = end;
22810 goto theend;
22811 }
22812
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022813 /* Check if the name is a Funcref. If so, use the value. */
22814 if (lv.ll_exp_name != NULL)
22815 {
22816 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022817 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022818 if (name == lv.ll_exp_name)
22819 name = NULL;
22820 }
22821 else
22822 {
22823 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022824 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022825 if (name == *pp)
22826 name = NULL;
22827 }
22828 if (name != NULL)
22829 {
22830 name = vim_strsave(name);
22831 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022832 if (STRNCMP(name, "<SNR>", 5) == 0)
22833 {
22834 /* Change "<SNR>" to the byte sequence. */
22835 name[0] = K_SPECIAL;
22836 name[1] = KS_EXTRA;
22837 name[2] = (int)KE_SNR;
22838 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22839 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022840 goto theend;
22841 }
22842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022843 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022845 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022846 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22847 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22848 {
22849 /* When there was "s:" already or the name expanded to get a
22850 * leading "s:" then remove it. */
22851 lv.ll_name += 2;
22852 len -= 2;
22853 lead = 2;
22854 }
22855 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022856 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022857 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022858 /* skip over "s:" and "g:" */
22859 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022860 lv.ll_name += 2;
22861 len = (int)(end - lv.ll_name);
22862 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022863
22864 /*
22865 * Copy the function name to allocated memory.
22866 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22867 * Accept <SNR>123_name() outside a script.
22868 */
22869 if (skip)
22870 lead = 0; /* do nothing */
22871 else if (lead > 0)
22872 {
22873 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022874 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22875 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022876 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022877 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022878 if (current_SID <= 0)
22879 {
22880 EMSG(_(e_usingsid));
22881 goto theend;
22882 }
22883 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22884 lead += (int)STRLEN(sid_buf);
22885 }
22886 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022887 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022888 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022889 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022890 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022891 goto theend;
22892 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022893 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022894 {
22895 char_u *cp = vim_strchr(lv.ll_name, ':');
22896
22897 if (cp != NULL && cp < end)
22898 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022899 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022900 goto theend;
22901 }
22902 }
22903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022904 name = alloc((unsigned)(len + lead + 1));
22905 if (name != NULL)
22906 {
22907 if (lead > 0)
22908 {
22909 name[0] = K_SPECIAL;
22910 name[1] = KS_EXTRA;
22911 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022912 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022913 STRCPY(name + 3, sid_buf);
22914 }
22915 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022916 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022917 }
22918 *pp = end;
22919
22920theend:
22921 clear_lval(&lv);
22922 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923}
22924
22925/*
22926 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22927 * Return 2 if "p" starts with "s:".
22928 * Return 0 otherwise.
22929 */
22930 static int
22931eval_fname_script(p)
22932 char_u *p;
22933{
22934 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22935 || STRNICMP(p + 1, "SNR>", 4) == 0))
22936 return 5;
22937 if (p[0] == 's' && p[1] == ':')
22938 return 2;
22939 return 0;
22940}
22941
22942/*
22943 * Return TRUE if "p" starts with "<SID>" or "s:".
22944 * Only works if eval_fname_script() returned non-zero for "p"!
22945 */
22946 static int
22947eval_fname_sid(p)
22948 char_u *p;
22949{
22950 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22951}
22952
22953/*
22954 * List the head of the function: "name(arg1, arg2)".
22955 */
22956 static void
22957list_func_head(fp, indent)
22958 ufunc_T *fp;
22959 int indent;
22960{
22961 int j;
22962
22963 msg_start();
22964 if (indent)
22965 MSG_PUTS(" ");
22966 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022967 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 {
22969 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022970 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
22972 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022973 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022975 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 {
22977 if (j)
22978 MSG_PUTS(", ");
22979 msg_puts(FUNCARG(fp, j));
22980 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022981 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 {
22983 if (j)
22984 MSG_PUTS(", ");
22985 MSG_PUTS("...");
22986 }
22987 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022988 if (fp->uf_flags & FC_ABORT)
22989 MSG_PUTS(" abort");
22990 if (fp->uf_flags & FC_RANGE)
22991 MSG_PUTS(" range");
22992 if (fp->uf_flags & FC_DICT)
22993 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022994 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022995 if (p_verbose > 0)
22996 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997}
22998
22999/*
23000 * Find a function by name, return pointer to it in ufuncs.
23001 * Return NULL for unknown function.
23002 */
23003 static ufunc_T *
23004find_func(name)
23005 char_u *name;
23006{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023007 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023009 hi = hash_find(&func_hashtab, name);
23010 if (!HASHITEM_EMPTY(hi))
23011 return HI2UF(hi);
23012 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013}
23014
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023015#if defined(EXITFREE) || defined(PROTO)
23016 void
23017free_all_functions()
23018{
23019 hashitem_T *hi;
23020
23021 /* Need to start all over every time, because func_free() may change the
23022 * hash table. */
23023 while (func_hashtab.ht_used > 0)
23024 for (hi = func_hashtab.ht_array; ; ++hi)
23025 if (!HASHITEM_EMPTY(hi))
23026 {
23027 func_free(HI2UF(hi));
23028 break;
23029 }
23030}
23031#endif
23032
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023033 int
23034translated_function_exists(name)
23035 char_u *name;
23036{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023037 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023038 return find_internal_func(name) >= 0;
23039 return find_func(name) != NULL;
23040}
23041
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023042/*
23043 * Return TRUE if a function "name" exists.
23044 */
23045 static int
23046function_exists(name)
23047 char_u *name;
23048{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023049 char_u *nm = name;
23050 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023051 int n = FALSE;
23052
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023053 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23054 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023055 nm = skipwhite(nm);
23056
23057 /* Only accept "funcname", "funcname ", "funcname (..." and
23058 * "funcname(...", not "funcname!...". */
23059 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023060 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023061 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023062 return n;
23063}
23064
Bram Moolenaara1544c02013-05-30 12:35:52 +020023065 char_u *
23066get_expanded_name(name, check)
23067 char_u *name;
23068 int check;
23069{
23070 char_u *nm = name;
23071 char_u *p;
23072
23073 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23074
23075 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023076 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023077 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023078
Bram Moolenaara1544c02013-05-30 12:35:52 +020023079 vim_free(p);
23080 return NULL;
23081}
23082
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023083/*
23084 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023085 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23086 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023087 */
23088 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023089builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023090 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023091 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023092{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023093 char_u *p;
23094
23095 if (!ASCII_ISLOWER(name[0]))
23096 return FALSE;
23097 p = vim_strchr(name, AUTOLOAD_CHAR);
23098 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023099}
23100
Bram Moolenaar05159a02005-02-26 23:04:13 +000023101#if defined(FEAT_PROFILE) || defined(PROTO)
23102/*
23103 * Start profiling function "fp".
23104 */
23105 static void
23106func_do_profile(fp)
23107 ufunc_T *fp;
23108{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023109 int len = fp->uf_lines.ga_len;
23110
23111 if (len == 0)
23112 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023113 fp->uf_tm_count = 0;
23114 profile_zero(&fp->uf_tm_self);
23115 profile_zero(&fp->uf_tm_total);
23116 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023117 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023118 if (fp->uf_tml_total == NULL)
23119 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023120 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023121 if (fp->uf_tml_self == NULL)
23122 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023123 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023124 fp->uf_tml_idx = -1;
23125 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23126 || fp->uf_tml_self == NULL)
23127 return; /* out of memory */
23128
23129 fp->uf_profiling = TRUE;
23130}
23131
23132/*
23133 * Dump the profiling results for all functions in file "fd".
23134 */
23135 void
23136func_dump_profile(fd)
23137 FILE *fd;
23138{
23139 hashitem_T *hi;
23140 int todo;
23141 ufunc_T *fp;
23142 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023143 ufunc_T **sorttab;
23144 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023146 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023147 if (todo == 0)
23148 return; /* nothing to dump */
23149
Bram Moolenaar73830342005-02-28 22:48:19 +000023150 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
23151
Bram Moolenaar05159a02005-02-26 23:04:13 +000023152 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23153 {
23154 if (!HASHITEM_EMPTY(hi))
23155 {
23156 --todo;
23157 fp = HI2UF(hi);
23158 if (fp->uf_profiling)
23159 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023160 if (sorttab != NULL)
23161 sorttab[st_len++] = fp;
23162
Bram Moolenaar05159a02005-02-26 23:04:13 +000023163 if (fp->uf_name[0] == K_SPECIAL)
23164 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23165 else
23166 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23167 if (fp->uf_tm_count == 1)
23168 fprintf(fd, "Called 1 time\n");
23169 else
23170 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23171 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23172 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23173 fprintf(fd, "\n");
23174 fprintf(fd, "count total (s) self (s)\n");
23175
23176 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23177 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023178 if (FUNCLINE(fp, i) == NULL)
23179 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023180 prof_func_line(fd, fp->uf_tml_count[i],
23181 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023182 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23183 }
23184 fprintf(fd, "\n");
23185 }
23186 }
23187 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023188
23189 if (sorttab != NULL && st_len > 0)
23190 {
23191 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23192 prof_total_cmp);
23193 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23194 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23195 prof_self_cmp);
23196 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23197 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023198
23199 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023200}
Bram Moolenaar73830342005-02-28 22:48:19 +000023201
23202 static void
23203prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23204 FILE *fd;
23205 ufunc_T **sorttab;
23206 int st_len;
23207 char *title;
23208 int prefer_self; /* when equal print only self time */
23209{
23210 int i;
23211 ufunc_T *fp;
23212
23213 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23214 fprintf(fd, "count total (s) self (s) function\n");
23215 for (i = 0; i < 20 && i < st_len; ++i)
23216 {
23217 fp = sorttab[i];
23218 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23219 prefer_self);
23220 if (fp->uf_name[0] == K_SPECIAL)
23221 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23222 else
23223 fprintf(fd, " %s()\n", fp->uf_name);
23224 }
23225 fprintf(fd, "\n");
23226}
23227
23228/*
23229 * Print the count and times for one function or function line.
23230 */
23231 static void
23232prof_func_line(fd, count, total, self, prefer_self)
23233 FILE *fd;
23234 int count;
23235 proftime_T *total;
23236 proftime_T *self;
23237 int prefer_self; /* when equal print only self time */
23238{
23239 if (count > 0)
23240 {
23241 fprintf(fd, "%5d ", count);
23242 if (prefer_self && profile_equal(total, self))
23243 fprintf(fd, " ");
23244 else
23245 fprintf(fd, "%s ", profile_msg(total));
23246 if (!prefer_self && profile_equal(total, self))
23247 fprintf(fd, " ");
23248 else
23249 fprintf(fd, "%s ", profile_msg(self));
23250 }
23251 else
23252 fprintf(fd, " ");
23253}
23254
23255/*
23256 * Compare function for total time sorting.
23257 */
23258 static int
23259#ifdef __BORLANDC__
23260_RTLENTRYF
23261#endif
23262prof_total_cmp(s1, s2)
23263 const void *s1;
23264 const void *s2;
23265{
23266 ufunc_T *p1, *p2;
23267
23268 p1 = *(ufunc_T **)s1;
23269 p2 = *(ufunc_T **)s2;
23270 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23271}
23272
23273/*
23274 * Compare function for self time sorting.
23275 */
23276 static int
23277#ifdef __BORLANDC__
23278_RTLENTRYF
23279#endif
23280prof_self_cmp(s1, s2)
23281 const void *s1;
23282 const void *s2;
23283{
23284 ufunc_T *p1, *p2;
23285
23286 p1 = *(ufunc_T **)s1;
23287 p2 = *(ufunc_T **)s2;
23288 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23289}
23290
Bram Moolenaar05159a02005-02-26 23:04:13 +000023291#endif
23292
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023293/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023294 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023295 * Return TRUE if a package was loaded.
23296 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023297 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023298script_autoload(name, reload)
23299 char_u *name;
23300 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023301{
23302 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023303 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023304 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023305 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023306
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023307 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023308 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023309 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023310 return FALSE;
23311
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023312 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023313
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023314 /* Find the name in the list of previously loaded package names. Skip
23315 * "autoload/", it's always the same. */
23316 for (i = 0; i < ga_loaded.ga_len; ++i)
23317 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23318 break;
23319 if (!reload && i < ga_loaded.ga_len)
23320 ret = FALSE; /* was loaded already */
23321 else
23322 {
23323 /* Remember the name if it wasn't loaded already. */
23324 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23325 {
23326 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23327 tofree = NULL;
23328 }
23329
23330 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023331 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023332 ret = TRUE;
23333 }
23334
23335 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023336 return ret;
23337}
23338
23339/*
23340 * Return the autoload script name for a function or variable name.
23341 * Returns NULL when out of memory.
23342 */
23343 static char_u *
23344autoload_name(name)
23345 char_u *name;
23346{
23347 char_u *p;
23348 char_u *scriptname;
23349
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023350 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023351 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23352 if (scriptname == NULL)
23353 return FALSE;
23354 STRCPY(scriptname, "autoload/");
23355 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023356 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023357 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023358 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023359 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023360 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023361}
23362
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23364
23365/*
23366 * Function given to ExpandGeneric() to obtain the list of user defined
23367 * function names.
23368 */
23369 char_u *
23370get_user_func_name(xp, idx)
23371 expand_T *xp;
23372 int idx;
23373{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023374 static long_u done;
23375 static hashitem_T *hi;
23376 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377
23378 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023380 done = 0;
23381 hi = func_hashtab.ht_array;
23382 }
23383 if (done < func_hashtab.ht_used)
23384 {
23385 if (done++ > 0)
23386 ++hi;
23387 while (HASHITEM_EMPTY(hi))
23388 ++hi;
23389 fp = HI2UF(hi);
23390
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023391 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023392 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023393
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023394 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23395 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396
23397 cat_func_name(IObuff, fp);
23398 if (xp->xp_context != EXPAND_USER_FUNC)
23399 {
23400 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023401 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 STRCAT(IObuff, ")");
23403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 return IObuff;
23405 }
23406 return NULL;
23407}
23408
23409#endif /* FEAT_CMDL_COMPL */
23410
23411/*
23412 * Copy the function name of "fp" to buffer "buf".
23413 * "buf" must be able to hold the function name plus three bytes.
23414 * Takes care of script-local function names.
23415 */
23416 static void
23417cat_func_name(buf, fp)
23418 char_u *buf;
23419 ufunc_T *fp;
23420{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023421 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 {
23423 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023424 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 }
23426 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023427 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428}
23429
23430/*
23431 * ":delfunction {name}"
23432 */
23433 void
23434ex_delfunction(eap)
23435 exarg_T *eap;
23436{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023437 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 char_u *p;
23439 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023440 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441
23442 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443 name = trans_function_name(&p, eap->skip, 0, &fudi);
23444 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023446 {
23447 if (fudi.fd_dict != NULL && !eap->skip)
23448 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 if (!ends_excmd(*skipwhite(p)))
23452 {
23453 vim_free(name);
23454 EMSG(_(e_trailing));
23455 return;
23456 }
23457 eap->nextcmd = check_nextcmd(p);
23458 if (eap->nextcmd != NULL)
23459 *p = NUL;
23460
23461 if (!eap->skip)
23462 fp = find_func(name);
23463 vim_free(name);
23464
23465 if (!eap->skip)
23466 {
23467 if (fp == NULL)
23468 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023469 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 return;
23471 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023472 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 {
23474 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23475 return;
23476 }
23477
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023478 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023480 /* Delete the dict item that refers to the function, it will
23481 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023482 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023484 else
23485 func_free(fp);
23486 }
23487}
23488
23489/*
23490 * Free a function and remove it from the list of functions.
23491 */
23492 static void
23493func_free(fp)
23494 ufunc_T *fp;
23495{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023496 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023497
23498 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023499 ga_clear_strings(&(fp->uf_args));
23500 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023501#ifdef FEAT_PROFILE
23502 vim_free(fp->uf_tml_count);
23503 vim_free(fp->uf_tml_total);
23504 vim_free(fp->uf_tml_self);
23505#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023506
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023507 /* remove the function from the function hashtable */
23508 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23509 if (HASHITEM_EMPTY(hi))
23510 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023511 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023512 hash_remove(&func_hashtab, hi);
23513
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023514 vim_free(fp);
23515}
23516
23517/*
23518 * Unreference a Function: decrement the reference count and free it when it
23519 * becomes zero. Only for numbered functions.
23520 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023521 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023522func_unref(name)
23523 char_u *name;
23524{
23525 ufunc_T *fp;
23526
23527 if (name != NULL && isdigit(*name))
23528 {
23529 fp = find_func(name);
23530 if (fp == NULL)
23531 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023532 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023533 {
23534 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 * when "uf_calls" becomes zero. */
23536 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023537 func_free(fp);
23538 }
23539 }
23540}
23541
23542/*
23543 * Count a reference to a Function.
23544 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023545 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023546func_ref(name)
23547 char_u *name;
23548{
23549 ufunc_T *fp;
23550
23551 if (name != NULL && isdigit(*name))
23552 {
23553 fp = find_func(name);
23554 if (fp == NULL)
23555 EMSG2(_(e_intern2), "func_ref()");
23556 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023557 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 }
23559}
23560
23561/*
23562 * Call a user function.
23563 */
23564 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023565call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 ufunc_T *fp; /* pointer to function */
23567 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023568 typval_T *argvars; /* arguments */
23569 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 linenr_T firstline; /* first line of range */
23571 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023572 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573{
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 char_u *save_sourcing_name;
23575 linenr_T save_sourcing_lnum;
23576 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023577 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 int save_did_emsg;
23579 static int depth = 0;
23580 dictitem_T *v;
23581 int fixvar_idx = 0; /* index in fixvar[] */
23582 int i;
23583 int ai;
23584 char_u numbuf[NUMBUFLEN];
23585 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023586#ifdef FEAT_PROFILE
23587 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023588 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590
23591 /* If depth of calling is getting too high, don't execute the function */
23592 if (depth >= p_mfd)
23593 {
23594 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023595 rettv->v_type = VAR_NUMBER;
23596 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 return;
23598 }
23599 ++depth;
23600
23601 line_breakcheck(); /* check for CTRL-C hit */
23602
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023603 fc = (funccall_T *)alloc(sizeof(funccall_T));
23604 fc->caller = current_funccal;
23605 current_funccal = fc;
23606 fc->func = fp;
23607 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023608 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023609 fc->linenr = 0;
23610 fc->returned = FALSE;
23611 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023613 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23614 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615
Bram Moolenaar33570922005-01-25 22:26:29 +000023616 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023617 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023618 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23619 * each argument variable and saves a lot of time.
23620 */
23621 /*
23622 * Init l: variables.
23623 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023624 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023625 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023626 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023627 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23628 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023629 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023630 name = v->di_key;
23631 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023632 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023633 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023634 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023635 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023636 v->di_tv.vval.v_dict = selfdict;
23637 ++selfdict->dv_refcount;
23638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023639
Bram Moolenaar33570922005-01-25 22:26:29 +000023640 /*
23641 * Init a: variables.
23642 * Set a:0 to "argcount".
23643 * Set a:000 to a list with room for the "..." arguments.
23644 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023645 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023646 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023647 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023648 /* Use "name" to avoid a warning from some compiler that checks the
23649 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023650 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023651 name = v->di_key;
23652 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023653 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023654 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023656 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023657 v->di_tv.vval.v_list = &fc->l_varlist;
23658 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23659 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23660 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023661
23662 /*
23663 * Set a:firstline to "firstline" and a:lastline to "lastline".
23664 * Set a:name to named arguments.
23665 * Set a:N to the "..." arguments.
23666 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023667 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023668 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023669 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023670 (varnumber_T)lastline);
23671 for (i = 0; i < argcount; ++i)
23672 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023673 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023674 if (ai < 0)
23675 /* named argument a:name */
23676 name = FUNCARG(fp, i);
23677 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023678 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023679 /* "..." argument a:1, a:2, etc. */
23680 sprintf((char *)numbuf, "%d", ai + 1);
23681 name = numbuf;
23682 }
23683 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23684 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023685 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023686 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23687 }
23688 else
23689 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023690 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23691 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023692 if (v == NULL)
23693 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023694 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023695 }
23696 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023697 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023698
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023699 /* Note: the values are copied directly to avoid alloc/free.
23700 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023701 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023702 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023703
23704 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23705 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023706 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23707 fc->l_listitems[ai].li_tv = argvars[i];
23708 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023709 }
23710 }
23711
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 /* Don't redraw while executing the function. */
23713 ++RedrawingDisabled;
23714 save_sourcing_name = sourcing_name;
23715 save_sourcing_lnum = sourcing_lnum;
23716 sourcing_lnum = 1;
23717 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023718 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 if (sourcing_name != NULL)
23720 {
23721 if (save_sourcing_name != NULL
23722 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23723 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23724 else
23725 STRCPY(sourcing_name, "function ");
23726 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23727
23728 if (p_verbose >= 12)
23729 {
23730 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023731 verbose_enter_scroll();
23732
Bram Moolenaar555b2802005-05-19 21:08:39 +000023733 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 if (p_verbose >= 14)
23735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023737 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023738 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023739 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740
23741 msg_puts((char_u *)"(");
23742 for (i = 0; i < argcount; ++i)
23743 {
23744 if (i > 0)
23745 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023746 if (argvars[i].v_type == VAR_NUMBER)
23747 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 else
23749 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023750 /* Do not want errors such as E724 here. */
23751 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023752 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023753 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023754 if (s != NULL)
23755 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023756 if (vim_strsize(s) > MSG_BUF_CLEN)
23757 {
23758 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23759 s = buf;
23760 }
23761 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023762 vim_free(tofree);
23763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 }
23765 }
23766 msg_puts((char_u *)")");
23767 }
23768 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023769
23770 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 --no_wait_return;
23772 }
23773 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023774#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023775 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023776 {
23777 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23778 func_do_profile(fp);
23779 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023780 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023781 {
23782 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023783 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023784 profile_zero(&fp->uf_tm_children);
23785 }
23786 script_prof_save(&wait_start);
23787 }
23788#endif
23789
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023791 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 save_did_emsg = did_emsg;
23793 did_emsg = FALSE;
23794
23795 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023796 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23798
23799 --RedrawingDisabled;
23800
23801 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023802 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023804 clear_tv(rettv);
23805 rettv->v_type = VAR_NUMBER;
23806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 }
23808
Bram Moolenaar05159a02005-02-26 23:04:13 +000023809#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023810 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023811 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023812 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023813 profile_end(&call_start);
23814 profile_sub_wait(&wait_start, &call_start);
23815 profile_add(&fp->uf_tm_total, &call_start);
23816 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023817 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023818 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023819 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23820 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023821 }
23822 }
23823#endif
23824
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 /* when being verbose, mention the return value */
23826 if (p_verbose >= 12)
23827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023829 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023832 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023833 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023834 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023835 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023838 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023839 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023840 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023841 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023842
Bram Moolenaar555b2802005-05-19 21:08:39 +000023843 /* The value may be very long. Skip the middle part, so that we
23844 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023845 * truncate it at the end. Don't want errors such as E724 here. */
23846 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023847 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023848 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023849 if (s != NULL)
23850 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023851 if (vim_strsize(s) > MSG_BUF_CLEN)
23852 {
23853 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23854 s = buf;
23855 }
23856 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023857 vim_free(tofree);
23858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 }
23860 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023861
23862 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 --no_wait_return;
23864 }
23865
23866 vim_free(sourcing_name);
23867 sourcing_name = save_sourcing_name;
23868 sourcing_lnum = save_sourcing_lnum;
23869 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023870#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023871 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023872 script_prof_restore(&wait_start);
23873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874
23875 if (p_verbose >= 12 && sourcing_name != NULL)
23876 {
23877 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023878 verbose_enter_scroll();
23879
Bram Moolenaar555b2802005-05-19 21:08:39 +000023880 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023882
23883 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 --no_wait_return;
23885 }
23886
23887 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023888 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023890
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023891 /* If the a:000 list and the l: and a: dicts are not referenced we can
23892 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023893 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23894 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23895 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23896 {
23897 free_funccal(fc, FALSE);
23898 }
23899 else
23900 {
23901 hashitem_T *hi;
23902 listitem_T *li;
23903 int todo;
23904
23905 /* "fc" is still in use. This can happen when returning "a:000" or
23906 * assigning "l:" to a global variable.
23907 * Link "fc" in the list for garbage collection later. */
23908 fc->caller = previous_funccal;
23909 previous_funccal = fc;
23910
23911 /* Make a copy of the a: variables, since we didn't do that above. */
23912 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23913 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23914 {
23915 if (!HASHITEM_EMPTY(hi))
23916 {
23917 --todo;
23918 v = HI2DI(hi);
23919 copy_tv(&v->di_tv, &v->di_tv);
23920 }
23921 }
23922
23923 /* Make a copy of the a:000 items, since we didn't do that above. */
23924 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23925 copy_tv(&li->li_tv, &li->li_tv);
23926 }
23927}
23928
23929/*
23930 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023931 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023932 */
23933 static int
23934can_free_funccal(fc, copyID)
23935 funccall_T *fc;
23936 int copyID;
23937{
23938 return (fc->l_varlist.lv_copyID != copyID
23939 && fc->l_vars.dv_copyID != copyID
23940 && fc->l_avars.dv_copyID != copyID);
23941}
23942
23943/*
23944 * Free "fc" and what it contains.
23945 */
23946 static void
23947free_funccal(fc, free_val)
23948 funccall_T *fc;
23949 int free_val; /* a: vars were allocated */
23950{
23951 listitem_T *li;
23952
23953 /* The a: variables typevals may not have been allocated, only free the
23954 * allocated variables. */
23955 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23956
23957 /* free all l: variables */
23958 vars_clear(&fc->l_vars.dv_hashtab);
23959
23960 /* Free the a:000 variables if they were allocated. */
23961 if (free_val)
23962 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23963 clear_tv(&li->li_tv);
23964
23965 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966}
23967
23968/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023969 * Add a number variable "name" to dict "dp" with value "nr".
23970 */
23971 static void
23972add_nr_var(dp, v, name, nr)
23973 dict_T *dp;
23974 dictitem_T *v;
23975 char *name;
23976 varnumber_T nr;
23977{
23978 STRCPY(v->di_key, name);
23979 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23980 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23981 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023982 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023983 v->di_tv.vval.v_number = nr;
23984}
23985
23986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 * ":return [expr]"
23988 */
23989 void
23990ex_return(eap)
23991 exarg_T *eap;
23992{
23993 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023994 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 int returning = FALSE;
23996
23997 if (current_funccal == NULL)
23998 {
23999 EMSG(_("E133: :return not inside a function"));
24000 return;
24001 }
24002
24003 if (eap->skip)
24004 ++emsg_skip;
24005
24006 eap->nextcmd = NULL;
24007 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024008 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 {
24010 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024011 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024013 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 }
24015 /* It's safer to return also on error. */
24016 else if (!eap->skip)
24017 {
24018 /*
24019 * Return unless the expression evaluation has been cancelled due to an
24020 * aborting error, an interrupt, or an exception.
24021 */
24022 if (!aborting())
24023 returning = do_return(eap, FALSE, TRUE, NULL);
24024 }
24025
24026 /* When skipping or the return gets pending, advance to the next command
24027 * in this line (!returning). Otherwise, ignore the rest of the line.
24028 * Following lines will be ignored by get_func_line(). */
24029 if (returning)
24030 eap->nextcmd = NULL;
24031 else if (eap->nextcmd == NULL) /* no argument */
24032 eap->nextcmd = check_nextcmd(arg);
24033
24034 if (eap->skip)
24035 --emsg_skip;
24036}
24037
24038/*
24039 * Return from a function. Possibly makes the return pending. Also called
24040 * for a pending return at the ":endtry" or after returning from an extra
24041 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024042 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024043 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 * FALSE when the return gets pending.
24045 */
24046 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024047do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 exarg_T *eap;
24049 int reanimate;
24050 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024051 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052{
24053 int idx;
24054 struct condstack *cstack = eap->cstack;
24055
24056 if (reanimate)
24057 /* Undo the return. */
24058 current_funccal->returned = FALSE;
24059
24060 /*
24061 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24062 * not in its finally clause (which then is to be executed next) is found.
24063 * In this case, make the ":return" pending for execution at the ":endtry".
24064 * Otherwise, return normally.
24065 */
24066 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24067 if (idx >= 0)
24068 {
24069 cstack->cs_pending[idx] = CSTP_RETURN;
24070
24071 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024072 /* A pending return again gets pending. "rettv" points to an
24073 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024075 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 else
24077 {
24078 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024079 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024081 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024083 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 {
24085 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024086 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024087 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 else
24089 EMSG(_(e_outofmem));
24090 }
24091 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024092 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093
24094 if (reanimate)
24095 {
24096 /* The pending return value could be overwritten by a ":return"
24097 * without argument in a finally clause; reset the default
24098 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024099 current_funccal->rettv->v_type = VAR_NUMBER;
24100 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 }
24102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024103 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 }
24105 else
24106 {
24107 current_funccal->returned = TRUE;
24108
24109 /* If the return is carried out now, store the return value. For
24110 * a return immediately after reanimation, the value is already
24111 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024112 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024114 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024115 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024117 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 }
24119 }
24120
24121 return idx < 0;
24122}
24123
24124/*
24125 * Free the variable with a pending return value.
24126 */
24127 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024128discard_pending_return(rettv)
24129 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130{
Bram Moolenaar33570922005-01-25 22:26:29 +000024131 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132}
24133
24134/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024135 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 * is an allocated string. Used by report_pending() for verbose messages.
24137 */
24138 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024139get_return_cmd(rettv)
24140 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024142 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024143 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024144 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024146 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024147 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024148 if (s == NULL)
24149 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024150
24151 STRCPY(IObuff, ":return ");
24152 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24153 if (STRLEN(s) + 8 >= IOSIZE)
24154 STRCPY(IObuff + IOSIZE - 4, "...");
24155 vim_free(tofree);
24156 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157}
24158
24159/*
24160 * Get next function line.
24161 * Called by do_cmdline() to get the next line.
24162 * Returns allocated string, or NULL for end of function.
24163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 char_u *
24165get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024166 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024168 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169{
Bram Moolenaar33570922005-01-25 22:26:29 +000024170 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024171 ufunc_T *fp = fcp->func;
24172 char_u *retval;
24173 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174
24175 /* If breakpoints have been added/deleted need to check for it. */
24176 if (fcp->dbg_tick != debug_tick)
24177 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024178 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 sourcing_lnum);
24180 fcp->dbg_tick = debug_tick;
24181 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024182#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024183 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024184 func_line_end(cookie);
24185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186
Bram Moolenaar05159a02005-02-26 23:04:13 +000024187 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024188 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24189 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 retval = NULL;
24191 else
24192 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024193 /* Skip NULL lines (continuation lines). */
24194 while (fcp->linenr < gap->ga_len
24195 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24196 ++fcp->linenr;
24197 if (fcp->linenr >= gap->ga_len)
24198 retval = NULL;
24199 else
24200 {
24201 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24202 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024203#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024204 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024205 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024206#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 }
24209
24210 /* Did we encounter a breakpoint? */
24211 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24212 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024213 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024215 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 sourcing_lnum);
24217 fcp->dbg_tick = debug_tick;
24218 }
24219
24220 return retval;
24221}
24222
Bram Moolenaar05159a02005-02-26 23:04:13 +000024223#if defined(FEAT_PROFILE) || defined(PROTO)
24224/*
24225 * Called when starting to read a function line.
24226 * "sourcing_lnum" must be correct!
24227 * When skipping lines it may not actually be executed, but we won't find out
24228 * until later and we need to store the time now.
24229 */
24230 void
24231func_line_start(cookie)
24232 void *cookie;
24233{
24234 funccall_T *fcp = (funccall_T *)cookie;
24235 ufunc_T *fp = fcp->func;
24236
24237 if (fp->uf_profiling && sourcing_lnum >= 1
24238 && sourcing_lnum <= fp->uf_lines.ga_len)
24239 {
24240 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024241 /* Skip continuation lines. */
24242 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24243 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024244 fp->uf_tml_execed = FALSE;
24245 profile_start(&fp->uf_tml_start);
24246 profile_zero(&fp->uf_tml_children);
24247 profile_get_wait(&fp->uf_tml_wait);
24248 }
24249}
24250
24251/*
24252 * Called when actually executing a function line.
24253 */
24254 void
24255func_line_exec(cookie)
24256 void *cookie;
24257{
24258 funccall_T *fcp = (funccall_T *)cookie;
24259 ufunc_T *fp = fcp->func;
24260
24261 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24262 fp->uf_tml_execed = TRUE;
24263}
24264
24265/*
24266 * Called when done with a function line.
24267 */
24268 void
24269func_line_end(cookie)
24270 void *cookie;
24271{
24272 funccall_T *fcp = (funccall_T *)cookie;
24273 ufunc_T *fp = fcp->func;
24274
24275 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24276 {
24277 if (fp->uf_tml_execed)
24278 {
24279 ++fp->uf_tml_count[fp->uf_tml_idx];
24280 profile_end(&fp->uf_tml_start);
24281 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024282 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024283 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24284 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024285 }
24286 fp->uf_tml_idx = -1;
24287 }
24288}
24289#endif
24290
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291/*
24292 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024293 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 */
24295 int
24296func_has_ended(cookie)
24297 void *cookie;
24298{
Bram Moolenaar33570922005-01-25 22:26:29 +000024299 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300
24301 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24302 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024303 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 || fcp->returned);
24305}
24306
24307/*
24308 * return TRUE if cookie indicates a function which "abort"s on errors.
24309 */
24310 int
24311func_has_abort(cookie)
24312 void *cookie;
24313{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024314 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315}
24316
24317#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24318typedef enum
24319{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024320 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24321 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24322 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323} var_flavour_T;
24324
24325static var_flavour_T var_flavour __ARGS((char_u *varname));
24326
24327 static var_flavour_T
24328var_flavour(varname)
24329 char_u *varname;
24330{
24331 char_u *p = varname;
24332
24333 if (ASCII_ISUPPER(*p))
24334 {
24335 while (*(++p))
24336 if (ASCII_ISLOWER(*p))
24337 return VAR_FLAVOUR_SESSION;
24338 return VAR_FLAVOUR_VIMINFO;
24339 }
24340 else
24341 return VAR_FLAVOUR_DEFAULT;
24342}
24343#endif
24344
24345#if defined(FEAT_VIMINFO) || defined(PROTO)
24346/*
24347 * Restore global vars that start with a capital from the viminfo file
24348 */
24349 int
24350read_viminfo_varlist(virp, writing)
24351 vir_T *virp;
24352 int writing;
24353{
24354 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024355 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024356 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357
24358 if (!writing && (find_viminfo_parameter('!') != NULL))
24359 {
24360 tab = vim_strchr(virp->vir_line + 1, '\t');
24361 if (tab != NULL)
24362 {
24363 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024364 switch (*tab)
24365 {
24366 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024367#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024368 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024369#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024370 case 'D': type = VAR_DICT; break;
24371 case 'L': type = VAR_LIST; break;
24372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373
24374 tab = vim_strchr(tab, '\t');
24375 if (tab != NULL)
24376 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024377 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024378 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024379 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024381#ifdef FEAT_FLOAT
24382 else if (type == VAR_FLOAT)
24383 (void)string2float(tab + 1, &tv.vval.v_float);
24384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024386 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024387 if (type == VAR_DICT || type == VAR_LIST)
24388 {
24389 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24390
24391 if (etv == NULL)
24392 /* Failed to parse back the dict or list, use it as a
24393 * string. */
24394 tv.v_type = VAR_STRING;
24395 else
24396 {
24397 vim_free(tv.vval.v_string);
24398 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024399 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024400 }
24401 }
24402
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024403 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024404
24405 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024406 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024407 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24408 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 }
24410 }
24411 }
24412
24413 return viminfo_readline(virp);
24414}
24415
24416/*
24417 * Write global vars that start with a capital to the viminfo file
24418 */
24419 void
24420write_viminfo_varlist(fp)
24421 FILE *fp;
24422{
Bram Moolenaar33570922005-01-25 22:26:29 +000024423 hashitem_T *hi;
24424 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024425 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024426 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024427 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024428 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024429 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430
24431 if (find_viminfo_parameter('!') == NULL)
24432 return;
24433
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024434 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024436 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024437 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024441 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024442 this_var = HI2DI(hi);
24443 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024445 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024446 {
24447 case VAR_STRING: s = "STR"; break;
24448 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024449#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024450 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024451#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024452 case VAR_DICT: s = "DIC"; break;
24453 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024454 default: continue;
24455 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024456 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024457 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024458 if (p != NULL)
24459 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024460 vim_free(tofree);
24461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 }
24463 }
24464}
24465#endif
24466
24467#if defined(FEAT_SESSION) || defined(PROTO)
24468 int
24469store_session_globals(fd)
24470 FILE *fd;
24471{
Bram Moolenaar33570922005-01-25 22:26:29 +000024472 hashitem_T *hi;
24473 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024474 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 char_u *p, *t;
24476
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024477 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024478 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024480 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024482 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024483 this_var = HI2DI(hi);
24484 if ((this_var->di_tv.v_type == VAR_NUMBER
24485 || this_var->di_tv.v_type == VAR_STRING)
24486 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024488 /* Escape special characters with a backslash. Turn a LF and
24489 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024490 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024491 (char_u *)"\\\"\n\r");
24492 if (p == NULL) /* out of memory */
24493 break;
24494 for (t = p; *t != NUL; ++t)
24495 if (*t == '\n')
24496 *t = 'n';
24497 else if (*t == '\r')
24498 *t = 'r';
24499 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024500 this_var->di_key,
24501 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24502 : ' ',
24503 p,
24504 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24505 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024506 || put_eol(fd) == FAIL)
24507 {
24508 vim_free(p);
24509 return FAIL;
24510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 vim_free(p);
24512 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024513#ifdef FEAT_FLOAT
24514 else if (this_var->di_tv.v_type == VAR_FLOAT
24515 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24516 {
24517 float_T f = this_var->di_tv.vval.v_float;
24518 int sign = ' ';
24519
24520 if (f < 0)
24521 {
24522 f = -f;
24523 sign = '-';
24524 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024525 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024526 this_var->di_key, sign, f) < 0)
24527 || put_eol(fd) == FAIL)
24528 return FAIL;
24529 }
24530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 }
24532 }
24533 return OK;
24534}
24535#endif
24536
Bram Moolenaar661b1822005-07-28 22:36:45 +000024537/*
24538 * Display script name where an item was last set.
24539 * Should only be invoked when 'verbose' is non-zero.
24540 */
24541 void
24542last_set_msg(scriptID)
24543 scid_T scriptID;
24544{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024545 char_u *p;
24546
Bram Moolenaar661b1822005-07-28 22:36:45 +000024547 if (scriptID != 0)
24548 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024549 p = home_replace_save(NULL, get_scriptname(scriptID));
24550 if (p != NULL)
24551 {
24552 verbose_enter();
24553 MSG_PUTS(_("\n\tLast set from "));
24554 MSG_PUTS(p);
24555 vim_free(p);
24556 verbose_leave();
24557 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024558 }
24559}
24560
Bram Moolenaard812df62008-11-09 12:46:09 +000024561/*
24562 * List v:oldfiles in a nice way.
24563 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024564 void
24565ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024566 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024567{
24568 list_T *l = vimvars[VV_OLDFILES].vv_list;
24569 listitem_T *li;
24570 int nr = 0;
24571
24572 if (l == NULL)
24573 msg((char_u *)_("No old files"));
24574 else
24575 {
24576 msg_start();
24577 msg_scroll = TRUE;
24578 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24579 {
24580 msg_outnum((long)++nr);
24581 MSG_PUTS(": ");
24582 msg_outtrans(get_tv_string(&li->li_tv));
24583 msg_putchar('\n');
24584 out_flush(); /* output one line at a time */
24585 ui_breakcheck();
24586 }
24587 /* Assume "got_int" was set to truncate the listing. */
24588 got_int = FALSE;
24589
24590#ifdef FEAT_BROWSE_CMD
24591 if (cmdmod.browse)
24592 {
24593 quit_more = FALSE;
24594 nr = prompt_for_number(FALSE);
24595 msg_starthere();
24596 if (nr > 0)
24597 {
24598 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24599 (long)nr);
24600
24601 if (p != NULL)
24602 {
24603 p = expand_env_save(p);
24604 eap->arg = p;
24605 eap->cmdidx = CMD_edit;
24606 cmdmod.browse = FALSE;
24607 do_exedit(eap, NULL);
24608 vim_free(p);
24609 }
24610 }
24611 }
24612#endif
24613 }
24614}
24615
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616#endif /* FEAT_EVAL */
24617
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024619#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620
24621#ifdef WIN3264
24622/*
24623 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24624 */
24625static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24626static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24627static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24628
24629/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024630 * Get the short path (8.3) for the filename in "fnamep".
24631 * Only works for a valid file name.
24632 * When the path gets longer "fnamep" is changed and the allocated buffer
24633 * is put in "bufp".
24634 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24635 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 */
24637 static int
24638get_short_pathname(fnamep, bufp, fnamelen)
24639 char_u **fnamep;
24640 char_u **bufp;
24641 int *fnamelen;
24642{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024643 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 char_u *newbuf;
24645
24646 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 l = GetShortPathName(*fnamep, *fnamep, len);
24648 if (l > len - 1)
24649 {
24650 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024651 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 newbuf = vim_strnsave(*fnamep, l);
24653 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024654 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655
24656 vim_free(*bufp);
24657 *fnamep = *bufp = newbuf;
24658
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024659 /* Really should always succeed, as the buffer is big enough. */
24660 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 }
24662
24663 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024664 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665}
24666
24667/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024668 * Get the short path (8.3) for the filename in "fname". The converted
24669 * path is returned in "bufp".
24670 *
24671 * Some of the directories specified in "fname" may not exist. This function
24672 * will shorten the existing directories at the beginning of the path and then
24673 * append the remaining non-existing path.
24674 *
24675 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024676 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024677 * bufp - Pointer to an allocated buffer for the filename.
24678 * fnamelen - Length of the filename pointed to by fname
24679 *
24680 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681 */
24682 static int
24683shortpath_for_invalid_fname(fname, bufp, fnamelen)
24684 char_u **fname;
24685 char_u **bufp;
24686 int *fnamelen;
24687{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024688 char_u *short_fname, *save_fname, *pbuf_unused;
24689 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024691 int old_len, len;
24692 int new_len, sfx_len;
24693 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694
24695 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024696 old_len = *fnamelen;
24697 save_fname = vim_strnsave(*fname, old_len);
24698 pbuf_unused = NULL;
24699 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024701 endp = save_fname + old_len - 1; /* Find the end of the copy */
24702 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024704 /*
24705 * Try shortening the supplied path till it succeeds by removing one
24706 * directory at a time from the tail of the path.
24707 */
24708 len = 0;
24709 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024711 /* go back one path-separator */
24712 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24713 --endp;
24714 if (endp <= save_fname)
24715 break; /* processed the complete path */
24716
24717 /*
24718 * Replace the path separator with a NUL and try to shorten the
24719 * resulting path.
24720 */
24721 ch = *endp;
24722 *endp = 0;
24723 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024724 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024725 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24726 {
24727 retval = FAIL;
24728 goto theend;
24729 }
24730 *endp = ch; /* preserve the string */
24731
24732 if (len > 0)
24733 break; /* successfully shortened the path */
24734
24735 /* failed to shorten the path. Skip the path separator */
24736 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 }
24738
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024739 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024741 /*
24742 * Succeeded in shortening the path. Now concatenate the shortened
24743 * path with the remaining path at the tail.
24744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024746 /* Compute the length of the new path. */
24747 sfx_len = (int)(save_endp - endp) + 1;
24748 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024750 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024752 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024754 /* There is not enough space in the currently allocated string,
24755 * copy it to a buffer big enough. */
24756 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024758 {
24759 retval = FAIL;
24760 goto theend;
24761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 }
24763 else
24764 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024765 /* Transfer short_fname to the main buffer (it's big enough),
24766 * unless get_short_pathname() did its work in-place. */
24767 *fname = *bufp = save_fname;
24768 if (short_fname != save_fname)
24769 vim_strncpy(save_fname, short_fname, len);
24770 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024772
24773 /* concat the not-shortened part of the path */
24774 vim_strncpy(*fname + len, endp, sfx_len);
24775 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024777
24778theend:
24779 vim_free(pbuf_unused);
24780 vim_free(save_fname);
24781
24782 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783}
24784
24785/*
24786 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024787 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 */
24789 static int
24790shortpath_for_partial(fnamep, bufp, fnamelen)
24791 char_u **fnamep;
24792 char_u **bufp;
24793 int *fnamelen;
24794{
24795 int sepcount, len, tflen;
24796 char_u *p;
24797 char_u *pbuf, *tfname;
24798 int hasTilde;
24799
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024800 /* Count up the path separators from the RHS.. so we know which part
24801 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024803 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 if (vim_ispathsep(*p))
24805 ++sepcount;
24806
24807 /* Need full path first (use expand_env() to remove a "~/") */
24808 hasTilde = (**fnamep == '~');
24809 if (hasTilde)
24810 pbuf = tfname = expand_env_save(*fnamep);
24811 else
24812 pbuf = tfname = FullName_save(*fnamep, FALSE);
24813
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024814 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024816 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818
24819 if (len == 0)
24820 {
24821 /* Don't have a valid filename, so shorten the rest of the
24822 * path if we can. This CAN give us invalid 8.3 filenames, but
24823 * there's not a lot of point in guessing what it might be.
24824 */
24825 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024826 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24827 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 }
24829
24830 /* Count the paths backward to find the beginning of the desired string. */
24831 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024832 {
24833#ifdef FEAT_MBYTE
24834 if (has_mbyte)
24835 p -= mb_head_off(tfname, p);
24836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 if (vim_ispathsep(*p))
24838 {
24839 if (sepcount == 0 || (hasTilde && sepcount == 1))
24840 break;
24841 else
24842 sepcount --;
24843 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845 if (hasTilde)
24846 {
24847 --p;
24848 if (p >= tfname)
24849 *p = '~';
24850 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852 }
24853 else
24854 ++p;
24855
24856 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24857 vim_free(*bufp);
24858 *fnamelen = (int)STRLEN(p);
24859 *bufp = pbuf;
24860 *fnamep = p;
24861
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024862 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863}
24864#endif /* WIN3264 */
24865
24866/*
24867 * Adjust a filename, according to a string of modifiers.
24868 * *fnamep must be NUL terminated when called. When returning, the length is
24869 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024870 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 * When there is an error, *fnamep is set to NULL.
24872 */
24873 int
24874modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24875 char_u *src; /* string with modifiers */
24876 int *usedlen; /* characters after src that are used */
24877 char_u **fnamep; /* file name so far */
24878 char_u **bufp; /* buffer for allocated file name or NULL */
24879 int *fnamelen; /* length of fnamep */
24880{
24881 int valid = 0;
24882 char_u *tail;
24883 char_u *s, *p, *pbuf;
24884 char_u dirname[MAXPATHL];
24885 int c;
24886 int has_fullname = 0;
24887#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024888 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889 int has_shortname = 0;
24890#endif
24891
24892repeat:
24893 /* ":p" - full path/file_name */
24894 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24895 {
24896 has_fullname = 1;
24897
24898 valid |= VALID_PATH;
24899 *usedlen += 2;
24900
24901 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24902 if ((*fnamep)[0] == '~'
24903#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24904 && ((*fnamep)[1] == '/'
24905# ifdef BACKSLASH_IN_FILENAME
24906 || (*fnamep)[1] == '\\'
24907# endif
24908 || (*fnamep)[1] == NUL)
24909
24910#endif
24911 )
24912 {
24913 *fnamep = expand_env_save(*fnamep);
24914 vim_free(*bufp); /* free any allocated file name */
24915 *bufp = *fnamep;
24916 if (*fnamep == NULL)
24917 return -1;
24918 }
24919
24920 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024921 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 {
24923 if (vim_ispathsep(*p)
24924 && p[1] == '.'
24925 && (p[2] == NUL
24926 || vim_ispathsep(p[2])
24927 || (p[2] == '.'
24928 && (p[3] == NUL || vim_ispathsep(p[3])))))
24929 break;
24930 }
24931
24932 /* FullName_save() is slow, don't use it when not needed. */
24933 if (*p != NUL || !vim_isAbsName(*fnamep))
24934 {
24935 *fnamep = FullName_save(*fnamep, *p != NUL);
24936 vim_free(*bufp); /* free any allocated file name */
24937 *bufp = *fnamep;
24938 if (*fnamep == NULL)
24939 return -1;
24940 }
24941
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024942#ifdef WIN3264
24943# if _WIN32_WINNT >= 0x0500
24944 if (vim_strchr(*fnamep, '~') != NULL)
24945 {
24946 /* Expand 8.3 filename to full path. Needed to make sure the same
24947 * file does not have two different names.
24948 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24949 p = alloc(_MAX_PATH + 1);
24950 if (p != NULL)
24951 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024952 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024953 {
24954 vim_free(*bufp);
24955 *bufp = *fnamep = p;
24956 }
24957 else
24958 vim_free(p);
24959 }
24960 }
24961# endif
24962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 /* Append a path separator to a directory. */
24964 if (mch_isdir(*fnamep))
24965 {
24966 /* Make room for one or two extra characters. */
24967 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24968 vim_free(*bufp); /* free any allocated file name */
24969 *bufp = *fnamep;
24970 if (*fnamep == NULL)
24971 return -1;
24972 add_pathsep(*fnamep);
24973 }
24974 }
24975
24976 /* ":." - path relative to the current directory */
24977 /* ":~" - path relative to the home directory */
24978 /* ":8" - shortname path - postponed till after */
24979 while (src[*usedlen] == ':'
24980 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24981 {
24982 *usedlen += 2;
24983 if (c == '8')
24984 {
24985#ifdef WIN3264
24986 has_shortname = 1; /* Postpone this. */
24987#endif
24988 continue;
24989 }
24990 pbuf = NULL;
24991 /* Need full path first (use expand_env() to remove a "~/") */
24992 if (!has_fullname)
24993 {
24994 if (c == '.' && **fnamep == '~')
24995 p = pbuf = expand_env_save(*fnamep);
24996 else
24997 p = pbuf = FullName_save(*fnamep, FALSE);
24998 }
24999 else
25000 p = *fnamep;
25001
25002 has_fullname = 0;
25003
25004 if (p != NULL)
25005 {
25006 if (c == '.')
25007 {
25008 mch_dirname(dirname, MAXPATHL);
25009 s = shorten_fname(p, dirname);
25010 if (s != NULL)
25011 {
25012 *fnamep = s;
25013 if (pbuf != NULL)
25014 {
25015 vim_free(*bufp); /* free any allocated file name */
25016 *bufp = pbuf;
25017 pbuf = NULL;
25018 }
25019 }
25020 }
25021 else
25022 {
25023 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25024 /* Only replace it when it starts with '~' */
25025 if (*dirname == '~')
25026 {
25027 s = vim_strsave(dirname);
25028 if (s != NULL)
25029 {
25030 *fnamep = s;
25031 vim_free(*bufp);
25032 *bufp = s;
25033 }
25034 }
25035 }
25036 vim_free(pbuf);
25037 }
25038 }
25039
25040 tail = gettail(*fnamep);
25041 *fnamelen = (int)STRLEN(*fnamep);
25042
25043 /* ":h" - head, remove "/file_name", can be repeated */
25044 /* Don't remove the first "/" or "c:\" */
25045 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25046 {
25047 valid |= VALID_HEAD;
25048 *usedlen += 2;
25049 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025050 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025051 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 *fnamelen = (int)(tail - *fnamep);
25053#ifdef VMS
25054 if (*fnamelen > 0)
25055 *fnamelen += 1; /* the path separator is part of the path */
25056#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025057 if (*fnamelen == 0)
25058 {
25059 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25060 p = vim_strsave((char_u *)".");
25061 if (p == NULL)
25062 return -1;
25063 vim_free(*bufp);
25064 *bufp = *fnamep = tail = p;
25065 *fnamelen = 1;
25066 }
25067 else
25068 {
25069 while (tail > s && !after_pathsep(s, tail))
25070 mb_ptr_back(*fnamep, tail);
25071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072 }
25073
25074 /* ":8" - shortname */
25075 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25076 {
25077 *usedlen += 2;
25078#ifdef WIN3264
25079 has_shortname = 1;
25080#endif
25081 }
25082
25083#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025084 /*
25085 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 */
25087 if (has_shortname)
25088 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025089 /* Copy the string if it is shortened by :h and when it wasn't copied
25090 * yet, because we are going to change it in place. Avoids changing
25091 * the buffer name for "%:8". */
25092 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 {
25094 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025095 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 return -1;
25097 vim_free(*bufp);
25098 *bufp = *fnamep = p;
25099 }
25100
25101 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025102 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103 if (!has_fullname && !vim_isAbsName(*fnamep))
25104 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025105 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 return -1;
25107 }
25108 else
25109 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025110 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111
Bram Moolenaardc935552011-08-17 15:23:23 +020025112 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025114 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 return -1;
25116
25117 if (l == 0)
25118 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025119 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025121 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 return -1;
25123 }
25124 *fnamelen = l;
25125 }
25126 }
25127#endif /* WIN3264 */
25128
25129 /* ":t" - tail, just the basename */
25130 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25131 {
25132 *usedlen += 2;
25133 *fnamelen -= (int)(tail - *fnamep);
25134 *fnamep = tail;
25135 }
25136
25137 /* ":e" - extension, can be repeated */
25138 /* ":r" - root, without extension, can be repeated */
25139 while (src[*usedlen] == ':'
25140 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25141 {
25142 /* find a '.' in the tail:
25143 * - for second :e: before the current fname
25144 * - otherwise: The last '.'
25145 */
25146 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25147 s = *fnamep - 2;
25148 else
25149 s = *fnamep + *fnamelen - 1;
25150 for ( ; s > tail; --s)
25151 if (s[0] == '.')
25152 break;
25153 if (src[*usedlen + 1] == 'e') /* :e */
25154 {
25155 if (s > tail)
25156 {
25157 *fnamelen += (int)(*fnamep - (s + 1));
25158 *fnamep = s + 1;
25159#ifdef VMS
25160 /* cut version from the extension */
25161 s = *fnamep + *fnamelen - 1;
25162 for ( ; s > *fnamep; --s)
25163 if (s[0] == ';')
25164 break;
25165 if (s > *fnamep)
25166 *fnamelen = s - *fnamep;
25167#endif
25168 }
25169 else if (*fnamep <= tail)
25170 *fnamelen = 0;
25171 }
25172 else /* :r */
25173 {
25174 if (s > tail) /* remove one extension */
25175 *fnamelen = (int)(s - *fnamep);
25176 }
25177 *usedlen += 2;
25178 }
25179
25180 /* ":s?pat?foo?" - substitute */
25181 /* ":gs?pat?foo?" - global substitute */
25182 if (src[*usedlen] == ':'
25183 && (src[*usedlen + 1] == 's'
25184 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25185 {
25186 char_u *str;
25187 char_u *pat;
25188 char_u *sub;
25189 int sep;
25190 char_u *flags;
25191 int didit = FALSE;
25192
25193 flags = (char_u *)"";
25194 s = src + *usedlen + 2;
25195 if (src[*usedlen + 1] == 'g')
25196 {
25197 flags = (char_u *)"g";
25198 ++s;
25199 }
25200
25201 sep = *s++;
25202 if (sep)
25203 {
25204 /* find end of pattern */
25205 p = vim_strchr(s, sep);
25206 if (p != NULL)
25207 {
25208 pat = vim_strnsave(s, (int)(p - s));
25209 if (pat != NULL)
25210 {
25211 s = p + 1;
25212 /* find end of substitution */
25213 p = vim_strchr(s, sep);
25214 if (p != NULL)
25215 {
25216 sub = vim_strnsave(s, (int)(p - s));
25217 str = vim_strnsave(*fnamep, *fnamelen);
25218 if (sub != NULL && str != NULL)
25219 {
25220 *usedlen = (int)(p + 1 - src);
25221 s = do_string_sub(str, pat, sub, flags);
25222 if (s != NULL)
25223 {
25224 *fnamep = s;
25225 *fnamelen = (int)STRLEN(s);
25226 vim_free(*bufp);
25227 *bufp = s;
25228 didit = TRUE;
25229 }
25230 }
25231 vim_free(sub);
25232 vim_free(str);
25233 }
25234 vim_free(pat);
25235 }
25236 }
25237 /* after using ":s", repeat all the modifiers */
25238 if (didit)
25239 goto repeat;
25240 }
25241 }
25242
Bram Moolenaar26df0922014-02-23 23:39:13 +010025243 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25244 {
25245 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25246 if (p == NULL)
25247 return -1;
25248 vim_free(*bufp);
25249 *bufp = *fnamep = p;
25250 *fnamelen = (int)STRLEN(p);
25251 *usedlen += 2;
25252 }
25253
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254 return valid;
25255}
25256
25257/*
25258 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25259 * "flags" can be "g" to do a global substitute.
25260 * Returns an allocated string, NULL for error.
25261 */
25262 char_u *
25263do_string_sub(str, pat, sub, flags)
25264 char_u *str;
25265 char_u *pat;
25266 char_u *sub;
25267 char_u *flags;
25268{
25269 int sublen;
25270 regmatch_T regmatch;
25271 int i;
25272 int do_all;
25273 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025274 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 garray_T ga;
25276 char_u *ret;
25277 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025278 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279
25280 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25281 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025282 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283
25284 ga_init2(&ga, 1, 200);
25285
25286 do_all = (flags[0] == 'g');
25287
25288 regmatch.rm_ic = p_ic;
25289 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25290 if (regmatch.regprog != NULL)
25291 {
25292 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025293 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25295 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025296 /* Skip empty match except for first match. */
25297 if (regmatch.startp[0] == regmatch.endp[0])
25298 {
25299 if (zero_width == regmatch.startp[0])
25300 {
25301 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025302 i = MB_PTR2LEN(tail);
25303 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25304 (size_t)i);
25305 ga.ga_len += i;
25306 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025307 continue;
25308 }
25309 zero_width = regmatch.startp[0];
25310 }
25311
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 /*
25313 * Get some space for a temporary buffer to do the substitution
25314 * into. It will contain:
25315 * - The text up to where the match is.
25316 * - The substituted text.
25317 * - The text after the match.
25318 */
25319 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025320 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25322 {
25323 ga_clear(&ga);
25324 break;
25325 }
25326
25327 /* copy the text up to where the match is */
25328 i = (int)(regmatch.startp[0] - tail);
25329 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25330 /* add the substituted text */
25331 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25332 + ga.ga_len + i, TRUE, TRUE, FALSE);
25333 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025334 tail = regmatch.endp[0];
25335 if (*tail == NUL)
25336 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 if (!do_all)
25338 break;
25339 }
25340
25341 if (ga.ga_data != NULL)
25342 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25343
Bram Moolenaar473de612013-06-08 18:19:48 +020025344 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 }
25346
25347 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25348 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025349 if (p_cpo == empty_option)
25350 p_cpo = save_cpo;
25351 else
25352 /* Darn, evaluating {sub} expression changed the value. */
25353 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354
25355 return ret;
25356}
25357
25358#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */